openclaw-overlay-plugin 0.7.22

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 (221) hide show
  1. package/README.md +406 -0
  2. package/SKILL.md +78 -0
  3. package/clawdbot.plugin.json +106 -0
  4. package/dist/cli-main.d.ts +7 -0
  5. package/dist/cli-main.js +192 -0
  6. package/dist/cli.d.ts +8 -0
  7. package/dist/cli.js +14 -0
  8. package/dist/core/config.d.ts +11 -0
  9. package/dist/core/config.js +13 -0
  10. package/dist/core/index.d.ts +25 -0
  11. package/dist/core/index.js +26 -0
  12. package/dist/core/payment.d.ts +16 -0
  13. package/dist/core/payment.js +94 -0
  14. package/dist/core/types.d.ts +94 -0
  15. package/dist/core/types.js +4 -0
  16. package/dist/core/verify.d.ts +28 -0
  17. package/dist/core/verify.js +104 -0
  18. package/dist/core/wallet.d.ts +99 -0
  19. package/dist/core/wallet.js +219 -0
  20. package/dist/scripts/baemail/commands.d.ts +64 -0
  21. package/dist/scripts/baemail/commands.js +258 -0
  22. package/dist/scripts/baemail/handler.d.ts +36 -0
  23. package/dist/scripts/baemail/handler.js +284 -0
  24. package/dist/scripts/baemail/index.d.ts +5 -0
  25. package/dist/scripts/baemail/index.js +5 -0
  26. package/dist/scripts/config.d.ts +48 -0
  27. package/dist/scripts/config.js +68 -0
  28. package/dist/scripts/index.d.ts +7 -0
  29. package/dist/scripts/index.js +7 -0
  30. package/dist/scripts/messaging/connect.d.ts +8 -0
  31. package/dist/scripts/messaging/connect.js +114 -0
  32. package/dist/scripts/messaging/handlers.d.ts +21 -0
  33. package/dist/scripts/messaging/handlers.js +334 -0
  34. package/dist/scripts/messaging/inbox.d.ts +11 -0
  35. package/dist/scripts/messaging/inbox.js +51 -0
  36. package/dist/scripts/messaging/index.d.ts +8 -0
  37. package/dist/scripts/messaging/index.js +8 -0
  38. package/dist/scripts/messaging/poll.d.ts +7 -0
  39. package/dist/scripts/messaging/poll.js +52 -0
  40. package/dist/scripts/messaging/send.d.ts +7 -0
  41. package/dist/scripts/messaging/send.js +43 -0
  42. package/dist/scripts/output.d.ts +12 -0
  43. package/dist/scripts/output.js +19 -0
  44. package/dist/scripts/overlay/discover.d.ts +7 -0
  45. package/dist/scripts/overlay/discover.js +72 -0
  46. package/dist/scripts/overlay/index.d.ts +7 -0
  47. package/dist/scripts/overlay/index.js +7 -0
  48. package/dist/scripts/overlay/registration.d.ts +19 -0
  49. package/dist/scripts/overlay/registration.js +176 -0
  50. package/dist/scripts/overlay/services.d.ts +29 -0
  51. package/dist/scripts/overlay/services.js +167 -0
  52. package/dist/scripts/overlay/transaction.d.ts +42 -0
  53. package/dist/scripts/overlay/transaction.js +103 -0
  54. package/dist/scripts/payment/build.d.ts +24 -0
  55. package/dist/scripts/payment/build.js +54 -0
  56. package/dist/scripts/payment/commands.d.ts +15 -0
  57. package/dist/scripts/payment/commands.js +73 -0
  58. package/dist/scripts/payment/index.d.ts +6 -0
  59. package/dist/scripts/payment/index.js +6 -0
  60. package/dist/scripts/payment/types.d.ts +56 -0
  61. package/dist/scripts/payment/types.js +4 -0
  62. package/dist/scripts/services/index.d.ts +6 -0
  63. package/dist/scripts/services/index.js +6 -0
  64. package/dist/scripts/services/queue.d.ts +11 -0
  65. package/dist/scripts/services/queue.js +28 -0
  66. package/dist/scripts/services/request.d.ts +7 -0
  67. package/dist/scripts/services/request.js +82 -0
  68. package/dist/scripts/services/respond.d.ts +11 -0
  69. package/dist/scripts/services/respond.js +132 -0
  70. package/dist/scripts/types.d.ts +107 -0
  71. package/dist/scripts/types.js +4 -0
  72. package/dist/scripts/utils/index.d.ts +6 -0
  73. package/dist/scripts/utils/index.js +6 -0
  74. package/dist/scripts/utils/merkle.d.ts +12 -0
  75. package/dist/scripts/utils/merkle.js +47 -0
  76. package/dist/scripts/utils/storage.d.ts +66 -0
  77. package/dist/scripts/utils/storage.js +211 -0
  78. package/dist/scripts/utils/woc.d.ts +26 -0
  79. package/dist/scripts/utils/woc.js +91 -0
  80. package/dist/scripts/wallet/balance.d.ts +22 -0
  81. package/dist/scripts/wallet/balance.js +240 -0
  82. package/dist/scripts/wallet/identity.d.ts +70 -0
  83. package/dist/scripts/wallet/identity.js +151 -0
  84. package/dist/scripts/wallet/index.d.ts +6 -0
  85. package/dist/scripts/wallet/index.js +6 -0
  86. package/dist/scripts/wallet/setup.d.ts +15 -0
  87. package/dist/scripts/wallet/setup.js +105 -0
  88. package/dist/scripts/x-verification/commands.d.ts +27 -0
  89. package/dist/scripts/x-verification/commands.js +222 -0
  90. package/dist/scripts/x-verification/index.d.ts +4 -0
  91. package/dist/scripts/x-verification/index.js +4 -0
  92. package/dist/services/built-in/api-proxy/index.d.ts +6 -0
  93. package/dist/services/built-in/api-proxy/index.js +23 -0
  94. package/dist/services/built-in/code-develop/index.d.ts +6 -0
  95. package/dist/services/built-in/code-develop/index.js +23 -0
  96. package/dist/services/built-in/code-review/index.d.ts +10 -0
  97. package/dist/services/built-in/code-review/index.js +51 -0
  98. package/dist/services/built-in/image-analysis/index.d.ts +6 -0
  99. package/dist/services/built-in/image-analysis/index.js +33 -0
  100. package/dist/services/built-in/memory-store/index.d.ts +6 -0
  101. package/dist/services/built-in/memory-store/index.js +22 -0
  102. package/dist/services/built-in/roulette/index.d.ts +6 -0
  103. package/dist/services/built-in/roulette/index.js +27 -0
  104. package/dist/services/built-in/summarize/index.d.ts +6 -0
  105. package/dist/services/built-in/summarize/index.js +21 -0
  106. package/dist/services/built-in/tell-joke/handler.d.ts +7 -0
  107. package/dist/services/built-in/tell-joke/handler.js +122 -0
  108. package/dist/services/built-in/tell-joke/index.d.ts +9 -0
  109. package/dist/services/built-in/tell-joke/index.js +31 -0
  110. package/dist/services/built-in/translate/index.d.ts +6 -0
  111. package/dist/services/built-in/translate/index.js +21 -0
  112. package/dist/services/built-in/web-research/index.d.ts +9 -0
  113. package/dist/services/built-in/web-research/index.js +51 -0
  114. package/dist/services/index.d.ts +13 -0
  115. package/dist/services/index.js +14 -0
  116. package/dist/services/loader.d.ts +77 -0
  117. package/dist/services/loader.js +292 -0
  118. package/dist/services/manager.d.ts +86 -0
  119. package/dist/services/manager.js +255 -0
  120. package/dist/services/registry.d.ts +98 -0
  121. package/dist/services/registry.js +204 -0
  122. package/dist/services/types.d.ts +230 -0
  123. package/dist/services/types.js +30 -0
  124. package/dist/test/cli.test.d.ts +7 -0
  125. package/dist/test/cli.test.js +329 -0
  126. package/dist/test/comprehensive-overlay.test.d.ts +13 -0
  127. package/dist/test/comprehensive-overlay.test.js +593 -0
  128. package/dist/test/key-derivation.test.d.ts +12 -0
  129. package/dist/test/key-derivation.test.js +86 -0
  130. package/dist/test/overlay-submit.test.d.ts +10 -0
  131. package/dist/test/overlay-submit.test.js +460 -0
  132. package/dist/test/request-response-flow.test.d.ts +5 -0
  133. package/dist/test/request-response-flow.test.js +209 -0
  134. package/dist/test/service-system.test.d.ts +5 -0
  135. package/dist/test/service-system.test.js +190 -0
  136. package/dist/test/utils/server-logic.d.ts +98 -0
  137. package/dist/test/utils/server-logic.js +286 -0
  138. package/dist/test/wallet.test.d.ts +7 -0
  139. package/dist/test/wallet.test.js +146 -0
  140. package/index.ts +1965 -0
  141. package/openclaw.plugin.json +106 -0
  142. package/package.json +73 -0
  143. package/src/cli-main.ts +230 -0
  144. package/src/cli.ts +16 -0
  145. package/src/core/README.md +246 -0
  146. package/src/core/config.ts +21 -0
  147. package/src/core/index.ts +42 -0
  148. package/src/core/payment.ts +111 -0
  149. package/src/core/types.ts +102 -0
  150. package/src/core/verify.ts +119 -0
  151. package/src/core/wallet.ts +282 -0
  152. package/src/scripts/baemail/commands.ts +326 -0
  153. package/src/scripts/baemail/handler.ts +338 -0
  154. package/src/scripts/baemail/index.ts +6 -0
  155. package/src/scripts/config.ts +81 -0
  156. package/src/scripts/index.ts +8 -0
  157. package/src/scripts/messaging/connect.ts +121 -0
  158. package/src/scripts/messaging/handlers.ts +394 -0
  159. package/src/scripts/messaging/inbox.ts +64 -0
  160. package/src/scripts/messaging/index.ts +9 -0
  161. package/src/scripts/messaging/poll.ts +59 -0
  162. package/src/scripts/messaging/send.ts +54 -0
  163. package/src/scripts/output.ts +21 -0
  164. package/src/scripts/overlay/discover.ts +81 -0
  165. package/src/scripts/overlay/index.ts +8 -0
  166. package/src/scripts/overlay/registration.ts +199 -0
  167. package/src/scripts/overlay/services.ts +199 -0
  168. package/src/scripts/overlay/transaction.ts +124 -0
  169. package/src/scripts/payment/build.ts +65 -0
  170. package/src/scripts/payment/commands.ts +92 -0
  171. package/src/scripts/payment/index.ts +7 -0
  172. package/src/scripts/payment/types.ts +62 -0
  173. package/src/scripts/services/index.ts +7 -0
  174. package/src/scripts/services/queue.ts +35 -0
  175. package/src/scripts/services/request.ts +98 -0
  176. package/src/scripts/services/respond.ts +149 -0
  177. package/src/scripts/types.ts +121 -0
  178. package/src/scripts/utils/index.ts +7 -0
  179. package/src/scripts/utils/merkle.ts +57 -0
  180. package/src/scripts/utils/storage.ts +231 -0
  181. package/src/scripts/utils/woc.ts +106 -0
  182. package/src/scripts/wallet/balance.ts +277 -0
  183. package/src/scripts/wallet/identity.ts +203 -0
  184. package/src/scripts/wallet/index.ts +7 -0
  185. package/src/scripts/wallet/setup.ts +121 -0
  186. package/src/scripts/x-verification/commands.ts +256 -0
  187. package/src/scripts/x-verification/index.ts +5 -0
  188. package/src/services/built-in/api-proxy/index.ts +26 -0
  189. package/src/services/built-in/api-proxy/prompt.md +26 -0
  190. package/src/services/built-in/code-develop/index.ts +26 -0
  191. package/src/services/built-in/code-develop/prompt.md +35 -0
  192. package/src/services/built-in/code-review/index.ts +54 -0
  193. package/src/services/built-in/code-review/prompt.md +105 -0
  194. package/src/services/built-in/image-analysis/index.ts +36 -0
  195. package/src/services/built-in/image-analysis/prompt.md +42 -0
  196. package/src/services/built-in/memory-store/index.ts +25 -0
  197. package/src/services/built-in/memory-store/prompt.md +45 -0
  198. package/src/services/built-in/roulette/index.ts +30 -0
  199. package/src/services/built-in/roulette/prompt.md +35 -0
  200. package/src/services/built-in/summarize/index.ts +24 -0
  201. package/src/services/built-in/summarize/prompt.md +27 -0
  202. package/src/services/built-in/tell-joke/handler.ts +134 -0
  203. package/src/services/built-in/tell-joke/index.ts +34 -0
  204. package/src/services/built-in/tell-joke/prompt.md +59 -0
  205. package/src/services/built-in/translate/index.ts +24 -0
  206. package/src/services/built-in/translate/prompt.md +23 -0
  207. package/src/services/built-in/web-research/index.ts +54 -0
  208. package/src/services/built-in/web-research/prompt.md +110 -0
  209. package/src/services/index.ts +16 -0
  210. package/src/services/loader.ts +344 -0
  211. package/src/services/manager.ts +304 -0
  212. package/src/services/registry.ts +246 -0
  213. package/src/services/types.ts +259 -0
  214. package/src/test/cli.test.ts +352 -0
  215. package/src/test/comprehensive-overlay.test.ts +729 -0
  216. package/src/test/key-derivation.test.ts +102 -0
  217. package/src/test/overlay-submit.test.ts +570 -0
  218. package/src/test/request-response-flow.test.ts +252 -0
  219. package/src/test/service-system.test.ts +241 -0
  220. package/src/test/utils/server-logic.ts +368 -0
  221. package/src/test/wallet.test.ts +166 -0
@@ -0,0 +1,99 @@
1
+ /**
2
+ * @a2a-bsv/core — BSVAgentWallet
3
+ *
4
+ * High-level wallet class for AI agent-to-agent BSV payments.
5
+ * Wraps @bsv/wallet-toolbox's Wallet + StorageKnex with a clean,
6
+ * minimal API surface designed for automated agent use.
7
+ */
8
+ import type { SetupWallet } from '@bsv/wallet-toolbox';
9
+ import type { WalletConfig, PaymentParams, PaymentResult, VerifyParams, VerifyResult, AcceptParams, AcceptResult } from './types.js';
10
+ /**
11
+ * BSVAgentWallet — the primary class for agent-to-agent BSV payments.
12
+ *
13
+ * Usage:
14
+ * ```ts
15
+ * // Create a new wallet (generates keys)
16
+ * const wallet = await BSVAgentWallet.load({ network: 'testnet', storageDir: './agent-wallet' });
17
+ *
18
+ * // Load an existing wallet
19
+ * const wallet = await BSVAgentWallet.load({ network: 'testnet', storageDir: './agent-wallet' });
20
+ *
21
+ * // Make a payment
22
+ * const payment = await wallet.createPayment({ to: recipientPubKey, satoshis: 500 });
23
+ *
24
+ * // Verify and accept a payment
25
+ * const verification = wallet.verifyPayment({ beef: payment.beef });
26
+ * if (verification.valid) {
27
+ * await wallet.acceptPayment({ beef: payment.beef, ...derivationInfo });
28
+ * }
29
+ * ```
30
+ */
31
+ export declare class BSVAgentWallet {
32
+ /** @internal — exposed for advanced operations (e.g. direct internalizeAction) */
33
+ readonly _setup: SetupWallet;
34
+ private constructor();
35
+ /**
36
+ * Create a new agent wallet. Generates a fresh root key and persists it.
37
+ * The SQLite database and identity file are written to `config.storageDir`.
38
+ */
39
+ private static create;
40
+ /**
41
+ * Load an existing agent wallet from its storage directory.
42
+ * Reads the persisted identity file and re-initializes the wallet.
43
+ */
44
+ static load(config: WalletConfig): Promise<BSVAgentWallet>;
45
+ /**
46
+ * Get this wallet's public identity key (compressed hex, 33 bytes).
47
+ * This is the key other agents use to send payments to you.
48
+ */
49
+ getIdentityKey(): Promise<string>;
50
+ /**
51
+ * Get the wallet's current balance in satoshis.
52
+ *
53
+ * Uses the BRC-100 wallet's balance method which sums spendable outputs
54
+ * in the default basket.
55
+ */
56
+ getBalance(): Promise<number>;
57
+ /**
58
+ * Cleanly shut down the wallet, releasing database connections and
59
+ * stopping the background monitor.
60
+ */
61
+ destroy(): Promise<void>;
62
+ /**
63
+ * Build a BRC-29 payment to another agent.
64
+ *
65
+ * The transaction is created with `noSend: true` — the sender does NOT
66
+ * broadcast it. Instead, the Atomic BEEF and derivation metadata are
67
+ * returned so they can be transmitted to the recipient, who will
68
+ * verify and internalize (broadcast) the payment.
69
+ *
70
+ * @param params.to — Recipient's compressed public key (hex).
71
+ * @param params.satoshis — Amount in satoshis.
72
+ * @param params.description — Optional human-readable note.
73
+ */
74
+ createPayment(params: PaymentParams): Promise<PaymentResult>;
75
+ /**
76
+ * Verify an incoming Atomic BEEF payment.
77
+ *
78
+ * This performs structural validation and SPV verification via tx.verify().
79
+ */
80
+ verifyPayment(params: VerifyParams): Promise<VerifyResult>;
81
+ /**
82
+ * Accept (internalize) a verified payment into this wallet.
83
+ *
84
+ * Uses the BRC-29 wallet payment protocol to derive the correct key
85
+ * and claim the output. This triggers SPV verification and, if the
86
+ * transaction hasn't been broadcast yet, broadcasts it.
87
+ */
88
+ acceptPayment(params: AcceptParams): Promise<AcceptResult>;
89
+ /** Get the underlying wallet-toolbox SetupWallet for advanced operations. */
90
+ getSetup(): SetupWallet;
91
+ /**
92
+ * Internal: manually construct a BRC-100 wallet backed by SQLite.
93
+ *
94
+ * We build this by hand instead of using Setup.createWalletSQLite because
95
+ * the toolbox has a bug where its internal randomBytesHex is a stub.
96
+ * We use the same components but wire them up correctly.
97
+ */
98
+ private static buildSetup;
99
+ }
@@ -0,0 +1,219 @@
1
+ /**
2
+ * @a2a-bsv/core — BSVAgentWallet
3
+ *
4
+ * High-level wallet class for AI agent-to-agent BSV payments.
5
+ * Wraps @bsv/wallet-toolbox's Wallet + StorageKnex with a clean,
6
+ * minimal API surface designed for automated agent use.
7
+ */
8
+ import { PrivateKey, CachedKeyDeriver } from '@bsv/sdk';
9
+ import { Wallet, WalletStorageManager, Services, Monitor, StorageKnex, randomBytesHex, ChaintracksServiceClient, } from '@bsv/wallet-toolbox';
10
+ import knexLib from 'knex';
11
+ import * as path from 'node:path';
12
+ import * as fs from 'node:fs';
13
+ import { toChain, DEFAULT_TAAL_API_KEYS, DEFAULT_DB_NAME } from './config.js';
14
+ import { buildPayment } from './payment.js';
15
+ import { verifyPayment, acceptPayment } from './verify.js';
16
+ /** Filename for the persisted wallet identity JSON. */
17
+ const IDENTITY_FILE = 'wallet-identity.json';
18
+ /**
19
+ * BSVAgentWallet — the primary class for agent-to-agent BSV payments.
20
+ *
21
+ * Usage:
22
+ * ```ts
23
+ * // Create a new wallet (generates keys)
24
+ * const wallet = await BSVAgentWallet.load({ network: 'testnet', storageDir: './agent-wallet' });
25
+ *
26
+ * // Load an existing wallet
27
+ * const wallet = await BSVAgentWallet.load({ network: 'testnet', storageDir: './agent-wallet' });
28
+ *
29
+ * // Make a payment
30
+ * const payment = await wallet.createPayment({ to: recipientPubKey, satoshis: 500 });
31
+ *
32
+ * // Verify and accept a payment
33
+ * const verification = wallet.verifyPayment({ beef: payment.beef });
34
+ * if (verification.valid) {
35
+ * await wallet.acceptPayment({ beef: payment.beef, ...derivationInfo });
36
+ * }
37
+ * ```
38
+ */
39
+ export class BSVAgentWallet {
40
+ /** @internal — exposed for advanced operations (e.g. direct internalizeAction) */
41
+ _setup;
42
+ constructor(setup) {
43
+ this._setup = setup;
44
+ }
45
+ // ---------------------------------------------------------------------------
46
+ // Factory methods
47
+ // ---------------------------------------------------------------------------
48
+ /**
49
+ * Create a new agent wallet. Generates a fresh root key and persists it.
50
+ * The SQLite database and identity file are written to `config.storageDir`.
51
+ */
52
+ static async create(config) {
53
+ // Generate a new root key (or use one provided in config)
54
+ const rootKeyHex = config.rootKeyHex ?? PrivateKey.fromRandom().toHex();
55
+ const rootKey = PrivateKey.fromHex(rootKeyHex);
56
+ const identityKey = rootKey.toPublicKey().toString();
57
+ // Ensure the storage directory exists
58
+ fs.mkdirSync(config.storageDir, { recursive: true });
59
+ // Persist identity for later loading
60
+ const identity = {
61
+ rootKeyHex,
62
+ identityKey,
63
+ network: config.network,
64
+ };
65
+ const identityPath = path.join(config.storageDir, IDENTITY_FILE);
66
+ fs.writeFileSync(identityPath, JSON.stringify(identity, null, 2), 'utf-8');
67
+ // Build the wallet
68
+ const setup = await BSVAgentWallet.buildSetup(config, rootKeyHex);
69
+ return new BSVAgentWallet(setup);
70
+ }
71
+ /**
72
+ * Load an existing agent wallet from its storage directory.
73
+ * Reads the persisted identity file and re-initializes the wallet.
74
+ */
75
+ static async load(config) {
76
+ const identityPath = path.join(config.storageDir, IDENTITY_FILE);
77
+ if (!fs.existsSync(identityPath)) {
78
+ return this.create(config);
79
+ }
80
+ const identity = JSON.parse(fs.readFileSync(identityPath, 'utf-8'));
81
+ const rootKeyHex = config.rootKeyHex ?? identity.rootKeyHex;
82
+ const setup = await BSVAgentWallet.buildSetup(config, rootKeyHex);
83
+ return new BSVAgentWallet(setup);
84
+ }
85
+ // ---------------------------------------------------------------------------
86
+ // Wallet lifecycle
87
+ // ---------------------------------------------------------------------------
88
+ /**
89
+ * Get this wallet's public identity key (compressed hex, 33 bytes).
90
+ * This is the key other agents use to send payments to you.
91
+ */
92
+ async getIdentityKey() {
93
+ return this._setup.identityKey;
94
+ }
95
+ /**
96
+ * Get the wallet's current balance in satoshis.
97
+ *
98
+ * Uses the BRC-100 wallet's balance method which sums spendable outputs
99
+ * in the default basket.
100
+ */
101
+ async getBalance() {
102
+ return await this._setup.wallet.balance();
103
+ }
104
+ /**
105
+ * Cleanly shut down the wallet, releasing database connections and
106
+ * stopping the background monitor.
107
+ */
108
+ async destroy() {
109
+ await this._setup.wallet.destroy();
110
+ }
111
+ // ---------------------------------------------------------------------------
112
+ // Payment creation (sender/payer side)
113
+ // ---------------------------------------------------------------------------
114
+ /**
115
+ * Build a BRC-29 payment to another agent.
116
+ *
117
+ * The transaction is created with `noSend: true` — the sender does NOT
118
+ * broadcast it. Instead, the Atomic BEEF and derivation metadata are
119
+ * returned so they can be transmitted to the recipient, who will
120
+ * verify and internalize (broadcast) the payment.
121
+ *
122
+ * @param params.to — Recipient's compressed public key (hex).
123
+ * @param params.satoshis — Amount in satoshis.
124
+ * @param params.description — Optional human-readable note.
125
+ */
126
+ async createPayment(params) {
127
+ return buildPayment(this._setup, params);
128
+ }
129
+ // ---------------------------------------------------------------------------
130
+ // Payment verification & acceptance (receiver/merchant side)
131
+ // ---------------------------------------------------------------------------
132
+ /**
133
+ * Verify an incoming Atomic BEEF payment.
134
+ *
135
+ * This performs structural validation and SPV verification via tx.verify().
136
+ */
137
+ async verifyPayment(params) {
138
+ return await verifyPayment(params);
139
+ }
140
+ /**
141
+ * Accept (internalize) a verified payment into this wallet.
142
+ *
143
+ * Uses the BRC-29 wallet payment protocol to derive the correct key
144
+ * and claim the output. This triggers SPV verification and, if the
145
+ * transaction hasn't been broadcast yet, broadcasts it.
146
+ */
147
+ async acceptPayment(params) {
148
+ return acceptPayment(this._setup, params);
149
+ }
150
+ // ---------------------------------------------------------------------------
151
+ // Access to underlying toolbox objects (for advanced use)
152
+ // ---------------------------------------------------------------------------
153
+ /** Get the underlying wallet-toolbox SetupWallet for advanced operations. */
154
+ getSetup() {
155
+ return this._setup;
156
+ }
157
+ // ---------------------------------------------------------------------------
158
+ // Private helpers
159
+ // ---------------------------------------------------------------------------
160
+ /**
161
+ * Internal: manually construct a BRC-100 wallet backed by SQLite.
162
+ *
163
+ * We build this by hand instead of using Setup.createWalletSQLite because
164
+ * the toolbox has a bug where its internal randomBytesHex is a stub.
165
+ * We use the same components but wire them up correctly.
166
+ */
167
+ static async buildSetup(config, rootKeyHex) {
168
+ const chain = toChain(config.network);
169
+ const taalApiKey = config.taalApiKey ?? DEFAULT_TAAL_API_KEYS[chain];
170
+ const rootKey = PrivateKey.fromHex(rootKeyHex);
171
+ const identityKey = rootKey.toPublicKey().toString();
172
+ // 1. Key derivation
173
+ const keyDeriver = new CachedKeyDeriver(rootKey);
174
+ // 2. Storage manager (empty initially)
175
+ const storage = new WalletStorageManager(identityKey);
176
+ // 3. Network services (ARC broadcasting, chain tracking, etc.)
177
+ const serviceOptions = Services.createDefaultOptions(chain);
178
+ serviceOptions.chaintracks = new ChaintracksServiceClient(chain, 'https://chaintracks-us-1.bsvb.tech');
179
+ serviceOptions.taalApiKey = taalApiKey;
180
+ const services = new Services(serviceOptions);
181
+ // 4. Background monitor
182
+ const monopts = Monitor.createDefaultWalletMonitorOptions(chain, storage, services);
183
+ const monitor = new Monitor(monopts);
184
+ monitor.addDefaultTasks();
185
+ // 5. The BRC-100 Wallet
186
+ const wallet = new Wallet({ chain, keyDeriver, storage, services, monitor });
187
+ // 6. SQLite storage via knex
188
+ const filePath = path.join(config.storageDir, `${DEFAULT_DB_NAME}.sqlite`);
189
+ const knex = knexLib({
190
+ client: 'sqlite3',
191
+ connection: { filename: filePath },
192
+ useNullAsDefault: true,
193
+ });
194
+ // Fee model: configurable via BSV_FEE_MODEL env var (default: 100 sat/KB)
195
+ const feeModelValue = config.feeModel ??
196
+ (process.env.BSV_FEE_MODEL ? parseInt(process.env.BSV_FEE_MODEL, 10) : 100);
197
+ const activeStorage = new StorageKnex({
198
+ chain,
199
+ knex,
200
+ commissionSatoshis: 0,
201
+ commissionPubKeyHex: undefined,
202
+ feeModel: { model: 'sat/kb', value: feeModelValue },
203
+ });
204
+ await activeStorage.migrate(DEFAULT_DB_NAME, randomBytesHex(33));
205
+ await activeStorage.makeAvailable();
206
+ await storage.addWalletStorageProvider(activeStorage);
207
+ await activeStorage.findOrInsertUser(identityKey);
208
+ return {
209
+ rootKey,
210
+ identityKey,
211
+ keyDeriver,
212
+ chain,
213
+ storage,
214
+ services,
215
+ monitor,
216
+ wallet,
217
+ };
218
+ }
219
+ }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Baemail commands - paid message forwarding service.
3
+ */
4
+ export interface BaemailConfig {
5
+ deliveryChannel: string;
6
+ tiers: {
7
+ standard: number;
8
+ priority: number;
9
+ urgent: number;
10
+ };
11
+ maxMessageLength: number;
12
+ blocklist: string[];
13
+ createdAt: string;
14
+ updatedAt: string;
15
+ }
16
+ export interface BaemailLogEntry {
17
+ requestId: string;
18
+ from: string;
19
+ senderName: string;
20
+ tier: string;
21
+ paidSats: number;
22
+ messageLength: number;
23
+ deliveryChannel: string;
24
+ deliverySuccess: boolean;
25
+ deliveryError: string | null;
26
+ paymentTxid: string;
27
+ refundStatus: string | null;
28
+ refundTxid?: string;
29
+ refundedAt?: string;
30
+ timestamp: string;
31
+ _lineIdx?: number;
32
+ }
33
+ /**
34
+ * Load baemail configuration.
35
+ */
36
+ export declare function loadBaemailConfig(): BaemailConfig | null;
37
+ /**
38
+ * Save baemail configuration.
39
+ */
40
+ export declare function saveBaemailConfig(config: BaemailConfig): void;
41
+ /**
42
+ * Setup baemail service with delivery channel and tier pricing.
43
+ */
44
+ export declare function cmdBaemailSetup(channel: string | undefined, standardStr: string | undefined, priorityStr?: string, urgentStr?: string): Promise<never>;
45
+ /**
46
+ * View current baemail configuration.
47
+ */
48
+ export declare function cmdBaemailConfig(): Promise<never>;
49
+ /**
50
+ * Block a sender from using baemail.
51
+ */
52
+ export declare function cmdBaemailBlock(identityKey: string | undefined): Promise<never>;
53
+ /**
54
+ * Unblock a sender.
55
+ */
56
+ export declare function cmdBaemailUnblock(identityKey: string | undefined): Promise<never>;
57
+ /**
58
+ * View baemail delivery log.
59
+ */
60
+ export declare function cmdBaemailLog(limitStr?: string): Promise<never>;
61
+ /**
62
+ * Refund a failed baemail delivery.
63
+ */
64
+ export declare function cmdBaemailRefund(requestId: string | undefined): Promise<never>;
@@ -0,0 +1,258 @@
1
+ /**
2
+ * Baemail commands - paid message forwarding service.
3
+ */
4
+ import fs from 'node:fs';
5
+ import { PATHS } from '../config.js';
6
+ import { ok, fail } from '../output.js';
7
+ import { loadIdentity } from '../wallet/identity.js';
8
+ import { ensureStateDir } from '../utils/storage.js';
9
+ import { fetchWithTimeout } from '../utils/woc.js';
10
+ /**
11
+ * Load baemail configuration.
12
+ */
13
+ export function loadBaemailConfig() {
14
+ try {
15
+ if (fs.existsSync(PATHS.baemailConfig)) {
16
+ return JSON.parse(fs.readFileSync(PATHS.baemailConfig, 'utf-8'));
17
+ }
18
+ }
19
+ catch (err) {
20
+ console.warn(`[baemail] Warning: Could not read config: ${err.message}`);
21
+ }
22
+ return null;
23
+ }
24
+ /**
25
+ * Save baemail configuration.
26
+ */
27
+ export function saveBaemailConfig(config) {
28
+ ensureStateDir();
29
+ fs.writeFileSync(PATHS.baemailConfig, JSON.stringify(config, null, 2));
30
+ }
31
+ /**
32
+ * Setup baemail service with delivery channel and tier pricing.
33
+ */
34
+ export async function cmdBaemailSetup(channel, standardStr, priorityStr, urgentStr) {
35
+ if (!channel || !standardStr) {
36
+ return fail('Usage: baemail-setup <channel> <standardSats> [prioritySats] [urgentSats]');
37
+ }
38
+ const standard = parseInt(standardStr, 10);
39
+ const priority = priorityStr ? parseInt(priorityStr, 10) : standard * 2;
40
+ const urgent = urgentStr ? parseInt(urgentStr, 10) : standard * 5;
41
+ if (isNaN(standard) || standard < 1) {
42
+ return fail('Standard rate must be a positive integer (sats)');
43
+ }
44
+ if (priority < standard) {
45
+ return fail('Priority rate must be >= standard rate');
46
+ }
47
+ if (urgent < priority) {
48
+ return fail('Urgent rate must be >= priority rate');
49
+ }
50
+ const config = {
51
+ deliveryChannel: channel,
52
+ tiers: { standard, priority, urgent },
53
+ maxMessageLength: 4000,
54
+ blocklist: [],
55
+ createdAt: new Date().toISOString(),
56
+ updatedAt: new Date().toISOString(),
57
+ };
58
+ saveBaemailConfig(config);
59
+ return ok({
60
+ configured: true,
61
+ deliveryChannel: channel,
62
+ tiers: config.tiers,
63
+ note: `Advertise with: cli advertise baemail "Baemail" "Paid message forwarding. Pay ${standard}+ sats to reach me." ${standard}`,
64
+ });
65
+ }
66
+ /**
67
+ * View current baemail configuration.
68
+ */
69
+ export async function cmdBaemailConfig() {
70
+ const config = loadBaemailConfig();
71
+ if (!config) {
72
+ return fail('Baemail not configured. Run: baemail-setup <channel> <standardSats> [prioritySats] [urgentSats]');
73
+ }
74
+ return ok(config);
75
+ }
76
+ /**
77
+ * Block a sender from using baemail.
78
+ */
79
+ export async function cmdBaemailBlock(identityKey) {
80
+ if (!identityKey)
81
+ return fail('Usage: baemail-block <identityKey>');
82
+ const config = loadBaemailConfig();
83
+ if (!config) {
84
+ return fail('Baemail not configured. Run baemail-setup first.');
85
+ }
86
+ if (!config.blocklist)
87
+ config.blocklist = [];
88
+ if (config.blocklist.includes(identityKey)) {
89
+ return fail('Identity already blocked');
90
+ }
91
+ config.blocklist.push(identityKey);
92
+ config.updatedAt = new Date().toISOString();
93
+ saveBaemailConfig(config);
94
+ return ok({ blocked: identityKey, totalBlocked: config.blocklist.length });
95
+ }
96
+ /**
97
+ * Unblock a sender.
98
+ */
99
+ export async function cmdBaemailUnblock(identityKey) {
100
+ if (!identityKey)
101
+ return fail('Usage: baemail-unblock <identityKey>');
102
+ const config = loadBaemailConfig();
103
+ if (!config) {
104
+ return fail('Baemail not configured. Run baemail-setup first.');
105
+ }
106
+ if (!config.blocklist || !config.blocklist.includes(identityKey)) {
107
+ return fail('Identity not in blocklist');
108
+ }
109
+ config.blocklist = config.blocklist.filter(k => k !== identityKey);
110
+ config.updatedAt = new Date().toISOString();
111
+ saveBaemailConfig(config);
112
+ return ok({ unblocked: identityKey, totalBlocked: config.blocklist.length });
113
+ }
114
+ /**
115
+ * View baemail delivery log.
116
+ */
117
+ export async function cmdBaemailLog(limitStr) {
118
+ const limit = parseInt(limitStr || '20', 10) || 20;
119
+ if (!fs.existsSync(PATHS.baemailLog)) {
120
+ return ok({ log: [], count: 0 });
121
+ }
122
+ const lines = fs.readFileSync(PATHS.baemailLog, 'utf-8').split('\n').filter(l => l.trim());
123
+ const entries = lines.map(l => {
124
+ try {
125
+ return JSON.parse(l);
126
+ }
127
+ catch {
128
+ return null;
129
+ }
130
+ }).filter(Boolean);
131
+ const recent = entries.slice(-limit).reverse();
132
+ return ok({ log: recent, count: entries.length, showing: recent.length });
133
+ }
134
+ /**
135
+ * Refund a failed baemail delivery.
136
+ */
137
+ export async function cmdBaemailRefund(requestId) {
138
+ if (!requestId)
139
+ return fail('Usage: baemail-refund <requestId>');
140
+ if (!fs.existsSync(PATHS.baemailLog)) {
141
+ return fail('No baemail log found');
142
+ }
143
+ // Find the entry
144
+ const lines = fs.readFileSync(PATHS.baemailLog, 'utf-8').split('\n').filter(l => l.trim());
145
+ const entries = lines.map((l, idx) => {
146
+ try {
147
+ return { ...JSON.parse(l), _lineIdx: idx };
148
+ }
149
+ catch {
150
+ return null;
151
+ }
152
+ }).filter(Boolean);
153
+ const entry = entries.find(e => e.requestId === requestId);
154
+ if (!entry) {
155
+ return fail(`Request ${requestId} not found in baemail log`);
156
+ }
157
+ if (entry.deliverySuccess) {
158
+ return fail('This delivery was successful — no refund needed');
159
+ }
160
+ if (entry.refundStatus === 'completed') {
161
+ return fail('Refund already processed for this request');
162
+ }
163
+ // Load wallet and SDK
164
+ const { identityKey, privKey } = await loadIdentity();
165
+ const walletIdentityRaw = fs.readFileSync(PATHS.walletIdentity, 'utf-8');
166
+ const walletIdentity = JSON.parse(walletIdentityRaw);
167
+ // Dynamic import SDK
168
+ let sdk;
169
+ try {
170
+ sdk = await import('@bsv/sdk');
171
+ }
172
+ catch {
173
+ return fail('Cannot load @bsv/sdk for refund transaction');
174
+ }
175
+ const { Transaction, P2PKH, PrivateKey, PublicKey, Hash } = sdk;
176
+ // Calculate refund amount
177
+ const refundSats = entry.paidSats - 1; // Keep 1 sat for tx fee
178
+ if (refundSats < 1) {
179
+ return fail('Amount too small to refund');
180
+ }
181
+ // Derive refund address from sender's identity key
182
+ const senderPubKey = PublicKey.fromString(entry.from);
183
+ const refundAddress = senderPubKey.toAddress().toString();
184
+ try {
185
+ // Load UTXOs
186
+ const address = walletIdentity.address;
187
+ const utxosResp = await fetchWithTimeout(`https://api.whatsonchain.com/v1/bsv/main/address/${address}/unspent`);
188
+ const utxos = await utxosResp.json();
189
+ if (!utxos || utxos.length === 0) {
190
+ return fail('No UTXOs available for refund');
191
+ }
192
+ // Build transaction
193
+ const tx = new Transaction();
194
+ let totalInput = 0;
195
+ const rootKey = PrivateKey.fromHex(walletIdentity.rootKeyHex);
196
+ for (const utxo of utxos) {
197
+ if (totalInput >= refundSats + 50)
198
+ break;
199
+ tx.addInput({
200
+ sourceTXID: utxo.tx_hash,
201
+ sourceOutputIndex: utxo.tx_pos,
202
+ sourceSatoshis: utxo.value,
203
+ script: new P2PKH().lock(rootKey.toPublicKey().toAddress()).toHex(),
204
+ unlockingScriptTemplate: new P2PKH().unlock(rootKey),
205
+ });
206
+ totalInput += utxo.value;
207
+ }
208
+ if (totalInput < refundSats + 10) {
209
+ return fail('Insufficient funds for refund');
210
+ }
211
+ // Refund output
212
+ tx.addOutput({
213
+ satoshis: refundSats,
214
+ lockingScript: new P2PKH().lock(refundAddress),
215
+ });
216
+ // Change output
217
+ const fee = 10;
218
+ const change = totalInput - refundSats - fee;
219
+ if (change > 1) {
220
+ tx.addOutput({
221
+ satoshis: change,
222
+ lockingScript: new P2PKH().lock(rootKey.toPublicKey().toAddress()),
223
+ });
224
+ }
225
+ await tx.sign();
226
+ // Broadcast
227
+ const broadcastResp = await fetchWithTimeout('https://api.whatsonchain.com/v1/bsv/main/tx/raw', {
228
+ method: 'POST',
229
+ headers: { 'Content-Type': 'application/json' },
230
+ body: JSON.stringify({ txhex: tx.toHex() }),
231
+ });
232
+ if (!broadcastResp.ok) {
233
+ const errBody = await broadcastResp.text();
234
+ return fail(`Broadcast failed: ${errBody}`);
235
+ }
236
+ const txid = tx.id('hex');
237
+ // Update log entry
238
+ const updatedLines = lines.map((l, idx) => {
239
+ if (idx === entry._lineIdx) {
240
+ const updated = { ...JSON.parse(l), refundStatus: 'completed', refundTxid: txid, refundedAt: new Date().toISOString() };
241
+ return JSON.stringify(updated);
242
+ }
243
+ return l;
244
+ });
245
+ fs.writeFileSync(PATHS.baemailLog, updatedLines.join('\n') + '\n');
246
+ return ok({
247
+ refunded: true,
248
+ requestId,
249
+ refundSats,
250
+ refundAddress,
251
+ txid,
252
+ note: `Refunded ${refundSats} sats to sender`,
253
+ });
254
+ }
255
+ catch (err) {
256
+ return fail(`Refund failed: ${err.message}`);
257
+ }
258
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Baemail service handler - processes incoming paid messages.
3
+ */
4
+ interface BaemailInput {
5
+ message?: string;
6
+ senderName?: string;
7
+ replyIdentityKey?: string;
8
+ }
9
+ interface ServiceMessage {
10
+ id: string;
11
+ from: string;
12
+ payload?: {
13
+ input?: BaemailInput;
14
+ payment?: any;
15
+ };
16
+ }
17
+ interface ProcessResult {
18
+ id: string;
19
+ type: string;
20
+ serviceId: string;
21
+ action: string;
22
+ tier?: string;
23
+ deliverySuccess?: boolean;
24
+ deliveryError?: string | null | undefined;
25
+ paymentAccepted?: boolean;
26
+ paymentTxid?: string;
27
+ satoshisReceived?: number;
28
+ from: string;
29
+ ack: boolean;
30
+ reason?: string | null;
31
+ }
32
+ /**
33
+ * Process incoming baemail service request.
34
+ */
35
+ export declare function processBaemail(msg: ServiceMessage, identityKey: string, privKey: any): Promise<ProcessResult>;
36
+ export {};