-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #197 from getAlby/feat/nwc-client
feat: Split NWC client from NostrWeblnProvider
- Loading branch information
Showing
17 changed files
with
1,460 additions
and
663 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", // hello@getalby.com | ||
}, | ||
{ | ||
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", // hello@getalby.com | ||
}, | ||
{ | ||
type: 34349334, | ||
value: "example keysend message", | ||
}, | ||
], | ||
}); | ||
|
||
console.info(response); | ||
|
||
client.close(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.