openclaw-overlay-plugin 0.8.15 → 0.8.17

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