xytara 3.1.13 → 3.1.15

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 (68) hide show
  1. package/.env.example +38 -4
  2. package/BSV_TERANODE_PLATFORM_DISCONTINUITY_RUNBOOK.md +126 -126
  3. package/REAL_PAYMENT_SETUP.md +14 -7
  4. package/RELEASE_NOTES.md +9 -4
  5. package/fixtures/catalog.js +22 -0
  6. package/lib/a2a_rpc.js +287 -280
  7. package/lib/account_auth.js +585 -585
  8. package/lib/agent_identity.js +162 -162
  9. package/lib/asyncapi_contract.js +3 -3
  10. package/lib/bolt_adapter.js +187 -187
  11. package/lib/bolt_payment_front.js +224 -224
  12. package/lib/buyer_event_log.js +215 -0
  13. package/lib/buyer_safety_policy.js +132 -0
  14. package/lib/checkout_processor_adapter.js +217 -217
  15. package/lib/command_flow.js +911 -722
  16. package/lib/commerce_artifacts.js +591 -591
  17. package/lib/commerce_bridge.js +274 -274
  18. package/lib/commerce_cases.js +399 -399
  19. package/lib/commerce_client.js +7694 -7694
  20. package/lib/commerce_quotes.js +29 -0
  21. package/lib/commerce_runtime.js +322 -322
  22. package/lib/execution_dedupe.js +86 -86
  23. package/lib/external_credit_bridge.js +142 -142
  24. package/lib/http_transport.js +99 -81
  25. package/lib/l402_adapter.js +241 -241
  26. package/lib/l402_payment_front.js +265 -265
  27. package/lib/mpp_adapter.js +237 -237
  28. package/lib/openapi_contract.js +4 -0
  29. package/lib/payment_config.js +257 -253
  30. package/lib/payment_verification.js +357 -357
  31. package/lib/provider_payout_execution.js +155 -155
  32. package/lib/provider_registry.js +852 -842
  33. package/lib/rate_limiter.js +81 -81
  34. package/lib/runtime_state_store.js +495 -369
  35. package/lib/settlement_bsv_live.js +600 -600
  36. package/lib/settlement_bsv_teranode.js +117 -117
  37. package/lib/stripe_mpp_payment_front.js +258 -258
  38. package/lib/x402_facilitator.js +198 -191
  39. package/lib/x402_lane_contract.js +5 -3
  40. package/lib/x402_settlement_recovery.js +175 -0
  41. package/package.json +9 -3
  42. package/scripts/buyer_events_daily_report.js +79 -0
  43. package/scripts/state_backup_offsite.js +196 -0
  44. package/scripts/verify_a2a_rpc.js +131 -105
  45. package/scripts/verify_agent_identity.js +153 -153
  46. package/scripts/verify_all.js +1 -1
  47. package/scripts/verify_bolt_adapter.js +128 -128
  48. package/scripts/verify_buyer_event_log.js +123 -0
  49. package/scripts/verify_buyer_safety.js +141 -0
  50. package/scripts/verify_cdp_facilitator.js +110 -110
  51. package/scripts/verify_checkout_processor.js +109 -109
  52. package/scripts/verify_commerce_bridge.js +177 -177
  53. package/scripts/verify_execution_dedupe.js +189 -189
  54. package/scripts/verify_execution_surfacing.js +76 -76
  55. package/scripts/verify_final_activation.js +191 -191
  56. package/scripts/verify_l402_adapter.js +86 -86
  57. package/scripts/verify_lifecycle_spend_binding.js +88 -0
  58. package/scripts/verify_mpp_adapter.js +128 -128
  59. package/scripts/verify_production_readiness.js +268 -268
  60. package/scripts/verify_rate_limiter.js +52 -52
  61. package/scripts/verify_refund_credit.js +106 -106
  62. package/scripts/verify_release_candidate.js +81 -81
  63. package/scripts/verify_runtime_state_guard.js +117 -0
  64. package/scripts/verify_service.js +15421 -15421
  65. package/scripts/verify_verify_execute_settle.js +209 -208
  66. package/scripts/verify_x402_facilitator.js +307 -286
  67. package/scripts/verify_x402_settlement_recovery.js +153 -0
  68. package/server.js +12242 -11974
@@ -1,153 +1,153 @@
1
- "use strict";
2
-
3
- // Verifies cryptographic agent identity for delegated-spend credentials:
4
- // ed25519 key binding + fresh signed-proof enforcement with replay (encoding-
5
- // agnostic, mandatory/fail-closed), expiry and future-dating guards; key binding
6
- // carried through rotation; invalid keys not bricking; and byte-for-byte backward
7
- // compatibility when no key is bound.
8
-
9
- const crypto = require("crypto");
10
- const assert = require("assert");
11
- const ai = require("../lib/agent_identity");
12
- const { createSpendCredential, resolveSpendCredential, createAuthorityBinding, rotateAuthorityBinding } = require("../lib/account_auth");
13
-
14
- function mkState() {
15
- return {
16
- accountSpendCredentials: new Map(),
17
- accountSpendCredentialsByToken: new Map(),
18
- accountAuthorityBindings: new Map(),
19
- acceptedAgentSignatures: new Map()
20
- };
21
- }
22
-
23
- function signAuth(privateKey, credentialId, timestamp) {
24
- const msg = ai.buildSpendAuthMessage(credentialId, String(timestamp));
25
- return crypto.sign(null, Buffer.from(msg, "utf8"), privateKey).toString("base64");
26
- }
27
-
28
- function main() {
29
- const { publicKey, privateKey } = crypto.generateKeyPairSync("ed25519");
30
- const pubPem = publicKey.export({ type: "spki", format: "pem" });
31
- const pubDerB64 = publicKey.export({ type: "spki", format: "der" }).toString("base64");
32
-
33
- // 1. Key helpers: ref stable across PEM/DER; invalid -> null; sign/verify.
34
- const ref = ai.deriveAgentKeyRef(pubPem);
35
- assert.ok(ref && ref.startsWith("ed25519-spki-sha256:"), "ref format");
36
- assert.strictEqual(ai.deriveAgentKeyRef(pubDerB64), ref, "PEM and DER give the same ref");
37
- assert.strictEqual(ai.deriveAgentKeyRef("not a key"), null, "invalid key -> null");
38
- const m = "hello";
39
- const sig = crypto.sign(null, Buffer.from(m), privateKey).toString("base64");
40
- assert.strictEqual(ai.verifyAgentSignature(pubPem, m, sig), true, "valid sig verifies");
41
- assert.strictEqual(ai.verifyAgentSignature(pubPem, "tampered", sig), false, "tampered msg fails");
42
-
43
- // 2. No key bound -> bearer-only, unchanged behavior.
44
- let state = mkState();
45
- const cred0 = createSpendCredential(state, { credential_id: "c0", account_id: "acct" });
46
- assert.strictEqual(cred0.identity_bound, false);
47
- assert.strictEqual(cred0.agent_key_ref, null);
48
- assert.strictEqual(resolveSpendCredential(state, cred0.bearer_token).ok, true, "bearer-only resolves");
49
-
50
- // 3. Invalid key provided -> NOT bricked: stored as bearer-only (consistent view).
51
- state = mkState();
52
- const credBad = createSpendCredential(state, { credential_id: "cbad", account_id: "acct", agent_public_key: "garbage-not-a-key" });
53
- assert.strictEqual(credBad.identity_bound, false, "invalid key is ignored");
54
- assert.strictEqual(credBad.agent_key_ref, null);
55
- assert.strictEqual(resolveSpendCredential(state, credBad.bearer_token).ok, true, "falls back to bearer-only");
56
-
57
- // 4. Key bound -> signed proof required + enforced.
58
- state = mkState();
59
- const cred = createSpendCredential(state, { credential_id: "c1", account_id: "acct", agent_public_key: pubPem });
60
- assert.strictEqual(cred.identity_bound, true);
61
- assert.strictEqual(cred.agent_key_ref, ref);
62
-
63
- assert.strictEqual(resolveSpendCredential(state, cred.bearer_token).reason, "agent_signature_required", "no proof -> required");
64
-
65
- const ts = Date.now();
66
- assert.strictEqual(resolveSpendCredential(state, cred.bearer_token, { signature: signAuth(privateKey, "c1", ts), timestamp: String(ts) }).ok, true, "valid proof authorizes");
67
-
68
- // 5. Replay is encoding-agnostic (canonical decoded-signature key).
69
- const ts2 = Date.now() + 1;
70
- const sig2 = signAuth(privateKey, "c1", ts2);
71
- assert.strictEqual(resolveSpendCredential(state, cred.bearer_token, { signature: sig2, timestamp: String(ts2) }).ok, true);
72
- const sig2UrlSafe = sig2.replace(/\+/g, "-").replace(/\//g, "_"); // re-encoded, same bytes
73
- assert.strictEqual(resolveSpendCredential(state, cred.bearer_token, { signature: sig2UrlSafe, timestamp: String(ts2) }).reason, "agent_signature_replayed", "re-encoded replay is caught");
74
-
75
- // 6. Expired (too old) and future-dated proofs rejected.
76
- const oldTs = Date.now() - 10 * 60 * 1000;
77
- assert.strictEqual(resolveSpendCredential(state, cred.bearer_token, { signature: signAuth(privateKey, "c1", oldTs), timestamp: String(oldTs) }).reason, "agent_signature_expired");
78
- const futureTs = Date.now() + 10 * 60 * 1000;
79
- assert.strictEqual(resolveSpendCredential(state, cred.bearer_token, { signature: signAuth(privateKey, "c1", futureTs), timestamp: String(futureTs) }).reason, "agent_signature_future");
80
-
81
- // 7. Wrong key -> invalid.
82
- const other = crypto.generateKeyPairSync("ed25519");
83
- const ts3 = Date.now();
84
- assert.strictEqual(resolveSpendCredential(state, cred.bearer_token, { signature: signAuth(other.privateKey, "c1", ts3), timestamp: String(ts3) }).reason, "agent_signature_invalid");
85
-
86
- // 8. Fail closed if the replay store is unavailable.
87
- const stateNoStore = { accountSpendCredentials: state.accountSpendCredentials, accountSpendCredentialsByToken: state.accountSpendCredentialsByToken, accountAuthorityBindings: new Map() };
88
- const ts4 = Date.now();
89
- assert.strictEqual(resolveSpendCredential(stateNoStore, cred.bearer_token, { signature: signAuth(privateKey, "c1", ts4), timestamp: String(ts4) }).reason, "agent_replay_store_unavailable");
90
-
91
- // 9. Authority binding threads the key through; rotation preserves it.
92
- state = mkState();
93
- const binding = createAuthorityBinding(state, { binding_id: "b1", account_id: "acct", agent_public_key: pubPem, max_commit_units: 100 });
94
- assert.strictEqual(binding.spend_credential.identity_bound, true, "binding credential key-bound");
95
- assert.strictEqual(binding.spend_credential.agent_key_ref, ref);
96
- const rotated = rotateAuthorityBinding(state, "b1", {});
97
- assert.strictEqual(rotated.successor_authority_binding.spend_credential.identity_bound, true, "rotation preserves key binding");
98
- assert.strictEqual(rotated.successor_authority_binding.spend_credential.agent_key_ref, ref, "rotation preserves the same key ref");
99
-
100
- // 10. Request binding (amount/task) — additive, back-compat with everything above.
101
- state = mkState();
102
- const credB = createSpendCredential(state, { credential_id: "cb", account_id: "acct", agent_public_key: pubPem });
103
- const bindReq = { account_id: "acct", units: 10, task_ref: "doc.analyze" };
104
- const signBound = (ts, binding) =>
105
- crypto.sign(null, Buffer.from(ai.buildSpendAuthMessage("cb", String(ts), ai.buildSpendBindingDigest(binding)), "utf8"), privateKey).toString("base64");
106
-
107
- // 10a. A bound proof matching the request authorizes.
108
- let tsb = Date.now();
109
- let res = resolveSpendCredential(state, credB.bearer_token, { signature: signBound(tsb, bindReq), timestamp: String(tsb) }, bindReq);
110
- assert.strictEqual(res.ok, true, "bound proof matching the request authorizes");
111
-
112
- // 10b. A proof bound to 10 units cannot authorize a 999-unit request (amount tampering).
113
- tsb = Date.now() + 1;
114
- res = resolveSpendCredential(state, credB.bearer_token,
115
- { signature: signBound(tsb, { account_id: "acct", units: 10, task_ref: "doc.analyze" }), timestamp: String(tsb) },
116
- { account_id: "acct", units: 999, task_ref: "doc.analyze" });
117
- assert.strictEqual(res.reason, "agent_signature_invalid", "amount-bound proof rejects a different amount");
118
-
119
- // 10c. A proof bound to one task cannot authorize another.
120
- tsb = Date.now() + 2;
121
- res = resolveSpendCredential(state, credB.bearer_token,
122
- { signature: signBound(tsb, { account_id: "acct", units: 10, task_ref: "doc.analyze" }), timestamp: String(tsb) },
123
- { account_id: "acct", units: 10, task_ref: "image.render" });
124
- assert.strictEqual(res.reason, "agent_signature_invalid", "task-bound proof rejects a different task");
125
-
126
- // 10d. An unbound proof still authorizes by default even when a binding is supplied.
127
- tsb = Date.now() + 3;
128
- res = resolveSpendCredential(state, credB.bearer_token, { signature: signAuth(privateKey, "cb", tsb), timestamp: String(tsb) }, bindReq);
129
- assert.strictEqual(res.ok, true, "unbound proof still authorizes by default (back-compat)");
130
-
131
- // 10e. Strict binding is CALLER-scoped (requestBinding.require=true), not a global flag — so
132
- // it applies only where a binding is supplied (execute path), never the lifecycle paths.
133
- const strictBind = { ...bindReq, require: true };
134
- tsb = Date.now() + 4;
135
- res = resolveSpendCredential(state, credB.bearer_token, { signature: signAuth(privateKey, "cb", tsb), timestamp: String(tsb) }, strictBind);
136
- assert.strictEqual(res.reason, "agent_signature_binding_required_or_invalid", "strict refuses an unbound proof");
137
- tsb = Date.now() + 5;
138
- res = resolveSpendCredential(state, credB.bearer_token, { signature: signBound(tsb, bindReq), timestamp: String(tsb) }, strictBind);
139
- assert.strictEqual(res.ok, true, "strict accepts a matching bound proof");
140
- tsb = Date.now() + 6;
141
- res = resolveSpendCredential(state, credB.bearer_token, { signature: signBound(tsb, bindReq), timestamp: String(tsb) }, { require: true });
142
- assert.strictEqual(res.reason, "spend_binding_required", "strict requires a request binding");
143
-
144
- // 10f. A key-bound credential on a NON-binding caller (no requestBinding) is unaffected by
145
- // strict mode — the lifecycle paths (reserve/release/reverse) keep working.
146
- tsb = Date.now() + 7;
147
- res = resolveSpendCredential(state, credB.bearer_token, { signature: signAuth(privateKey, "cb", tsb), timestamp: String(tsb) });
148
- assert.strictEqual(res.ok, true, "no-binding caller still authorizes under back-compat");
149
-
150
- console.log("verify_agent_identity: all checks passed");
151
- }
152
-
153
- main();
1
+ "use strict";
2
+
3
+ // Verifies cryptographic agent identity for delegated-spend credentials:
4
+ // ed25519 key binding + fresh signed-proof enforcement with replay (encoding-
5
+ // agnostic, mandatory/fail-closed), expiry and future-dating guards; key binding
6
+ // carried through rotation; invalid keys not bricking; and byte-for-byte backward
7
+ // compatibility when no key is bound.
8
+
9
+ const crypto = require("crypto");
10
+ const assert = require("assert");
11
+ const ai = require("../lib/agent_identity");
12
+ const { createSpendCredential, resolveSpendCredential, createAuthorityBinding, rotateAuthorityBinding } = require("../lib/account_auth");
13
+
14
+ function mkState() {
15
+ return {
16
+ accountSpendCredentials: new Map(),
17
+ accountSpendCredentialsByToken: new Map(),
18
+ accountAuthorityBindings: new Map(),
19
+ acceptedAgentSignatures: new Map()
20
+ };
21
+ }
22
+
23
+ function signAuth(privateKey, credentialId, timestamp) {
24
+ const msg = ai.buildSpendAuthMessage(credentialId, String(timestamp));
25
+ return crypto.sign(null, Buffer.from(msg, "utf8"), privateKey).toString("base64");
26
+ }
27
+
28
+ function main() {
29
+ const { publicKey, privateKey } = crypto.generateKeyPairSync("ed25519");
30
+ const pubPem = publicKey.export({ type: "spki", format: "pem" });
31
+ const pubDerB64 = publicKey.export({ type: "spki", format: "der" }).toString("base64");
32
+
33
+ // 1. Key helpers: ref stable across PEM/DER; invalid -> null; sign/verify.
34
+ const ref = ai.deriveAgentKeyRef(pubPem);
35
+ assert.ok(ref && ref.startsWith("ed25519-spki-sha256:"), "ref format");
36
+ assert.strictEqual(ai.deriveAgentKeyRef(pubDerB64), ref, "PEM and DER give the same ref");
37
+ assert.strictEqual(ai.deriveAgentKeyRef("not a key"), null, "invalid key -> null");
38
+ const m = "hello";
39
+ const sig = crypto.sign(null, Buffer.from(m), privateKey).toString("base64");
40
+ assert.strictEqual(ai.verifyAgentSignature(pubPem, m, sig), true, "valid sig verifies");
41
+ assert.strictEqual(ai.verifyAgentSignature(pubPem, "tampered", sig), false, "tampered msg fails");
42
+
43
+ // 2. No key bound -> bearer-only, unchanged behavior.
44
+ let state = mkState();
45
+ const cred0 = createSpendCredential(state, { credential_id: "c0", account_id: "acct" });
46
+ assert.strictEqual(cred0.identity_bound, false);
47
+ assert.strictEqual(cred0.agent_key_ref, null);
48
+ assert.strictEqual(resolveSpendCredential(state, cred0.bearer_token).ok, true, "bearer-only resolves");
49
+
50
+ // 3. Invalid key provided -> NOT bricked: stored as bearer-only (consistent view).
51
+ state = mkState();
52
+ const credBad = createSpendCredential(state, { credential_id: "cbad", account_id: "acct", agent_public_key: "garbage-not-a-key" });
53
+ assert.strictEqual(credBad.identity_bound, false, "invalid key is ignored");
54
+ assert.strictEqual(credBad.agent_key_ref, null);
55
+ assert.strictEqual(resolveSpendCredential(state, credBad.bearer_token).ok, true, "falls back to bearer-only");
56
+
57
+ // 4. Key bound -> signed proof required + enforced.
58
+ state = mkState();
59
+ const cred = createSpendCredential(state, { credential_id: "c1", account_id: "acct", agent_public_key: pubPem });
60
+ assert.strictEqual(cred.identity_bound, true);
61
+ assert.strictEqual(cred.agent_key_ref, ref);
62
+
63
+ assert.strictEqual(resolveSpendCredential(state, cred.bearer_token).reason, "agent_signature_required", "no proof -> required");
64
+
65
+ const ts = Date.now();
66
+ assert.strictEqual(resolveSpendCredential(state, cred.bearer_token, { signature: signAuth(privateKey, "c1", ts), timestamp: String(ts) }).ok, true, "valid proof authorizes");
67
+
68
+ // 5. Replay is encoding-agnostic (canonical decoded-signature key).
69
+ const ts2 = Date.now() + 1;
70
+ const sig2 = signAuth(privateKey, "c1", ts2);
71
+ assert.strictEqual(resolveSpendCredential(state, cred.bearer_token, { signature: sig2, timestamp: String(ts2) }).ok, true);
72
+ const sig2UrlSafe = sig2.replace(/\+/g, "-").replace(/\//g, "_"); // re-encoded, same bytes
73
+ assert.strictEqual(resolveSpendCredential(state, cred.bearer_token, { signature: sig2UrlSafe, timestamp: String(ts2) }).reason, "agent_signature_replayed", "re-encoded replay is caught");
74
+
75
+ // 6. Expired (too old) and future-dated proofs rejected.
76
+ const oldTs = Date.now() - 10 * 60 * 1000;
77
+ assert.strictEqual(resolveSpendCredential(state, cred.bearer_token, { signature: signAuth(privateKey, "c1", oldTs), timestamp: String(oldTs) }).reason, "agent_signature_expired");
78
+ const futureTs = Date.now() + 10 * 60 * 1000;
79
+ assert.strictEqual(resolveSpendCredential(state, cred.bearer_token, { signature: signAuth(privateKey, "c1", futureTs), timestamp: String(futureTs) }).reason, "agent_signature_future");
80
+
81
+ // 7. Wrong key -> invalid.
82
+ const other = crypto.generateKeyPairSync("ed25519");
83
+ const ts3 = Date.now();
84
+ assert.strictEqual(resolveSpendCredential(state, cred.bearer_token, { signature: signAuth(other.privateKey, "c1", ts3), timestamp: String(ts3) }).reason, "agent_signature_invalid");
85
+
86
+ // 8. Fail closed if the replay store is unavailable.
87
+ const stateNoStore = { accountSpendCredentials: state.accountSpendCredentials, accountSpendCredentialsByToken: state.accountSpendCredentialsByToken, accountAuthorityBindings: new Map() };
88
+ const ts4 = Date.now();
89
+ assert.strictEqual(resolveSpendCredential(stateNoStore, cred.bearer_token, { signature: signAuth(privateKey, "c1", ts4), timestamp: String(ts4) }).reason, "agent_replay_store_unavailable");
90
+
91
+ // 9. Authority binding threads the key through; rotation preserves it.
92
+ state = mkState();
93
+ const binding = createAuthorityBinding(state, { binding_id: "b1", account_id: "acct", agent_public_key: pubPem, max_commit_units: 100 });
94
+ assert.strictEqual(binding.spend_credential.identity_bound, true, "binding credential key-bound");
95
+ assert.strictEqual(binding.spend_credential.agent_key_ref, ref);
96
+ const rotated = rotateAuthorityBinding(state, "b1", {});
97
+ assert.strictEqual(rotated.successor_authority_binding.spend_credential.identity_bound, true, "rotation preserves key binding");
98
+ assert.strictEqual(rotated.successor_authority_binding.spend_credential.agent_key_ref, ref, "rotation preserves the same key ref");
99
+
100
+ // 10. Request binding (amount/task) — additive, back-compat with everything above.
101
+ state = mkState();
102
+ const credB = createSpendCredential(state, { credential_id: "cb", account_id: "acct", agent_public_key: pubPem });
103
+ const bindReq = { account_id: "acct", units: 10, task_ref: "doc.analyze" };
104
+ const signBound = (ts, binding) =>
105
+ crypto.sign(null, Buffer.from(ai.buildSpendAuthMessage("cb", String(ts), ai.buildSpendBindingDigest(binding)), "utf8"), privateKey).toString("base64");
106
+
107
+ // 10a. A bound proof matching the request authorizes.
108
+ let tsb = Date.now();
109
+ let res = resolveSpendCredential(state, credB.bearer_token, { signature: signBound(tsb, bindReq), timestamp: String(tsb) }, bindReq);
110
+ assert.strictEqual(res.ok, true, "bound proof matching the request authorizes");
111
+
112
+ // 10b. A proof bound to 10 units cannot authorize a 999-unit request (amount tampering).
113
+ tsb = Date.now() + 1;
114
+ res = resolveSpendCredential(state, credB.bearer_token,
115
+ { signature: signBound(tsb, { account_id: "acct", units: 10, task_ref: "doc.analyze" }), timestamp: String(tsb) },
116
+ { account_id: "acct", units: 999, task_ref: "doc.analyze" });
117
+ assert.strictEqual(res.reason, "agent_signature_invalid", "amount-bound proof rejects a different amount");
118
+
119
+ // 10c. A proof bound to one task cannot authorize another.
120
+ tsb = Date.now() + 2;
121
+ res = resolveSpendCredential(state, credB.bearer_token,
122
+ { signature: signBound(tsb, { account_id: "acct", units: 10, task_ref: "doc.analyze" }), timestamp: String(tsb) },
123
+ { account_id: "acct", units: 10, task_ref: "image.render" });
124
+ assert.strictEqual(res.reason, "agent_signature_invalid", "task-bound proof rejects a different task");
125
+
126
+ // 10d. An unbound proof still authorizes by default even when a binding is supplied.
127
+ tsb = Date.now() + 3;
128
+ res = resolveSpendCredential(state, credB.bearer_token, { signature: signAuth(privateKey, "cb", tsb), timestamp: String(tsb) }, bindReq);
129
+ assert.strictEqual(res.ok, true, "unbound proof still authorizes by default (back-compat)");
130
+
131
+ // 10e. Strict binding is CALLER-scoped (requestBinding.require=true), not a global flag — so
132
+ // it applies only where a binding is supplied (execute path), never the lifecycle paths.
133
+ const strictBind = { ...bindReq, require: true };
134
+ tsb = Date.now() + 4;
135
+ res = resolveSpendCredential(state, credB.bearer_token, { signature: signAuth(privateKey, "cb", tsb), timestamp: String(tsb) }, strictBind);
136
+ assert.strictEqual(res.reason, "agent_signature_binding_required_or_invalid", "strict refuses an unbound proof");
137
+ tsb = Date.now() + 5;
138
+ res = resolveSpendCredential(state, credB.bearer_token, { signature: signBound(tsb, bindReq), timestamp: String(tsb) }, strictBind);
139
+ assert.strictEqual(res.ok, true, "strict accepts a matching bound proof");
140
+ tsb = Date.now() + 6;
141
+ res = resolveSpendCredential(state, credB.bearer_token, { signature: signBound(tsb, bindReq), timestamp: String(tsb) }, { require: true });
142
+ assert.strictEqual(res.reason, "spend_binding_required", "strict requires a request binding");
143
+
144
+ // 10f. A key-bound credential on a NON-binding caller (no requestBinding) is unaffected by
145
+ // strict mode — the lifecycle paths (reserve/release/reverse) keep working.
146
+ tsb = Date.now() + 7;
147
+ res = resolveSpendCredential(state, credB.bearer_token, { signature: signAuth(privateKey, "cb", tsb), timestamp: String(tsb) });
148
+ assert.strictEqual(res.ok, true, "no-binding caller still authorizes under back-compat");
149
+
150
+ console.log("verify_agent_identity: all checks passed");
151
+ }
152
+
153
+ main();
@@ -4960,4 +4960,4 @@ main().catch((error) => {
4960
4960
  console.error(error);
4961
4961
  process.exit(1);
4962
4962
  });
4963
-
4963
+
@@ -1,128 +1,128 @@
1
- "use strict";
2
-
3
- // Verifies the BSV/UTXO (BOLT) adapter (lib/bolt_adapter.js). Offline: the chain
4
- // fetch is injected as a mock, so no network. Exercises config gating, header
5
- // parsing, and the on-chain amount/address verification + confirmations logic.
6
-
7
- const assert = require("assert");
8
- const m = require("../lib/bolt_adapter");
9
-
10
- function setEnv(env) {
11
- const saved = {};
12
- for (const k in env) {
13
- saved[k] = process.env[k];
14
- if (env[k] === undefined) delete process.env[k];
15
- else process.env[k] = env[k];
16
- }
17
- return () => {
18
- for (const k in saved) {
19
- if (saved[k] === undefined) delete process.env[k];
20
- else process.env[k] = saved[k];
21
- }
22
- };
23
- }
24
-
25
- const PAYTO = "1NaxytraPayToAddressExampleXXXXXXXXXX";
26
- const TXID = "a".repeat(64);
27
-
28
- function mockTx(voutValueBsv, address, confirmations) {
29
- return async () => ({ status: 200, json: { confirmations, vout: [{ value: voutValueBsv, scriptPubKey: { addresses: [address] } }] } });
30
- }
31
-
32
- async function main() {
33
- // 1. Not configured.
34
- let restore = setEnv({ XYTARA_BOLT_PROVIDER: undefined });
35
- let cfg = m.loadBoltConfig();
36
- assert.strictEqual(cfg.provider, null);
37
- assert.strictEqual(m.isConfigured(cfg), false);
38
- assert.strictEqual((await m.verifyBsvPayment(cfg, TXID)).reason, "bolt_provider_not_configured");
39
- restore();
40
-
41
- // 2. Configured (whatsonchain).
42
- restore = setEnv({
43
- XYTARA_BOLT_PROVIDER: "whatsonchain",
44
- XYTARA_BOLT_PAYTO_ADDRESS: PAYTO,
45
- XYTARA_BOLT_PRICE_SATS: "1000",
46
- XYTARA_BOLT_MIN_CONFIRMATIONS: "1",
47
- XYTARA_BOLT_NETWORK: "main"
48
- });
49
- cfg = m.loadBoltConfig();
50
- assert.strictEqual(cfg.provider, "whatsonchain");
51
- assert.ok(cfg.apiUrl.includes("whatsonchain.com"));
52
-
53
- // Header parsing.
54
- assert.deepStrictEqual(m.parseBoltHeader(`BOLT ${TXID}`), { txid: TXID });
55
- assert.strictEqual(m.parseBoltHeader("BOLT not-a-txid"), null);
56
- assert.strictEqual(m.parseBoltHeader("Bearer x"), null);
57
-
58
- // Challenge shape.
59
- const ch = m.buildBoltChallenge(cfg);
60
- assert.strictEqual(ch.address, PAYTO);
61
- assert.strictEqual(ch.amount_sats, 1000);
62
-
63
- // Invalid txid -> rejected before any fetch.
64
- assert.strictEqual((await m.verifyBsvPayment(cfg, "xyz")).reason, "bolt_txid_invalid");
65
-
66
- // Sufficient payment (0.00002 BSV = 2000 sats to PAYTO, 3 confs) -> ok.
67
- let r = await m.verifyBsvPayment(cfg, TXID, mockTx(0.00002, PAYTO, 3));
68
- assert.strictEqual(r.ok, true);
69
- assert.strictEqual(r.paid_sats, 2000);
70
- assert.strictEqual(r.confirmations, 3);
71
-
72
- // --- Per-quote binding: unique exact amount ---
73
- const reqSats = m.computeRequiredSats(cfg, { quote_id: "quote_x" });
74
- assert.ok(reqSats >= 1000 && reqSats < 1000 + cfg.bindingWindowSats, "required = price + bounded offset");
75
- assert.strictEqual(reqSats, m.computeRequiredSats(cfg, { quote_id: "quote_x" }), "deterministic");
76
- // Paying the EXACT bound amount -> ok.
77
- r = await m.verifyBsvPayment(cfg, TXID, mockTx(reqSats / 1e8, PAYTO, 3), { requiredSats: reqSats });
78
- assert.strictEqual(r.ok, true);
79
- assert.strictEqual(r.paid_sats, reqSats);
80
- assert.strictEqual(r.required_sats, reqSats);
81
- // Paying a DIFFERENT amount (even to the right address) -> mismatch, so an
82
- // unrelated payment cannot be reused to satisfy this quote.
83
- r = await m.verifyBsvPayment(cfg, TXID, mockTx((reqSats + 1) / 1e8, PAYTO, 3), { requiredSats: reqSats });
84
- assert.strictEqual(r.reason, "bolt_amount_mismatch");
85
- // Challenge advertises the exact amount.
86
- const boundChallenge = m.buildBoltChallenge(cfg, { requiredSats: reqSats });
87
- assert.strictEqual(boundChallenge.amount_sats, reqSats);
88
- assert.strictEqual(boundChallenge.exact, true);
89
-
90
- // Insufficient amount (500 sats < 1000) -> rejected.
91
- r = await m.verifyBsvPayment(cfg, TXID, mockTx(0.000005, PAYTO, 3));
92
- assert.strictEqual(r.ok, false);
93
- assert.strictEqual(r.reason, "bolt_amount_insufficient");
94
-
95
- // Paid to the wrong address -> counts as zero -> insufficient.
96
- r = await m.verifyBsvPayment(cfg, TXID, mockTx(0.001, "1SomeOtherAddressXXXXXXXXXXXXXXXXXXX", 3));
97
- assert.strictEqual(r.reason, "bolt_amount_insufficient");
98
-
99
- // Too few confirmations -> rejected.
100
- r = await m.verifyBsvPayment(cfg, TXID, mockTx(0.00002, PAYTO, 0));
101
- assert.strictEqual(r.reason, "bolt_insufficient_confirmations");
102
-
103
- // Default min_confirmations is 1 (not 0) when unset -> mempool (0-conf) rejected.
104
- const dflt = setEnv({ XYTARA_BOLT_PROVIDER: "whatsonchain", XYTARA_BOLT_PAYTO_ADDRESS: PAYTO, XYTARA_BOLT_PRICE_SATS: "1000", XYTARA_BOLT_MIN_CONFIRMATIONS: undefined });
105
- const dcfg = m.loadBoltConfig();
106
- assert.strictEqual(dcfg.minConfirmations, 1);
107
- r = await m.verifyBsvPayment(dcfg, TXID, mockTx(0.00002, PAYTO, 0));
108
- assert.strictEqual(r.reason, "bolt_insufficient_confirmations");
109
- dflt();
110
-
111
- // Tx not found (404) -> rejected.
112
- r = await m.verifyBsvPayment(cfg, TXID, async () => ({ status: 404, json: null }));
113
- assert.strictEqual(r.reason, "bolt_tx_not_found");
114
- restore();
115
-
116
- // 3. Configured but missing payto/price -> explicit reason (no fetch).
117
- restore = setEnv({ XYTARA_BOLT_PROVIDER: "whatsonchain", XYTARA_BOLT_PAYTO_ADDRESS: undefined, XYTARA_BOLT_PRICE_SATS: undefined });
118
- cfg = m.loadBoltConfig();
119
- assert.strictEqual((await m.verifyBsvPayment(cfg, TXID)).reason, "bolt_payto_or_price_missing");
120
- restore();
121
-
122
- console.log("verify_bolt_adapter: all BSV/UTXO (BOLT) adapter checks passed");
123
- }
124
-
125
- main().catch((error) => {
126
- console.error("verify_bolt_adapter failed:", error && error.message ? error.message : error);
127
- process.exitCode = 1;
128
- });
1
+ "use strict";
2
+
3
+ // Verifies the BSV/UTXO (BOLT) adapter (lib/bolt_adapter.js). Offline: the chain
4
+ // fetch is injected as a mock, so no network. Exercises config gating, header
5
+ // parsing, and the on-chain amount/address verification + confirmations logic.
6
+
7
+ const assert = require("assert");
8
+ const m = require("../lib/bolt_adapter");
9
+
10
+ function setEnv(env) {
11
+ const saved = {};
12
+ for (const k in env) {
13
+ saved[k] = process.env[k];
14
+ if (env[k] === undefined) delete process.env[k];
15
+ else process.env[k] = env[k];
16
+ }
17
+ return () => {
18
+ for (const k in saved) {
19
+ if (saved[k] === undefined) delete process.env[k];
20
+ else process.env[k] = saved[k];
21
+ }
22
+ };
23
+ }
24
+
25
+ const PAYTO = "1NaxytraPayToAddressExampleXXXXXXXXXX";
26
+ const TXID = "a".repeat(64);
27
+
28
+ function mockTx(voutValueBsv, address, confirmations) {
29
+ return async () => ({ status: 200, json: { confirmations, vout: [{ value: voutValueBsv, scriptPubKey: { addresses: [address] } }] } });
30
+ }
31
+
32
+ async function main() {
33
+ // 1. Not configured.
34
+ let restore = setEnv({ XYTARA_BOLT_PROVIDER: undefined });
35
+ let cfg = m.loadBoltConfig();
36
+ assert.strictEqual(cfg.provider, null);
37
+ assert.strictEqual(m.isConfigured(cfg), false);
38
+ assert.strictEqual((await m.verifyBsvPayment(cfg, TXID)).reason, "bolt_provider_not_configured");
39
+ restore();
40
+
41
+ // 2. Configured (whatsonchain).
42
+ restore = setEnv({
43
+ XYTARA_BOLT_PROVIDER: "whatsonchain",
44
+ XYTARA_BOLT_PAYTO_ADDRESS: PAYTO,
45
+ XYTARA_BOLT_PRICE_SATS: "1000",
46
+ XYTARA_BOLT_MIN_CONFIRMATIONS: "1",
47
+ XYTARA_BOLT_NETWORK: "main"
48
+ });
49
+ cfg = m.loadBoltConfig();
50
+ assert.strictEqual(cfg.provider, "whatsonchain");
51
+ assert.ok(cfg.apiUrl.includes("whatsonchain.com"));
52
+
53
+ // Header parsing.
54
+ assert.deepStrictEqual(m.parseBoltHeader(`BOLT ${TXID}`), { txid: TXID });
55
+ assert.strictEqual(m.parseBoltHeader("BOLT not-a-txid"), null);
56
+ assert.strictEqual(m.parseBoltHeader("Bearer x"), null);
57
+
58
+ // Challenge shape.
59
+ const ch = m.buildBoltChallenge(cfg);
60
+ assert.strictEqual(ch.address, PAYTO);
61
+ assert.strictEqual(ch.amount_sats, 1000);
62
+
63
+ // Invalid txid -> rejected before any fetch.
64
+ assert.strictEqual((await m.verifyBsvPayment(cfg, "xyz")).reason, "bolt_txid_invalid");
65
+
66
+ // Sufficient payment (0.00002 BSV = 2000 sats to PAYTO, 3 confs) -> ok.
67
+ let r = await m.verifyBsvPayment(cfg, TXID, mockTx(0.00002, PAYTO, 3));
68
+ assert.strictEqual(r.ok, true);
69
+ assert.strictEqual(r.paid_sats, 2000);
70
+ assert.strictEqual(r.confirmations, 3);
71
+
72
+ // --- Per-quote binding: unique exact amount ---
73
+ const reqSats = m.computeRequiredSats(cfg, { quote_id: "quote_x" });
74
+ assert.ok(reqSats >= 1000 && reqSats < 1000 + cfg.bindingWindowSats, "required = price + bounded offset");
75
+ assert.strictEqual(reqSats, m.computeRequiredSats(cfg, { quote_id: "quote_x" }), "deterministic");
76
+ // Paying the EXACT bound amount -> ok.
77
+ r = await m.verifyBsvPayment(cfg, TXID, mockTx(reqSats / 1e8, PAYTO, 3), { requiredSats: reqSats });
78
+ assert.strictEqual(r.ok, true);
79
+ assert.strictEqual(r.paid_sats, reqSats);
80
+ assert.strictEqual(r.required_sats, reqSats);
81
+ // Paying a DIFFERENT amount (even to the right address) -> mismatch, so an
82
+ // unrelated payment cannot be reused to satisfy this quote.
83
+ r = await m.verifyBsvPayment(cfg, TXID, mockTx((reqSats + 1) / 1e8, PAYTO, 3), { requiredSats: reqSats });
84
+ assert.strictEqual(r.reason, "bolt_amount_mismatch");
85
+ // Challenge advertises the exact amount.
86
+ const boundChallenge = m.buildBoltChallenge(cfg, { requiredSats: reqSats });
87
+ assert.strictEqual(boundChallenge.amount_sats, reqSats);
88
+ assert.strictEqual(boundChallenge.exact, true);
89
+
90
+ // Insufficient amount (500 sats < 1000) -> rejected.
91
+ r = await m.verifyBsvPayment(cfg, TXID, mockTx(0.000005, PAYTO, 3));
92
+ assert.strictEqual(r.ok, false);
93
+ assert.strictEqual(r.reason, "bolt_amount_insufficient");
94
+
95
+ // Paid to the wrong address -> counts as zero -> insufficient.
96
+ r = await m.verifyBsvPayment(cfg, TXID, mockTx(0.001, "1SomeOtherAddressXXXXXXXXXXXXXXXXXXX", 3));
97
+ assert.strictEqual(r.reason, "bolt_amount_insufficient");
98
+
99
+ // Too few confirmations -> rejected.
100
+ r = await m.verifyBsvPayment(cfg, TXID, mockTx(0.00002, PAYTO, 0));
101
+ assert.strictEqual(r.reason, "bolt_insufficient_confirmations");
102
+
103
+ // Default min_confirmations is 1 (not 0) when unset -> mempool (0-conf) rejected.
104
+ const dflt = setEnv({ XYTARA_BOLT_PROVIDER: "whatsonchain", XYTARA_BOLT_PAYTO_ADDRESS: PAYTO, XYTARA_BOLT_PRICE_SATS: "1000", XYTARA_BOLT_MIN_CONFIRMATIONS: undefined });
105
+ const dcfg = m.loadBoltConfig();
106
+ assert.strictEqual(dcfg.minConfirmations, 1);
107
+ r = await m.verifyBsvPayment(dcfg, TXID, mockTx(0.00002, PAYTO, 0));
108
+ assert.strictEqual(r.reason, "bolt_insufficient_confirmations");
109
+ dflt();
110
+
111
+ // Tx not found (404) -> rejected.
112
+ r = await m.verifyBsvPayment(cfg, TXID, async () => ({ status: 404, json: null }));
113
+ assert.strictEqual(r.reason, "bolt_tx_not_found");
114
+ restore();
115
+
116
+ // 3. Configured but missing payto/price -> explicit reason (no fetch).
117
+ restore = setEnv({ XYTARA_BOLT_PROVIDER: "whatsonchain", XYTARA_BOLT_PAYTO_ADDRESS: undefined, XYTARA_BOLT_PRICE_SATS: undefined });
118
+ cfg = m.loadBoltConfig();
119
+ assert.strictEqual((await m.verifyBsvPayment(cfg, TXID)).reason, "bolt_payto_or_price_missing");
120
+ restore();
121
+
122
+ console.log("verify_bolt_adapter: all BSV/UTXO (BOLT) adapter checks passed");
123
+ }
124
+
125
+ main().catch((error) => {
126
+ console.error("verify_bolt_adapter failed:", error && error.message ? error.message : error);
127
+ process.exitCode = 1;
128
+ });