Skip to content

Commit 36bdc2e

Browse files
committed
add validation to check if nip07 extension supports nip44
1 parent d12b136 commit 36bdc2e

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

ndk/src/signers/nip07/index.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import debug from "debug";
22

33
import type { NostrEvent } from "../../events/index.js";
4-
import { NDKUser } from "../../user/index.js";
4+
import { Hexpubkey, NDKUser } from "../../user/index.js";
55
import { DEFAULT_ENCRYPTION_SCHEME, ENCRYPTION_SCHEMES, type NDKSigner } from "../index.js";
66
import { NDKRelay } from "../../relay/index.js";
77
import type { NDK } from "../../ndk/index.js";
@@ -119,12 +119,20 @@ export class NDKNip07Signer implements NDKSigner {
119119

120120
public async nip44Encrypt(recipient: NDKUser, value: string): Promise<string> {
121121
await this.waitForExtension();
122-
return await window.nostr!.nip44!.encrypt(recipient.pubkey, value);
122+
return await this.nip44.encrypt(recipient.pubkey, value);
123+
}
124+
125+
get nip44(): Nip44 {
126+
if (!window.nostr?.nip44) {
127+
throw new Error("NIP-44 not supported by your browser extension");
128+
}
129+
130+
return window.nostr.nip44;
123131
}
124132

125133
public async nip44Decrypt(sender: NDKUser, value: string): Promise<string> {
126134
await this.waitForExtension();
127-
return await window.nostr!.nip44!.decrypt(sender.pubkey, value);
135+
return await this.nip44.decrypt(sender.pubkey, value);
128136
}
129137

130138
public async nip04Encrypt(recipient: NDKUser, value: string): Promise<string> {
@@ -232,6 +240,11 @@ export class NDKNip07Signer implements NDKSigner {
232240
}
233241
}
234242

243+
type Nip44 = {
244+
encrypt: (recipient: Hexpubkey, value: string) => Promise<string>;
245+
decrypt: (sender: Hexpubkey, value: string) => Promise<string>;
246+
}
247+
235248
declare global {
236249
interface Window {
237250
nostr?: {
@@ -242,9 +255,7 @@ declare global {
242255
encrypt(recipientHexPubKey: string, value: string): Promise<string>;
243256
decrypt(senderHexPubKey: string, value: string): Promise<string>;
244257
};
245-
nip44?: {
246-
encrypt(recipientHexPubKey: string, value: string): Promise<string>;
247-
decrypt(senderHexPubKey: string, value: string): Promise<string>;
258+
nip44?: Nip44;
248259
};
249260
};
250261
}

0 commit comments

Comments
 (0)