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
package/lib/commerce_runtime.js
CHANGED
|
@@ -1,322 +1,322 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { createCommandArtifacts } = require("./commerce_artifacts");
|
|
4
|
-
const { createQuote, hydrateQuoteStates, markQuoteExecuted } = require("./commerce_quotes");
|
|
5
|
-
const { listRecords } = require("./record_queries");
|
|
6
|
-
const { createBsvSettlementRecord } = require("./settlement_bsv_live");
|
|
7
|
-
const {
|
|
8
|
-
actOnDispute,
|
|
9
|
-
actOnRefund,
|
|
10
|
-
actOnRemediationTicket,
|
|
11
|
-
createDispute,
|
|
12
|
-
createRefund,
|
|
13
|
-
createRemediationTicket,
|
|
14
|
-
settleRefundAsCredit
|
|
15
|
-
} = require("./commerce_cases");
|
|
16
|
-
|
|
17
|
-
function createRuntimeState() {
|
|
18
|
-
return {
|
|
19
|
-
quotes: new Map(),
|
|
20
|
-
checkoutPurchaseIntents: new Map(),
|
|
21
|
-
checkoutSessions: new Map(),
|
|
22
|
-
checkoutEvents: new Map(),
|
|
23
|
-
externalCreditGrants: new Map(),
|
|
24
|
-
accountAuthorityBindings: new Map(),
|
|
25
|
-
accountSpendCredentials: new Map(),
|
|
26
|
-
accountSpendCredentialsByToken: new Map(),
|
|
27
|
-
machineIdentityBindings: new Map(),
|
|
28
|
-
operatorParticipants: new Map(),
|
|
29
|
-
networkParticipants: new Map(),
|
|
30
|
-
runtimeBridgeDispatches: new Map(),
|
|
31
|
-
agentAccounts: new Map(),
|
|
32
|
-
transactions: new Map(),
|
|
33
|
-
receipts: new Map(),
|
|
34
|
-
paymentLedger: [],
|
|
35
|
-
bsvSettlementRecords: new Map(),
|
|
36
|
-
providerRegistry: new Map(),
|
|
37
|
-
providerSettlementLedger: [],
|
|
38
|
-
providerPayouts: [],
|
|
39
|
-
acceptedPaymentNonces: new Set(),
|
|
40
|
-
acceptedAgentSignatures: new Map(),
|
|
41
|
-
inFlightPaymentAuthorizations: new Map(),
|
|
42
|
-
economicsBudgets: new Map(),
|
|
43
|
-
economicsAgents: new Map(),
|
|
44
|
-
economicsReceipts: new Map(),
|
|
45
|
-
economicsAllocations: [],
|
|
46
|
-
economicsRailCredits: [],
|
|
47
|
-
economicsEntitlements: new Map(),
|
|
48
|
-
economicsCreditSpends: [],
|
|
49
|
-
economicsUsageMeters: new Map(),
|
|
50
|
-
economicsInterpretationHandoffs: [],
|
|
51
|
-
economicsJobActions: [],
|
|
52
|
-
coordinationHandoffs: [],
|
|
53
|
-
zoneAdmissionCases: [],
|
|
54
|
-
deliveries: new Map(),
|
|
55
|
-
pendingExecutions: new Map(),
|
|
56
|
-
disputes: new Map(),
|
|
57
|
-
refunds: new Map(),
|
|
58
|
-
remediationTickets: new Map()
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function buildDeliveryEvent(event, delivery, atIso, extra) {
|
|
63
|
-
return {
|
|
64
|
-
event,
|
|
65
|
-
at_iso: atIso,
|
|
66
|
-
status: delivery.status,
|
|
67
|
-
callback_url: delivery.callback_url || null,
|
|
68
|
-
output_ref: delivery.output_ref || null,
|
|
69
|
-
proof_ref: delivery.proof_ref || null,
|
|
70
|
-
...(extra || {})
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function buildCallbackDeliveryState(callbackUrl, deliveredAtIso, options = {}) {
|
|
75
|
-
if (!callbackUrl) return null;
|
|
76
|
-
const opts = options || {};
|
|
77
|
-
const initialStatus = opts.initial_status || (deliveredAtIso ? "queued" : "pending_execution");
|
|
78
|
-
return {
|
|
79
|
-
status: initialStatus,
|
|
80
|
-
attempt_count: 0,
|
|
81
|
-
max_attempts: null,
|
|
82
|
-
last_attempt_at_iso: null,
|
|
83
|
-
next_attempt_at_iso: deliveredAtIso || null,
|
|
84
|
-
delivered_at_iso: null,
|
|
85
|
-
failed_at_iso: null,
|
|
86
|
-
last_error: null,
|
|
87
|
-
last_response_status: null,
|
|
88
|
-
signature_mode: "hmac_sha256"
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function completePendingExecution(state, transactionId) {
|
|
93
|
-
if (!transactionId || !state.pendingExecutions.has(transactionId) || !state.transactions.has(transactionId)) {
|
|
94
|
-
return state.transactions.get(transactionId) || null;
|
|
95
|
-
}
|
|
96
|
-
const pending = state.pendingExecutions.get(transactionId);
|
|
97
|
-
const current = state.transactions.get(transactionId);
|
|
98
|
-
if (!pending || !current || Date.now() < pending.readyAtMs) {
|
|
99
|
-
return current;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const completedAtIso = new Date(pending.readyAtMs).toISOString();
|
|
103
|
-
const next = {
|
|
104
|
-
transaction: {
|
|
105
|
-
...current.transaction,
|
|
106
|
-
status: "completed",
|
|
107
|
-
completed_at_iso: completedAtIso,
|
|
108
|
-
timing: {
|
|
109
|
-
...current.transaction.timing,
|
|
110
|
-
completed_at_iso: completedAtIso,
|
|
111
|
-
duration_ms: pending.durationMs,
|
|
112
|
-
observed_latency_class: pending.observedLatencyClass,
|
|
113
|
-
assessment: pending.assessment
|
|
114
|
-
},
|
|
115
|
-
execution: {
|
|
116
|
-
...current.transaction.execution,
|
|
117
|
-
status: "completed"
|
|
118
|
-
},
|
|
119
|
-
remediation: {
|
|
120
|
-
...current.transaction.remediation,
|
|
121
|
-
updated_at_iso: completedAtIso
|
|
122
|
-
}
|
|
123
|
-
},
|
|
124
|
-
receipt: {
|
|
125
|
-
...current.receipt,
|
|
126
|
-
status: "completed",
|
|
127
|
-
created_at_iso: completedAtIso,
|
|
128
|
-
timing: {
|
|
129
|
-
...current.receipt.timing,
|
|
130
|
-
completed_at_iso: completedAtIso,
|
|
131
|
-
duration_ms: pending.durationMs,
|
|
132
|
-
observed_latency_class: pending.observedLatencyClass,
|
|
133
|
-
assessment: pending.assessment
|
|
134
|
-
},
|
|
135
|
-
remediation: {
|
|
136
|
-
...current.receipt.remediation,
|
|
137
|
-
updated_at_iso: completedAtIso
|
|
138
|
-
}
|
|
139
|
-
},
|
|
140
|
-
command: current.command
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
state.transactions.set(transactionId, next);
|
|
144
|
-
state.receipts.set(next.receipt.receipt_id, next);
|
|
145
|
-
if (pending.quoteId && state.quotes.has(pending.quoteId)) {
|
|
146
|
-
const quote = state.quotes.get(pending.quoteId);
|
|
147
|
-
state.quotes.set(pending.quoteId, {
|
|
148
|
-
...quote,
|
|
149
|
-
status: "executed",
|
|
150
|
-
payment_recorded_at_iso: completedAtIso,
|
|
151
|
-
executed_at_iso: completedAtIso
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
if (pending.deliveryId && state.deliveries.has(pending.deliveryId)) {
|
|
155
|
-
const delivery = state.deliveries.get(pending.deliveryId);
|
|
156
|
-
const nextDelivery = {
|
|
157
|
-
...delivery,
|
|
158
|
-
status: "delivered",
|
|
159
|
-
delivered_at_iso: completedAtIso,
|
|
160
|
-
receipt_id: next.receipt.receipt_id,
|
|
161
|
-
proof_ref: next.receipt.proof_ref,
|
|
162
|
-
attempt_count: (delivery.attempt_count || 0) + 1,
|
|
163
|
-
last_attempt_at_iso: completedAtIso,
|
|
164
|
-
history: [
|
|
165
|
-
...(Array.isArray(delivery.history) ? delivery.history : []),
|
|
166
|
-
buildDeliveryEvent("delivered", { ...delivery, status: "delivered", proof_ref: next.receipt.proof_ref }, completedAtIso, {
|
|
167
|
-
attempt_number: (delivery.attempt_count || 0) + 1
|
|
168
|
-
})
|
|
169
|
-
],
|
|
170
|
-
callback_delivery: delivery.callback_url
|
|
171
|
-
? {
|
|
172
|
-
...(delivery.callback_delivery || buildCallbackDeliveryState(delivery.callback_url, completedAtIso)),
|
|
173
|
-
status: "queued",
|
|
174
|
-
next_attempt_at_iso: completedAtIso,
|
|
175
|
-
last_error: null,
|
|
176
|
-
failed_at_iso: null
|
|
177
|
-
}
|
|
178
|
-
: null
|
|
179
|
-
};
|
|
180
|
-
state.deliveries.set(pending.deliveryId, nextDelivery);
|
|
181
|
-
}
|
|
182
|
-
state.pendingExecutions.delete(transactionId);
|
|
183
|
-
return next;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function hydratePendingExecutions(state) {
|
|
187
|
-
for (const transactionId of Array.from(state.pendingExecutions.keys())) {
|
|
188
|
-
completePendingExecution(state, transactionId);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function createCommandResult(state, payload, quote, options) {
|
|
193
|
-
const opts = options || {};
|
|
194
|
-
const deferredCompletion = payload && payload.defer_completion === true;
|
|
195
|
-
const { transaction, receipt, command } = createCommandArtifacts(payload, quote, {
|
|
196
|
-
deferredCompletion,
|
|
197
|
-
paymentVerification: opts.paymentVerification || null,
|
|
198
|
-
externalExecution: opts.externalExecution || null
|
|
199
|
-
});
|
|
200
|
-
const result = { transaction, receipt, command };
|
|
201
|
-
state.transactions.set(transaction.transaction_id, result);
|
|
202
|
-
state.receipts.set(receipt.receipt_id, result);
|
|
203
|
-
let paymentRecord = null;
|
|
204
|
-
if (!opts.skipPaymentLedger) {
|
|
205
|
-
paymentRecord = {
|
|
206
|
-
payment_id: `pay_${quote.quote_id}`,
|
|
207
|
-
quote_id: quote.quote_id,
|
|
208
|
-
transaction_id: transaction.transaction_id,
|
|
209
|
-
receipt_id: receipt.receipt_id,
|
|
210
|
-
account_id: quote.account_id,
|
|
211
|
-
amount_minor: quote.amount_minor,
|
|
212
|
-
currency: quote.currency,
|
|
213
|
-
settlement_adapter: quote.settlement_adapter,
|
|
214
|
-
settlement_integration_id: quote.settlement_integration_id || null,
|
|
215
|
-
recorded_at_iso: receipt.created_at_iso,
|
|
216
|
-
payment: transaction.payment,
|
|
217
|
-
settlement: transaction.settlement,
|
|
218
|
-
treasury: transaction.treasury,
|
|
219
|
-
default_path: transaction.default_path,
|
|
220
|
-
payment_verification: opts.paymentVerification
|
|
221
|
-
? {
|
|
222
|
-
verification_mode: opts.paymentVerification.verification_mode || null,
|
|
223
|
-
wallet_id: opts.paymentVerification.wallet_id || null,
|
|
224
|
-
age_seconds: Number.isFinite(Number(opts.paymentVerification.age_seconds))
|
|
225
|
-
? Number(opts.paymentVerification.age_seconds)
|
|
226
|
-
: null
|
|
227
|
-
}
|
|
228
|
-
: null
|
|
229
|
-
};
|
|
230
|
-
state.paymentLedger = [
|
|
231
|
-
paymentRecord,
|
|
232
|
-
...(Array.isArray(state.paymentLedger) ? state.paymentLedger : [])
|
|
233
|
-
].slice(0, 100);
|
|
234
|
-
const liveSettlement = createBsvSettlementRecord(state, transaction, receipt, paymentRecord);
|
|
235
|
-
if (liveSettlement) {
|
|
236
|
-
paymentRecord.live_settlement = {
|
|
237
|
-
settlement_id: liveSettlement.settlement_id,
|
|
238
|
-
status: liveSettlement.status,
|
|
239
|
-
finality_status: liveSettlement.finality_status,
|
|
240
|
-
txid: liveSettlement.txid,
|
|
241
|
-
rail: liveSettlement.rail
|
|
242
|
-
};
|
|
243
|
-
transaction.settlement.live = paymentRecord.live_settlement;
|
|
244
|
-
receipt.settlement.live = paymentRecord.live_settlement;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
if (opts.creditSpend) {
|
|
248
|
-
transaction.credit_spend = opts.creditSpend;
|
|
249
|
-
receipt.credit_spend = opts.creditSpend;
|
|
250
|
-
}
|
|
251
|
-
const deliveryRecord = {
|
|
252
|
-
delivery_id: `delivery_${transaction.transaction_id}`,
|
|
253
|
-
transaction_id: transaction.transaction_id,
|
|
254
|
-
receipt_id: receipt.receipt_id,
|
|
255
|
-
quote_id: quote.quote_id,
|
|
256
|
-
account_id: quote.account_id,
|
|
257
|
-
callback_url: transaction.callback_url,
|
|
258
|
-
output_ref: transaction.output_ref,
|
|
259
|
-
proof_ref: receipt.proof_ref,
|
|
260
|
-
payment_id: paymentRecord ? paymentRecord.payment_id : null,
|
|
261
|
-
delivery_target_kind: transaction.callback_url ? "callback" : "direct",
|
|
262
|
-
status: deferredCompletion ? "pending_delivery" : "delivered",
|
|
263
|
-
created_at_iso: transaction.created_at_iso,
|
|
264
|
-
delivered_at_iso: deferredCompletion ? null : receipt.created_at_iso,
|
|
265
|
-
attempt_count: deferredCompletion ? 0 : 1,
|
|
266
|
-
last_attempt_at_iso: deferredCompletion ? null : receipt.created_at_iso,
|
|
267
|
-
callback_delivery: buildCallbackDeliveryState(
|
|
268
|
-
transaction.callback_url,
|
|
269
|
-
deferredCompletion ? null : receipt.created_at_iso,
|
|
270
|
-
{
|
|
271
|
-
initial_status: deferredCompletion ? "pending_execution" : "queued"
|
|
272
|
-
}
|
|
273
|
-
),
|
|
274
|
-
history: [
|
|
275
|
-
buildDeliveryEvent("created", {
|
|
276
|
-
callback_url: transaction.callback_url,
|
|
277
|
-
output_ref: transaction.output_ref,
|
|
278
|
-
proof_ref: receipt.proof_ref,
|
|
279
|
-
status: deferredCompletion ? "pending_delivery" : "delivered"
|
|
280
|
-
}, transaction.created_at_iso),
|
|
281
|
-
...(!deferredCompletion ? [
|
|
282
|
-
buildDeliveryEvent("delivered", {
|
|
283
|
-
callback_url: transaction.callback_url,
|
|
284
|
-
output_ref: transaction.output_ref,
|
|
285
|
-
proof_ref: receipt.proof_ref,
|
|
286
|
-
status: "delivered"
|
|
287
|
-
}, receipt.created_at_iso, { attempt_number: 1 })
|
|
288
|
-
] : [])
|
|
289
|
-
]
|
|
290
|
-
};
|
|
291
|
-
state.deliveries.set(deliveryRecord.delivery_id, deliveryRecord);
|
|
292
|
-
markQuoteExecuted(state, quote, result, paymentRecord, { deferredCompletion });
|
|
293
|
-
if (deferredCompletion) {
|
|
294
|
-
state.pendingExecutions.set(transaction.transaction_id, {
|
|
295
|
-
readyAtMs: Date.now() + 300,
|
|
296
|
-
quoteId: quote.quote_id,
|
|
297
|
-
deliveryId: deliveryRecord.delivery_id,
|
|
298
|
-
durationMs: 250,
|
|
299
|
-
observedLatencyClass: "instant",
|
|
300
|
-
assessment: "within_posture"
|
|
301
|
-
});
|
|
302
|
-
}
|
|
303
|
-
return result;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
module.exports = {
|
|
307
|
-
buildCallbackDeliveryState,
|
|
308
|
-
completePendingExecution,
|
|
309
|
-
createCommandResult,
|
|
310
|
-
createDispute,
|
|
311
|
-
createQuote,
|
|
312
|
-
createRefund,
|
|
313
|
-
createRemediationTicket,
|
|
314
|
-
createRuntimeState,
|
|
315
|
-
hydrateQuoteStates,
|
|
316
|
-
hydratePendingExecutions,
|
|
317
|
-
actOnDispute,
|
|
318
|
-
actOnRefund,
|
|
319
|
-
actOnRemediationTicket,
|
|
320
|
-
settleRefundAsCredit,
|
|
321
|
-
listRecords
|
|
322
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { createCommandArtifacts } = require("./commerce_artifacts");
|
|
4
|
+
const { createQuote, hydrateQuoteStates, markQuoteExecuted } = require("./commerce_quotes");
|
|
5
|
+
const { listRecords } = require("./record_queries");
|
|
6
|
+
const { createBsvSettlementRecord } = require("./settlement_bsv_live");
|
|
7
|
+
const {
|
|
8
|
+
actOnDispute,
|
|
9
|
+
actOnRefund,
|
|
10
|
+
actOnRemediationTicket,
|
|
11
|
+
createDispute,
|
|
12
|
+
createRefund,
|
|
13
|
+
createRemediationTicket,
|
|
14
|
+
settleRefundAsCredit
|
|
15
|
+
} = require("./commerce_cases");
|
|
16
|
+
|
|
17
|
+
function createRuntimeState() {
|
|
18
|
+
return {
|
|
19
|
+
quotes: new Map(),
|
|
20
|
+
checkoutPurchaseIntents: new Map(),
|
|
21
|
+
checkoutSessions: new Map(),
|
|
22
|
+
checkoutEvents: new Map(),
|
|
23
|
+
externalCreditGrants: new Map(),
|
|
24
|
+
accountAuthorityBindings: new Map(),
|
|
25
|
+
accountSpendCredentials: new Map(),
|
|
26
|
+
accountSpendCredentialsByToken: new Map(),
|
|
27
|
+
machineIdentityBindings: new Map(),
|
|
28
|
+
operatorParticipants: new Map(),
|
|
29
|
+
networkParticipants: new Map(),
|
|
30
|
+
runtimeBridgeDispatches: new Map(),
|
|
31
|
+
agentAccounts: new Map(),
|
|
32
|
+
transactions: new Map(),
|
|
33
|
+
receipts: new Map(),
|
|
34
|
+
paymentLedger: [],
|
|
35
|
+
bsvSettlementRecords: new Map(),
|
|
36
|
+
providerRegistry: new Map(),
|
|
37
|
+
providerSettlementLedger: [],
|
|
38
|
+
providerPayouts: [],
|
|
39
|
+
acceptedPaymentNonces: new Set(),
|
|
40
|
+
acceptedAgentSignatures: new Map(),
|
|
41
|
+
inFlightPaymentAuthorizations: new Map(),
|
|
42
|
+
economicsBudgets: new Map(),
|
|
43
|
+
economicsAgents: new Map(),
|
|
44
|
+
economicsReceipts: new Map(),
|
|
45
|
+
economicsAllocations: [],
|
|
46
|
+
economicsRailCredits: [],
|
|
47
|
+
economicsEntitlements: new Map(),
|
|
48
|
+
economicsCreditSpends: [],
|
|
49
|
+
economicsUsageMeters: new Map(),
|
|
50
|
+
economicsInterpretationHandoffs: [],
|
|
51
|
+
economicsJobActions: [],
|
|
52
|
+
coordinationHandoffs: [],
|
|
53
|
+
zoneAdmissionCases: [],
|
|
54
|
+
deliveries: new Map(),
|
|
55
|
+
pendingExecutions: new Map(),
|
|
56
|
+
disputes: new Map(),
|
|
57
|
+
refunds: new Map(),
|
|
58
|
+
remediationTickets: new Map()
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function buildDeliveryEvent(event, delivery, atIso, extra) {
|
|
63
|
+
return {
|
|
64
|
+
event,
|
|
65
|
+
at_iso: atIso,
|
|
66
|
+
status: delivery.status,
|
|
67
|
+
callback_url: delivery.callback_url || null,
|
|
68
|
+
output_ref: delivery.output_ref || null,
|
|
69
|
+
proof_ref: delivery.proof_ref || null,
|
|
70
|
+
...(extra || {})
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function buildCallbackDeliveryState(callbackUrl, deliveredAtIso, options = {}) {
|
|
75
|
+
if (!callbackUrl) return null;
|
|
76
|
+
const opts = options || {};
|
|
77
|
+
const initialStatus = opts.initial_status || (deliveredAtIso ? "queued" : "pending_execution");
|
|
78
|
+
return {
|
|
79
|
+
status: initialStatus,
|
|
80
|
+
attempt_count: 0,
|
|
81
|
+
max_attempts: null,
|
|
82
|
+
last_attempt_at_iso: null,
|
|
83
|
+
next_attempt_at_iso: deliveredAtIso || null,
|
|
84
|
+
delivered_at_iso: null,
|
|
85
|
+
failed_at_iso: null,
|
|
86
|
+
last_error: null,
|
|
87
|
+
last_response_status: null,
|
|
88
|
+
signature_mode: "hmac_sha256"
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function completePendingExecution(state, transactionId) {
|
|
93
|
+
if (!transactionId || !state.pendingExecutions.has(transactionId) || !state.transactions.has(transactionId)) {
|
|
94
|
+
return state.transactions.get(transactionId) || null;
|
|
95
|
+
}
|
|
96
|
+
const pending = state.pendingExecutions.get(transactionId);
|
|
97
|
+
const current = state.transactions.get(transactionId);
|
|
98
|
+
if (!pending || !current || Date.now() < pending.readyAtMs) {
|
|
99
|
+
return current;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const completedAtIso = new Date(pending.readyAtMs).toISOString();
|
|
103
|
+
const next = {
|
|
104
|
+
transaction: {
|
|
105
|
+
...current.transaction,
|
|
106
|
+
status: "completed",
|
|
107
|
+
completed_at_iso: completedAtIso,
|
|
108
|
+
timing: {
|
|
109
|
+
...current.transaction.timing,
|
|
110
|
+
completed_at_iso: completedAtIso,
|
|
111
|
+
duration_ms: pending.durationMs,
|
|
112
|
+
observed_latency_class: pending.observedLatencyClass,
|
|
113
|
+
assessment: pending.assessment
|
|
114
|
+
},
|
|
115
|
+
execution: {
|
|
116
|
+
...current.transaction.execution,
|
|
117
|
+
status: "completed"
|
|
118
|
+
},
|
|
119
|
+
remediation: {
|
|
120
|
+
...current.transaction.remediation,
|
|
121
|
+
updated_at_iso: completedAtIso
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
receipt: {
|
|
125
|
+
...current.receipt,
|
|
126
|
+
status: "completed",
|
|
127
|
+
created_at_iso: completedAtIso,
|
|
128
|
+
timing: {
|
|
129
|
+
...current.receipt.timing,
|
|
130
|
+
completed_at_iso: completedAtIso,
|
|
131
|
+
duration_ms: pending.durationMs,
|
|
132
|
+
observed_latency_class: pending.observedLatencyClass,
|
|
133
|
+
assessment: pending.assessment
|
|
134
|
+
},
|
|
135
|
+
remediation: {
|
|
136
|
+
...current.receipt.remediation,
|
|
137
|
+
updated_at_iso: completedAtIso
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
command: current.command
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
state.transactions.set(transactionId, next);
|
|
144
|
+
state.receipts.set(next.receipt.receipt_id, next);
|
|
145
|
+
if (pending.quoteId && state.quotes.has(pending.quoteId)) {
|
|
146
|
+
const quote = state.quotes.get(pending.quoteId);
|
|
147
|
+
state.quotes.set(pending.quoteId, {
|
|
148
|
+
...quote,
|
|
149
|
+
status: "executed",
|
|
150
|
+
payment_recorded_at_iso: completedAtIso,
|
|
151
|
+
executed_at_iso: completedAtIso
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
if (pending.deliveryId && state.deliveries.has(pending.deliveryId)) {
|
|
155
|
+
const delivery = state.deliveries.get(pending.deliveryId);
|
|
156
|
+
const nextDelivery = {
|
|
157
|
+
...delivery,
|
|
158
|
+
status: "delivered",
|
|
159
|
+
delivered_at_iso: completedAtIso,
|
|
160
|
+
receipt_id: next.receipt.receipt_id,
|
|
161
|
+
proof_ref: next.receipt.proof_ref,
|
|
162
|
+
attempt_count: (delivery.attempt_count || 0) + 1,
|
|
163
|
+
last_attempt_at_iso: completedAtIso,
|
|
164
|
+
history: [
|
|
165
|
+
...(Array.isArray(delivery.history) ? delivery.history : []),
|
|
166
|
+
buildDeliveryEvent("delivered", { ...delivery, status: "delivered", proof_ref: next.receipt.proof_ref }, completedAtIso, {
|
|
167
|
+
attempt_number: (delivery.attempt_count || 0) + 1
|
|
168
|
+
})
|
|
169
|
+
],
|
|
170
|
+
callback_delivery: delivery.callback_url
|
|
171
|
+
? {
|
|
172
|
+
...(delivery.callback_delivery || buildCallbackDeliveryState(delivery.callback_url, completedAtIso)),
|
|
173
|
+
status: "queued",
|
|
174
|
+
next_attempt_at_iso: completedAtIso,
|
|
175
|
+
last_error: null,
|
|
176
|
+
failed_at_iso: null
|
|
177
|
+
}
|
|
178
|
+
: null
|
|
179
|
+
};
|
|
180
|
+
state.deliveries.set(pending.deliveryId, nextDelivery);
|
|
181
|
+
}
|
|
182
|
+
state.pendingExecutions.delete(transactionId);
|
|
183
|
+
return next;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function hydratePendingExecutions(state) {
|
|
187
|
+
for (const transactionId of Array.from(state.pendingExecutions.keys())) {
|
|
188
|
+
completePendingExecution(state, transactionId);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function createCommandResult(state, payload, quote, options) {
|
|
193
|
+
const opts = options || {};
|
|
194
|
+
const deferredCompletion = payload && payload.defer_completion === true;
|
|
195
|
+
const { transaction, receipt, command } = createCommandArtifacts(payload, quote, {
|
|
196
|
+
deferredCompletion,
|
|
197
|
+
paymentVerification: opts.paymentVerification || null,
|
|
198
|
+
externalExecution: opts.externalExecution || null
|
|
199
|
+
});
|
|
200
|
+
const result = { transaction, receipt, command };
|
|
201
|
+
state.transactions.set(transaction.transaction_id, result);
|
|
202
|
+
state.receipts.set(receipt.receipt_id, result);
|
|
203
|
+
let paymentRecord = null;
|
|
204
|
+
if (!opts.skipPaymentLedger) {
|
|
205
|
+
paymentRecord = {
|
|
206
|
+
payment_id: `pay_${quote.quote_id}`,
|
|
207
|
+
quote_id: quote.quote_id,
|
|
208
|
+
transaction_id: transaction.transaction_id,
|
|
209
|
+
receipt_id: receipt.receipt_id,
|
|
210
|
+
account_id: quote.account_id,
|
|
211
|
+
amount_minor: quote.amount_minor,
|
|
212
|
+
currency: quote.currency,
|
|
213
|
+
settlement_adapter: quote.settlement_adapter,
|
|
214
|
+
settlement_integration_id: quote.settlement_integration_id || null,
|
|
215
|
+
recorded_at_iso: receipt.created_at_iso,
|
|
216
|
+
payment: transaction.payment,
|
|
217
|
+
settlement: transaction.settlement,
|
|
218
|
+
treasury: transaction.treasury,
|
|
219
|
+
default_path: transaction.default_path,
|
|
220
|
+
payment_verification: opts.paymentVerification
|
|
221
|
+
? {
|
|
222
|
+
verification_mode: opts.paymentVerification.verification_mode || null,
|
|
223
|
+
wallet_id: opts.paymentVerification.wallet_id || null,
|
|
224
|
+
age_seconds: Number.isFinite(Number(opts.paymentVerification.age_seconds))
|
|
225
|
+
? Number(opts.paymentVerification.age_seconds)
|
|
226
|
+
: null
|
|
227
|
+
}
|
|
228
|
+
: null
|
|
229
|
+
};
|
|
230
|
+
state.paymentLedger = [
|
|
231
|
+
paymentRecord,
|
|
232
|
+
...(Array.isArray(state.paymentLedger) ? state.paymentLedger : [])
|
|
233
|
+
].slice(0, 100);
|
|
234
|
+
const liveSettlement = createBsvSettlementRecord(state, transaction, receipt, paymentRecord);
|
|
235
|
+
if (liveSettlement) {
|
|
236
|
+
paymentRecord.live_settlement = {
|
|
237
|
+
settlement_id: liveSettlement.settlement_id,
|
|
238
|
+
status: liveSettlement.status,
|
|
239
|
+
finality_status: liveSettlement.finality_status,
|
|
240
|
+
txid: liveSettlement.txid,
|
|
241
|
+
rail: liveSettlement.rail
|
|
242
|
+
};
|
|
243
|
+
transaction.settlement.live = paymentRecord.live_settlement;
|
|
244
|
+
receipt.settlement.live = paymentRecord.live_settlement;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (opts.creditSpend) {
|
|
248
|
+
transaction.credit_spend = opts.creditSpend;
|
|
249
|
+
receipt.credit_spend = opts.creditSpend;
|
|
250
|
+
}
|
|
251
|
+
const deliveryRecord = {
|
|
252
|
+
delivery_id: `delivery_${transaction.transaction_id}`,
|
|
253
|
+
transaction_id: transaction.transaction_id,
|
|
254
|
+
receipt_id: receipt.receipt_id,
|
|
255
|
+
quote_id: quote.quote_id,
|
|
256
|
+
account_id: quote.account_id,
|
|
257
|
+
callback_url: transaction.callback_url,
|
|
258
|
+
output_ref: transaction.output_ref,
|
|
259
|
+
proof_ref: receipt.proof_ref,
|
|
260
|
+
payment_id: paymentRecord ? paymentRecord.payment_id : null,
|
|
261
|
+
delivery_target_kind: transaction.callback_url ? "callback" : "direct",
|
|
262
|
+
status: deferredCompletion ? "pending_delivery" : "delivered",
|
|
263
|
+
created_at_iso: transaction.created_at_iso,
|
|
264
|
+
delivered_at_iso: deferredCompletion ? null : receipt.created_at_iso,
|
|
265
|
+
attempt_count: deferredCompletion ? 0 : 1,
|
|
266
|
+
last_attempt_at_iso: deferredCompletion ? null : receipt.created_at_iso,
|
|
267
|
+
callback_delivery: buildCallbackDeliveryState(
|
|
268
|
+
transaction.callback_url,
|
|
269
|
+
deferredCompletion ? null : receipt.created_at_iso,
|
|
270
|
+
{
|
|
271
|
+
initial_status: deferredCompletion ? "pending_execution" : "queued"
|
|
272
|
+
}
|
|
273
|
+
),
|
|
274
|
+
history: [
|
|
275
|
+
buildDeliveryEvent("created", {
|
|
276
|
+
callback_url: transaction.callback_url,
|
|
277
|
+
output_ref: transaction.output_ref,
|
|
278
|
+
proof_ref: receipt.proof_ref,
|
|
279
|
+
status: deferredCompletion ? "pending_delivery" : "delivered"
|
|
280
|
+
}, transaction.created_at_iso),
|
|
281
|
+
...(!deferredCompletion ? [
|
|
282
|
+
buildDeliveryEvent("delivered", {
|
|
283
|
+
callback_url: transaction.callback_url,
|
|
284
|
+
output_ref: transaction.output_ref,
|
|
285
|
+
proof_ref: receipt.proof_ref,
|
|
286
|
+
status: "delivered"
|
|
287
|
+
}, receipt.created_at_iso, { attempt_number: 1 })
|
|
288
|
+
] : [])
|
|
289
|
+
]
|
|
290
|
+
};
|
|
291
|
+
state.deliveries.set(deliveryRecord.delivery_id, deliveryRecord);
|
|
292
|
+
markQuoteExecuted(state, quote, result, paymentRecord, { deferredCompletion });
|
|
293
|
+
if (deferredCompletion) {
|
|
294
|
+
state.pendingExecutions.set(transaction.transaction_id, {
|
|
295
|
+
readyAtMs: Date.now() + 300,
|
|
296
|
+
quoteId: quote.quote_id,
|
|
297
|
+
deliveryId: deliveryRecord.delivery_id,
|
|
298
|
+
durationMs: 250,
|
|
299
|
+
observedLatencyClass: "instant",
|
|
300
|
+
assessment: "within_posture"
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
return result;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
module.exports = {
|
|
307
|
+
buildCallbackDeliveryState,
|
|
308
|
+
completePendingExecution,
|
|
309
|
+
createCommandResult,
|
|
310
|
+
createDispute,
|
|
311
|
+
createQuote,
|
|
312
|
+
createRefund,
|
|
313
|
+
createRemediationTicket,
|
|
314
|
+
createRuntimeState,
|
|
315
|
+
hydrateQuoteStates,
|
|
316
|
+
hydratePendingExecutions,
|
|
317
|
+
actOnDispute,
|
|
318
|
+
actOnRefund,
|
|
319
|
+
actOnRemediationTicket,
|
|
320
|
+
settleRefundAsCredit,
|
|
321
|
+
listRecords
|
|
322
|
+
};
|