Skip to content

Commit a8411a1

Browse files
committed
add default permissions when blocking the signer until it's ready and add error handling to sign fcn
1 parent 3a8a3a2 commit a8411a1

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

ndk-mobile/src/signers/nip55.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import * as Nip55 from "expo-nip55";
22
import { NDKEncryptionScheme, NDKSigner, NDKUser, NostrEvent } from "@nostr-dev-kit/ndk";
33

4+
export interface Permission {
5+
type: string;
6+
kind?: number;
7+
checked?: boolean;
8+
}
9+
410
export class NDKNip55Signer implements NDKSigner {
511
private _pubkey: string;
612
private _user?: NDKUser;
@@ -9,18 +15,26 @@ export class NDKNip55Signer implements NDKSigner {
915
constructor(packageName: string) {
1016
this.packageName = packageName;
1117
}
12-
18+
1319
/**
1420
* Blocks until the signer is ready and returns the associated NDKUser.
1521
* @returns A promise that resolves to the NDKUser instance.
1622
*/
1723
async blockUntilReady(): Promise<NDKUser> {
1824
if (this._user) return this._user;
19-
25+
2026
await Nip55.setPackageName(this.packageName);
21-
22-
const data = await Nip55.getPublicKey();
23-
if (!data) throw new Error('No signer available found');
27+
28+
const perms: Permission[] = [
29+
{ type: "sign_event", kind: 22242 },
30+
{ type: "nip04_encrypt" },
31+
{ type: "nip04_decrypt" },
32+
{ type: "nip44_encrypt" },
33+
{ type: "nip44_decrypt" },
34+
];
35+
36+
const data = await Nip55.getPublicKey(this.packageName, perms);
37+
if (!data) throw new Error("No signer available found");
2438

2539
this._user = new NDKUser({ npub: data.npub });
2640
this._pubkey = this._user.pubkey;
@@ -40,14 +54,23 @@ export class NDKNip55Signer implements NDKSigner {
4054
*/
4155
async sign(event: NostrEvent): Promise<string> {
4256
console.log('NIP-55 SIGNER SIGNING', event)
43-
const result = await Nip55.signEvent(
44-
this.packageName,
45-
JSON.stringify(event),
46-
event.id,
47-
this._pubkey
57+
58+
let result: { signature: string }
59+
60+
try {
61+
result = await Nip55.signEvent(
62+
this.packageName,
63+
JSON.stringify(event),
64+
event.id,
65+
this._pubkey
4866
)
4967
console.log('NIP-55 SIGNER SIGNED', result)
50-
return result.signature;
68+
} catch(error) {
69+
console.error("Error sign failed:", error);
70+
throw error;
71+
}
72+
73+
return result.signature;
5174
}
5275

5376
/**

0 commit comments

Comments
 (0)