UNPKG

n8n-nodes-nwc

Version:

n8n nodes for Nostr Wallet Connect (NWC) protocol

292 lines (291 loc) 12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Nwc = void 0; require("websocket-polyfill"); const lightning_tools_1 = require("@getalby/lightning-tools"); const sdk_1 = require("@getalby/sdk"); class Nwc { constructor() { this.description = { displayName: 'NWC', name: 'nwc', icon: 'file:nwc.svg', group: ['transform'], version: 1, subtitle: '={{$parameter["operation"]}}', description: 'Interact with Nostr Wallet Connect (NWC) protocol', defaults: { name: 'NWC', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'nwcUrl', required: true, }, ], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'Send Satoshis', value: 'send', description: 'Send satoshis to a lightning address or invoice', action: 'Send satoshis to a lightning address or invoice', }, { name: 'Make invoice', value: 'makeInvoice', description: 'Create an invoice to receive satoshis', action: 'Create an invoice to receive satoshis', }, { name: 'Lookup Invoice', value: 'lookupInvoice', description: 'Look up invoice details from a BOLT-11 invoice or payment hash', action: 'Look up invoice details', }, { name: 'List Transactions', value: 'list', description: 'List recent transactions', action: 'List recent transactions', }, { name: 'Get Info', value: 'getInfo', description: 'Get wallet information and capabilities', action: 'Get wallet information and capabilities', }, { name: 'Get Balance', value: 'getBalance', description: 'Get current wallet balance', action: 'Get current wallet balance', }, { name: 'Make Keysend Payment', value: 'keysend', description: 'Send payment using keysend (no invoice required)', action: 'Send payment using keysend', }, { name: 'Sign Message', value: 'signMessage', description: 'Sign a message with the wallet', action: 'Sign a message with the wallet', }, ], default: 'send', }, { displayName: 'Amount (sats)', name: 'amount', type: 'number', default: 1000, displayOptions: { show: { operation: ['send', 'makeInvoice', 'keysend'], }, }, description: 'Amount in satoshis', required: true, }, { displayName: 'Recipient', name: 'recipient', type: 'string', default: '', displayOptions: { show: { operation: ['send'], }, }, description: 'Lightning address (e.g., user@domain.com) or BOLT-11 invoice to send to', required: true, }, { displayName: 'Description', name: 'description', type: 'string', default: '', displayOptions: { show: { operation: ['send', 'makeInvoice'], }, }, description: 'Optional description for the payment/invoice', }, { displayName: 'Limit', name: 'limit', type: 'number', default: 10, displayOptions: { show: { operation: ['list'], }, }, description: 'Maximum number of transactions to return', }, { displayName: 'Destination', name: 'destination', type: 'string', default: '', displayOptions: { show: { operation: ['keysend'], }, }, description: 'Destination node public key for keysend payment', required: true, }, { displayName: 'Memo', name: 'keysendMemo', type: 'string', default: '', displayOptions: { show: { operation: ['keysend'], }, }, description: 'Optional memo for the keysend payment', }, { displayName: 'Message', name: 'message', type: 'string', default: '', displayOptions: { show: { operation: ['signMessage'], }, }, description: 'Message to sign', required: true, }, { displayName: 'Invoice or Payment Hash', name: 'invoiceOrHash', type: 'string', default: '', displayOptions: { show: { operation: ['lookupInvoice'], }, }, description: 'BOLT-11 invoice or payment hash to look up', required: true, }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; const operation = this.getNodeParameter('operation', 0); const credentials = await this.getCredentials('nwcUrl'); const nostrWalletConnectUrl = credentials.nwcUrl; for (let i = 0; i < items.length; i++) { let nwcClient; try { nwcClient = new sdk_1.nwc.NWCClient({ nostrWalletConnectUrl, }); if (operation === 'send') { const amount = this.getNodeParameter('amount', i); const description = this.getNodeParameter('description', i); let invoice = this.getNodeParameter('recipient', i); if (invoice.includes('@')) { const lnAddress = new lightning_tools_1.LightningAddress(invoice); await lnAddress.fetch(); const invoiceResponse = await lnAddress.requestInvoice({ satoshi: amount, comment: description, }); invoice = invoiceResponse.paymentRequest; } const result = await nwcClient.payInvoice({ amount, invoice, }); returnData.push(result); } else if (operation === 'makeInvoice') { const amount = this.getNodeParameter('amount', i); const description = this.getNodeParameter('description', i); const result = await nwcClient.makeInvoice({ amount, description, }); returnData.push(result); } else if (operation === 'list') { const limit = this.getNodeParameter('limit', i); const result = await nwcClient.listTransactions({ limit, }); returnData.push(result); } else if (operation === 'getInfo') { const result = await nwcClient.getInfo(); returnData.push(result); } else if (operation === 'getBalance') { const result = await nwcClient.getBalance(); returnData.push(result); } else if (operation === 'keysend') { const destination = this.getNodeParameter('destination', i); const amount = this.getNodeParameter('amount', i); const memo = this.getNodeParameter('keysendMemo', i); const result = await nwcClient.payKeysend({ amount, pubkey: destination, tlv_records: memo ? [{ type: 34349334, value: memo }] : undefined, }); returnData.push(result); } else if (operation === 'signMessage') { const message = this.getNodeParameter('message', i); const result = await nwcClient.signMessage({ message, }); returnData.push(result); } else if (operation === 'lookupInvoice') { const invoiceOrHash = this.getNodeParameter('invoiceOrHash', i); const params = invoiceOrHash.startsWith('lnbc') ? { invoice: invoiceOrHash } : { payment_hash: invoiceOrHash }; const result = await nwcClient.lookupInvoice(params); returnData.push(result); } else { returnData.push({ error: 'Invalid operation' }); } } catch (error) { if (this.continueOnFail()) { returnData.push({ error: error instanceof Error ? error.message : String(error) }); continue; } throw error; } finally { if (nwcClient) { await nwcClient.close(); } } } return [this.helpers.returnJsonArray(returnData)]; } } exports.Nwc = Nwc;