diff --git a/packages/system-react/package.json b/packages/system-react/package.json index 7257bff1..a05a3c37 100644 --- a/packages/system-react/package.json +++ b/packages/system-react/package.json @@ -1,6 +1,6 @@ { "name": "@snort/system-react", - "version": "1.2.1", + "version": "1.2.6", "description": "React hooks for @snort/system", "main": "dist/index.js", "module": "src/index.ts", @@ -16,8 +16,8 @@ "dist" ], "dependencies": { - "@snort/shared": "^1.0.12", - "@snort/system": "^1.2.1", + "@snort/shared": "^1.0.13", + "@snort/system": "^1.2.6", "react": "^18.2.0" }, "devDependencies": { diff --git a/packages/system/package.json b/packages/system/package.json index d25eec53..9654c87c 100644 --- a/packages/system/package.json +++ b/packages/system/package.json @@ -1,6 +1,6 @@ { "name": "@snort/system", - "version": "1.2.2", + "version": "1.2.8", "description": "Snort nostr system package", "type": "module", "main": "dist/index.js", diff --git a/packages/system/src/impl/nip46.ts b/packages/system/src/impl/nip46.ts index e15ff5cc..3986180b 100644 --- a/packages/system/src/impl/nip46.ts +++ b/packages/system/src/impl/nip46.ts @@ -88,7 +88,12 @@ export class Nip46Signer extends EventEmitter implements EventSigne } } - async init() { + /** + * Connect to the bunker relay + * @param autoConnect Start connect flow for pubkey + * @returns + */ + async init(autoConnect = true) { const isBunker = this.#proto === "bunker:"; if (isBunker) { this.#remotePubkey = this.#localPubkey; @@ -112,33 +117,24 @@ export class Nip46Signer extends EventEmitter implements EventSigne () => { }, ); - if (isBunker) { - const rsp = await this.#connect(unwrap(this.#remotePubkey)); - if (rsp.result === "auth_url") { - // re-insert the command into queue for result of oAuth flow - this.#commandQueue.set(rsp.id, { - resolve: async (o: Nip46Response) => { - if (o.result === "ack") { - resolve(); - } else { - reject(o.error); - } - }, - reject, - }); - this.emit("oauth", rsp.error); - } else if (rsp.result === "ack") { - resolve(); + if (autoConnect) { + if (isBunker) { + const rsp = await this.#connect(unwrap(this.#remotePubkey)); + if (rsp.result === "ack") { + resolve(); + } else { + reject(rsp.error); + } } else { - reject(rsp.error); + this.#commandQueue.set("connect", { + reject, + resolve: () => { + resolve(); + }, + }); } } else { - this.#commandQueue.set("connect", { - reject, - resolve: () => { - resolve(); - }, - }); + resolve(); } }); this.#conn.connect(); @@ -162,8 +158,9 @@ export class Nip46Signer extends EventEmitter implements EventSigne } async getPubKey() { - const rsp = await this.#rpc("get_public_key", []); - return rsp.result as string; + //const rsp = await this.#rpc("get_public_key", []); + //return rsp.result as string; + return this.#remotePubkey!; } async nip4Encrypt(content: string, otherKey: string) { @@ -173,11 +170,7 @@ export class Nip46Signer extends EventEmitter implements EventSigne async nip4Decrypt(content: string, otherKey: string) { const rsp = await this.#rpc("nip04_decrypt", [otherKey, content]); - try { - return JSON.parse(rsp.result)[0]; - } catch { - return ""; - } + return rsp.result as string; } nip44Encrypt(content: string, key: string): Promise { @@ -201,21 +194,10 @@ export class Nip46Signer extends EventEmitter implements EventSigne * @returns */ async createAccount(name: string, domain: string, email?: string) { + await this.init(false); const rsp = await this.#rpc("create_account", [name, domain, email ?? ""]); - if (rsp.result === "auth_url") { - return await new Promise((resolve, reject) => { - this.#commandQueue.set(rsp.id, { - resolve: async (o: Nip46Response) => { - if (o.result === "ack") { - resolve(); - } else { - reject(o.error); - } - }, - reject, - }); - this.emit("oauth", rsp.error); - }); + if (!rsp.error) { + this.#remotePubkey = rsp.result as string; } } @@ -258,8 +240,17 @@ export class Nip46Signer extends EventEmitter implements EventSigne throw new Error("No pending command found"); } - pending.resolve(reply as Nip46Response); - this.#commandQueue.delete(reply.id); + if ("result" in reply && reply.result === "auth_url") { + this.emit("oauth", reply.error); + } else { + const rx = reply as Nip46Response; + if (rx.error) { + pending.reject(new Error(rx.error)); + } else { + pending.resolve(rx); + } + this.#commandQueue.delete(reply.id); + } } async #rpc(method: string, params: Array) {