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,42 @@
1
+ /**
2
+ * @a2a-bsv/core — Agent-to-agent BSV payment library.
3
+ *
4
+ * Wraps @bsv/sdk and @bsv/wallet-toolbox to provide a clean, minimal API
5
+ * for AI agents to pay each other using BSV blockchain transactions.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { BSVAgentWallet } from '@a2a-bsv/core';
10
+ *
11
+ * const wallet = await BSVAgentWallet.load({
12
+ * network: 'testnet',
13
+ * storageDir: './my-agent-wallet',
14
+ * });
15
+ *
16
+ * const identityKey = await wallet.getIdentityKey();
17
+ * console.log('My identity:', identityKey);
18
+ * ```
19
+ */
20
+
21
+ // Main wallet class
22
+ export { BSVAgentWallet } from './wallet.js';
23
+
24
+ // All types
25
+ export type {
26
+ WalletConfig,
27
+ WalletIdentity,
28
+ PaymentParams,
29
+ PaymentResult,
30
+ VerifyParams,
31
+ VerifyResult,
32
+ AcceptParams,
33
+ AcceptResult,
34
+ } from './types.js';
35
+
36
+ // Config helpers (for advanced use)
37
+ export { toChain, DEFAULT_TAAL_API_KEYS, DEFAULT_DB_NAME } from './config.js';
38
+ export type { Chain } from './config.js';
39
+
40
+ // Lower-level helpers (for advanced use)
41
+ export { buildPayment } from './payment.js';
42
+ export { verifyPayment, acceptPayment } from './verify.js';
@@ -0,0 +1,111 @@
1
+ /**
2
+ * @a2a-bsv/core — Payment construction helpers.
3
+ *
4
+ * Uses BRC-29 key derivation so the recipient can internalize the payment
5
+ * without ever reusing an address.
6
+ */
7
+
8
+ import { Beef, Utils } from '@bsv/sdk';
9
+ import { randomBytesBase64, ScriptTemplateBRC29 } from '@bsv/wallet-toolbox';
10
+ import type { SetupWallet } from '@bsv/wallet-toolbox';
11
+ import type { PaymentParams, PaymentResult } from './types.js';
12
+ import type { CachedKeyDeriver } from '@bsv/sdk';
13
+
14
+ /**
15
+ * Build a BRC-29 payment transaction using the wallet's createAction API.
16
+ *
17
+ * The transaction is created with `acceptDelayedBroadcast: false` — the sender
18
+ * broadcasts immediately. The resulting Atomic BEEF and derivation metadata are
19
+ * returned so the recipient can verify and internalize the payment on their side.
20
+ */
21
+ export async function buildPayment(
22
+ setup: SetupWallet,
23
+ params: PaymentParams,
24
+ ): Promise<PaymentResult> {
25
+ const { to, satoshis, description } = params;
26
+ const desc = normalizeDescription(description ?? 'agent payment');
27
+
28
+ // Generate unique BRC-29 derivation prefixes and suffixes
29
+ const derivationPrefix = randomBytesBase64(8);
30
+ const derivationSuffix = randomBytesBase64(8);
31
+
32
+ // Build BRC-29 locking script
33
+ const keyDeriver = setup.keyDeriver as CachedKeyDeriver;
34
+ const t = new ScriptTemplateBRC29({
35
+ derivationPrefix,
36
+ derivationSuffix,
37
+ keyDeriver,
38
+ });
39
+
40
+ // Determine the recipient identity key.
41
+ // If `to` is a compressed public key hex (66 chars, starts with 02/03), use directly.
42
+ // Otherwise treat as an address — for BRC-29 we need a public key.
43
+ let recipientPubKey: string;
44
+ if (/^0[23][0-9a-fA-F]{64}$/.test(to)) {
45
+ recipientPubKey = to;
46
+ } else {
47
+ // If it's an address, we can't do BRC-29 (needs pubkey). Throw a clear error.
48
+ throw new Error(
49
+ 'PaymentParams.to must be a compressed public key (hex) for BRC-29 payments. ' +
50
+ 'Raw BSV addresses are not supported — the recipient must share their identity key.'
51
+ );
52
+ }
53
+
54
+ const lockingScript = t.lock(setup.rootKey.toString(), recipientPubKey);
55
+
56
+ const label = 'a2a-payment';
57
+ const car = await setup.wallet.createAction({
58
+ outputs: [
59
+ {
60
+ lockingScript: lockingScript.toHex(),
61
+ satoshis,
62
+ outputDescription: desc,
63
+ tags: ['relinquish'],
64
+ customInstructions: JSON.stringify({
65
+ derivationPrefix,
66
+ derivationSuffix,
67
+ type: 'BRC29',
68
+ }),
69
+ },
70
+ ],
71
+ options: {
72
+ randomizeOutputs: false,
73
+ acceptDelayedBroadcast: false,
74
+ },
75
+ labels: [label],
76
+ description: desc,
77
+ });
78
+
79
+ // Extract the txid from the createAction result.
80
+ // The tx field is a number[] (AtomicBEEF binary). Parse it to get txid.
81
+ if (!car.tx) {
82
+ throw new Error('createAction did not return a transaction. Check wallet funding.');
83
+ }
84
+
85
+ const beef = Beef.fromBinary(car.tx);
86
+ // The last transaction in the beef is our new tx
87
+ const lastTx = beef.txs[beef.txs.length - 1];
88
+ const txid = lastTx.txid;
89
+
90
+ // Encode the atomic BEEF as base64
91
+ const atomicBinary = beef.toBinaryAtomic(txid);
92
+ const beefBase64 = Utils.toBase64(atomicBinary);
93
+
94
+ return {
95
+ beef: beefBase64,
96
+ txid,
97
+ satoshis,
98
+ derivationPrefix,
99
+ derivationSuffix,
100
+ senderIdentityKey: setup.identityKey,
101
+ };
102
+ }
103
+
104
+ /**
105
+ * Ensure description meets BRC-100's 5-50 character requirement.
106
+ */
107
+ function normalizeDescription(desc: string): string {
108
+ if (desc.length < 5) return desc.padEnd(5, ' ');
109
+ if (desc.length > 50) return desc.slice(0, 50);
110
+ return desc;
111
+ }
@@ -0,0 +1,102 @@
1
+ /**
2
+ * @a2a-bsv/core — Type definitions for agent-to-agent BSV payments.
3
+ */
4
+
5
+ /** Wallet configuration for creating or loading an agent wallet. */
6
+ export interface WalletConfig {
7
+ /** BSV network to use. */
8
+ network: 'mainnet' | 'testnet';
9
+ /** Directory path for SQLite wallet persistence. */
10
+ storageDir: string;
11
+ /** Optional: pre-existing root private key hex. If omitted on create(), a new one is generated. */
12
+ rootKeyHex?: string;
13
+ /** Optional TAAL API key for ARC broadcasting. Falls back to public default. */
14
+ taalApiKey?: string;
15
+ /** Optional fee model in sat/KB. Falls back to BSV_FEE_MODEL env var or default 100 sat/KB. */
16
+ feeModel?: number;
17
+ }
18
+
19
+ /** Parameters for building a payment transaction. */
20
+ export interface PaymentParams {
21
+ /** Recipient's compressed public key (hex) or BSV address. */
22
+ to: string;
23
+ /** Amount to pay in satoshis. */
24
+ satoshis: number;
25
+ /** Human-readable description (5-50 chars per BRC-100). */
26
+ description?: string;
27
+ /** Optional metadata embedded as OP_RETURN (future use). */
28
+ metadata?: {
29
+ taskId?: string;
30
+ protocol?: string;
31
+ };
32
+ }
33
+
34
+ /** Result from building a payment. */
35
+ export interface PaymentResult {
36
+ /** Base64-encoded Atomic BEEF transaction data. */
37
+ beef: string;
38
+ /** Transaction ID (hex). */
39
+ txid: string;
40
+ /** Amount paid in satoshis. */
41
+ satoshis: number;
42
+ /** BRC-29 derivation prefix (base64). Needed by recipient to internalize. */
43
+ derivationPrefix: string;
44
+ /** BRC-29 derivation suffix (base64). Needed by recipient to internalize. */
45
+ derivationSuffix: string;
46
+ /** Sender's identity key (compressed hex). Needed by recipient to internalize. */
47
+ senderIdentityKey: string;
48
+ }
49
+
50
+ /** Parameters for verifying an incoming payment. */
51
+ export interface VerifyParams {
52
+ /** Base64-encoded Atomic BEEF data. */
53
+ beef: string;
54
+ /** Expected payment amount in satoshis. */
55
+ expectedAmount?: number;
56
+ /** Expected sender identity key (optional). */
57
+ expectedSender?: string;
58
+ }
59
+
60
+ /** Result from verifying a payment. */
61
+ export interface VerifyResult {
62
+ /** Whether the payment passes all checks. */
63
+ valid: boolean;
64
+ /** Transaction ID (hex). */
65
+ txid: string;
66
+ /** Number of outputs found in the transaction. */
67
+ outputCount: number;
68
+ /** Errors encountered during verification. */
69
+ errors: string[];
70
+ }
71
+
72
+ /** Parameters for accepting (internalizing) a verified payment. */
73
+ export interface AcceptParams {
74
+ /** Base64-encoded Atomic BEEF data. */
75
+ beef: string;
76
+ /** The output index to internalize (default: 0). */
77
+ vout?: number;
78
+ /** BRC-29 derivation prefix from the PaymentResult. */
79
+ derivationPrefix: string;
80
+ /** BRC-29 derivation suffix from the PaymentResult. */
81
+ derivationSuffix: string;
82
+ /** Sender's identity key from the PaymentResult. */
83
+ senderIdentityKey: string;
84
+ /** Human-readable description for wallet records (5-50 chars). */
85
+ description?: string;
86
+ }
87
+
88
+ /** Result from accepting a payment. */
89
+ export interface AcceptResult {
90
+ /** Whether the payment was accepted. */
91
+ accepted: boolean;
92
+ }
93
+
94
+ /** Serializable wallet identity info, persisted alongside the SQLite database. */
95
+ export interface WalletIdentity {
96
+ /** The root private key (hex). Guard this carefully. */
97
+ rootKeyHex: string;
98
+ /** The wallet's public identity key (compressed hex). */
99
+ identityKey: string;
100
+ /** Network this wallet targets. */
101
+ network: 'mainnet' | 'testnet';
102
+ }
@@ -0,0 +1,119 @@
1
+ /**
2
+ * @a2a-bsv/core — Payment verification and acceptance helpers.
3
+ *
4
+ * Verification: parse the Atomic BEEF, validate structure.
5
+ * Acceptance: internalize the payment into the recipient wallet via BRC-29
6
+ * wallet payment protocol.
7
+ */
8
+
9
+ import { Beef, Utils } from '@bsv/sdk';
10
+ import type { SetupWallet } from '@bsv/wallet-toolbox';
11
+ import type { VerifyParams, VerifyResult, AcceptParams, AcceptResult } from './types.js';
12
+ import type { InternalizeActionArgs } from '@bsv/sdk';
13
+
14
+ /**
15
+ * Verify an incoming Atomic BEEF payment.
16
+ *
17
+ * This performs structural validation:
18
+ * - Decodes the base64 BEEF
19
+ * - Checks the BEEF is parseable
20
+ * - Checks there is at least one transaction
21
+ * - Runs SPV verification via tx.verify()
22
+ * - Optionally checks the sender identity key
23
+ */
24
+ export async function verifyPayment(params: VerifyParams): Promise<VerifyResult> {
25
+ const errors: string[] = [];
26
+ let txid = '';
27
+ let outputCount = 0;
28
+
29
+ try {
30
+ const binary = Utils.toArray(params.beef, 'base64');
31
+ const beef = Beef.fromBinary(binary);
32
+
33
+ if (beef.txs.length === 0) {
34
+ errors.push('BEEF contains no transactions');
35
+ } else {
36
+ const lastTx = beef.txs[beef.txs.length - 1];
37
+ txid = lastTx.txid;
38
+
39
+ // Parse the atomic transaction to count outputs
40
+ const tx = beef.findAtomicTransaction(txid);
41
+ if (tx) {
42
+ outputCount = tx.outputs.length;
43
+
44
+ // Run SPV verification
45
+ try {
46
+ await tx.verify();
47
+ } catch (err: unknown) {
48
+ const message = err instanceof Error ? err.message : String(err);
49
+ errors.push(`SPV verification failed: ${message}`);
50
+ }
51
+ } else {
52
+ errors.push('Could not find atomic transaction in BEEF');
53
+ }
54
+ }
55
+
56
+ } catch (err: unknown) {
57
+ const message = err instanceof Error ? err.message : String(err);
58
+ errors.push(`BEEF parse error: ${message}`);
59
+ }
60
+
61
+ // Sender validation is independent of BEEF parsing
62
+ if (params.expectedSender) {
63
+ if (!/^0[23][0-9a-fA-F]{64}$/.test(params.expectedSender)) {
64
+ errors.push('expectedSender is not a valid compressed public key');
65
+ }
66
+ }
67
+
68
+ return {
69
+ valid: errors.length === 0,
70
+ txid,
71
+ outputCount,
72
+ errors,
73
+ };
74
+ }
75
+
76
+ /**
77
+ * Accept (internalize) a verified BRC-29 payment into the recipient's wallet.
78
+ *
79
+ * This calls wallet.internalizeAction with the 'wallet payment' protocol,
80
+ * providing the BRC-29 derivation info so the wallet can derive the correct
81
+ * key and claim the output.
82
+ */
83
+ export async function acceptPayment(
84
+ setup: SetupWallet,
85
+ params: AcceptParams,
86
+ ): Promise<AcceptResult> {
87
+ const desc = normalizeDescription(params.description ?? 'received payment');
88
+ const vout = params.vout ?? 0;
89
+
90
+ const binary = Utils.toArray(params.beef, 'base64');
91
+
92
+ const args: InternalizeActionArgs = {
93
+ tx: binary,
94
+ outputs: [
95
+ {
96
+ outputIndex: vout,
97
+ protocol: 'wallet payment',
98
+ paymentRemittance: {
99
+ derivationPrefix: params.derivationPrefix,
100
+ derivationSuffix: params.derivationSuffix,
101
+ senderIdentityKey: params.senderIdentityKey,
102
+ },
103
+ },
104
+ ],
105
+ description: desc,
106
+ };
107
+
108
+ const result = await setup.wallet.internalizeAction(args);
109
+
110
+ return {
111
+ accepted: result.accepted,
112
+ };
113
+ }
114
+
115
+ function normalizeDescription(desc: string): string {
116
+ if (desc.length < 5) return desc.padEnd(5, ' ');
117
+ if (desc.length > 50) return desc.slice(0, 50);
118
+ return desc;
119
+ }
@@ -0,0 +1,282 @@
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
+
9
+ import { PrivateKey, CachedKeyDeriver } from '@bsv/sdk';
10
+ import {
11
+ Wallet,
12
+ WalletStorageManager,
13
+ Services,
14
+ Monitor,
15
+ StorageKnex,
16
+ randomBytesHex,
17
+ ChaintracksServiceClient,
18
+ } from '@bsv/wallet-toolbox';
19
+ import type { SetupWallet } from '@bsv/wallet-toolbox';
20
+ import knexLib from 'knex';
21
+ import * as path from 'node:path';
22
+ import * as fs from 'node:fs';
23
+
24
+ import type {
25
+ WalletConfig,
26
+ WalletIdentity,
27
+ PaymentParams,
28
+ PaymentResult,
29
+ VerifyParams,
30
+ VerifyResult,
31
+ AcceptParams,
32
+ AcceptResult,
33
+ } from './types.js';
34
+ import { toChain, DEFAULT_TAAL_API_KEYS, DEFAULT_DB_NAME } from './config.js';
35
+ import { buildPayment } from './payment.js';
36
+ import { verifyPayment, acceptPayment } from './verify.js';
37
+
38
+ /** Filename for the persisted wallet identity JSON. */
39
+ const IDENTITY_FILE = 'wallet-identity.json';
40
+
41
+ /**
42
+ * BSVAgentWallet — the primary class for agent-to-agent BSV payments.
43
+ *
44
+ * Usage:
45
+ * ```ts
46
+ * // Create a new wallet (generates keys)
47
+ * const wallet = await BSVAgentWallet.load({ network: 'testnet', storageDir: './agent-wallet' });
48
+ *
49
+ * // Load an existing wallet
50
+ * const wallet = await BSVAgentWallet.load({ network: 'testnet', storageDir: './agent-wallet' });
51
+ *
52
+ * // Make a payment
53
+ * const payment = await wallet.createPayment({ to: recipientPubKey, satoshis: 500 });
54
+ *
55
+ * // Verify and accept a payment
56
+ * const verification = wallet.verifyPayment({ beef: payment.beef });
57
+ * if (verification.valid) {
58
+ * await wallet.acceptPayment({ beef: payment.beef, ...derivationInfo });
59
+ * }
60
+ * ```
61
+ */
62
+ export class BSVAgentWallet {
63
+ /** @internal — exposed for advanced operations (e.g. direct internalizeAction) */
64
+ public readonly _setup: SetupWallet;
65
+
66
+ private constructor(setup: SetupWallet) {
67
+ this._setup = setup;
68
+ }
69
+
70
+ // ---------------------------------------------------------------------------
71
+ // Factory methods
72
+ // ---------------------------------------------------------------------------
73
+
74
+ /**
75
+ * Create a new agent wallet. Generates a fresh root key and persists it.
76
+ * The SQLite database and identity file are written to `config.storageDir`.
77
+ */
78
+ private static async create(config: WalletConfig): Promise<BSVAgentWallet> {
79
+ // Generate a new root key (or use one provided in config)
80
+ const rootKeyHex = config.rootKeyHex ?? PrivateKey.fromRandom().toHex();
81
+ const rootKey = PrivateKey.fromHex(rootKeyHex);
82
+ const identityKey = rootKey.toPublicKey().toString();
83
+
84
+ // Ensure the storage directory exists
85
+ fs.mkdirSync(config.storageDir, { recursive: true });
86
+
87
+ // Persist identity for later loading
88
+ const identity: WalletIdentity = {
89
+ rootKeyHex,
90
+ identityKey,
91
+ network: config.network,
92
+ };
93
+ const identityPath = path.join(config.storageDir, IDENTITY_FILE);
94
+ fs.writeFileSync(identityPath, JSON.stringify(identity, null, 2), 'utf-8');
95
+
96
+ // Build the wallet
97
+ const setup = await BSVAgentWallet.buildSetup(config, rootKeyHex);
98
+
99
+ return new BSVAgentWallet(setup);
100
+ }
101
+
102
+ /**
103
+ * Load an existing agent wallet from its storage directory.
104
+ * Reads the persisted identity file and re-initializes the wallet.
105
+ */
106
+ static async load(config: WalletConfig): Promise<BSVAgentWallet> {
107
+ const identityPath = path.join(config.storageDir, IDENTITY_FILE);
108
+ if (!fs.existsSync(identityPath)) {
109
+ return this.create(config);
110
+ }
111
+
112
+ const identity: WalletIdentity = JSON.parse(
113
+ fs.readFileSync(identityPath, 'utf-8'),
114
+ );
115
+
116
+ const rootKeyHex = config.rootKeyHex ?? identity.rootKeyHex;
117
+ const setup = await BSVAgentWallet.buildSetup(config, rootKeyHex);
118
+
119
+ return new BSVAgentWallet(setup);
120
+ }
121
+
122
+ // ---------------------------------------------------------------------------
123
+ // Wallet lifecycle
124
+ // ---------------------------------------------------------------------------
125
+
126
+ /**
127
+ * Get this wallet's public identity key (compressed hex, 33 bytes).
128
+ * This is the key other agents use to send payments to you.
129
+ */
130
+ async getIdentityKey(): Promise<string> {
131
+ return this._setup.identityKey;
132
+ }
133
+
134
+ /**
135
+ * Get the wallet's current balance in satoshis.
136
+ *
137
+ * Uses the BRC-100 wallet's balance method which sums spendable outputs
138
+ * in the default basket.
139
+ */
140
+ async getBalance(): Promise<number> {
141
+ return await this._setup.wallet.balance();
142
+ }
143
+
144
+ /**
145
+ * Cleanly shut down the wallet, releasing database connections and
146
+ * stopping the background monitor.
147
+ */
148
+ async destroy(): Promise<void> {
149
+ await this._setup.wallet.destroy();
150
+ }
151
+
152
+ // ---------------------------------------------------------------------------
153
+ // Payment creation (sender/payer side)
154
+ // ---------------------------------------------------------------------------
155
+
156
+ /**
157
+ * Build a BRC-29 payment to another agent.
158
+ *
159
+ * The transaction is created with `noSend: true` — the sender does NOT
160
+ * broadcast it. Instead, the Atomic BEEF and derivation metadata are
161
+ * returned so they can be transmitted to the recipient, who will
162
+ * verify and internalize (broadcast) the payment.
163
+ *
164
+ * @param params.to — Recipient's compressed public key (hex).
165
+ * @param params.satoshis — Amount in satoshis.
166
+ * @param params.description — Optional human-readable note.
167
+ */
168
+ async createPayment(params: PaymentParams): Promise<PaymentResult> {
169
+ return buildPayment(this._setup, params);
170
+ }
171
+
172
+ // ---------------------------------------------------------------------------
173
+ // Payment verification & acceptance (receiver/merchant side)
174
+ // ---------------------------------------------------------------------------
175
+
176
+ /**
177
+ * Verify an incoming Atomic BEEF payment.
178
+ *
179
+ * This performs structural validation and SPV verification via tx.verify().
180
+ */
181
+ async verifyPayment(params: VerifyParams): Promise<VerifyResult> {
182
+ return await verifyPayment(params);
183
+ }
184
+
185
+ /**
186
+ * Accept (internalize) a verified payment into this wallet.
187
+ *
188
+ * Uses the BRC-29 wallet payment protocol to derive the correct key
189
+ * and claim the output. This triggers SPV verification and, if the
190
+ * transaction hasn't been broadcast yet, broadcasts it.
191
+ */
192
+ async acceptPayment(params: AcceptParams): Promise<AcceptResult> {
193
+ return acceptPayment(this._setup, params);
194
+ }
195
+
196
+ // ---------------------------------------------------------------------------
197
+ // Access to underlying toolbox objects (for advanced use)
198
+ // ---------------------------------------------------------------------------
199
+
200
+ /** Get the underlying wallet-toolbox SetupWallet for advanced operations. */
201
+ getSetup(): SetupWallet {
202
+ return this._setup;
203
+ }
204
+
205
+ // ---------------------------------------------------------------------------
206
+ // Private helpers
207
+ // ---------------------------------------------------------------------------
208
+
209
+ /**
210
+ * Internal: manually construct a BRC-100 wallet backed by SQLite.
211
+ *
212
+ * We build this by hand instead of using Setup.createWalletSQLite because
213
+ * the toolbox has a bug where its internal randomBytesHex is a stub.
214
+ * We use the same components but wire them up correctly.
215
+ */
216
+ private static async buildSetup(
217
+ config: WalletConfig,
218
+ rootKeyHex: string,
219
+ ): Promise<SetupWallet> {
220
+ const chain = toChain(config.network);
221
+ const taalApiKey = config.taalApiKey ?? DEFAULT_TAAL_API_KEYS[chain];
222
+
223
+ const rootKey = PrivateKey.fromHex(rootKeyHex);
224
+ const identityKey = rootKey.toPublicKey().toString();
225
+
226
+ // 1. Key derivation
227
+ const keyDeriver = new CachedKeyDeriver(rootKey);
228
+
229
+ // 2. Storage manager (empty initially)
230
+ const storage = new WalletStorageManager(identityKey);
231
+
232
+ // 3. Network services (ARC broadcasting, chain tracking, etc.)
233
+ const serviceOptions = Services.createDefaultOptions(chain);
234
+ serviceOptions.chaintracks = new ChaintracksServiceClient(chain, 'https://chaintracks-us-1.bsvb.tech');
235
+ serviceOptions.taalApiKey = taalApiKey;
236
+ const services = new Services(serviceOptions);
237
+
238
+ // 4. Background monitor
239
+ const monopts = Monitor.createDefaultWalletMonitorOptions(chain, storage, services);
240
+ const monitor = new Monitor(monopts);
241
+ monitor.addDefaultTasks();
242
+
243
+ // 5. The BRC-100 Wallet
244
+ const wallet = new Wallet({ chain, keyDeriver, storage, services, monitor });
245
+
246
+ // 6. SQLite storage via knex
247
+ const filePath = path.join(config.storageDir, `${DEFAULT_DB_NAME}.sqlite`);
248
+ const knex = knexLib({
249
+ client: 'sqlite3',
250
+ connection: { filename: filePath },
251
+ useNullAsDefault: true,
252
+ });
253
+
254
+ // Fee model: configurable via BSV_FEE_MODEL env var (default: 100 sat/KB)
255
+ const feeModelValue = config.feeModel ??
256
+ (process.env.BSV_FEE_MODEL ? parseInt(process.env.BSV_FEE_MODEL, 10) : 100);
257
+
258
+ const activeStorage = new StorageKnex({
259
+ chain,
260
+ knex,
261
+ commissionSatoshis: 0,
262
+ commissionPubKeyHex: undefined,
263
+ feeModel: { model: 'sat/kb', value: feeModelValue },
264
+ });
265
+
266
+ await activeStorage.migrate(DEFAULT_DB_NAME, randomBytesHex(33));
267
+ await activeStorage.makeAvailable();
268
+ await storage.addWalletStorageProvider(activeStorage);
269
+ await activeStorage.findOrInsertUser(identityKey);
270
+
271
+ return {
272
+ rootKey,
273
+ identityKey,
274
+ keyDeriver,
275
+ chain,
276
+ storage,
277
+ services,
278
+ monitor,
279
+ wallet,
280
+ };
281
+ }
282
+ }