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,121 @@
1
+ /**
2
+ * Shared TypeScript interfaces for the overlay CLI.
3
+ */
4
+
5
+ export interface WalletIdentity {
6
+ rootKeyHex: string;
7
+ identityKey: string;
8
+ network: 'mainnet' | 'testnet';
9
+ }
10
+
11
+ export interface PaymentResult {
12
+ beef: string;
13
+ txid: string;
14
+ satoshis: number;
15
+ derivationPrefix: string;
16
+ derivationSuffix: string;
17
+ senderIdentityKey: string;
18
+ }
19
+
20
+ export interface PaymentParams {
21
+ to: string;
22
+ satoshis: number;
23
+ description?: string;
24
+ }
25
+
26
+ export interface ServiceAdvertisement {
27
+ serviceId: string;
28
+ name: string;
29
+ description: string;
30
+ priceSats: number;
31
+ txid?: string;
32
+ registeredAt?: string;
33
+ }
34
+
35
+ export interface Message {
36
+ id: string;
37
+ from: string;
38
+ to: string;
39
+ type: string;
40
+ payload: unknown;
41
+ signature?: string;
42
+ timestamp?: number;
43
+ }
44
+
45
+ export interface CommandResult<T = unknown> {
46
+ success: boolean;
47
+ data?: T;
48
+ error?: string;
49
+ }
50
+
51
+ export interface Registration {
52
+ identityKey: string;
53
+ agentName: string;
54
+ agentDescription: string;
55
+ overlayUrl: string;
56
+ identityTxid: string;
57
+ serviceTxid: string | null;
58
+ funded: string;
59
+ registeredAt: string;
60
+ }
61
+
62
+ export interface OverlayPayload {
63
+ protocol: string;
64
+ type: string;
65
+ identityKey?: string;
66
+ [key: string]: unknown;
67
+ }
68
+
69
+ export interface VerifyAndAcceptResult {
70
+ accepted: boolean;
71
+ txid: string | null;
72
+ satoshis: number;
73
+ outputIndex: number;
74
+ walletAccepted: boolean;
75
+ error: string | null;
76
+ }
77
+
78
+ export interface ProcessMessageResult {
79
+ id: string;
80
+ type: string;
81
+ action: string;
82
+ from: string;
83
+ ack: boolean;
84
+ [key: string]: unknown;
85
+ }
86
+
87
+ export interface RelayMessage {
88
+ id: string;
89
+ from: string;
90
+ to: string;
91
+ type: string;
92
+ payload: Record<string, unknown>;
93
+ signature?: string;
94
+ }
95
+
96
+ export interface XVerification {
97
+ identityKey: string;
98
+ xHandle: string;
99
+ xUserId: string;
100
+ tweetId: string;
101
+ tweetUrl: string;
102
+ signature: string;
103
+ verifiedAt: string;
104
+ txid?: string | null;
105
+ }
106
+
107
+ export interface StoredChange {
108
+ txHex: string;
109
+ txid: string;
110
+ vout: number;
111
+ satoshis: number;
112
+ sourceChain?: SourceChainEntry[];
113
+ savedAt: string;
114
+ }
115
+
116
+ export interface SourceChainEntry {
117
+ txHex: string;
118
+ txid: string;
119
+ merklePathHex?: string;
120
+ blockHeight?: number;
121
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Utility module exports.
3
+ */
4
+
5
+ export * from './woc.js';
6
+ export * from './storage.js';
7
+ export * from './merkle.js';
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Merkle path utilities for SPV proofs.
3
+ */
4
+
5
+ import type { MerklePath as MerklePathType } from '@bsv/sdk';
6
+
7
+ // We'll import MerklePath dynamically to avoid issues with ESM resolution
8
+ let _MerklePath: typeof MerklePathType | null = null;
9
+
10
+ async function getMerklePath(): Promise<typeof MerklePathType> {
11
+ if (_MerklePath) return _MerklePath;
12
+ const sdk = await import('@bsv/sdk');
13
+ _MerklePath = sdk.MerklePath;
14
+ return _MerklePath;
15
+ }
16
+
17
+ /**
18
+ * Build a MerklePath from TSC (Transaction Status Check) proof data.
19
+ * @param txid - Transaction ID
20
+ * @param txIndex - Transaction's index in the block
21
+ * @param nodes - Array of sibling hashes (or '*' for duplicate)
22
+ * @param blockHeight - Block height
23
+ */
24
+ export async function buildMerklePathFromTSC(
25
+ txid: string,
26
+ txIndex: number,
27
+ nodes: string[],
28
+ blockHeight: number
29
+ ): Promise<MerklePathType> {
30
+ const MerklePath = await getMerklePath();
31
+ const treeHeight = nodes.length;
32
+ const mpPath: Array<Array<{ offset: number; hash?: string; txid?: boolean; duplicate?: boolean }>> = [];
33
+
34
+ // Level 0
35
+ const level0: Array<{ offset: number; hash?: string; txid?: boolean; duplicate?: boolean }> = [
36
+ { offset: txIndex, hash: txid, txid: true }
37
+ ];
38
+ if (nodes[0] === '*') {
39
+ level0.push({ offset: txIndex ^ 1, duplicate: true });
40
+ } else {
41
+ level0.push({ offset: txIndex ^ 1, hash: nodes[0] });
42
+ }
43
+ level0.sort((a, b) => a.offset - b.offset);
44
+ mpPath.push(level0);
45
+
46
+ // Higher levels
47
+ for (let i = 1; i < treeHeight; i++) {
48
+ const siblingOffset = (txIndex >> i) ^ 1;
49
+ if (nodes[i] === '*') {
50
+ mpPath.push([{ offset: siblingOffset, duplicate: true }]);
51
+ } else {
52
+ mpPath.push([{ offset: siblingOffset, hash: nodes[i] }]);
53
+ }
54
+ }
55
+
56
+ return new MerklePath(blockHeight, mpPath);
57
+ }
@@ -0,0 +1,231 @@
1
+ /**
2
+ * File-based storage helpers for registration, services, and queues.
3
+ */
4
+
5
+ import fs from 'node:fs';
6
+ import { OVERLAY_STATE_DIR, PATHS } from '../config.js';
7
+ import type { Registration, ServiceAdvertisement, XVerification, StoredChange } from '../types.js';
8
+
9
+ /**
10
+ * Ensure the overlay state directory exists.
11
+ */
12
+ export function ensureStateDir(): void {
13
+ fs.mkdirSync(OVERLAY_STATE_DIR, { recursive: true });
14
+ }
15
+
16
+ /**
17
+ * Load registration data from disk.
18
+ */
19
+ export function loadRegistration(): Registration | null {
20
+ try {
21
+ if (fs.existsSync(PATHS.registration)) {
22
+ return JSON.parse(fs.readFileSync(PATHS.registration, 'utf-8'));
23
+ }
24
+ } catch {
25
+ // Ignore parse errors
26
+ }
27
+ return null;
28
+ }
29
+
30
+ /**
31
+ * Save registration data to disk.
32
+ */
33
+ export function saveRegistration(data: Registration): void {
34
+ ensureStateDir();
35
+ fs.writeFileSync(PATHS.registration, JSON.stringify(data, null, 2), 'utf-8');
36
+ }
37
+
38
+ /**
39
+ * Delete registration file.
40
+ */
41
+ export function deleteRegistration(): void {
42
+ try {
43
+ fs.unlinkSync(PATHS.registration);
44
+ } catch {
45
+ // Ignore if file doesn't exist
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Load services list from disk.
51
+ */
52
+ export function loadServices(): ServiceAdvertisement[] {
53
+ try {
54
+ if (fs.existsSync(PATHS.services)) {
55
+ return JSON.parse(fs.readFileSync(PATHS.services, 'utf-8'));
56
+ }
57
+ } catch {
58
+ // Ignore parse errors
59
+ }
60
+ return [];
61
+ }
62
+
63
+ /**
64
+ * Save services list to disk.
65
+ */
66
+ export function saveServices(services: ServiceAdvertisement[]): void {
67
+ ensureStateDir();
68
+ fs.writeFileSync(PATHS.services, JSON.stringify(services, null, 2), 'utf-8');
69
+ }
70
+
71
+ /**
72
+ * Load X verifications from disk.
73
+ */
74
+ export function loadXVerifications(): XVerification[] {
75
+ try {
76
+ if (fs.existsSync(PATHS.xVerifications)) {
77
+ return JSON.parse(fs.readFileSync(PATHS.xVerifications, 'utf-8'));
78
+ }
79
+ } catch {
80
+ // Ignore parse errors
81
+ }
82
+ return [];
83
+ }
84
+
85
+ /**
86
+ * Save X verifications to disk.
87
+ */
88
+ export function saveXVerifications(verifications: XVerification[]): void {
89
+ ensureStateDir();
90
+ fs.writeFileSync(PATHS.xVerifications, JSON.stringify(verifications, null, 2), 'utf-8');
91
+ }
92
+
93
+ /**
94
+ * Append a line to a JSONL file.
95
+ */
96
+ export function appendToJsonl(filePath: string, entry: Record<string, unknown>): void {
97
+ ensureStateDir();
98
+ fs.appendFileSync(filePath, JSON.stringify(entry) + '\n');
99
+ }
100
+
101
+ /**
102
+ * Read and parse a JSONL file.
103
+ */
104
+ export function readJsonl<T>(filePath: string): T[] {
105
+ if (!fs.existsSync(filePath)) return [];
106
+ const lines = fs.readFileSync(filePath, 'utf-8').trim().split('\n').filter(Boolean);
107
+ return lines.map(line => {
108
+ try {
109
+ return JSON.parse(line);
110
+ } catch {
111
+ return null;
112
+ }
113
+ }).filter(Boolean) as T[];
114
+ }
115
+
116
+ /**
117
+ * Load stored change BEEF data.
118
+ */
119
+ export function loadStoredChange(): StoredChange | null {
120
+ try {
121
+ if (fs.existsSync(PATHS.latestChange)) {
122
+ return JSON.parse(fs.readFileSync(PATHS.latestChange, 'utf-8'));
123
+ }
124
+ } catch {
125
+ // Ignore parse errors
126
+ }
127
+ return null;
128
+ }
129
+
130
+ /**
131
+ * Save stored change BEEF data.
132
+ */
133
+ export function saveStoredChange(data: StoredChange): void {
134
+ ensureStateDir();
135
+ fs.writeFileSync(PATHS.latestChange, JSON.stringify(data));
136
+ }
137
+
138
+ /**
139
+ * Delete stored change file.
140
+ */
141
+ export function deleteStoredChange(): void {
142
+ try {
143
+ fs.unlinkSync(PATHS.latestChange);
144
+ } catch {
145
+ // Ignore if file doesn't exist
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Clean up old entries from service queue.
151
+ * Removes entries older than maxAgeMs or entries with final statuses older than finalStatusMaxAgeMs.
152
+ */
153
+ export function cleanupServiceQueue(maxAgeMs: number = 24 * 60 * 60 * 1000, finalStatusMaxAgeMs: number = 2 * 60 * 60 * 1000): void {
154
+ if (!fs.existsSync(PATHS.serviceQueue)) return;
155
+
156
+ const now = Date.now();
157
+ const finalStatuses = ['fulfilled', 'rejected', 'delivery_failed', 'failed', 'error'];
158
+
159
+ const lines = fs.readFileSync(PATHS.serviceQueue, 'utf-8').trim().split('\n').filter(Boolean);
160
+ const keptLines: string[] = [];
161
+ let removedCount = 0;
162
+
163
+ for (const line of lines) {
164
+ try {
165
+ const entry = JSON.parse(line);
166
+ const entryAge = now - (entry._ts || 0);
167
+
168
+ // Always keep pending entries that aren't too old
169
+ if (entry.status === 'pending' && entryAge < maxAgeMs) {
170
+ keptLines.push(line);
171
+ continue;
172
+ }
173
+
174
+ // Keep final status entries only if they're recent
175
+ if (finalStatuses.includes(entry.status) && entryAge < finalStatusMaxAgeMs) {
176
+ keptLines.push(line);
177
+ continue;
178
+ }
179
+
180
+ // Remove this entry
181
+ removedCount++;
182
+ } catch {
183
+ // Keep malformed entries to avoid data loss
184
+ keptLines.push(line);
185
+ }
186
+ }
187
+
188
+ if (removedCount > 0) {
189
+ fs.writeFileSync(PATHS.serviceQueue, keptLines.join('\n') + (keptLines.length ? '\n' : ''));
190
+ console.error(JSON.stringify({ event: 'queue-cleanup', removed: removedCount, kept: keptLines.length }));
191
+ }
192
+ }
193
+
194
+ /**
195
+ * Atomically update a service queue entry status.
196
+ * Returns true if the entry was found and updated, false otherwise.
197
+ */
198
+ export function updateServiceQueueStatus(
199
+ requestId: string,
200
+ newStatus: string,
201
+ additionalFields: Record<string, any> = {}
202
+ ): boolean {
203
+ if (!fs.existsSync(PATHS.serviceQueue)) return false;
204
+
205
+ const lines = fs.readFileSync(PATHS.serviceQueue, 'utf-8').trim().split('\n').filter(Boolean);
206
+ let updated = false;
207
+
208
+ const updatedLines = lines.map(line => {
209
+ try {
210
+ const entry = JSON.parse(line);
211
+ if (entry.requestId === requestId) {
212
+ updated = true;
213
+ return JSON.stringify({
214
+ ...entry,
215
+ status: newStatus,
216
+ ...additionalFields,
217
+ updatedAt: Date.now()
218
+ });
219
+ }
220
+ return line;
221
+ } catch {
222
+ return line;
223
+ }
224
+ });
225
+
226
+ if (updated) {
227
+ fs.writeFileSync(PATHS.serviceQueue, updatedLines.join('\n') + '\n');
228
+ }
229
+
230
+ return updated;
231
+ }
@@ -0,0 +1,106 @@
1
+ /**
2
+ * WhatsOnChain API helpers with retry logic and rate limiting.
3
+ */
4
+
5
+ import { NETWORK, WOC_API_KEY } from '../config.js';
6
+
7
+ /**
8
+ * Fetch from WhatsonChain with optional API key auth and retry logic.
9
+ * Retries on 429 (rate limit) and 5xx errors with exponential backoff.
10
+ * Includes timeout to prevent hanging indefinitely.
11
+ */
12
+ export async function wocFetch(
13
+ urlPath: string,
14
+ options: RequestInit = {},
15
+ maxRetries = 3,
16
+ timeoutMs = 30000
17
+ ): Promise<Response> {
18
+ const wocNet = NETWORK === 'mainnet' ? 'main' : 'test';
19
+ const base = `https://api.whatsonchain.com/v1/bsv/${wocNet}`;
20
+ const url = urlPath.startsWith('http') ? urlPath : `${base}${urlPath}`;
21
+ const headers: Record<string, string> = { ...(options.headers as Record<string, string> || {}) };
22
+ if (WOC_API_KEY) {
23
+ headers['Authorization'] = `Bearer ${WOC_API_KEY}`;
24
+ }
25
+
26
+ let lastError: Error | undefined;
27
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
28
+ try {
29
+ const controller = new AbortController();
30
+ const timeout = setTimeout(() => controller.abort(), timeoutMs);
31
+
32
+ const resp = await fetch(url, { ...options, headers, signal: controller.signal });
33
+ clearTimeout(timeout);
34
+
35
+ // Retry on 429 (rate limit) or 5xx (server error)
36
+ if ((resp.status === 429 || resp.status >= 500) && attempt < maxRetries) {
37
+ const delayMs = Math.min(1000 * Math.pow(2, attempt), 8000);
38
+ await new Promise(r => setTimeout(r, delayMs));
39
+ continue;
40
+ }
41
+
42
+ return resp;
43
+ } catch (err) {
44
+ lastError = err as Error;
45
+ if (attempt < maxRetries) {
46
+ const delayMs = Math.min(1000 * Math.pow(2, attempt), 8000);
47
+ await new Promise(r => setTimeout(r, delayMs));
48
+ continue;
49
+ }
50
+ }
51
+ }
52
+
53
+ throw lastError || new Error('WoC fetch failed after retries');
54
+ }
55
+
56
+ /**
57
+ * Fetch with timeout using AbortController.
58
+ */
59
+ export async function fetchWithTimeout(
60
+ url: string,
61
+ options: RequestInit = {},
62
+ timeoutMs = 15000
63
+ ): Promise<Response> {
64
+ const controller = new AbortController();
65
+ const timeout = setTimeout(() => controller.abort(), timeoutMs);
66
+ try {
67
+ const resp = await fetch(url, { ...options, signal: controller.signal });
68
+ return resp;
69
+ } finally {
70
+ clearTimeout(timeout);
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Fetch a pre-built BEEF from WhatsonChain for a given txid.
76
+ * WoC returns raw binary BEEF that includes the full source chain and merkle proofs.
77
+ */
78
+ export async function fetchBeefFromWoC(txid: string): Promise<Uint8Array | null> {
79
+ try {
80
+ const resp = await wocFetch(`/tx/${txid}/beef`);
81
+ if (!resp.ok) return null;
82
+ const hexStr = (await resp.text()).trim();
83
+ if (!hexStr || hexStr.length < 8) return null;
84
+ const bytes = hexStr.match(/.{2}/g)!.map(h => parseInt(h, 16));
85
+ return new Uint8Array(bytes);
86
+ } catch {
87
+ return null;
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Get the WoC base URL for the current network.
93
+ */
94
+ export function getWocBaseUrl(): string {
95
+ const wocNet = NETWORK === 'mainnet' ? 'main' : 'test';
96
+ return `https://api.whatsonchain.com/v1/bsv/${wocNet}`;
97
+ }
98
+
99
+ /**
100
+ * Get the explorer base URL for the current network.
101
+ */
102
+ export function getExplorerBaseUrl(): string {
103
+ return NETWORK === 'mainnet'
104
+ ? 'https://whatsonchain.com'
105
+ : 'https://test.whatsonchain.com';
106
+ }