Skip to content

Commit

Permalink
fix: allow choosing request methods in authorization url
Browse files Browse the repository at this point in the history
also rename c to name (already supported by nwc)
  • Loading branch information
rolznz committed Dec 28, 2023
1 parent 4710871 commit 3c4bffb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,14 @@ export type Invoice = {
};
} & Record<string, unknown>;

export type GetNWCAuthorizationUrlOptions = {
/**
* @deprecated please use NWCAuthorizationUrlOptions
*/
export type GetNWCAuthorizationUrlOptions = NWCAuthorizationUrlOptions;

export type NWCAuthorizationUrlOptions = {
name?: string;
requestMethods?: string[];
returnTo?: string;
expiresAt?: Date;
maxAmount?: number;
Expand Down
3 changes: 2 additions & 1 deletion src/webln/NostrWeblnProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
});
});
15 changes: 10 additions & 5 deletions src/webln/NostrWeblnProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, NostrWebLNOptions> = {
alby: {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 3c4bffb

Please sign in to comment.