Skip to content

Commit

Permalink
feat: add budget options to initNWC
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jun 24, 2023
1 parent 3c30704 commit 2652c82
Show file tree
Hide file tree
Showing 6 changed files with 1,733 additions and 20 deletions.
5 changes: 5 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"scripts": {
"prebuild": "yarn run clean",
"prepack": "yarn run build",
"test": "echo tests are missing",
"test": "jest",
"clean": "rm -rf dist",
"build": "microbundle",
"dev": "microbundle watch"
Expand All @@ -35,10 +35,14 @@
},
"devDependencies": {
"@types/crypto-js": "^4.1.1",
"@types/jest": "^29.4.0",
"@types/node": "^18.11.0",
"@webbtc/webln-types": "^1.0.11",
"express": "^4.18.2",
"jest": "^29.5.0",
"microbundle": "^0.15.1",
"ts-jest": "^29.0.5",
"ts-node": "^10.9.1",
"typescript": "^4.8.4",
"websocket-polyfill": "^0.0.3"
},
Expand Down
11 changes: 10 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,13 @@ export type Invoice = {
payer_data?: unknown;
zap_request?: unknown;
}
} & Record<string, unknown>;
} & Record<string, unknown>;

export type GetNWCAuthorizationUrlOptions = {
name?: string;
returnTo?: string;
expiresAt?: Date;
maxAmount?: number;
budgetRenewal?: "never" | "daily" | "weekly" | "monthly" | "yearly";
editable?: boolean;
};
17 changes: 17 additions & 0 deletions src/webln/NostrWeblnProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { NostrWebLNProvider } from "./NostrWeblnProvider";
import "websocket-polyfill";

describe("isValidAmount", () => {
test("getAuthorizationUrl generates correct url with budget and pubkey from custom secret", () => {
expect(new NostrWebLNProvider({
secret: "906d83543db6022b851f395ebbef055c980e5e1432bd54c30318a842fbaf84c3"
}).getAuthorizationUrl({
budgetRenewal: "weekly",
editable: false,
expiresAt: new Date("2023-07-21"),
maxAmount: 100,
name: "TestApp",
returnTo: "https://example.com"
}).toString()).toEqual("https://nwc.getalby.com/apps/new?c=TestApp&pubkey=c5dc47856f533dad6c016b979ee3b21f83f88ae0f0058001b67a4b348339fe94&return_to=https%3A%2F%2Fexample.com&budget_renewal=weekly&expires_at=2023-07-21T00%3A00%3A00.000Z&max_amount=100&editable=false");
});
});
21 changes: 19 additions & 2 deletions src/webln/NostrWeblnProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from 'nostr-tools';
import { KeysendArgs, RequestInvoiceArgs, RequestInvoiceResponse, RequestMethod, SendPaymentResponse, SignMessageResponse, WebLNNode, WebLNProvider } from "@webbtc/webln-types";
import { GetInfoResponse } from '@webbtc/webln-types';
import { GetNWCAuthorizationUrlOptions } from '../types';

const NWCs: Record<string,NostrWebLNOptions> = {
alby: {
Expand Down Expand Up @@ -265,7 +266,7 @@ export class NostrWebLNProvider implements WebLNProvider {
throw new Error('Method not implemented.');
}

getAuthorizationUrl(options: { name?: string, returnTo?: string }) {
getAuthorizationUrl(options: GetNWCAuthorizationUrlOptions) {
if (!this.options.authorizationUrl) {
throw new Error("Missing authorizationUrl option");
}
Expand All @@ -277,10 +278,26 @@ export class NostrWebLNProvider implements WebLNProvider {
if (options?.returnTo) {
url.searchParams.set('return_to', options.returnTo);
}

if (options.budgetRenewal) {
url.searchParams.set("budget_renewal", options.budgetRenewal)
}
if (options.expiresAt) {
url.searchParams.set("expires_at", options.expiresAt.toISOString())
}
if (options.maxAmount) {
url.searchParams.set("max_amount", options.maxAmount.toString())
}
if (options.editable !== undefined) {
url.searchParams.set("editable", options.editable.toString())
}

return url;
}

initNWC(options: { name?: string, returnTo?: string } = {}) {


initNWC(options: GetNWCAuthorizationUrlOptions = {}) {
// 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
Loading

0 comments on commit 2652c82

Please sign in to comment.