2020117-agent 0.4.3 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/session.js +80 -20
  2. package/package.json +1 -1
package/dist/session.js CHANGED
@@ -43,12 +43,17 @@ for (const arg of process.argv.slice(2)) {
43
43
  case '--mint':
44
44
  process.env.CASHU_MINT_URL = val;
45
45
  break;
46
+ case '--nwc':
47
+ process.env.NWC_URI = val;
48
+ break;
46
49
  }
47
50
  }
48
51
  import { SwarmNode, topicFromKind } from './swarm.js';
49
52
  import { queryProviderSkill } from './p2p-customer.js';
50
53
  import { walletPayInvoice, walletGetBalance, hasApiKey } from './api.js';
51
54
  import { decodeCashuToken, sendCashuToken, createMintQuote, claimMintQuote } from './cashu.js';
55
+ import { parseNwcUri, nwcGetBalance, nwcPayInvoice } from './nwc.js';
56
+ import { loadSovereignKeys } from './nostr.js';
52
57
  import { randomBytes } from 'crypto';
53
58
  import { createServer } from 'http';
54
59
  import { createInterface } from 'readline';
@@ -60,6 +65,11 @@ const BUDGET = Number(process.env.BUDGET_SATS) || 500;
60
65
  const PORT = Number(process.env.SESSION_PORT) || 8080;
61
66
  const CASHU_TOKEN = process.env.CASHU_TOKEN || '';
62
67
  const MINT_URL = process.env.CASHU_MINT_URL || 'https://8333.space:3338';
68
+ // Load NWC URI: --nwc flag > .2020117_keys nwc_uri > none
69
+ let nwcParsed = null;
70
+ const nwcUri = process.env.NWC_URI
71
+ || loadSovereignKeys(process.env.AGENT)?.nwc_uri
72
+ || null;
63
73
  // Mutable Cashu wallet state (loaded from CASHU_TOKEN at startup)
64
74
  let cashuState = null;
65
75
  const TICK_INTERVAL_MS = 60_000;
@@ -184,23 +194,43 @@ function setupMessageHandler() {
184
194
  break;
185
195
  }
186
196
  if (msg.bolt11) {
187
- // Invoice mode: pay bolt11 via wallet
197
+ // Invoice mode: pay bolt11 via NWC direct or platform API
188
198
  log(`Paying invoice: ${amount} sats...`);
189
- const payResult = await walletPayInvoice(msg.bolt11);
190
- if (payResult.ok) {
191
- state.totalSpent += amount;
192
- log(`Paid ${amount} sats (total: ${state.totalSpent}, ~${estimatedMinutesLeft()} min left)`);
193
- state.node.send(state.socket, {
194
- type: 'session_tick_ack',
195
- id: msg.id,
196
- session_id: state.sessionId,
197
- preimage: payResult.preimage,
198
- amount,
199
- });
199
+ if (nwcParsed) {
200
+ try {
201
+ const { preimage } = await nwcPayInvoice(nwcParsed, msg.bolt11);
202
+ state.totalSpent += amount;
203
+ log(`Paid ${amount} sats via NWC (total: ${state.totalSpent}, ~${estimatedMinutesLeft()} min left)`);
204
+ state.node.send(state.socket, {
205
+ type: 'session_tick_ack',
206
+ id: msg.id,
207
+ session_id: state.sessionId,
208
+ preimage,
209
+ amount,
210
+ });
211
+ }
212
+ catch (e) {
213
+ warn(`NWC invoice payment failed: ${e.message} — ending session`);
214
+ endSession();
215
+ }
200
216
  }
201
217
  else {
202
- warn(`Invoice payment failed: ${payResult.error} ending session`);
203
- endSession();
218
+ const payResult = await walletPayInvoice(msg.bolt11);
219
+ if (payResult.ok) {
220
+ state.totalSpent += amount;
221
+ log(`Paid ${amount} sats (total: ${state.totalSpent}, ~${estimatedMinutesLeft()} min left)`);
222
+ state.node.send(state.socket, {
223
+ type: 'session_tick_ack',
224
+ id: msg.id,
225
+ session_id: state.sessionId,
226
+ preimage: payResult.preimage,
227
+ amount,
228
+ });
229
+ }
230
+ else {
231
+ warn(`Invoice payment failed: ${payResult.error} — ending session`);
232
+ endSession();
233
+ }
204
234
  }
205
235
  }
206
236
  else if (cashuState) {
@@ -743,8 +773,40 @@ async function main() {
743
773
  warn(`Cashu token (${tokenAmount} sats) is less than budget (${BUDGET} sats)`);
744
774
  }
745
775
  }
776
+ else if (nwcUri) {
777
+ // NWC direct: auto-mint Cashu tokens via local NWC wallet (no platform API)
778
+ nwcParsed = parseNwcUri(nwcUri);
779
+ const { balance_msats } = await nwcGetBalance(nwcParsed);
780
+ const balance = Math.floor(balance_msats / 1000);
781
+ if (balance <= 0) {
782
+ warn('NWC wallet balance is 0. Cannot auto-mint Cashu tokens.');
783
+ await node.destroy();
784
+ process.exit(1);
785
+ }
786
+ const mintAmount = Math.min(balance, BUDGET);
787
+ log(`NWC wallet balance: ${balance} sats — minting ${mintAmount} sats from ${MINT_URL}`);
788
+ try {
789
+ const { quote, invoice } = await createMintQuote(MINT_URL, mintAmount);
790
+ log(`Mint quote: ${quote} (invoice: ${invoice.slice(0, 30)}...)`);
791
+ log('Paying mint invoice via NWC...');
792
+ const { preimage } = await nwcPayInvoice(nwcParsed, invoice);
793
+ log(`Invoice paid (preimage: ${preimage?.slice(0, 16)}...)`);
794
+ log('Claiming minted tokens...');
795
+ const token = await claimMintQuote(MINT_URL, mintAmount, quote);
796
+ const { mint, proofs } = decodeCashuToken(token);
797
+ cashuState = { mintUrl: mint, proofs };
798
+ paymentMethod = 'cashu';
799
+ const totalMinted = proofs.reduce((s, p) => s + p.amount, 0);
800
+ log(`Minted ${totalMinted} sats Cashu token — using Cashu payment mode`);
801
+ }
802
+ catch (e) {
803
+ warn(`NWC auto-mint failed: ${e.message}`);
804
+ await node.destroy();
805
+ process.exit(1);
806
+ }
807
+ }
746
808
  else if (hasApiKey()) {
747
- // Auto-mint Cashu tokens via NWC wallet
809
+ // Platform API fallback: auto-mint Cashu tokens via platform wallet proxy
748
810
  const balance = await walletGetBalance();
749
811
  if (balance <= 0) {
750
812
  warn('Wallet balance is 0. Cannot auto-mint Cashu tokens.');
@@ -754,17 +816,14 @@ async function main() {
754
816
  const mintAmount = Math.min(balance, BUDGET);
755
817
  log(`Wallet balance: ${balance} sats — minting ${mintAmount} sats from ${MINT_URL}`);
756
818
  try {
757
- // 1. Request mint quote (Lightning invoice)
758
819
  const { quote, invoice } = await createMintQuote(MINT_URL, mintAmount);
759
820
  log(`Mint quote: ${quote} (invoice: ${invoice.slice(0, 30)}...)`);
760
- // 2. Pay the invoice via platform NWC wallet
761
- log('Paying mint invoice via NWC wallet...');
821
+ log('Paying mint invoice via platform wallet...');
762
822
  const payResult = await walletPayInvoice(invoice);
763
823
  if (!payResult.ok) {
764
824
  throw new Error(`Payment failed: ${payResult.error}`);
765
825
  }
766
826
  log(`Invoice paid (preimage: ${payResult.preimage?.slice(0, 16)}...)`);
767
- // 3. Claim minted proofs
768
827
  log('Claiming minted tokens...');
769
828
  const token = await claimMintQuote(MINT_URL, mintAmount, quote);
770
829
  const { mint, proofs } = decodeCashuToken(token);
@@ -782,7 +841,8 @@ async function main() {
782
841
  else {
783
842
  warn('No payment method available.');
784
843
  warn(' Option 1 (default): --cashu-token=cashuA... (Cashu eCash token)');
785
- warn(' Option 2: --agent=NAME (auto-mints Cashu via NWC wallet)');
844
+ warn(' Option 2: --nwc="nostr+walletconnect://..." (NWC direct wallet)');
845
+ warn(' Option 3: --agent=NAME (auto-mints via platform API)');
786
846
  await node.destroy();
787
847
  process.exit(1);
788
848
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "2020117-agent",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "description": "2020117 agent runtime — API polling + Hyperswarm P2P + Sovereign Nostr mode + Cashu/Lightning payments",
5
5
  "type": "module",
6
6
  "bin": {