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,6 @@
1
+ /**
2
+ * Payment module exports.
3
+ */
4
+ export * from './types.js';
5
+ export * from './build.js';
6
+ export * from './commands.js';
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Payment module exports.
3
+ */
4
+ export * from './types.js';
5
+ export * from './build.js';
6
+ export * from './commands.js';
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Payment-specific type definitions.
3
+ */
4
+ export interface PaymentResult {
5
+ /** Base64-encoded Atomic BEEF transaction data */
6
+ beef: string;
7
+ /** Transaction ID (hex) */
8
+ txid: string;
9
+ /** Amount paid in satoshis */
10
+ satoshis: number;
11
+ /** BRC-29 derivation prefix (base64) - needed by recipient */
12
+ derivationPrefix: string;
13
+ /** BRC-29 derivation suffix (base64) - needed by recipient */
14
+ derivationSuffix: string;
15
+ /** Sender's identity key (compressed hex) - needed by recipient */
16
+ senderIdentityKey: string;
17
+ }
18
+ export interface PaymentParams {
19
+ /** Recipient's compressed public key (hex, 66 chars starting with 02/03) */
20
+ to: string;
21
+ /** Amount to pay in satoshis */
22
+ satoshis: number;
23
+ /** Optional human-readable description */
24
+ description?: string;
25
+ }
26
+ export interface VerifyParams {
27
+ /** Base64-encoded BEEF */
28
+ beef: string;
29
+ /** Expected amount (optional) */
30
+ expectedAmount?: number;
31
+ /** Expected sender identity key (optional) */
32
+ expectedSender?: string;
33
+ }
34
+ export interface VerifyResult {
35
+ valid: boolean;
36
+ txid: string;
37
+ outputCount: number;
38
+ errors: string[];
39
+ }
40
+ export interface AcceptParams {
41
+ /** Base64-encoded Atomic BEEF */
42
+ beef: string;
43
+ /** Output index (default: 0) */
44
+ vout?: number;
45
+ /** BRC-29 derivation prefix from PaymentResult */
46
+ derivationPrefix: string;
47
+ /** BRC-29 derivation suffix from PaymentResult */
48
+ derivationSuffix: string;
49
+ /** Sender's identity key from PaymentResult */
50
+ senderIdentityKey: string;
51
+ /** Optional description */
52
+ description?: string;
53
+ }
54
+ export interface AcceptResult {
55
+ accepted: boolean;
56
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Payment-specific type definitions.
3
+ */
4
+ export {};
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Services module exports.
3
+ */
4
+ export * from './request.js';
5
+ export * from './respond.js';
6
+ export * from './queue.js';
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Services module exports.
3
+ */
4
+ export * from './request.js';
5
+ export * from './respond.js';
6
+ export * from './queue.js';
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Service queue commands.
3
+ */
4
+ /**
5
+ * Service queue command: list pending service requests.
6
+ */
7
+ export declare function cmdServiceQueue(): Promise<never>;
8
+ /**
9
+ * Research queue command: list pending research requests.
10
+ */
11
+ export declare function cmdResearchQueue(): Promise<never>;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Service queue commands.
3
+ */
4
+ import fs from 'node:fs';
5
+ import { PATHS } from '../config.js';
6
+ import { ok } from '../output.js';
7
+ import { readJsonl } from '../utils/storage.js';
8
+ /**
9
+ * Service queue command: list pending service requests.
10
+ */
11
+ export async function cmdServiceQueue() {
12
+ if (!fs.existsSync(PATHS.serviceQueue)) {
13
+ return ok({ pending: [], count: 0 });
14
+ }
15
+ const entries = readJsonl(PATHS.serviceQueue);
16
+ const pending = entries.filter(e => e.status === 'pending');
17
+ return ok({ pending, count: pending.length, total: entries.length });
18
+ }
19
+ /**
20
+ * Research queue command: list pending research requests.
21
+ */
22
+ export async function cmdResearchQueue() {
23
+ if (!fs.existsSync(PATHS.researchQueue)) {
24
+ return ok({ pending: [] });
25
+ }
26
+ const entries = readJsonl(PATHS.researchQueue);
27
+ return ok({ pending: entries, count: entries.length });
28
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Service request command.
3
+ */
4
+ /**
5
+ * Request service command: send a service request with optional payment.
6
+ */
7
+ export declare function cmdRequestService(targetKey: string | undefined, serviceId: string | undefined, satsStr?: string, inputJsonStr?: string): Promise<never>;
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Service request command.
3
+ */
4
+ import { OVERLAY_URL } from '../config.js';
5
+ import { ok, fail } from '../output.js';
6
+ import { loadIdentity, signRelayMessage } from '../wallet/identity.js';
7
+ import { buildDirectPayment } from '../payment/build.js';
8
+ /**
9
+ * Request service command: send a service request with optional payment.
10
+ */
11
+ export async function cmdRequestService(targetKey, serviceId, satsStr, inputJsonStr) {
12
+ if (!targetKey || !serviceId) {
13
+ return fail('Usage: request-service <identityKey> <serviceId> [sats] [inputJson]');
14
+ }
15
+ if (!/^0[23][0-9a-fA-F]{64}$/.test(targetKey)) {
16
+ return fail('Target must be a compressed public key (66 hex chars, 02/03 prefix)');
17
+ }
18
+ const { identityKey, privKey } = await loadIdentity();
19
+ const sats = parseInt(satsStr || '5', 10);
20
+ // Parse optional input JSON
21
+ let inputData = null;
22
+ if (inputJsonStr) {
23
+ try {
24
+ inputData = JSON.parse(inputJsonStr);
25
+ }
26
+ catch {
27
+ return fail('inputJson must be valid JSON');
28
+ }
29
+ }
30
+ // Build the service request payload
31
+ let paymentData = null;
32
+ if (sats > 0) {
33
+ try {
34
+ const payment = await buildDirectPayment(targetKey, sats, `service-request: ${serviceId}`);
35
+ paymentData = {
36
+ beef: payment.beef,
37
+ txid: payment.txid,
38
+ satoshis: payment.satoshis,
39
+ derivationPrefix: payment.derivationPrefix,
40
+ derivationSuffix: payment.derivationSuffix,
41
+ senderIdentityKey: payment.senderIdentityKey,
42
+ };
43
+ }
44
+ catch (err) {
45
+ // Payment failed — send request without payment
46
+ paymentData = { error: String(err.message || err) };
47
+ }
48
+ }
49
+ const requestPayload = {
50
+ serviceId,
51
+ ...(inputData ? { input: inputData } : {}),
52
+ payment: paymentData,
53
+ requestedAt: new Date().toISOString(),
54
+ };
55
+ const signature = await signRelayMessage(privKey, targetKey, 'service-request', requestPayload);
56
+ const resp = await fetch(`${OVERLAY_URL}/relay/send`, {
57
+ method: 'POST',
58
+ headers: { 'Content-Type': 'application/json' },
59
+ body: JSON.stringify({
60
+ from: identityKey,
61
+ to: targetKey,
62
+ type: 'service-request',
63
+ payload: requestPayload,
64
+ signature,
65
+ }),
66
+ });
67
+ if (!resp.ok) {
68
+ const body = await resp.text();
69
+ return fail(`Relay send failed (${resp.status}): ${body}`);
70
+ }
71
+ const result = await resp.json();
72
+ return ok({
73
+ sent: true,
74
+ requestId: result.id,
75
+ to: targetKey,
76
+ serviceId,
77
+ paymentIncluded: paymentData && !paymentData.error,
78
+ paymentTxid: paymentData?.txid || null,
79
+ satoshis: paymentData?.satoshis || 0,
80
+ note: 'Poll for service-response to get the result',
81
+ });
82
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Service response commands.
3
+ */
4
+ /**
5
+ * Respond to a service request.
6
+ */
7
+ export declare function cmdRespondService(requestId: string | undefined, recipientKey: string | undefined, serviceId: string | undefined, resultJson: string | undefined): Promise<never>;
8
+ /**
9
+ * Respond to a research request with results.
10
+ */
11
+ export declare function cmdResearchRespond(resultJsonPath: string | undefined): Promise<never>;
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Service response commands.
3
+ */
4
+ import fs from 'node:fs';
5
+ import { OVERLAY_URL, PATHS } from '../config.js';
6
+ import { ok, fail } from '../output.js';
7
+ import { loadIdentity, signRelayMessage } from '../wallet/identity.js';
8
+ import { updateServiceQueueStatus } from '../utils/storage.js';
9
+ /**
10
+ * Respond to a service request.
11
+ */
12
+ export async function cmdRespondService(requestId, recipientKey, serviceId, resultJson) {
13
+ if (!requestId || !recipientKey || !serviceId || !resultJson) {
14
+ return fail('Usage: respond-service <requestId> <recipientKey> <serviceId> <resultJson>');
15
+ }
16
+ let result;
17
+ try {
18
+ result = JSON.parse(resultJson);
19
+ }
20
+ catch {
21
+ return fail('resultJson must be valid JSON');
22
+ }
23
+ const { identityKey, privKey } = await loadIdentity();
24
+ // Check if already processed before sending response (idempotency)
25
+ if (fs.existsSync(PATHS.serviceQueue)) {
26
+ const lines = fs.readFileSync(PATHS.serviceQueue, 'utf-8').trim().split('\n').filter(Boolean);
27
+ const finalStatuses = ['fulfilled', 'rejected', 'delivery_failed', 'failed', 'error'];
28
+ for (const line of lines) {
29
+ try {
30
+ const entry = JSON.parse(line);
31
+ if (entry.requestId === requestId && finalStatuses.includes(entry.status)) {
32
+ return ok({
33
+ sent: false,
34
+ requestId,
35
+ serviceId,
36
+ to: recipientKey,
37
+ message: `Request already processed with status: ${entry.status}`,
38
+ alreadyProcessed: true,
39
+ previousStatus: entry.status
40
+ });
41
+ }
42
+ }
43
+ catch { }
44
+ }
45
+ }
46
+ const responsePayload = {
47
+ requestId,
48
+ serviceId,
49
+ status: 'fulfilled',
50
+ result,
51
+ };
52
+ const sig = await signRelayMessage(privKey, recipientKey, 'service-response', responsePayload);
53
+ const resp = await fetch(`${OVERLAY_URL}/relay/send`, {
54
+ method: 'POST',
55
+ headers: { 'Content-Type': 'application/json' },
56
+ body: JSON.stringify({
57
+ from: identityKey,
58
+ to: recipientKey,
59
+ type: 'service-response',
60
+ payload: responsePayload,
61
+ signature: sig,
62
+ }),
63
+ });
64
+ if (!resp.ok) {
65
+ // Mark as failed in queue using atomic update
66
+ updateServiceQueueStatus(requestId, 'failed', {
67
+ failedAt: Date.now(),
68
+ error: `Relay send failed: ${resp.status}`
69
+ });
70
+ return fail(`Relay send failed: ${resp.status}`);
71
+ }
72
+ // Mark as fulfilled in queue using atomic update
73
+ updateServiceQueueStatus(requestId, 'fulfilled', {
74
+ fulfilledAt: Date.now()
75
+ });
76
+ return ok({ sent: true, requestId, serviceId, to: recipientKey });
77
+ }
78
+ /**
79
+ * Respond to a research request with results.
80
+ */
81
+ export async function cmdResearchRespond(resultJsonPath) {
82
+ if (!resultJsonPath)
83
+ return fail('Usage: research-respond <resultJsonFile>');
84
+ if (!fs.existsSync(resultJsonPath))
85
+ return fail(`File not found: ${resultJsonPath}`);
86
+ const result = JSON.parse(fs.readFileSync(resultJsonPath, 'utf-8'));
87
+ const { requestId, from: recipientKey, query, research } = result;
88
+ if (!requestId || !recipientKey || !research) {
89
+ return fail('Result JSON must have: requestId, from, query, research');
90
+ }
91
+ const { identityKey, privKey } = await loadIdentity();
92
+ const responsePayload = {
93
+ requestId,
94
+ serviceId: 'web-research',
95
+ status: 'fulfilled',
96
+ result: research,
97
+ paymentAccepted: true,
98
+ paymentTxid: result.paymentTxid || null,
99
+ satoshisReceived: result.satoshisReceived || 0,
100
+ walletAccepted: result.walletAccepted ?? true,
101
+ };
102
+ const sig = await signRelayMessage(privKey, recipientKey, 'service-response', responsePayload);
103
+ const sendResp = await fetch(`${OVERLAY_URL}/relay/send`, {
104
+ method: 'POST',
105
+ headers: { 'Content-Type': 'application/json' },
106
+ body: JSON.stringify({
107
+ from: identityKey,
108
+ to: recipientKey,
109
+ type: 'service-response',
110
+ payload: responsePayload,
111
+ signature: sig,
112
+ }),
113
+ });
114
+ if (!sendResp.ok) {
115
+ return fail(`Failed to send response: ${await sendResp.text()}`);
116
+ }
117
+ const sendResult = await sendResp.json();
118
+ // Remove from queue
119
+ if (fs.existsSync(PATHS.researchQueue)) {
120
+ const lines = fs.readFileSync(PATHS.researchQueue, 'utf-8').trim().split('\n').filter(Boolean);
121
+ const remaining = lines.filter(l => {
122
+ try {
123
+ return JSON.parse(l).requestId !== requestId;
124
+ }
125
+ catch {
126
+ return true;
127
+ }
128
+ });
129
+ fs.writeFileSync(PATHS.researchQueue, remaining.length ? remaining.join('\n') + '\n' : '');
130
+ }
131
+ return ok({ responded: true, requestId, to: recipientKey, query, pushed: sendResult.pushed });
132
+ }
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Shared TypeScript interfaces for the overlay CLI.
3
+ */
4
+ export interface WalletIdentity {
5
+ rootKeyHex: string;
6
+ identityKey: string;
7
+ network: 'mainnet' | 'testnet';
8
+ }
9
+ export interface PaymentResult {
10
+ beef: string;
11
+ txid: string;
12
+ satoshis: number;
13
+ derivationPrefix: string;
14
+ derivationSuffix: string;
15
+ senderIdentityKey: string;
16
+ }
17
+ export interface PaymentParams {
18
+ to: string;
19
+ satoshis: number;
20
+ description?: string;
21
+ }
22
+ export interface ServiceAdvertisement {
23
+ serviceId: string;
24
+ name: string;
25
+ description: string;
26
+ priceSats: number;
27
+ txid?: string;
28
+ registeredAt?: string;
29
+ }
30
+ export interface Message {
31
+ id: string;
32
+ from: string;
33
+ to: string;
34
+ type: string;
35
+ payload: unknown;
36
+ signature?: string;
37
+ timestamp?: number;
38
+ }
39
+ export interface CommandResult<T = unknown> {
40
+ success: boolean;
41
+ data?: T;
42
+ error?: string;
43
+ }
44
+ export interface Registration {
45
+ identityKey: string;
46
+ agentName: string;
47
+ agentDescription: string;
48
+ overlayUrl: string;
49
+ identityTxid: string;
50
+ serviceTxid: string | null;
51
+ funded: string;
52
+ registeredAt: string;
53
+ }
54
+ export interface OverlayPayload {
55
+ protocol: string;
56
+ type: string;
57
+ identityKey?: string;
58
+ [key: string]: unknown;
59
+ }
60
+ export interface VerifyAndAcceptResult {
61
+ accepted: boolean;
62
+ txid: string | null;
63
+ satoshis: number;
64
+ outputIndex: number;
65
+ walletAccepted: boolean;
66
+ error: string | null;
67
+ }
68
+ export interface ProcessMessageResult {
69
+ id: string;
70
+ type: string;
71
+ action: string;
72
+ from: string;
73
+ ack: boolean;
74
+ [key: string]: unknown;
75
+ }
76
+ export interface RelayMessage {
77
+ id: string;
78
+ from: string;
79
+ to: string;
80
+ type: string;
81
+ payload: Record<string, unknown>;
82
+ signature?: string;
83
+ }
84
+ export interface XVerification {
85
+ identityKey: string;
86
+ xHandle: string;
87
+ xUserId: string;
88
+ tweetId: string;
89
+ tweetUrl: string;
90
+ signature: string;
91
+ verifiedAt: string;
92
+ txid?: string | null;
93
+ }
94
+ export interface StoredChange {
95
+ txHex: string;
96
+ txid: string;
97
+ vout: number;
98
+ satoshis: number;
99
+ sourceChain?: SourceChainEntry[];
100
+ savedAt: string;
101
+ }
102
+ export interface SourceChainEntry {
103
+ txHex: string;
104
+ txid: string;
105
+ merklePathHex?: string;
106
+ blockHeight?: number;
107
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Shared TypeScript interfaces for the overlay CLI.
3
+ */
4
+ export {};
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Utility module exports.
3
+ */
4
+ export * from './woc.js';
5
+ export * from './storage.js';
6
+ export * from './merkle.js';
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Utility module exports.
3
+ */
4
+ export * from './woc.js';
5
+ export * from './storage.js';
6
+ export * from './merkle.js';
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Merkle path utilities for SPV proofs.
3
+ */
4
+ import type { MerklePath as MerklePathType } from '@bsv/sdk';
5
+ /**
6
+ * Build a MerklePath from TSC (Transaction Status Check) proof data.
7
+ * @param txid - Transaction ID
8
+ * @param txIndex - Transaction's index in the block
9
+ * @param nodes - Array of sibling hashes (or '*' for duplicate)
10
+ * @param blockHeight - Block height
11
+ */
12
+ export declare function buildMerklePathFromTSC(txid: string, txIndex: number, nodes: string[], blockHeight: number): Promise<MerklePathType>;
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Merkle path utilities for SPV proofs.
3
+ */
4
+ // We'll import MerklePath dynamically to avoid issues with ESM resolution
5
+ let _MerklePath = null;
6
+ async function getMerklePath() {
7
+ if (_MerklePath)
8
+ return _MerklePath;
9
+ const sdk = await import('@bsv/sdk');
10
+ _MerklePath = sdk.MerklePath;
11
+ return _MerklePath;
12
+ }
13
+ /**
14
+ * Build a MerklePath from TSC (Transaction Status Check) proof data.
15
+ * @param txid - Transaction ID
16
+ * @param txIndex - Transaction's index in the block
17
+ * @param nodes - Array of sibling hashes (or '*' for duplicate)
18
+ * @param blockHeight - Block height
19
+ */
20
+ export async function buildMerklePathFromTSC(txid, txIndex, nodes, blockHeight) {
21
+ const MerklePath = await getMerklePath();
22
+ const treeHeight = nodes.length;
23
+ const mpPath = [];
24
+ // Level 0
25
+ const level0 = [
26
+ { offset: txIndex, hash: txid, txid: true }
27
+ ];
28
+ if (nodes[0] === '*') {
29
+ level0.push({ offset: txIndex ^ 1, duplicate: true });
30
+ }
31
+ else {
32
+ level0.push({ offset: txIndex ^ 1, hash: nodes[0] });
33
+ }
34
+ level0.sort((a, b) => a.offset - b.offset);
35
+ mpPath.push(level0);
36
+ // Higher levels
37
+ for (let i = 1; i < treeHeight; i++) {
38
+ const siblingOffset = (txIndex >> i) ^ 1;
39
+ if (nodes[i] === '*') {
40
+ mpPath.push([{ offset: siblingOffset, duplicate: true }]);
41
+ }
42
+ else {
43
+ mpPath.push([{ offset: siblingOffset, hash: nodes[i] }]);
44
+ }
45
+ }
46
+ return new MerklePath(blockHeight, mpPath);
47
+ }
@@ -0,0 +1,66 @@
1
+ /**
2
+ * File-based storage helpers for registration, services, and queues.
3
+ */
4
+ import type { Registration, ServiceAdvertisement, XVerification, StoredChange } from '../types.js';
5
+ /**
6
+ * Ensure the overlay state directory exists.
7
+ */
8
+ export declare function ensureStateDir(): void;
9
+ /**
10
+ * Load registration data from disk.
11
+ */
12
+ export declare function loadRegistration(): Registration | null;
13
+ /**
14
+ * Save registration data to disk.
15
+ */
16
+ export declare function saveRegistration(data: Registration): void;
17
+ /**
18
+ * Delete registration file.
19
+ */
20
+ export declare function deleteRegistration(): void;
21
+ /**
22
+ * Load services list from disk.
23
+ */
24
+ export declare function loadServices(): ServiceAdvertisement[];
25
+ /**
26
+ * Save services list to disk.
27
+ */
28
+ export declare function saveServices(services: ServiceAdvertisement[]): void;
29
+ /**
30
+ * Load X verifications from disk.
31
+ */
32
+ export declare function loadXVerifications(): XVerification[];
33
+ /**
34
+ * Save X verifications to disk.
35
+ */
36
+ export declare function saveXVerifications(verifications: XVerification[]): void;
37
+ /**
38
+ * Append a line to a JSONL file.
39
+ */
40
+ export declare function appendToJsonl(filePath: string, entry: Record<string, unknown>): void;
41
+ /**
42
+ * Read and parse a JSONL file.
43
+ */
44
+ export declare function readJsonl<T>(filePath: string): T[];
45
+ /**
46
+ * Load stored change BEEF data.
47
+ */
48
+ export declare function loadStoredChange(): StoredChange | null;
49
+ /**
50
+ * Save stored change BEEF data.
51
+ */
52
+ export declare function saveStoredChange(data: StoredChange): void;
53
+ /**
54
+ * Delete stored change file.
55
+ */
56
+ export declare function deleteStoredChange(): void;
57
+ /**
58
+ * Clean up old entries from service queue.
59
+ * Removes entries older than maxAgeMs or entries with final statuses older than finalStatusMaxAgeMs.
60
+ */
61
+ export declare function cleanupServiceQueue(maxAgeMs?: number, finalStatusMaxAgeMs?: number): void;
62
+ /**
63
+ * Atomically update a service queue entry status.
64
+ * Returns true if the entry was found and updated, false otherwise.
65
+ */
66
+ export declare function updateServiceQueueStatus(requestId: string, newStatus: string, additionalFields?: Record<string, any>): boolean;