Skip to content

Commit

Permalink
Merge branch 'master' into feat/info-notifications-tag
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz authored Apr 10, 2024
2 parents 45e6c22 + f55216d commit 1b15c47
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 9 deletions.
27 changes: 27 additions & 0 deletions examples/nwc/client/sign-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as crypto from "node:crypto"; // required in node.js
global.crypto = crypto; // required in node.js
import "websocket-polyfill"; // required in node.js

import * as readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";

import { nwc } from "../../../dist/index.module.js";

const rl = readline.createInterface({ input, output });

const nwcUrl =
process.env.NWC_URL ||
(await rl.question("Nostr Wallet Connect URL (nostr+walletconnect://...): "));

rl.close();

const client = new nwc.NWCClient({
nostrWalletConnectUrl: nwcUrl,
});
const response = await client.signMessage({
message: "Hello, world!",
});

console.info(response);

client.close();
26 changes: 26 additions & 0 deletions examples/nwc/sign-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as crypto from "node:crypto"; // required in node.js
global.crypto = crypto; // required in node.js
import "websocket-polyfill"; // required in node.js

import * as readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";

import { webln as providers } from "../../dist/index.module.js";

const rl = readline.createInterface({ input, output });

const nwcUrl =
process.env.NWC_URL ||
(await rl.question("Nostr Wallet Connect URL (nostr+walletconnect://...): "));

rl.close();

const webln = new providers.NostrWebLNProvider({
nostrWalletConnectUrl: nwcUrl,
});
await webln.enable();
const response = await webln.signMessage("Hello, world!");

console.info(response);

webln.close();
28 changes: 27 additions & 1 deletion src/NWCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ type Nip47SingleMethod =
| "pay_invoice"
| "pay_keysend"
| "lookup_invoice"
| "list_transactions";
| "list_transactions"
| "sign_message";

type Nip47MultiMethod = "multi_pay_invoice" | "multi_pay_keysend";

Expand Down Expand Up @@ -131,6 +132,15 @@ export type Nip47LookupInvoiceRequest = {
invoice?: string;
};

export type Nip47SignMessageRequest = {
message: string;
};

export type Nip47SignMessageResponse = {
message: string;
signature: string;
};

export interface NWCOptions {
authorizationUrl?: string; // the URL to the NWC interface for the user to confirm the session
relayUrl: string;
Expand Down Expand Up @@ -503,6 +513,22 @@ export class NWCClient {
throw error;
}
}
async signMessage(
request: Nip47SignMessageRequest,
): Promise<Nip47SignMessageResponse> {
try {
const result = await this.executeNip47Request<Nip47SignMessageResponse>(
"sign_message",
request,
(result) => result.message === request.message && !!result.signature,
);

return result;
} catch (error) {
console.error("Failed to request sign_message", error);
throw error;
}
}

async multiPayInvoice(
request: Nip47MultiPayInvoiceRequest,
Expand Down
30 changes: 22 additions & 8 deletions src/webln/NostrWeblnProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const nip47ToWeblnRequestMap: Record<Nip47Method, WebLNMethod> = {
list_transactions: "listTransactions",
multi_pay_invoice: "sendMultiPayment",
multi_pay_keysend: "multiKeysend",
sign_message: "signMessage",
};

export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
Expand Down Expand Up @@ -292,7 +293,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {

const nip47Result = await this.client.payInvoice({ invoice });

const result = { preimage: nip47Result.preimage };
const result: SendPaymentResponse = { preimage: nip47Result.preimage };
this.notify("sendPayment", result);

return result;
Expand All @@ -312,11 +313,27 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
async keysend(args: KeysendArgs): Promise<SendPaymentResponse> {
await this.checkEnabled();

const nip47Result = await this.client.payKeysend(
const nip47Result: SendPaymentResponse = await this.client.payKeysend(
mapKeysendToNip47Keysend(args),
);

const result = { preimage: nip47Result.preimage };
const result: SendPaymentResponse = { preimage: nip47Result.preimage };
this.notify("keysend", result);

return result;
}

async signMessage(message: string): Promise<SignMessageResponse> {
await this.checkEnabled();

const nip47Result = await this.client.signMessage({
message,
});

const result: SignMessageResponse = {
message: nip47Result.message,
signature: nip47Result.signature,
};
this.notify("keysend", result);

return result;
Expand All @@ -343,7 +360,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
//description_hash: "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
});

const result = { paymentRequest: nip47Result.invoice };
const result: MakeInvoiceResponse = { paymentRequest: nip47Result.invoice };

this.notify("makeInvoice", result);

Expand Down Expand Up @@ -376,7 +393,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {

const nip47Result = await this.client.listTransactions(args);

const result = {
const result: ListTransactionsResponse = {
transactions: nip47Result.transactions.map(
mapNip47TransactionToTransaction,
),
Expand Down Expand Up @@ -460,9 +477,6 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
request(method: WebLNRequestMethod, args?: unknown): Promise<unknown> {
throw new Error("Method not implemented.");
}
signMessage(message: string): Promise<SignMessageResponse> {
throw new Error("Method not implemented.");
}
verifyMessage(signature: string, message: string): Promise<void> {
throw new Error("Method not implemented.");
}
Expand Down

0 comments on commit 1b15c47

Please sign in to comment.