xytara 3.1.13 → 3.1.14
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.
- package/.env.example +8 -1
- package/BSV_TERANODE_PLATFORM_DISCONTINUITY_RUNBOOK.md +126 -126
- package/RELEASE_NOTES.md +6 -4
- package/lib/a2a_rpc.js +280 -280
- package/lib/account_auth.js +585 -585
- package/lib/agent_identity.js +162 -162
- package/lib/bolt_adapter.js +187 -187
- package/lib/bolt_payment_front.js +224 -224
- package/lib/checkout_processor_adapter.js +217 -217
- package/lib/command_flow.js +722 -722
- package/lib/commerce_artifacts.js +591 -591
- package/lib/commerce_bridge.js +274 -274
- package/lib/commerce_cases.js +399 -399
- package/lib/commerce_client.js +7694 -7694
- package/lib/commerce_quotes.js +29 -0
- package/lib/commerce_runtime.js +322 -322
- package/lib/execution_dedupe.js +86 -86
- package/lib/external_credit_bridge.js +142 -142
- package/lib/http_transport.js +81 -81
- package/lib/l402_adapter.js +241 -241
- package/lib/l402_payment_front.js +265 -265
- package/lib/mpp_adapter.js +237 -237
- package/lib/payment_config.js +253 -253
- package/lib/payment_verification.js +357 -357
- package/lib/provider_payout_execution.js +155 -155
- package/lib/provider_registry.js +842 -842
- package/lib/rate_limiter.js +81 -81
- package/lib/runtime_state_store.js +491 -369
- package/lib/settlement_bsv_live.js +600 -600
- package/lib/settlement_bsv_teranode.js +117 -117
- package/lib/stripe_mpp_payment_front.js +258 -258
- package/lib/x402_facilitator.js +191 -191
- package/package.json +5 -3
- package/scripts/state_backup_offsite.js +196 -0
- package/scripts/verify_a2a_rpc.js +105 -105
- package/scripts/verify_agent_identity.js +153 -153
- package/scripts/verify_all.js +1 -1
- package/scripts/verify_bolt_adapter.js +128 -128
- package/scripts/verify_cdp_facilitator.js +110 -110
- package/scripts/verify_checkout_processor.js +109 -109
- package/scripts/verify_commerce_bridge.js +177 -177
- package/scripts/verify_execution_dedupe.js +189 -189
- package/scripts/verify_execution_surfacing.js +76 -76
- package/scripts/verify_final_activation.js +191 -191
- package/scripts/verify_l402_adapter.js +86 -86
- package/scripts/verify_mpp_adapter.js +128 -128
- package/scripts/verify_production_readiness.js +268 -268
- package/scripts/verify_rate_limiter.js +52 -52
- package/scripts/verify_refund_credit.js +106 -106
- package/scripts/verify_release_candidate.js +81 -81
- package/scripts/verify_runtime_state_guard.js +117 -0
- package/scripts/verify_service.js +15421 -15421
- package/scripts/verify_verify_execute_settle.js +208 -208
- package/scripts/verify_x402_facilitator.js +286 -286
- package/server.js +11976 -11974
|
@@ -1,224 +1,224 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const crypto = require("crypto");
|
|
4
|
-
const boltAdapter = require("./bolt_adapter");
|
|
5
|
-
|
|
6
|
-
function normalizeString(value) {
|
|
7
|
-
return typeof value === "string" ? value.trim() : "";
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function safeEqualHex(left, right) {
|
|
11
|
-
const a = Buffer.from(String(left || ""), "hex");
|
|
12
|
-
const b = Buffer.from(String(right || ""), "hex");
|
|
13
|
-
if (a.length === 0 || b.length === 0 || a.length !== b.length) return false;
|
|
14
|
-
return crypto.timingSafeEqual(a, b);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function decodeBase64UrlJson(value) {
|
|
18
|
-
try {
|
|
19
|
-
return JSON.parse(Buffer.from(String(value || ""), "base64url").toString("utf8"));
|
|
20
|
-
} catch (_) {
|
|
21
|
-
return null;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function loadBoltFrontConfig() {
|
|
26
|
-
const sharedSecret = normalizeString(process.env.XYTARA_BOLT_SHARED_SECRET);
|
|
27
|
-
return {
|
|
28
|
-
enabled: String(process.env.XYTARA_BOLT_ENABLED || (sharedSecret ? "true" : "false")).trim().toLowerCase() !== "false",
|
|
29
|
-
shared_secret: sharedSecret || null,
|
|
30
|
-
network: normalizeString(process.env.XYTARA_BOLT_NETWORK) || normalizeString(process.env.XYTARA_BSV_TERANODE_NETWORK) || "mainnet",
|
|
31
|
-
proof_mode: normalizeString(process.env.XYTARA_BOLT_PROOF_MODE) || "spv_or_block_header_verifiable",
|
|
32
|
-
max_age_seconds: Math.max(30, Number(process.env.XYTARA_BOLT_MAX_AGE_SECONDS || 300) || 300),
|
|
33
|
-
required_headers: ["authorization"],
|
|
34
|
-
auth_scheme: "BOLT",
|
|
35
|
-
token_shape: "base64url_json_with_utxo_spv_proof_and_hmac_signed_payload"
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function parseBoltAuthorization(headers) {
|
|
40
|
-
const authorization = headers && typeof headers.authorization === "string"
|
|
41
|
-
? headers.authorization.trim()
|
|
42
|
-
: "";
|
|
43
|
-
const match = authorization.match(/^BOLT\s+(.+)$/i);
|
|
44
|
-
if (!match) return null;
|
|
45
|
-
const decoded = decodeBase64UrlJson(match[1]);
|
|
46
|
-
if (!decoded || typeof decoded !== "object") return null;
|
|
47
|
-
return {
|
|
48
|
-
wallet_id: decoded.wallet_id || decoded.gateway_id || "bolt_gateway",
|
|
49
|
-
encoded: decoded.payload || null,
|
|
50
|
-
signature: decoded.signature || null,
|
|
51
|
-
txid: decoded.txid || null,
|
|
52
|
-
spv_proof_ref: decoded.spv_proof_ref || null,
|
|
53
|
-
merkle_path_ref: decoded.merkle_path_ref || null,
|
|
54
|
-
block_header_ref: decoded.block_header_ref || null,
|
|
55
|
-
network: decoded.network || null,
|
|
56
|
-
proof_bundle_ref: decoded.proof_bundle_ref || null,
|
|
57
|
-
scheme: "bolt"
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function verifyBoltPaymentPayload(paymentPayload, quote, walletId, maxAgeSeconds, config) {
|
|
62
|
-
if (!paymentPayload || typeof paymentPayload !== "object") return { ok: false, reason: "bolt_payload_invalid" };
|
|
63
|
-
if (!paymentPayload.quote_id || paymentPayload.quote_id !== quote.quote_id) return { ok: false, reason: "bolt_quote_mismatch" };
|
|
64
|
-
if (paymentPayload.amount_minor !== quote.amount_minor) return { ok: false, reason: "bolt_amount_mismatch" };
|
|
65
|
-
if (paymentPayload.currency !== quote.currency) return { ok: false, reason: "bolt_currency_mismatch" };
|
|
66
|
-
if (paymentPayload.account_id && paymentPayload.account_id !== quote.account_id) return { ok: false, reason: "bolt_account_mismatch" };
|
|
67
|
-
if (walletId && paymentPayload.wallet_id && paymentPayload.wallet_id !== walletId) return { ok: false, reason: "bolt_wallet_mismatch" };
|
|
68
|
-
if (!paymentPayload.nonce || typeof paymentPayload.nonce !== "string") return { ok: false, reason: "bolt_nonce_missing" };
|
|
69
|
-
if (!paymentPayload.issued_at_iso || Number.isNaN(Date.parse(paymentPayload.issued_at_iso))) return { ok: false, reason: "bolt_issued_at_invalid" };
|
|
70
|
-
const ageSeconds = Math.max(0, Math.floor((Date.now() - Date.parse(paymentPayload.issued_at_iso)) / 1000));
|
|
71
|
-
if (ageSeconds > maxAgeSeconds) return { ok: false, reason: "bolt_token_expired" };
|
|
72
|
-
if (!normalizeString(paymentPayload.txid || paymentPayload.transaction_id)) return { ok: false, reason: "bolt_txid_missing" };
|
|
73
|
-
if (
|
|
74
|
-
!normalizeString(paymentPayload.spv_proof_ref || paymentPayload.proof_bundle_ref)
|
|
75
|
-
&& !normalizeString(paymentPayload.merkle_path_ref)
|
|
76
|
-
&& !normalizeString(paymentPayload.block_header_ref)
|
|
77
|
-
) {
|
|
78
|
-
return { ok: false, reason: "bolt_proof_missing" };
|
|
79
|
-
}
|
|
80
|
-
if (
|
|
81
|
-
normalizeString(paymentPayload.network)
|
|
82
|
-
&& normalizeString(config && config.network)
|
|
83
|
-
&& normalizeString(paymentPayload.network) !== normalizeString(config.network)
|
|
84
|
-
) {
|
|
85
|
-
return { ok: false, reason: "bolt_network_mismatch" };
|
|
86
|
-
}
|
|
87
|
-
return { ok: true, age_seconds: ageSeconds };
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
async function verifyBoltAccessToken(headers, quote, state) {
|
|
91
|
-
// Real BSV path: when a chain provider is configured, verify an on-chain BSV
|
|
92
|
-
// payment ("BOLT <txid>") that pays the operator address >= the required amount,
|
|
93
|
-
// with single-use (per-txid) replay protection. Otherwise fall through to the
|
|
94
|
-
// existing gateway-signed token scheme below (unchanged).
|
|
95
|
-
const bsvConfig = boltAdapter.loadBoltConfig();
|
|
96
|
-
if (boltAdapter.isConfigured(bsvConfig)) {
|
|
97
|
-
const authorization = headers && typeof headers.authorization === "string" ? headers.authorization : "";
|
|
98
|
-
const parsed = boltAdapter.parseBoltHeader(authorization);
|
|
99
|
-
if (!parsed) return { ok: false, status: 402, reason: "bolt_authorization_malformed" };
|
|
100
|
-
if (state && state.acceptedPaymentNonces && state.acceptedPaymentNonces.has(parsed.txid)) {
|
|
101
|
-
return { ok: false, status: 403, reason: "bolt_txid_replayed" };
|
|
102
|
-
}
|
|
103
|
-
const requiredSats = boltAdapter.computeRequiredSats(bsvConfig, quote);
|
|
104
|
-
const result = await boltAdapter.verifyBsvPayment(bsvConfig, parsed.txid, undefined, { requiredSats });
|
|
105
|
-
if (!result.ok) return { ok: false, status: 403, reason: result.reason, detail: result.detail || null };
|
|
106
|
-
return {
|
|
107
|
-
ok: true,
|
|
108
|
-
verification_mode: "bolt_bsv_onchain",
|
|
109
|
-
wallet_id: "bolt",
|
|
110
|
-
payment_payload: {
|
|
111
|
-
nonce: result.txid,
|
|
112
|
-
txid: result.txid,
|
|
113
|
-
paid_sats: result.paid_sats,
|
|
114
|
-
confirmations: result.confirmations,
|
|
115
|
-
quote_id: quote && quote.quote_id,
|
|
116
|
-
issued_at_iso: new Date().toISOString()
|
|
117
|
-
},
|
|
118
|
-
bolt: { txid: result.txid, paid_sats: result.paid_sats, confirmations: result.confirmations, network: result.network }
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const config = loadBoltFrontConfig();
|
|
123
|
-
if (!config.enabled) return { ok: false, status: 503, reason: "bolt_not_enabled" };
|
|
124
|
-
if (!config.shared_secret) return { ok: false, status: 503, reason: "bolt_shared_secret_missing" };
|
|
125
|
-
const payment = parseBoltAuthorization(headers || {});
|
|
126
|
-
if (!payment || !payment.encoded || !payment.signature) {
|
|
127
|
-
return { ok: false, status: 402, reason: "bolt_headers_incomplete" };
|
|
128
|
-
}
|
|
129
|
-
const expectedSignature = crypto.createHmac("sha256", config.shared_secret).update(payment.encoded).digest("hex");
|
|
130
|
-
if (!safeEqualHex(expectedSignature, payment.signature)) {
|
|
131
|
-
return { ok: false, status: 403, reason: "bolt_signature_invalid" };
|
|
132
|
-
}
|
|
133
|
-
const payload = decodeBase64UrlJson(payment.encoded);
|
|
134
|
-
const validation = verifyBoltPaymentPayload(payload, quote, payment.wallet_id, config.max_age_seconds, config);
|
|
135
|
-
if (!validation.ok) return { ok: false, status: 403, reason: validation.reason };
|
|
136
|
-
return {
|
|
137
|
-
ok: true,
|
|
138
|
-
verification_mode: "bolt_gateway_signed",
|
|
139
|
-
wallet_id: payment.wallet_id,
|
|
140
|
-
payment_payload: payload,
|
|
141
|
-
age_seconds: validation.age_seconds,
|
|
142
|
-
bolt: {
|
|
143
|
-
txid: payment.txid || payload.txid || payload.transaction_id || null,
|
|
144
|
-
spv_proof_ref: payment.spv_proof_ref || payload.spv_proof_ref || payload.proof_bundle_ref || null,
|
|
145
|
-
merkle_path_ref: payment.merkle_path_ref || payload.merkle_path_ref || null,
|
|
146
|
-
block_header_ref: payment.block_header_ref || payload.block_header_ref || null,
|
|
147
|
-
proof_bundle_ref: payment.proof_bundle_ref || payload.proof_bundle_ref || null,
|
|
148
|
-
network: payment.network || payload.network || config.network,
|
|
149
|
-
proof_mode: config.proof_mode
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function buildBoltSecuritySummary() {
|
|
155
|
-
const config = loadBoltFrontConfig();
|
|
156
|
-
const blockers = [];
|
|
157
|
-
if (config.enabled && !config.shared_secret) blockers.push("bolt_shared_secret_missing");
|
|
158
|
-
return {
|
|
159
|
-
ok: true,
|
|
160
|
-
category: "machine-commerce-bolt-security-summary",
|
|
161
|
-
summary_version: "xytara-bolt-security-summary-v1",
|
|
162
|
-
enabled: config.enabled,
|
|
163
|
-
shared_secret_configured: Boolean(config.shared_secret),
|
|
164
|
-
network: config.network,
|
|
165
|
-
proof_mode: config.proof_mode,
|
|
166
|
-
max_age_seconds: config.max_age_seconds,
|
|
167
|
-
auth_scheme: config.auth_scheme,
|
|
168
|
-
token_shape: config.token_shape,
|
|
169
|
-
ready: config.enabled && blockers.length === 0,
|
|
170
|
-
blockers,
|
|
171
|
-
linked_surfaces: {
|
|
172
|
-
bolt_lane_ref: "/v1/bolt",
|
|
173
|
-
commands_execute_ref: "/bolt/commands/execute",
|
|
174
|
-
mcp_invoke_ref: "/bolt/mcp/tools/invoke"
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function buildBoltChallengePayload(quote) {
|
|
180
|
-
// Real BSV path: advertise where to pay and how much so the caller can send a
|
|
181
|
-
// BSV transaction and retry with "BOLT <txid>".
|
|
182
|
-
const bsvConfig = boltAdapter.loadBoltConfig();
|
|
183
|
-
if (boltAdapter.isConfigured(bsvConfig) && bsvConfig.payToAddress && bsvConfig.priceSats) {
|
|
184
|
-
const requiredSats = boltAdapter.computeRequiredSats(bsvConfig, quote);
|
|
185
|
-
return {
|
|
186
|
-
ok: false,
|
|
187
|
-
payment_required: {
|
|
188
|
-
scheme: "bolt",
|
|
189
|
-
quote,
|
|
190
|
-
bsv: boltAdapter.buildBoltChallenge(bsvConfig, { requiredSats }),
|
|
191
|
-
operator_requirements: {
|
|
192
|
-
auth_scheme: "BOLT",
|
|
193
|
-
token_shape: "authorization_header_BOLT_txid",
|
|
194
|
-
security_ref: "/v1/bolt/security",
|
|
195
|
-
proof_requirements: ["pay_the_exact_amount_sats_to_address_then_present_txid"]
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
return {
|
|
201
|
-
ok: false,
|
|
202
|
-
payment_required: {
|
|
203
|
-
scheme: "bolt",
|
|
204
|
-
quote,
|
|
205
|
-
operator_requirements: {
|
|
206
|
-
auth_scheme: "BOLT",
|
|
207
|
-
token_shape: "base64url_json_with_utxo_spv_proof_and_hmac_signed_payload",
|
|
208
|
-
security_ref: "/v1/bolt/security",
|
|
209
|
-
proof_requirements: [
|
|
210
|
-
"txid",
|
|
211
|
-
"one_of:spv_proof_ref|merkle_path_ref|block_header_ref|proof_bundle_ref"
|
|
212
|
-
]
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
module.exports = {
|
|
219
|
-
buildBoltChallengePayload,
|
|
220
|
-
buildBoltSecuritySummary,
|
|
221
|
-
loadBoltFrontConfig,
|
|
222
|
-
parseBoltAuthorization,
|
|
223
|
-
verifyBoltAccessToken
|
|
224
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const crypto = require("crypto");
|
|
4
|
+
const boltAdapter = require("./bolt_adapter");
|
|
5
|
+
|
|
6
|
+
function normalizeString(value) {
|
|
7
|
+
return typeof value === "string" ? value.trim() : "";
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function safeEqualHex(left, right) {
|
|
11
|
+
const a = Buffer.from(String(left || ""), "hex");
|
|
12
|
+
const b = Buffer.from(String(right || ""), "hex");
|
|
13
|
+
if (a.length === 0 || b.length === 0 || a.length !== b.length) return false;
|
|
14
|
+
return crypto.timingSafeEqual(a, b);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function decodeBase64UrlJson(value) {
|
|
18
|
+
try {
|
|
19
|
+
return JSON.parse(Buffer.from(String(value || ""), "base64url").toString("utf8"));
|
|
20
|
+
} catch (_) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function loadBoltFrontConfig() {
|
|
26
|
+
const sharedSecret = normalizeString(process.env.XYTARA_BOLT_SHARED_SECRET);
|
|
27
|
+
return {
|
|
28
|
+
enabled: String(process.env.XYTARA_BOLT_ENABLED || (sharedSecret ? "true" : "false")).trim().toLowerCase() !== "false",
|
|
29
|
+
shared_secret: sharedSecret || null,
|
|
30
|
+
network: normalizeString(process.env.XYTARA_BOLT_NETWORK) || normalizeString(process.env.XYTARA_BSV_TERANODE_NETWORK) || "mainnet",
|
|
31
|
+
proof_mode: normalizeString(process.env.XYTARA_BOLT_PROOF_MODE) || "spv_or_block_header_verifiable",
|
|
32
|
+
max_age_seconds: Math.max(30, Number(process.env.XYTARA_BOLT_MAX_AGE_SECONDS || 300) || 300),
|
|
33
|
+
required_headers: ["authorization"],
|
|
34
|
+
auth_scheme: "BOLT",
|
|
35
|
+
token_shape: "base64url_json_with_utxo_spv_proof_and_hmac_signed_payload"
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function parseBoltAuthorization(headers) {
|
|
40
|
+
const authorization = headers && typeof headers.authorization === "string"
|
|
41
|
+
? headers.authorization.trim()
|
|
42
|
+
: "";
|
|
43
|
+
const match = authorization.match(/^BOLT\s+(.+)$/i);
|
|
44
|
+
if (!match) return null;
|
|
45
|
+
const decoded = decodeBase64UrlJson(match[1]);
|
|
46
|
+
if (!decoded || typeof decoded !== "object") return null;
|
|
47
|
+
return {
|
|
48
|
+
wallet_id: decoded.wallet_id || decoded.gateway_id || "bolt_gateway",
|
|
49
|
+
encoded: decoded.payload || null,
|
|
50
|
+
signature: decoded.signature || null,
|
|
51
|
+
txid: decoded.txid || null,
|
|
52
|
+
spv_proof_ref: decoded.spv_proof_ref || null,
|
|
53
|
+
merkle_path_ref: decoded.merkle_path_ref || null,
|
|
54
|
+
block_header_ref: decoded.block_header_ref || null,
|
|
55
|
+
network: decoded.network || null,
|
|
56
|
+
proof_bundle_ref: decoded.proof_bundle_ref || null,
|
|
57
|
+
scheme: "bolt"
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function verifyBoltPaymentPayload(paymentPayload, quote, walletId, maxAgeSeconds, config) {
|
|
62
|
+
if (!paymentPayload || typeof paymentPayload !== "object") return { ok: false, reason: "bolt_payload_invalid" };
|
|
63
|
+
if (!paymentPayload.quote_id || paymentPayload.quote_id !== quote.quote_id) return { ok: false, reason: "bolt_quote_mismatch" };
|
|
64
|
+
if (paymentPayload.amount_minor !== quote.amount_minor) return { ok: false, reason: "bolt_amount_mismatch" };
|
|
65
|
+
if (paymentPayload.currency !== quote.currency) return { ok: false, reason: "bolt_currency_mismatch" };
|
|
66
|
+
if (paymentPayload.account_id && paymentPayload.account_id !== quote.account_id) return { ok: false, reason: "bolt_account_mismatch" };
|
|
67
|
+
if (walletId && paymentPayload.wallet_id && paymentPayload.wallet_id !== walletId) return { ok: false, reason: "bolt_wallet_mismatch" };
|
|
68
|
+
if (!paymentPayload.nonce || typeof paymentPayload.nonce !== "string") return { ok: false, reason: "bolt_nonce_missing" };
|
|
69
|
+
if (!paymentPayload.issued_at_iso || Number.isNaN(Date.parse(paymentPayload.issued_at_iso))) return { ok: false, reason: "bolt_issued_at_invalid" };
|
|
70
|
+
const ageSeconds = Math.max(0, Math.floor((Date.now() - Date.parse(paymentPayload.issued_at_iso)) / 1000));
|
|
71
|
+
if (ageSeconds > maxAgeSeconds) return { ok: false, reason: "bolt_token_expired" };
|
|
72
|
+
if (!normalizeString(paymentPayload.txid || paymentPayload.transaction_id)) return { ok: false, reason: "bolt_txid_missing" };
|
|
73
|
+
if (
|
|
74
|
+
!normalizeString(paymentPayload.spv_proof_ref || paymentPayload.proof_bundle_ref)
|
|
75
|
+
&& !normalizeString(paymentPayload.merkle_path_ref)
|
|
76
|
+
&& !normalizeString(paymentPayload.block_header_ref)
|
|
77
|
+
) {
|
|
78
|
+
return { ok: false, reason: "bolt_proof_missing" };
|
|
79
|
+
}
|
|
80
|
+
if (
|
|
81
|
+
normalizeString(paymentPayload.network)
|
|
82
|
+
&& normalizeString(config && config.network)
|
|
83
|
+
&& normalizeString(paymentPayload.network) !== normalizeString(config.network)
|
|
84
|
+
) {
|
|
85
|
+
return { ok: false, reason: "bolt_network_mismatch" };
|
|
86
|
+
}
|
|
87
|
+
return { ok: true, age_seconds: ageSeconds };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function verifyBoltAccessToken(headers, quote, state) {
|
|
91
|
+
// Real BSV path: when a chain provider is configured, verify an on-chain BSV
|
|
92
|
+
// payment ("BOLT <txid>") that pays the operator address >= the required amount,
|
|
93
|
+
// with single-use (per-txid) replay protection. Otherwise fall through to the
|
|
94
|
+
// existing gateway-signed token scheme below (unchanged).
|
|
95
|
+
const bsvConfig = boltAdapter.loadBoltConfig();
|
|
96
|
+
if (boltAdapter.isConfigured(bsvConfig)) {
|
|
97
|
+
const authorization = headers && typeof headers.authorization === "string" ? headers.authorization : "";
|
|
98
|
+
const parsed = boltAdapter.parseBoltHeader(authorization);
|
|
99
|
+
if (!parsed) return { ok: false, status: 402, reason: "bolt_authorization_malformed" };
|
|
100
|
+
if (state && state.acceptedPaymentNonces && state.acceptedPaymentNonces.has(parsed.txid)) {
|
|
101
|
+
return { ok: false, status: 403, reason: "bolt_txid_replayed" };
|
|
102
|
+
}
|
|
103
|
+
const requiredSats = boltAdapter.computeRequiredSats(bsvConfig, quote);
|
|
104
|
+
const result = await boltAdapter.verifyBsvPayment(bsvConfig, parsed.txid, undefined, { requiredSats });
|
|
105
|
+
if (!result.ok) return { ok: false, status: 403, reason: result.reason, detail: result.detail || null };
|
|
106
|
+
return {
|
|
107
|
+
ok: true,
|
|
108
|
+
verification_mode: "bolt_bsv_onchain",
|
|
109
|
+
wallet_id: "bolt",
|
|
110
|
+
payment_payload: {
|
|
111
|
+
nonce: result.txid,
|
|
112
|
+
txid: result.txid,
|
|
113
|
+
paid_sats: result.paid_sats,
|
|
114
|
+
confirmations: result.confirmations,
|
|
115
|
+
quote_id: quote && quote.quote_id,
|
|
116
|
+
issued_at_iso: new Date().toISOString()
|
|
117
|
+
},
|
|
118
|
+
bolt: { txid: result.txid, paid_sats: result.paid_sats, confirmations: result.confirmations, network: result.network }
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const config = loadBoltFrontConfig();
|
|
123
|
+
if (!config.enabled) return { ok: false, status: 503, reason: "bolt_not_enabled" };
|
|
124
|
+
if (!config.shared_secret) return { ok: false, status: 503, reason: "bolt_shared_secret_missing" };
|
|
125
|
+
const payment = parseBoltAuthorization(headers || {});
|
|
126
|
+
if (!payment || !payment.encoded || !payment.signature) {
|
|
127
|
+
return { ok: false, status: 402, reason: "bolt_headers_incomplete" };
|
|
128
|
+
}
|
|
129
|
+
const expectedSignature = crypto.createHmac("sha256", config.shared_secret).update(payment.encoded).digest("hex");
|
|
130
|
+
if (!safeEqualHex(expectedSignature, payment.signature)) {
|
|
131
|
+
return { ok: false, status: 403, reason: "bolt_signature_invalid" };
|
|
132
|
+
}
|
|
133
|
+
const payload = decodeBase64UrlJson(payment.encoded);
|
|
134
|
+
const validation = verifyBoltPaymentPayload(payload, quote, payment.wallet_id, config.max_age_seconds, config);
|
|
135
|
+
if (!validation.ok) return { ok: false, status: 403, reason: validation.reason };
|
|
136
|
+
return {
|
|
137
|
+
ok: true,
|
|
138
|
+
verification_mode: "bolt_gateway_signed",
|
|
139
|
+
wallet_id: payment.wallet_id,
|
|
140
|
+
payment_payload: payload,
|
|
141
|
+
age_seconds: validation.age_seconds,
|
|
142
|
+
bolt: {
|
|
143
|
+
txid: payment.txid || payload.txid || payload.transaction_id || null,
|
|
144
|
+
spv_proof_ref: payment.spv_proof_ref || payload.spv_proof_ref || payload.proof_bundle_ref || null,
|
|
145
|
+
merkle_path_ref: payment.merkle_path_ref || payload.merkle_path_ref || null,
|
|
146
|
+
block_header_ref: payment.block_header_ref || payload.block_header_ref || null,
|
|
147
|
+
proof_bundle_ref: payment.proof_bundle_ref || payload.proof_bundle_ref || null,
|
|
148
|
+
network: payment.network || payload.network || config.network,
|
|
149
|
+
proof_mode: config.proof_mode
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function buildBoltSecuritySummary() {
|
|
155
|
+
const config = loadBoltFrontConfig();
|
|
156
|
+
const blockers = [];
|
|
157
|
+
if (config.enabled && !config.shared_secret) blockers.push("bolt_shared_secret_missing");
|
|
158
|
+
return {
|
|
159
|
+
ok: true,
|
|
160
|
+
category: "machine-commerce-bolt-security-summary",
|
|
161
|
+
summary_version: "xytara-bolt-security-summary-v1",
|
|
162
|
+
enabled: config.enabled,
|
|
163
|
+
shared_secret_configured: Boolean(config.shared_secret),
|
|
164
|
+
network: config.network,
|
|
165
|
+
proof_mode: config.proof_mode,
|
|
166
|
+
max_age_seconds: config.max_age_seconds,
|
|
167
|
+
auth_scheme: config.auth_scheme,
|
|
168
|
+
token_shape: config.token_shape,
|
|
169
|
+
ready: config.enabled && blockers.length === 0,
|
|
170
|
+
blockers,
|
|
171
|
+
linked_surfaces: {
|
|
172
|
+
bolt_lane_ref: "/v1/bolt",
|
|
173
|
+
commands_execute_ref: "/bolt/commands/execute",
|
|
174
|
+
mcp_invoke_ref: "/bolt/mcp/tools/invoke"
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function buildBoltChallengePayload(quote) {
|
|
180
|
+
// Real BSV path: advertise where to pay and how much so the caller can send a
|
|
181
|
+
// BSV transaction and retry with "BOLT <txid>".
|
|
182
|
+
const bsvConfig = boltAdapter.loadBoltConfig();
|
|
183
|
+
if (boltAdapter.isConfigured(bsvConfig) && bsvConfig.payToAddress && bsvConfig.priceSats) {
|
|
184
|
+
const requiredSats = boltAdapter.computeRequiredSats(bsvConfig, quote);
|
|
185
|
+
return {
|
|
186
|
+
ok: false,
|
|
187
|
+
payment_required: {
|
|
188
|
+
scheme: "bolt",
|
|
189
|
+
quote,
|
|
190
|
+
bsv: boltAdapter.buildBoltChallenge(bsvConfig, { requiredSats }),
|
|
191
|
+
operator_requirements: {
|
|
192
|
+
auth_scheme: "BOLT",
|
|
193
|
+
token_shape: "authorization_header_BOLT_txid",
|
|
194
|
+
security_ref: "/v1/bolt/security",
|
|
195
|
+
proof_requirements: ["pay_the_exact_amount_sats_to_address_then_present_txid"]
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
return {
|
|
201
|
+
ok: false,
|
|
202
|
+
payment_required: {
|
|
203
|
+
scheme: "bolt",
|
|
204
|
+
quote,
|
|
205
|
+
operator_requirements: {
|
|
206
|
+
auth_scheme: "BOLT",
|
|
207
|
+
token_shape: "base64url_json_with_utxo_spv_proof_and_hmac_signed_payload",
|
|
208
|
+
security_ref: "/v1/bolt/security",
|
|
209
|
+
proof_requirements: [
|
|
210
|
+
"txid",
|
|
211
|
+
"one_of:spv_proof_ref|merkle_path_ref|block_header_ref|proof_bundle_ref"
|
|
212
|
+
]
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
module.exports = {
|
|
219
|
+
buildBoltChallengePayload,
|
|
220
|
+
buildBoltSecuritySummary,
|
|
221
|
+
loadBoltFrontConfig,
|
|
222
|
+
parseBoltAuthorization,
|
|
223
|
+
verifyBoltAccessToken
|
|
224
|
+
};
|