Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Split NWC client from NostrWeblnProvider #197

Merged
merged 7 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions examples/nwc/client/get-balance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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.getBalance();

console.info(response);

client.close();
24 changes: 24 additions & 0 deletions examples/nwc/client/get-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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.getInfo();

console.info(response);

client.close();
37 changes: 37 additions & 0 deletions examples/nwc/client/list-transactions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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 ONE_WEEK_IN_SECONDS = 60 * 60 * 24 * 7;
const response = await client.listTransactions({
from: Math.floor(new Date().getTime() / 1000 - ONE_WEEK_IN_SECONDS),
until: Math.ceil(new Date().getTime() / 1000),
limit: 30,
// type: "incoming",
// unpaid: true,
});

console.info(
response.transactions.length + " transactions, ",
response.transactions.filter((t) => t.type === "incoming").length +
" incoming",
response,
);

client.close();
35 changes: 35 additions & 0 deletions examples/nwc/client/lookup-invoice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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://...): "));

const invoiceOrPaymentHash = await rl.question("Invoice or payment hash: ");
rl.close();

const client = new nwc.NWCClient({
nostrWalletConnectUrl: nwcUrl,
});

const response = await client.lookupInvoice({
// provide one of the below
invoice: invoiceOrPaymentHash.startsWith("ln")
? invoiceOrPaymentHash
: undefined,
payment_hash: !invoiceOrPaymentHash.startsWith("ln")
? invoiceOrPaymentHash
: undefined,
});

console.info(response);

client.close();
28 changes: 28 additions & 0 deletions examples/nwc/client/make-invoice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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.makeInvoice({
amount: 1000, // in millisats
description: "NWC Client example",
});

console.info(response);

client.close();
52 changes: 52 additions & 0 deletions examples/nwc/client/multi-pay-invoice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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 { LightningAddress } from "@getalby/lightning-tools";

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 ln = new LightningAddress(process.env.LN_ADDRESS || "[email protected]");
// fetch the LNURL data
await ln.fetch();

// generate 2 invoices to pay
const invoices = (
await Promise.all(
[1, 2].map((v) =>
ln.requestInvoice({
satoshi: 1,
comment: `Multi-pay invoice #${v}`,
}),
),
)
).map((invoice) => invoice.paymentRequest);

console.info("Generated two invoices", invoices);

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

const client = new nwc.NWCClient({
nostrWalletConnectUrl: nwcUrl,
});

try {
const response = await client.multiPayInvoice({
invoices: invoices.map((invoice) => ({
invoice,
})),
});
console.info(response);
} catch (error) {
console.error("multi_pay_invoice failed", error);
}

client.close();
61 changes: 61 additions & 0 deletions examples/nwc/client/multi-pay-keysend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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 keysends = [
{
pubkey:
"030a58b8653d32b99200a2334cfe913e51dc7d155aa0116c176657a4f1722677a3",
amount: 1000, // millisats
tlv_records: [
{
type: 696969,
value: "017rsl75kNnSke4mMHYE", // [email protected]
},
{
type: 34349334,
value: "first keysend message",
},
],
},
{
pubkey:
"030a58b8653d32b99200a2334cfe913e51dc7d155aa0116c176657a4f1722677a3",
amount: 1000, // millisats
tlv_records: [
{
type: 696969,
value: "1KOZHzhLs2U7JIx3BmEY", // another Alby account
},
{
type: 34349334,
value: "second keysend message",
},
],
},
];

try {
const response = await client.multiPayKeysend({ keysends });
console.info(JSON.stringify(response));
} catch (error) {
console.error("multi_pay_keysend failed", error);
}

client.close();
26 changes: 26 additions & 0 deletions examples/nwc/client/pay-invoice.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 { 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://...): "));
const invoice = await rl.question("Lightning invoice: ");
rl.close();

const client = new nwc.NWCClient({
nostrWalletConnectUrl: nwcUrl,
});

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

console.info(response);

client.close();
38 changes: 38 additions & 0 deletions examples/nwc/client/pay-keysend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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.payKeysend({
amount: 1000, // millisats
pubkey: "030a58b8653d32b99200a2334cfe913e51dc7d155aa0116c176657a4f1722677a3",
tlv_records: [
{
type: 696969,
value: "017rsl75kNnSke4mMHYE", // [email protected]
},
{
type: 34349334,
value: "example keysend message",
},
],
});

console.info(response);

client.close();
13 changes: 6 additions & 7 deletions examples/nwc/keysend.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ const rl = readline.createInterface({ input, output });
const nwcUrl =
process.env.NWC_URL ||
(await rl.question("Nostr Wallet Connect URL (nostr+walletconnect://...): "));
const destination =
(await rl.question("Enter destination pubkey: ")) ||
"030a58b8653d32b99200a2334cfe913e51dc7d155aa0116c176657a4f1722677a3";
const amount = await rl.question("Enter amount: ");

rl.close();

const webln = new providers.NostrWebLNProvider({
nostrWalletConnectUrl: nwcUrl,
});
await webln.enable();
const response = await webln.keysend({
amount,
destination,
amount: 1,
destination:
"030a58b8653d32b99200a2334cfe913e51dc7d155aa0116c176657a4f1722677a3",
customRecords: {
696969: "1KOZHzhLs2U7JIx3BmEY",
696969: "017rsl75kNnSke4mMHYE", // [email protected]
34349334: "example keysend message",
},
});

Expand Down
Loading
Loading