From 3c4bffbfbe7b2f789e7365f8abfa32e514732684 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Thu, 28 Dec 2023 21:00:20 +0700 Subject: [PATCH] fix: allow choosing request methods in authorization url also rename c to name (already supported by nwc) --- package.json | 2 +- src/types.ts | 8 +++++++- src/webln/NostrWeblnProvider.test.ts | 3 ++- src/webln/NostrWeblnProvider.ts | 15 ++++++++++----- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index c618a7d..12a7650 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@getalby/sdk", - "version": "3.2.2", + "version": "3.2.3", "description": "The SDK to integrate with Nostr Wallet Connect and the Alby API", "repository": "https://github.com/getAlby/js-sdk.git", "bugs": "https://github.com/getAlby/js-sdk/issues", diff --git a/src/types.ts b/src/types.ts index 5734878..ea32871 100644 --- a/src/types.ts +++ b/src/types.ts @@ -229,8 +229,14 @@ export type Invoice = { }; } & Record; -export type GetNWCAuthorizationUrlOptions = { +/** + * @deprecated please use NWCAuthorizationUrlOptions + */ +export type GetNWCAuthorizationUrlOptions = NWCAuthorizationUrlOptions; + +export type NWCAuthorizationUrlOptions = { name?: string; + requestMethods?: string[]; returnTo?: string; expiresAt?: Date; maxAmount?: number; diff --git a/src/webln/NostrWeblnProvider.test.ts b/src/webln/NostrWeblnProvider.test.ts index e22e484..6917473 100644 --- a/src/webln/NostrWeblnProvider.test.ts +++ b/src/webln/NostrWeblnProvider.test.ts @@ -15,10 +15,11 @@ describe("isValidAmount", () => { maxAmount: 100, name: "TestApp", returnTo: "https://example.com", + requestMethods: ["pay_invoice", "get_balance"], }) .toString(), ).toEqual( - "https://nwc.getalby.com/apps/new?c=TestApp&pubkey=c5dc47856f533dad6c016b979ee3b21f83f88ae0f0058001b67a4b348339fe94&return_to=https%3A%2F%2Fexample.com&budget_renewal=weekly&expires_at=1689897600&max_amount=100&editable=false", + "https://nwc.getalby.com/apps/new?name=TestApp&pubkey=c5dc47856f533dad6c016b979ee3b21f83f88ae0f0058001b67a4b348339fe94&return_to=https%3A%2F%2Fexample.com&budget_renewal=weekly&expires_at=1689897600&max_amount=100&editable=false&request_methods=pay_invoice+get_balance", ); }); }); diff --git a/src/webln/NostrWeblnProvider.ts b/src/webln/NostrWeblnProvider.ts index cd78c35..99fa6db 100644 --- a/src/webln/NostrWeblnProvider.ts +++ b/src/webln/NostrWeblnProvider.ts @@ -25,7 +25,7 @@ import { LookupInvoiceResponse, } from "@webbtc/webln-types"; import { GetInfoResponse } from "@webbtc/webln-types"; -import { GetNWCAuthorizationUrlOptions } from "../types"; +import { NWCAuthorizationUrlOptions } from "../types"; const NWCs: Record = { alby: { @@ -439,13 +439,13 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider { throw new Error("Method not implemented."); } - getAuthorizationUrl(options?: GetNWCAuthorizationUrlOptions) { + getAuthorizationUrl(options?: NWCAuthorizationUrlOptions) { if (!this.options.authorizationUrl) { throw new Error("Missing authorizationUrl option"); } const url = new URL(this.options.authorizationUrl); if (options?.name) { - url.searchParams.set("c", options?.name); + url.searchParams.set("name", options?.name); } url.searchParams.set("pubkey", this.publicKey); if (options?.returnTo) { @@ -468,10 +468,14 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider { url.searchParams.set("editable", options.editable.toString()); } + if (options?.requestMethods) { + url.searchParams.set("request_methods", options.requestMethods.join(" ")); + } + return url; } - initNWC(options: GetNWCAuthorizationUrlOptions = {}) { + initNWC(options: NWCAuthorizationUrlOptions = {}) { // here we assume an browser context and window/document is available // we set the location.host as a default name if none is given if (!options.name) { @@ -585,13 +589,14 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider { const replyTimeoutCheck = setTimeout(replyTimeout, 60000); sub.on("event", async (event) => { - //console.log(`Received reply event: `, event); + // console.log(`Received reply event: `, event); clearTimeout(replyTimeoutCheck); sub.unsub(); const decryptedContent = await this.decrypt( this.walletPubkey, event.content, ); + // console.log(`Decrypted content: `, decryptedContent); let response; try { response = JSON.parse(decryptedContent);