Skip to content

Commit 601a729

Browse files
committed
feat(core): SignerNostrPublicKeyReadonly
1 parent 8c7e000 commit 601a729

File tree

5 files changed

+45
-15
lines changed

5 files changed

+45
-15
lines changed

.changeset/strange-lies-fetch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ckb-ccc/core": minor
3+
---
4+
5+
feat(core): SignerNostrPublicKeyReadonly
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./signerNostr.js";
22
export * from "./signerNostrPrivateKey.js";
3+
export * from "./signerNostrPublicKeyReadonly.js";
34
export * from "./verify.js";

packages/core/src/signer/nostr/signerNostr.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ export abstract class SignerNostr extends Signer {
5050
*
5151
* @returns A promise that resolves to the signed event.
5252
*/
53-
abstract signNostrEvent(event: NostrEvent): Promise<Required<NostrEvent>>;
53+
async signNostrEvent(_event: NostrEvent): Promise<Required<NostrEvent>> {
54+
throw Error("SignerNostr.signNostrEvent not implemented");
55+
}
5456

5557
/**
5658
* Sign a message.

packages/core/src/signer/nostr/signerNostrPrivateKey.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
import { schnorr } from "@noble/curves/secp256k1";
22
import { Client } from "../../client/index.js";
33
import { Hex, hexFrom, HexLike } from "../../hex/index.js";
4-
import { NostrEvent, SignerNostr } from "./signerNostr.js";
4+
import { NostrEvent } from "./signerNostr.js";
5+
import { SignerNostrPublicKeyReadonly } from "./signerNostrPublicKeyReadonly.js";
56
import { nostrEventHash } from "./verify.js";
67

7-
export class SignerNostrPrivateKey extends SignerNostr {
8+
export class SignerNostrPrivateKey extends SignerNostrPublicKeyReadonly {
89
private readonly privateKey: Hex;
910

10-
constructor(client: Client, privateKey: HexLike) {
11-
super(client);
12-
this.privateKey = hexFrom(privateKey);
13-
}
14-
15-
async connect(): Promise<void> {}
16-
17-
async isConnected(): Promise<boolean> {
18-
return true;
19-
}
11+
constructor(client: Client, privateKeyLike: HexLike) {
12+
const privateKey = hexFrom(privateKeyLike);
13+
super(client, schnorr.getPublicKey(privateKey.slice(2)));
2014

21-
async getNostrPublicKey(): Promise<Hex> {
22-
return hexFrom(schnorr.getPublicKey(this.privateKey.slice(2)));
15+
this.privateKey = privateKey;
2316
}
2417

2518
async signNostrEvent(event: NostrEvent): Promise<Required<NostrEvent>> {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { bech32 } from "bech32";
2+
import { Client } from "../../client/index.js";
3+
import { Hex, hexFrom, HexLike } from "../../hex/index.js";
4+
import { SignerNostr } from "./signerNostr.js";
5+
6+
export class SignerNostrPublicKeyReadonly extends SignerNostr {
7+
public readonly publicKey: Hex;
8+
9+
constructor(client: Client, publicKey: HexLike) {
10+
super(client);
11+
12+
if (typeof publicKey === "string" && publicKey.startsWith("npub")) {
13+
const { words } = bech32.decode(publicKey);
14+
this.publicKey = hexFrom(bech32.fromWords(words));
15+
} else {
16+
this.publicKey = hexFrom(publicKey);
17+
}
18+
}
19+
20+
async connect(): Promise<void> {}
21+
22+
async isConnected(): Promise<boolean> {
23+
return true;
24+
}
25+
26+
async getNostrPublicKey(): Promise<Hex> {
27+
return this.publicKey;
28+
}
29+
}

0 commit comments

Comments
 (0)