@haven_ai/sdk 0.1.27-alpha.0 → 0.1.28-alpha.0
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/dist/index.cjs +1865 -1704
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +30 -160
- package/dist/index.d.ts +30 -160
- package/dist/index.js +1865 -1704
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from 'async_hooks';
|
|
2
|
-
import { exact } from 'x402/schemes';
|
|
3
1
|
import { recoverTypedDataAddress, hashTypedData } from 'viem';
|
|
4
|
-
import { privateKeyToAccount } from 'viem/accounts';
|
|
5
2
|
import { ethers } from 'ethers';
|
|
6
3
|
import { createHash } from 'crypto';
|
|
4
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
5
|
+
import { exact } from 'x402/schemes';
|
|
6
|
+
import { privateKeyToAccount } from 'viem/accounts';
|
|
7
7
|
|
|
8
8
|
// src/client.ts
|
|
9
9
|
|
|
@@ -392,24 +392,6 @@ function verifySignature(hash, signature, expectedAddress) {
|
|
|
392
392
|
return false;
|
|
393
393
|
}
|
|
394
394
|
}
|
|
395
|
-
var RECEIPT_VERSION = "haven-receipt-1";
|
|
396
|
-
function defaultRecover(hash, signature) {
|
|
397
|
-
return ethers.recoverAddress(hash, signature);
|
|
398
|
-
}
|
|
399
|
-
function verifyPaymentReceipt(receipt, recover = defaultRecover) {
|
|
400
|
-
const { delegate, signHash: signHash2, signature } = receipt.authorization;
|
|
401
|
-
if (!signature) return { verified: false, reason: "missing_signature" };
|
|
402
|
-
let recovered;
|
|
403
|
-
try {
|
|
404
|
-
recovered = recover(signHash2, signature);
|
|
405
|
-
} catch {
|
|
406
|
-
return { verified: false, reason: "bad_signature" };
|
|
407
|
-
}
|
|
408
|
-
if (recovered.toLowerCase() !== delegate.toLowerCase()) {
|
|
409
|
-
return { verified: false, reason: "signer_mismatch", recoveredSigner: recovered };
|
|
410
|
-
}
|
|
411
|
-
return { verified: true, recoveredSigner: recovered };
|
|
412
|
-
}
|
|
413
395
|
|
|
414
396
|
// src/base64.ts
|
|
415
397
|
function normalizeBase64(value) {
|
|
@@ -956,61 +938,173 @@ function stableStringify2(value) {
|
|
|
956
938
|
const object = value;
|
|
957
939
|
return `{${Object.keys(object).sort().map((key) => `${JSON.stringify(key)}:${stableStringify2(object[key])}`).join(",")}}`;
|
|
958
940
|
}
|
|
959
|
-
function createJsonRpcProvider(url) {
|
|
960
|
-
return new ethers.JsonRpcProvider(url);
|
|
961
|
-
}
|
|
962
|
-
function createWallet(privateKey, provider) {
|
|
963
|
-
return new ethers.Wallet(privateKey, provider);
|
|
964
|
-
}
|
|
965
|
-
function createErc20Contract(address, abi, runner) {
|
|
966
|
-
return new ethers.Contract(address, abi, runner);
|
|
967
|
-
}
|
|
968
|
-
|
|
969
|
-
// src/client.ts
|
|
970
941
|
var DEFAULT_BASE_URL = "http://localhost:3001";
|
|
971
|
-
var
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
942
|
+
var DEFAULT_REQUEST_TIMEOUT = 3e4;
|
|
943
|
+
var HavenApiTransport = class {
|
|
944
|
+
apiKey;
|
|
945
|
+
baseUrl;
|
|
946
|
+
requestTimeout;
|
|
947
|
+
defaultHeaders;
|
|
948
|
+
requestContext = new AsyncLocalStorage();
|
|
949
|
+
constructor(config) {
|
|
950
|
+
this.apiKey = config.apiKey;
|
|
951
|
+
this.baseUrl = (config.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
952
|
+
this.requestTimeout = config.requestTimeout ?? DEFAULT_REQUEST_TIMEOUT;
|
|
953
|
+
this.defaultHeaders = { ...config.defaultHeaders ?? {} };
|
|
954
|
+
}
|
|
955
|
+
/** Run `fn` with extra headers scoped to its asynchronous Haven API work. */
|
|
956
|
+
withRequestContext(headers, fn) {
|
|
957
|
+
return this.requestContext.run({ headers: { ...headers } }, fn);
|
|
958
|
+
}
|
|
959
|
+
async post(path, body) {
|
|
960
|
+
return this.request("POST", path, body);
|
|
961
|
+
}
|
|
962
|
+
async get(path) {
|
|
963
|
+
return this.request("GET", path);
|
|
964
|
+
}
|
|
965
|
+
async request(method, path, body) {
|
|
966
|
+
const url = `${this.baseUrl}${path}`;
|
|
967
|
+
const controller = new AbortController();
|
|
968
|
+
const timeout = setTimeout(() => controller.abort(), this.requestTimeout);
|
|
969
|
+
try {
|
|
970
|
+
const contextHeaders = this.requestContext.getStore()?.headers ?? {};
|
|
971
|
+
const res = await fetch(url, {
|
|
972
|
+
method,
|
|
973
|
+
headers: {
|
|
974
|
+
"Content-Type": "application/json",
|
|
975
|
+
"Authorization": `Bearer ${this.apiKey}`,
|
|
976
|
+
...this.defaultHeaders,
|
|
977
|
+
...contextHeaders
|
|
978
|
+
},
|
|
979
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
980
|
+
signal: controller.signal
|
|
981
|
+
});
|
|
982
|
+
const data = await res.json();
|
|
983
|
+
if (!res.ok) {
|
|
984
|
+
const record = data;
|
|
985
|
+
const errorText = typeof record.error === "string" ? record.error : void 0;
|
|
986
|
+
const rawDetails = record.details ?? record.detail;
|
|
987
|
+
const detailsText = typeof rawDetails === "string" ? rawDetails : rawDetails != null ? JSON.stringify(rawDetails) : void 0;
|
|
988
|
+
const message = errorText && detailsText ? `${errorText}: ${detailsText}` : errorText ?? detailsText ?? "API request failed";
|
|
989
|
+
throw new HavenApiError(message, res.status, data);
|
|
990
|
+
}
|
|
991
|
+
return data;
|
|
992
|
+
} catch (err) {
|
|
993
|
+
if (err instanceof HavenApiError) throw err;
|
|
994
|
+
if (err instanceof Error && err.name === "AbortError") {
|
|
995
|
+
throw new HavenApiError(`Request to ${path} timed out`, 408);
|
|
996
|
+
}
|
|
997
|
+
throw new HavenApiError(
|
|
998
|
+
`Request to ${path} failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
999
|
+
0
|
|
1000
|
+
);
|
|
1001
|
+
} finally {
|
|
1002
|
+
clearTimeout(timeout);
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
978
1005
|
};
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
1006
|
+
|
|
1007
|
+
// src/payment-mappers.ts
|
|
1008
|
+
function mapPaymentResult(raw, buildExplorerUrl2) {
|
|
1009
|
+
return {
|
|
1010
|
+
paymentId: raw.payment_id,
|
|
1011
|
+
status: raw.status,
|
|
1012
|
+
token: raw.token,
|
|
1013
|
+
amount: raw.amount,
|
|
1014
|
+
to: raw.to,
|
|
1015
|
+
txHash: raw.tx_hash,
|
|
1016
|
+
errorMessage: raw.error_message,
|
|
1017
|
+
explorerUrl: raw.explorer_url ?? (raw.tx_hash ? buildExplorerUrl2(raw.chain_id, raw.tx_hash) : null),
|
|
1018
|
+
fee: raw.fee ? {
|
|
1019
|
+
amount: raw.fee.amount,
|
|
1020
|
+
token: raw.fee.token,
|
|
1021
|
+
basisPoints: raw.fee.basis_points,
|
|
1022
|
+
applied: raw.fee.applied
|
|
1023
|
+
} : null,
|
|
1024
|
+
createdAt: raw.created_at,
|
|
1025
|
+
signedAt: raw.signed_at,
|
|
1026
|
+
submittedAt: raw.submitted_at,
|
|
1027
|
+
confirmedAt: raw.confirmed_at,
|
|
1028
|
+
expiresAt: raw.expires_at
|
|
1029
|
+
};
|
|
985
1030
|
}
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
1031
|
+
function mapPaymentStatusResult(raw) {
|
|
1032
|
+
return {
|
|
1033
|
+
paymentId: raw.payment_id,
|
|
1034
|
+
kind: raw.kind,
|
|
1035
|
+
rail: raw.rail,
|
|
1036
|
+
status: raw.status,
|
|
1037
|
+
phase: raw.phase,
|
|
1038
|
+
nextAction: raw.next_action,
|
|
1039
|
+
amount: raw.amount,
|
|
1040
|
+
token: raw.token,
|
|
1041
|
+
resourceUrl: raw.resource_url,
|
|
1042
|
+
merchantAddress: raw.merchant_address,
|
|
1043
|
+
payerAddress: raw.payer_address ?? null,
|
|
1044
|
+
txHash: raw.tx_hash,
|
|
1045
|
+
expiresAt: raw.expires_at,
|
|
1046
|
+
chainId: raw.chain_id,
|
|
1047
|
+
message: raw.message,
|
|
1048
|
+
fee: raw.fee ? {
|
|
1049
|
+
amount: raw.fee.amount,
|
|
1050
|
+
token: raw.fee.token,
|
|
1051
|
+
basisPoints: raw.fee.basis_points,
|
|
1052
|
+
applied: raw.fee.applied
|
|
1053
|
+
} : null,
|
|
1054
|
+
amountAtomic: raw.amount_atomic ?? raw.x402?.amount_atomic ?? null,
|
|
1055
|
+
asset: raw.asset ?? raw.x402?.asset ?? null,
|
|
1056
|
+
network: raw.network ?? raw.x402?.network ?? null,
|
|
1057
|
+
description: raw.description ?? raw.x402?.description ?? null,
|
|
1058
|
+
idempotencyKey: raw.idempotency_key ?? raw.x402?.idempotency_key ?? null,
|
|
1059
|
+
x402: raw.x402 ? {
|
|
1060
|
+
amountAtomic: raw.x402.amount_atomic ?? raw.amount_atomic ?? null,
|
|
1061
|
+
asset: raw.x402.asset ?? raw.asset ?? null,
|
|
1062
|
+
network: raw.x402.network ?? raw.network ?? null,
|
|
1063
|
+
resourceUrl: raw.x402.resource_url ?? raw.resource_url,
|
|
1064
|
+
merchantAddress: raw.x402.merchant_address ?? raw.merchant_address,
|
|
1065
|
+
description: raw.x402.description ?? raw.description ?? null,
|
|
1066
|
+
idempotencyKey: raw.x402.idempotency_key ?? raw.idempotency_key ?? null
|
|
1067
|
+
} : void 0
|
|
1068
|
+
};
|
|
997
1069
|
}
|
|
998
|
-
function
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1070
|
+
function mapPaymentReceipt(raw) {
|
|
1071
|
+
const receipt = {
|
|
1072
|
+
id: raw.id,
|
|
1073
|
+
paymentId: raw.payment_id,
|
|
1074
|
+
rail: raw.rail,
|
|
1075
|
+
proofStatus: raw.proof_status,
|
|
1076
|
+
txHash: raw.tx_hash,
|
|
1077
|
+
chainId: raw.chain_id,
|
|
1078
|
+
resourceUrl: raw.resource_url,
|
|
1079
|
+
merchantAddress: raw.merchant_address,
|
|
1080
|
+
payerAddress: raw.payer_address,
|
|
1081
|
+
settlementAddress: raw.settlement_address,
|
|
1082
|
+
tokenSymbol: raw.token_symbol,
|
|
1083
|
+
tokenAddress: raw.token_address,
|
|
1084
|
+
amountRaw: raw.amount_raw,
|
|
1085
|
+
amount: raw.amount_human,
|
|
1086
|
+
challengeId: raw.challenge_id,
|
|
1087
|
+
idempotencyKey: raw.idempotency_key,
|
|
1088
|
+
challengePayload: raw.challenge_payload,
|
|
1089
|
+
selectedPayment: raw.selected_payment,
|
|
1090
|
+
paymentProofHeaderName: raw.payment_proof_header_name,
|
|
1091
|
+
protocolReceiptHeaderName: raw.protocol_receipt_header_name,
|
|
1092
|
+
protocolReceiptPayload: raw.protocol_receipt_payload,
|
|
1093
|
+
merchantStatus: raw.merchant_status,
|
|
1094
|
+
confirmedAt: raw.confirmed_at,
|
|
1095
|
+
createdAt: raw.created_at,
|
|
1096
|
+
updatedAt: raw.updated_at
|
|
1097
|
+
};
|
|
1098
|
+
if ("payment_intent_id" in raw) {
|
|
1099
|
+
receipt.paymentIntentId = raw.payment_intent_id ?? null;
|
|
1003
1100
|
}
|
|
1101
|
+
if ("approval_request_id" in raw) {
|
|
1102
|
+
receipt.approvalRequestId = raw.approval_request_id ?? null;
|
|
1103
|
+
}
|
|
1104
|
+
return receipt;
|
|
1004
1105
|
}
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
const hasSpendable = allowances.some((a) => safeBigInt(a.remainingAtomic) > 0n);
|
|
1008
|
-
return hasSpendable ? "ready" : "needs_approval";
|
|
1009
|
-
}
|
|
1010
|
-
var MCP_PROTOCOL_VERSION = "2025-06-18";
|
|
1011
|
-
var MCP_ACCEPT = "application/json, text/event-stream";
|
|
1012
|
-
var MCP_CLIENT_INFO = { name: "haven-sdk", version: "1" };
|
|
1013
|
-
var MERCHANT_BODY_SNIPPET_LIMIT = 1e3;
|
|
1106
|
+
|
|
1107
|
+
// src/payment-state.ts
|
|
1014
1108
|
var PAYMENT_STATE_STATUS_CODES = {
|
|
1015
1109
|
pending: 202,
|
|
1016
1110
|
pending_approval: 202,
|
|
@@ -1023,15 +1117,8 @@ var PAYMENT_STATE_STATUS_CODES = {
|
|
|
1023
1117
|
failed: 502,
|
|
1024
1118
|
rejected: 409
|
|
1025
1119
|
};
|
|
1026
|
-
function
|
|
1027
|
-
|
|
1028
|
-
if (network === "base-sepolia") return 84532;
|
|
1029
|
-
if (!network?.startsWith("eip155:")) return void 0;
|
|
1030
|
-
const chainId = Number(network.slice("eip155:".length));
|
|
1031
|
-
return Number.isFinite(chainId) ? chainId : void 0;
|
|
1032
|
-
}
|
|
1033
|
-
function chainIdOrNull(network) {
|
|
1034
|
-
return chainIdFromNetwork(network) ?? null;
|
|
1120
|
+
function paymentStateStatusCode(status, fallback = 502) {
|
|
1121
|
+
return PAYMENT_STATE_STATUS_CODES[status] ?? fallback;
|
|
1035
1122
|
}
|
|
1036
1123
|
function phaseForStatus(status) {
|
|
1037
1124
|
if (status === "pending_signature") return AgentPaymentPhase.AgentSignatureRequired;
|
|
@@ -1074,69 +1161,1484 @@ function messageForState(label, status, paymentId, nextAction) {
|
|
|
1074
1161
|
}
|
|
1075
1162
|
return `${label} is ${status}; next_action=${nextAction} (payment_id: ${paymentId}).`;
|
|
1076
1163
|
}
|
|
1077
|
-
function
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
const
|
|
1083
|
-
const
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1164
|
+
function paymentStateFromRaw(label, raw) {
|
|
1165
|
+
if (!raw.payment_id || !raw.status) return null;
|
|
1166
|
+
const phase = raw.phase ?? phaseForStatus(raw.status);
|
|
1167
|
+
const nextAction = raw.next_action ?? nextActionForStatus(raw.status);
|
|
1168
|
+
if (!phase || !nextAction) return null;
|
|
1169
|
+
const amount = raw.amount ?? raw.requested ?? "";
|
|
1170
|
+
const token = raw.token ?? "";
|
|
1171
|
+
const message = raw.message ?? raw.error ?? messageForState(label, raw.status, raw.payment_id, nextAction);
|
|
1172
|
+
return {
|
|
1173
|
+
paymentId: raw.payment_id,
|
|
1174
|
+
kind: raw.kind === "payment_intent" ? "payment_intent" : "approval_request",
|
|
1175
|
+
rail: raw.rail ?? "direct",
|
|
1176
|
+
status: raw.status === "pending" ? "pending_approval" : raw.status,
|
|
1177
|
+
phase,
|
|
1178
|
+
nextAction,
|
|
1179
|
+
amount,
|
|
1180
|
+
token,
|
|
1181
|
+
resourceUrl: raw.resource_url ?? null,
|
|
1182
|
+
merchantAddress: raw.merchant_address ?? raw.merchant_to ?? null,
|
|
1183
|
+
txHash: raw.tx_hash ?? null,
|
|
1184
|
+
expiresAt: raw.expires_at ?? "",
|
|
1185
|
+
chainId: raw.chain_id ?? 0,
|
|
1186
|
+
message,
|
|
1187
|
+
amountAtomic: raw.amount_atomic ?? raw.x402?.amount_atomic ?? raw.mpp?.amount_atomic ?? null,
|
|
1188
|
+
asset: raw.asset ?? raw.x402?.asset ?? raw.mpp?.asset ?? null,
|
|
1189
|
+
network: raw.network ?? raw.x402?.network ?? raw.mpp?.network ?? null,
|
|
1190
|
+
description: raw.description ?? raw.x402?.description ?? raw.mpp?.description ?? null,
|
|
1191
|
+
idempotencyKey: raw.idempotency_key ?? raw.x402?.idempotency_key ?? raw.mpp?.idempotency_key ?? null,
|
|
1192
|
+
x402: raw.x402 ? {
|
|
1193
|
+
amountAtomic: raw.x402.amount_atomic ?? raw.amount_atomic ?? null,
|
|
1194
|
+
asset: raw.x402.asset ?? raw.asset ?? null,
|
|
1195
|
+
network: raw.x402.network ?? raw.network ?? null,
|
|
1196
|
+
resourceUrl: raw.x402.resource_url ?? raw.resource_url ?? null,
|
|
1197
|
+
merchantAddress: raw.x402.merchant_address ?? raw.merchant_address ?? raw.merchant_to ?? null,
|
|
1198
|
+
description: raw.x402.description ?? raw.description ?? null,
|
|
1199
|
+
idempotencyKey: raw.x402.idempotency_key ?? raw.idempotency_key ?? null
|
|
1200
|
+
} : void 0,
|
|
1201
|
+
mpp: raw.mpp ? {
|
|
1202
|
+
amountAtomic: raw.mpp.amount_atomic ?? raw.amount_atomic ?? null,
|
|
1203
|
+
asset: raw.mpp.asset ?? raw.asset ?? null,
|
|
1204
|
+
network: raw.mpp.network ?? raw.network ?? null,
|
|
1205
|
+
resourceUrl: raw.mpp.resource_url ?? raw.resource_url ?? null,
|
|
1206
|
+
merchantAddress: raw.mpp.merchant_address ?? raw.merchant_address ?? raw.merchant_to ?? null,
|
|
1207
|
+
description: raw.mpp.description ?? raw.description ?? null,
|
|
1208
|
+
idempotencyKey: raw.mpp.idempotency_key ?? raw.idempotency_key ?? null,
|
|
1209
|
+
challengeId: raw.mpp.challenge_id ?? raw.challenge_id ?? null
|
|
1210
|
+
} : void 0
|
|
1211
|
+
};
|
|
1098
1212
|
}
|
|
1099
|
-
function
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1213
|
+
function throwPaymentStateError(label, raw) {
|
|
1214
|
+
const statusCode = paymentStateStatusCode(raw.status);
|
|
1215
|
+
const state = paymentStateFromRaw(label, raw);
|
|
1216
|
+
if (state) {
|
|
1217
|
+
throw new HavenPaymentStateError(state.message, statusCode, state, raw);
|
|
1104
1218
|
}
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1219
|
+
if (raw.status === "pending_approval") {
|
|
1220
|
+
throw new HavenApiError(
|
|
1221
|
+
`${label} exceeds the on-chain allowance and was queued for owner approval (payment_id: ${raw.payment_id}).`,
|
|
1222
|
+
statusCode,
|
|
1223
|
+
raw
|
|
1224
|
+
);
|
|
1225
|
+
}
|
|
1226
|
+
if (raw.status === "expired") {
|
|
1227
|
+
throw new HavenApiError(
|
|
1228
|
+
`${label} expired before it could be completed (payment_id: ${raw.payment_id}).`,
|
|
1229
|
+
statusCode,
|
|
1230
|
+
raw
|
|
1231
|
+
);
|
|
1112
1232
|
}
|
|
1233
|
+
const paymentId = raw.payment_id ? ` (payment_id: ${raw.payment_id})` : "";
|
|
1234
|
+
const message = raw.error ?? `${label} ${raw.status}${paymentId}`;
|
|
1235
|
+
throw new HavenApiError(message, statusCode, raw);
|
|
1113
1236
|
}
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1237
|
+
|
|
1238
|
+
// src/mcp-merchant-transport.ts
|
|
1239
|
+
var DEFAULT_MERCHANT_TIMEOUT = 3e5;
|
|
1240
|
+
var MCP_NOTIFICATION_TIMEOUT = 1e4;
|
|
1241
|
+
var MCP_PROTOCOL_VERSION = "2025-06-18";
|
|
1242
|
+
var MCP_ACCEPT = "application/json, text/event-stream";
|
|
1243
|
+
var MCP_CLIENT_INFO = { name: "haven-sdk", version: "1" };
|
|
1244
|
+
var McpMerchantTransport = class {
|
|
1245
|
+
merchantTimeout;
|
|
1246
|
+
fetchImpl;
|
|
1247
|
+
requestId = 0;
|
|
1248
|
+
constructor(options = {}) {
|
|
1249
|
+
this.merchantTimeout = options.merchantTimeout ?? DEFAULT_MERCHANT_TIMEOUT;
|
|
1250
|
+
this.fetchImpl = options.fetch ?? ((input, init) => globalThis.fetch(input, init));
|
|
1251
|
+
}
|
|
1252
|
+
/** Fetch a merchant with a settlement-sized timeout and caller cancellation. */
|
|
1253
|
+
async fetch(url, init = {}, timeoutMs = this.merchantTimeout) {
|
|
1254
|
+
const timeoutSignal = AbortSignal.timeout(timeoutMs);
|
|
1255
|
+
const signal = init.signal ? AbortSignal.any([init.signal, timeoutSignal]) : timeoutSignal;
|
|
1119
1256
|
try {
|
|
1120
|
-
|
|
1257
|
+
return await this.fetchImpl(url, { ...init, signal });
|
|
1258
|
+
} catch (err) {
|
|
1259
|
+
if (timeoutSignal.aborted) {
|
|
1260
|
+
throw new MerchantTimeoutError(`Merchant request timed out after ${timeoutMs}ms: ${url}`);
|
|
1261
|
+
}
|
|
1262
|
+
throw err;
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
/**
|
|
1266
|
+
* Establish an MCP Streamable-HTTP session. Any handshake failure degrades
|
|
1267
|
+
* to `undefined`, allowing the caller to fall back to plain x402.
|
|
1268
|
+
*/
|
|
1269
|
+
async initialize(url, init, wallet) {
|
|
1270
|
+
try {
|
|
1271
|
+
const headers = new Headers(init?.headers);
|
|
1272
|
+
headers.set("Content-Type", "application/json");
|
|
1273
|
+
headers.set("Accept", MCP_ACCEPT);
|
|
1274
|
+
if (wallet && !headers.has("x402-wallet")) headers.set("x402-wallet", wallet);
|
|
1275
|
+
const response = await this.fetch(url, {
|
|
1276
|
+
method: "POST",
|
|
1277
|
+
headers,
|
|
1278
|
+
body: JSON.stringify({
|
|
1279
|
+
jsonrpc: "2.0",
|
|
1280
|
+
id: ++this.requestId,
|
|
1281
|
+
method: "initialize",
|
|
1282
|
+
params: {
|
|
1283
|
+
protocolVersion: MCP_PROTOCOL_VERSION,
|
|
1284
|
+
capabilities: {},
|
|
1285
|
+
clientInfo: MCP_CLIENT_INFO
|
|
1286
|
+
}
|
|
1287
|
+
})
|
|
1288
|
+
});
|
|
1289
|
+
if (!response.ok) return void 0;
|
|
1290
|
+
const sessionId = response.headers.get("mcp-session-id");
|
|
1291
|
+
if (!sessionId) return void 0;
|
|
1292
|
+
const message = await this.readMessage(response);
|
|
1293
|
+
if (message && "error" in message) return void 0;
|
|
1294
|
+
await this.notifyInitialized(url, init, sessionId, wallet);
|
|
1295
|
+
return sessionId;
|
|
1121
1296
|
} catch {
|
|
1297
|
+
return void 0;
|
|
1122
1298
|
}
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1299
|
+
}
|
|
1300
|
+
/** Add the MCP session and response-content negotiation headers. */
|
|
1301
|
+
withSessionHeaders(init, sessionId) {
|
|
1302
|
+
const headers = new Headers(init?.headers);
|
|
1303
|
+
headers.set("mcp-session-id", sessionId);
|
|
1304
|
+
headers.set("Accept", MCP_ACCEPT);
|
|
1305
|
+
return { ...init, headers };
|
|
1306
|
+
}
|
|
1307
|
+
/** Read one JSON-RPC message from a JSON or SSE response without consuming it. */
|
|
1308
|
+
async readMessage(response) {
|
|
1309
|
+
let text;
|
|
1310
|
+
try {
|
|
1311
|
+
text = await response.clone().text();
|
|
1312
|
+
} catch {
|
|
1313
|
+
return void 0;
|
|
1129
1314
|
}
|
|
1130
|
-
if (
|
|
1131
|
-
|
|
1315
|
+
if ((response.headers.get("content-type") ?? "").includes("text/event-stream")) {
|
|
1316
|
+
return selectJsonRpcResult(parseSseJsonRpcMessages(text));
|
|
1317
|
+
}
|
|
1318
|
+
try {
|
|
1319
|
+
return JSON.parse(text);
|
|
1320
|
+
} catch {
|
|
1321
|
+
return void 0;
|
|
1132
1322
|
}
|
|
1133
1323
|
}
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1324
|
+
/**
|
|
1325
|
+
* Collapse an MCP SSE response to the JSON-RPC result. Non-SSE and
|
|
1326
|
+
* unparseable responses pass through with their original body untouched.
|
|
1327
|
+
*/
|
|
1328
|
+
async surfaceResult(response) {
|
|
1329
|
+
if (!(response.headers.get("content-type") ?? "").includes("text/event-stream")) {
|
|
1330
|
+
return response;
|
|
1331
|
+
}
|
|
1332
|
+
let text;
|
|
1333
|
+
try {
|
|
1334
|
+
text = await response.clone().text();
|
|
1335
|
+
} catch {
|
|
1336
|
+
return response;
|
|
1337
|
+
}
|
|
1338
|
+
const message = selectJsonRpcResult(parseSseJsonRpcMessages(text));
|
|
1339
|
+
if (!message) return response;
|
|
1340
|
+
const body = "result" in message ? message.result : message;
|
|
1341
|
+
const headers = new Headers(response.headers);
|
|
1342
|
+
headers.set("content-type", "application/json");
|
|
1343
|
+
headers.delete("content-length");
|
|
1344
|
+
headers.delete("mcp-session-id");
|
|
1345
|
+
return new Response(JSON.stringify(body), {
|
|
1346
|
+
status: response.status,
|
|
1347
|
+
statusText: response.statusText,
|
|
1348
|
+
headers
|
|
1349
|
+
});
|
|
1350
|
+
}
|
|
1351
|
+
/** Detect MCP transport from the URL or Coinbase Bazaar extension. */
|
|
1352
|
+
async detect(url, paymentRequired, response) {
|
|
1353
|
+
if (isMcpUrl(url)) return { handshakeRequired: true, source: "path" };
|
|
1354
|
+
if (paymentRequired.extensions?.bazaar != null) {
|
|
1355
|
+
return { handshakeRequired: true, source: "bazaar" };
|
|
1356
|
+
}
|
|
1357
|
+
if (await responseHasBazaarExtension(response)) {
|
|
1358
|
+
return { handshakeRequired: true, source: "bazaar" };
|
|
1359
|
+
}
|
|
1360
|
+
return void 0;
|
|
1361
|
+
}
|
|
1362
|
+
/** Identify the conventional Streamable-HTTP MCP path without probing it. */
|
|
1363
|
+
isMcpUrl(url) {
|
|
1364
|
+
return isMcpUrl(url);
|
|
1365
|
+
}
|
|
1366
|
+
/** Detect Bazaar metadata without consuming the merchant response. */
|
|
1367
|
+
hasBazaarExtension(response) {
|
|
1368
|
+
return responseHasBazaarExtension(response);
|
|
1369
|
+
}
|
|
1370
|
+
/** Deliver an already-signed x402 header without changing the caller body. */
|
|
1371
|
+
async deliverPayment(url, init, paymentHeader) {
|
|
1372
|
+
const headers = new Headers(init?.headers);
|
|
1373
|
+
headers.set("X-PAYMENT", paymentHeader);
|
|
1374
|
+
return this.fetch(url, { ...init, headers });
|
|
1375
|
+
}
|
|
1376
|
+
async notifyInitialized(url, init, sessionId, wallet) {
|
|
1377
|
+
try {
|
|
1378
|
+
const headers = new Headers(init?.headers);
|
|
1379
|
+
headers.set("Content-Type", "application/json");
|
|
1380
|
+
headers.set("Accept", MCP_ACCEPT);
|
|
1381
|
+
headers.set("mcp-session-id", sessionId);
|
|
1382
|
+
if (wallet && !headers.has("x402-wallet")) headers.set("x402-wallet", wallet);
|
|
1383
|
+
await this.fetch(
|
|
1384
|
+
url,
|
|
1385
|
+
{
|
|
1386
|
+
method: "POST",
|
|
1387
|
+
headers,
|
|
1388
|
+
body: JSON.stringify({ jsonrpc: "2.0", method: "notifications/initialized" })
|
|
1389
|
+
},
|
|
1390
|
+
MCP_NOTIFICATION_TIMEOUT
|
|
1391
|
+
);
|
|
1392
|
+
} catch {
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
};
|
|
1396
|
+
async function captureMerchantResponse(response) {
|
|
1397
|
+
const merchant_body = await response.text().catch(() => "");
|
|
1398
|
+
return {
|
|
1399
|
+
merchant_status: response.status,
|
|
1400
|
+
merchant_status_text: response.statusText,
|
|
1401
|
+
merchant_headers: Object.fromEntries(response.headers.entries()),
|
|
1402
|
+
merchant_body
|
|
1403
|
+
};
|
|
1404
|
+
}
|
|
1405
|
+
function isMcpUrl(url) {
|
|
1406
|
+
try {
|
|
1407
|
+
return new URL(url).pathname.replace(/\/+$/, "").endsWith("/mcp");
|
|
1408
|
+
} catch {
|
|
1409
|
+
return /\/mcp(?:[/?#]|$)/.test(url);
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
async function responseHasBazaarExtension(response) {
|
|
1413
|
+
try {
|
|
1414
|
+
const body = await response.clone().json();
|
|
1415
|
+
return body?.extensions?.bazaar != null;
|
|
1416
|
+
} catch {
|
|
1417
|
+
return false;
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
function parseSseJsonRpcMessages(text) {
|
|
1421
|
+
const messages = [];
|
|
1422
|
+
let dataLines = [];
|
|
1423
|
+
const flush = () => {
|
|
1424
|
+
if (dataLines.length === 0) return;
|
|
1425
|
+
try {
|
|
1426
|
+
messages.push(JSON.parse(dataLines.join("\n")));
|
|
1427
|
+
} catch {
|
|
1428
|
+
}
|
|
1429
|
+
dataLines = [];
|
|
1430
|
+
};
|
|
1431
|
+
for (const line of text.split(/\r?\n/)) {
|
|
1432
|
+
if (line === "") {
|
|
1433
|
+
flush();
|
|
1434
|
+
continue;
|
|
1435
|
+
}
|
|
1436
|
+
if (line.startsWith("data:")) dataLines.push(line.slice(5).replace(/^ /, ""));
|
|
1437
|
+
}
|
|
1438
|
+
flush();
|
|
1439
|
+
return messages;
|
|
1440
|
+
}
|
|
1441
|
+
function selectJsonRpcResult(messages) {
|
|
1442
|
+
return messages.find((message) => "result" in message || "error" in message) ?? messages.at(-1);
|
|
1443
|
+
}
|
|
1444
|
+
var RECEIPT_VERSION = "haven-receipt-1";
|
|
1445
|
+
function defaultRecover(hash, signature) {
|
|
1446
|
+
return ethers.recoverAddress(hash, signature);
|
|
1447
|
+
}
|
|
1448
|
+
function verifyPaymentReceipt(receipt, recover = defaultRecover) {
|
|
1449
|
+
const { delegate, signHash: signHash2, signature } = receipt.authorization;
|
|
1450
|
+
if (!signature) return { verified: false, reason: "missing_signature" };
|
|
1451
|
+
let recovered;
|
|
1452
|
+
try {
|
|
1453
|
+
recovered = recover(signHash2, signature);
|
|
1454
|
+
} catch {
|
|
1455
|
+
return { verified: false, reason: "bad_signature" };
|
|
1456
|
+
}
|
|
1457
|
+
if (recovered.toLowerCase() !== delegate.toLowerCase()) {
|
|
1458
|
+
return { verified: false, reason: "signer_mismatch", recoveredSigner: recovered };
|
|
1459
|
+
}
|
|
1460
|
+
return { verified: true, recoveredSigner: recovered };
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
// src/account-reads.ts
|
|
1464
|
+
function safeBigInt(value) {
|
|
1465
|
+
try {
|
|
1466
|
+
return BigInt(value);
|
|
1467
|
+
} catch {
|
|
1468
|
+
return 0n;
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
function formatAtomicAmount(atomic, decimals) {
|
|
1472
|
+
if (atomic < 0n) return "0.0";
|
|
1473
|
+
const value = atomic.toString().padStart(decimals + 1, "0");
|
|
1474
|
+
const whole = value.slice(0, value.length - decimals) || "0";
|
|
1475
|
+
const fraction = value.slice(value.length - decimals).replace(/0+$/, "") || "0";
|
|
1476
|
+
return `${whole}.${fraction}`;
|
|
1477
|
+
}
|
|
1478
|
+
function deriveReadiness(status, allowances) {
|
|
1479
|
+
if (status !== "active") return "revoked";
|
|
1480
|
+
return allowances.some((allowance) => safeBigInt(allowance.remainingAtomic) > 0n) ? "ready" : "needs_approval";
|
|
1481
|
+
}
|
|
1482
|
+
var AccountReads = class {
|
|
1483
|
+
transport;
|
|
1484
|
+
getPaymentStatus;
|
|
1485
|
+
agentInFlight = null;
|
|
1486
|
+
constructor(options) {
|
|
1487
|
+
this.transport = options.transport;
|
|
1488
|
+
this.getPaymentStatus = options.getPaymentStatus;
|
|
1489
|
+
}
|
|
1490
|
+
async getAgent() {
|
|
1491
|
+
if (this.agentInFlight) return this.agentInFlight;
|
|
1492
|
+
const request = this.fetchAgent();
|
|
1493
|
+
this.agentInFlight = request;
|
|
1494
|
+
request.finally(() => {
|
|
1495
|
+
this.agentInFlight = null;
|
|
1496
|
+
}).catch(() => {
|
|
1497
|
+
});
|
|
1498
|
+
return request;
|
|
1499
|
+
}
|
|
1500
|
+
async getAgentSummary() {
|
|
1501
|
+
const [agent, allowanceSummary] = await Promise.all([this.getAgent(), this.getAllowances()]);
|
|
1502
|
+
const allowances = allowanceSummary.allowances.map((allowance) => {
|
|
1503
|
+
const token = resolveTokenFromAddress(allowance.tokenAddress);
|
|
1504
|
+
const remainingDisplay = token ? `${formatAtomicAmount(safeBigInt(allowance.onchain.remaining), token.decimals)} ${allowance.tokenSymbol}` : `${allowance.onchain.remaining} ${allowance.tokenSymbol} (atomic; unknown decimals)`;
|
|
1505
|
+
return {
|
|
1506
|
+
tokenSymbol: allowance.tokenSymbol,
|
|
1507
|
+
remainingAtomic: allowance.onchain.remaining,
|
|
1508
|
+
remainingDisplay,
|
|
1509
|
+
configuredAmount: allowance.configuredAmount,
|
|
1510
|
+
resetPeriodMin: allowance.resetPeriodMin,
|
|
1511
|
+
isResetPending: allowance.onchain.isResetPending
|
|
1512
|
+
};
|
|
1513
|
+
});
|
|
1514
|
+
const readiness = deriveReadiness(agent.status, allowances);
|
|
1515
|
+
return { ...agent, readiness, spend_authority_readiness: readiness, allowances };
|
|
1516
|
+
}
|
|
1517
|
+
async getAllowances() {
|
|
1518
|
+
const raw = await this.transport.get("/machine-payments/allowances");
|
|
1519
|
+
return {
|
|
1520
|
+
agentId: raw.agent_id,
|
|
1521
|
+
safeAddress: raw.safe_address,
|
|
1522
|
+
delegateAddress: raw.delegate_address,
|
|
1523
|
+
chainId: raw.chain_id,
|
|
1524
|
+
allowances: raw.allowances.map((allowance) => ({
|
|
1525
|
+
id: allowance.id,
|
|
1526
|
+
tokenAddress: allowance.token_address,
|
|
1527
|
+
tokenSymbol: allowance.token_symbol,
|
|
1528
|
+
configuredAmount: allowance.configured_amount,
|
|
1529
|
+
resetPeriodMin: allowance.reset_period_min,
|
|
1530
|
+
onchain: {
|
|
1531
|
+
amount: allowance.onchain.amount,
|
|
1532
|
+
spent: allowance.onchain.spent,
|
|
1533
|
+
remaining: allowance.onchain.remaining,
|
|
1534
|
+
effectiveSpent: allowance.onchain.effective_spent,
|
|
1535
|
+
resetTimeMin: allowance.onchain.reset_time_min,
|
|
1536
|
+
lastResetMin: allowance.onchain.last_reset_min,
|
|
1537
|
+
nonce: allowance.onchain.nonce,
|
|
1538
|
+
isResetPending: allowance.onchain.is_reset_pending,
|
|
1539
|
+
remainingIsFromChain: allowance.onchain.remaining_is_from_chain
|
|
1540
|
+
}
|
|
1541
|
+
}))
|
|
1542
|
+
};
|
|
1543
|
+
}
|
|
1544
|
+
async getPostPurchaseAllowanceSummary(paymentId) {
|
|
1545
|
+
const unavailable = (detail, payment2 = null) => ({
|
|
1546
|
+
payment: payment2,
|
|
1547
|
+
allowance: null,
|
|
1548
|
+
warnings: [{
|
|
1549
|
+
code: AgentPaymentWarningCode.AllowanceCheckUnavailable,
|
|
1550
|
+
message: `Could not read the post-purchase allowance/budget for payment ${paymentId} (${detail}). The payment itself succeeded \u2014 the on-chain policy remains the actual spend gate; this only affects the remaining-budget figure reported here.`
|
|
1551
|
+
}]
|
|
1552
|
+
});
|
|
1553
|
+
const [statusResult, agentResult, allowanceResult] = await Promise.allSettled([
|
|
1554
|
+
this.getPaymentStatus(paymentId),
|
|
1555
|
+
this.getAgent(),
|
|
1556
|
+
this.getAllowances()
|
|
1557
|
+
]);
|
|
1558
|
+
if (statusResult.status === "rejected") {
|
|
1559
|
+
return unavailable(statusResult.reason instanceof Error ? statusResult.reason.message : String(statusResult.reason));
|
|
1560
|
+
}
|
|
1561
|
+
const payment = statusResult.value;
|
|
1562
|
+
if (agentResult.status === "rejected") {
|
|
1563
|
+
return unavailable(agentResult.reason instanceof Error ? agentResult.reason.message : String(agentResult.reason), payment);
|
|
1564
|
+
}
|
|
1565
|
+
if (allowanceResult.status === "rejected") {
|
|
1566
|
+
return unavailable(allowanceResult.reason instanceof Error ? allowanceResult.reason.message : String(allowanceResult.reason), payment);
|
|
1567
|
+
}
|
|
1568
|
+
try {
|
|
1569
|
+
const tokenAddress = payment.asset ?? payment.x402?.asset ?? null;
|
|
1570
|
+
if (!tokenAddress) return unavailable("the settled payment does not carry a resolvable token address", payment);
|
|
1571
|
+
const match = allowanceResult.value.allowances.find(
|
|
1572
|
+
(allowance) => allowance.tokenAddress.toLowerCase() === tokenAddress.toLowerCase()
|
|
1573
|
+
);
|
|
1574
|
+
if (!match) return unavailable("no allowance/budget row matches the settled token", payment);
|
|
1575
|
+
const token = resolveTokenFromAddress(match.tokenAddress);
|
|
1576
|
+
const remainingDisplay = token ? `${formatAtomicAmount(safeBigInt(match.onchain.remaining), token.decimals)} ${match.tokenSymbol}` : void 0;
|
|
1577
|
+
const rail = agentResult.value.executionRail;
|
|
1578
|
+
return {
|
|
1579
|
+
payment,
|
|
1580
|
+
allowance: {
|
|
1581
|
+
rail,
|
|
1582
|
+
remaining_atomic: match.onchain.remaining,
|
|
1583
|
+
...remainingDisplay ? { remaining_display: remainingDisplay } : {},
|
|
1584
|
+
token_symbol: match.tokenSymbol,
|
|
1585
|
+
token_address: match.tokenAddress,
|
|
1586
|
+
reset_period: match.resetPeriodMin,
|
|
1587
|
+
source: rail === "delegation" ? "active_delegations" : "allowance_module"
|
|
1588
|
+
},
|
|
1589
|
+
warnings: []
|
|
1590
|
+
};
|
|
1591
|
+
} catch (error) {
|
|
1592
|
+
return unavailable(error instanceof Error ? error.message : String(error));
|
|
1593
|
+
}
|
|
1594
|
+
}
|
|
1595
|
+
async listReceipts(options = {}) {
|
|
1596
|
+
const query = options.limit ? `?limit=${encodeURIComponent(String(options.limit))}` : "";
|
|
1597
|
+
const raw = await this.transport.get(`/machine-payments/receipts${query}`);
|
|
1598
|
+
return raw.receipts.map(mapPaymentReceipt);
|
|
1599
|
+
}
|
|
1600
|
+
async getReceipt(paymentId) {
|
|
1601
|
+
const { receipt } = await this.transport.get(`/payments/${paymentId}/receipt`);
|
|
1602
|
+
return { receipt, verification: verifyPaymentReceipt(receipt) };
|
|
1603
|
+
}
|
|
1604
|
+
async fetchAgent() {
|
|
1605
|
+
const raw = await this.transport.get("/machine-payments/agent");
|
|
1606
|
+
return {
|
|
1607
|
+
id: raw.id,
|
|
1608
|
+
name: raw.name,
|
|
1609
|
+
status: raw.status,
|
|
1610
|
+
safeAddress: raw.safe_address,
|
|
1611
|
+
delegateAddress: raw.delegate_address,
|
|
1612
|
+
chainId: raw.chain_id,
|
|
1613
|
+
executionRail: raw.execution_rail === "delegation" ? "delegation" : "legacy"
|
|
1614
|
+
};
|
|
1615
|
+
}
|
|
1616
|
+
};
|
|
1617
|
+
function createJsonRpcProvider(url) {
|
|
1618
|
+
return new ethers.JsonRpcProvider(url);
|
|
1619
|
+
}
|
|
1620
|
+
function createWallet(privateKey, provider) {
|
|
1621
|
+
return new ethers.Wallet(privateKey, provider);
|
|
1622
|
+
}
|
|
1623
|
+
function createErc20Contract(address, abi, runner) {
|
|
1624
|
+
return new ethers.Contract(address, abi, runner);
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
// src/delegate-sweep.ts
|
|
1628
|
+
var DelegateSweepApi = class {
|
|
1629
|
+
constructor(options) {
|
|
1630
|
+
this.options = options;
|
|
1631
|
+
}
|
|
1632
|
+
options;
|
|
1633
|
+
async sweepDelegate() {
|
|
1634
|
+
if (!this.options.delegateKey) throw new HavenSigningError("delegateKey is required for sweepDelegate.");
|
|
1635
|
+
const agent = await this.options.getAgent();
|
|
1636
|
+
if (!agent.delegateAddress) throw new HavenApiError("Agent has no delegate address.", 422);
|
|
1637
|
+
const rpcUrl = this.options.chainRpcs[agent.chainId];
|
|
1638
|
+
if (!rpcUrl) throw new HavenApiError(`chainRpcs[${agent.chainId}] must be configured to sweep the delegate wallet.`, 422);
|
|
1639
|
+
const provider = createJsonRpcProvider(rpcUrl);
|
|
1640
|
+
const wallet = createWallet(this.options.delegateKey, provider);
|
|
1641
|
+
const transfers = [];
|
|
1642
|
+
if (isSweepableChain(agent.chainId)) {
|
|
1643
|
+
const contract = createErc20Contract(sweepUsdcAddress(agent.chainId), ["function balanceOf(address) view returns (uint256)", "function transfer(address to, uint256 amount) returns (bool)"], wallet);
|
|
1644
|
+
const balance2 = await contract.balanceOf(agent.delegateAddress);
|
|
1645
|
+
if (balance2 > 0n) {
|
|
1646
|
+
const tx = await contract.transfer(agent.safeAddress, balance2);
|
|
1647
|
+
const receipt = await tx.wait(1);
|
|
1648
|
+
const txHash = receipt?.hash ?? tx.hash;
|
|
1649
|
+
transfers.push({ asset: "USDC", amount: format(balance2, 6), amountAtomic: balance2.toString(), txHash, explorerUrl: this.options.buildExplorerUrl(agent.chainId, txHash) });
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
const balance = await provider.getBalance(agent.delegateAddress);
|
|
1653
|
+
if (balance > 0n) {
|
|
1654
|
+
const fee = await provider.getFeeData();
|
|
1655
|
+
const send = balance - (fee.maxFeePerGas ?? fee.gasPrice ?? 1000000n) * 21000n * 2n;
|
|
1656
|
+
if (send > 0n) {
|
|
1657
|
+
const tx = await wallet.sendTransaction({ to: agent.safeAddress, value: send });
|
|
1658
|
+
const receipt = await tx.wait(1);
|
|
1659
|
+
const txHash = receipt?.hash ?? tx.hash;
|
|
1660
|
+
transfers.push({ asset: "ETH", amount: format(send, 18), amountAtomic: send.toString(), txHash, explorerUrl: this.options.buildExplorerUrl(agent.chainId, txHash) });
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
return { fromAddress: agent.delegateAddress, toAddress: agent.safeAddress, chainId: agent.chainId, transfers };
|
|
1664
|
+
}
|
|
1665
|
+
prepareSweep() {
|
|
1666
|
+
return this.options.transport.post("/machine-payments/sweep/prepare", {});
|
|
1667
|
+
}
|
|
1668
|
+
submitSweep(authorization, signature) {
|
|
1669
|
+
return this.options.transport.post("/machine-payments/sweep/submit", { authorization, signature });
|
|
1670
|
+
}
|
|
1671
|
+
};
|
|
1672
|
+
function format(value, decimals) {
|
|
1673
|
+
const raw = value.toString().padStart(decimals + 1, "0");
|
|
1674
|
+
return `${raw.slice(0, -decimals) || "0"}.${raw.slice(-decimals).replace(/0+$/, "") || "0"}`;
|
|
1675
|
+
}
|
|
1676
|
+
|
|
1677
|
+
// src/x402-protocol.ts
|
|
1678
|
+
var CHAIN_EXPLORER_TX = {
|
|
1679
|
+
100: "https://gnosisscan.io/tx",
|
|
1680
|
+
8453: "https://basescan.org/tx"
|
|
1681
|
+
};
|
|
1682
|
+
function buildExplorerUrl(chainId, txHash) {
|
|
1683
|
+
const base = CHAIN_EXPLORER_TX[chainId ?? 8453] ?? CHAIN_EXPLORER_TX[8453];
|
|
1684
|
+
return `${base}/${txHash}`;
|
|
1685
|
+
}
|
|
1686
|
+
function explorerUrlOrEmpty(chainId, txHash) {
|
|
1687
|
+
return txHash ? buildExplorerUrl(chainId, txHash) : "";
|
|
1688
|
+
}
|
|
1689
|
+
function chainIdFromNetwork(network) {
|
|
1690
|
+
if (network === "base") return 8453;
|
|
1691
|
+
if (network === "base-sepolia") return 84532;
|
|
1692
|
+
if (!network?.startsWith("eip155:")) return void 0;
|
|
1693
|
+
const chainId = Number(network.slice("eip155:".length));
|
|
1694
|
+
return Number.isFinite(chainId) ? chainId : void 0;
|
|
1695
|
+
}
|
|
1696
|
+
function chainIdOrNull(network) {
|
|
1697
|
+
return chainIdFromNetwork(network) ?? null;
|
|
1698
|
+
}
|
|
1699
|
+
function sameAddress3(a, b) {
|
|
1700
|
+
return Boolean(a && b && a.toLowerCase() === b.toLowerCase());
|
|
1701
|
+
}
|
|
1702
|
+
function decimalFromUsdcAtomic(value) {
|
|
1703
|
+
const amount = BigInt(value);
|
|
1704
|
+
const whole = amount / 1000000n;
|
|
1705
|
+
const fraction = (amount % 1000000n).toString().padStart(6, "0").replace(/0+$/, "");
|
|
1706
|
+
return fraction ? `${whole}.${fraction}` : whole.toString();
|
|
1707
|
+
}
|
|
1708
|
+
function normalizeDecimal(value) {
|
|
1709
|
+
if (!value.includes(".")) return value.replace(/^0+(?=\d)/, "") || "0";
|
|
1710
|
+
const [whole, fraction = ""] = value.split(".");
|
|
1711
|
+
const normalizedWhole = whole.replace(/^0+(?=\d)/, "") || "0";
|
|
1712
|
+
const normalizedFraction = fraction.replace(/0+$/, "");
|
|
1713
|
+
return normalizedFraction ? `${normalizedWhole}.${normalizedFraction}` : normalizedWhole;
|
|
1714
|
+
}
|
|
1715
|
+
function x402PayerAddress(delegateAddress, x402Wallet) {
|
|
1716
|
+
return delegateAddress ?? x402Wallet;
|
|
1717
|
+
}
|
|
1718
|
+
function withX402Wallet(init, wallet) {
|
|
1719
|
+
if (!wallet) return init;
|
|
1720
|
+
const headers = new Headers(init?.headers);
|
|
1721
|
+
if (!headers.has("x402-wallet")) {
|
|
1722
|
+
headers.set("x402-wallet", wallet);
|
|
1723
|
+
}
|
|
1724
|
+
return {
|
|
1725
|
+
...init,
|
|
1726
|
+
headers
|
|
1727
|
+
};
|
|
1728
|
+
}
|
|
1729
|
+
function snapshotRequestBody(body) {
|
|
1730
|
+
if (body == null) return void 0;
|
|
1731
|
+
if (typeof body === "string") return body;
|
|
1732
|
+
if (body instanceof URLSearchParams) return body.toString();
|
|
1733
|
+
throw new HavenApiError(
|
|
1734
|
+
"Quote helpers can only capture resumable request bodies that are strings or URLSearchParams. For streams, blobs, or binary bodies, preserve the original request yourself and call the matching resume method with fresh init.",
|
|
1735
|
+
400
|
|
1736
|
+
);
|
|
1737
|
+
}
|
|
1738
|
+
function snapshotX402Request(url, init) {
|
|
1739
|
+
return {
|
|
1740
|
+
url,
|
|
1741
|
+
method: init?.method ?? "GET",
|
|
1742
|
+
headers: Array.from(new Headers(init?.headers).entries()),
|
|
1743
|
+
body: snapshotRequestBody(init?.body)
|
|
1744
|
+
};
|
|
1745
|
+
}
|
|
1746
|
+
function requestInitFromSnapshot(request) {
|
|
1747
|
+
return {
|
|
1748
|
+
method: request.method,
|
|
1749
|
+
headers: request.headers,
|
|
1750
|
+
body: request.body
|
|
1751
|
+
};
|
|
1752
|
+
}
|
|
1753
|
+
function buildX402Quote(paymentRequired, request, idempotencyKey, mcpTransport) {
|
|
1754
|
+
const option = selectStandardPaymentOption(paymentRequired.accepts);
|
|
1755
|
+
if (!option) {
|
|
1756
|
+
throw new HavenApiError(
|
|
1757
|
+
"No compatible payment option found in x402 requirements. Haven supports standard x402 exact payments on Base USDC.",
|
|
1758
|
+
400
|
|
1759
|
+
);
|
|
1760
|
+
}
|
|
1761
|
+
const token = resolveTokenFromAddress(option.asset, option.network);
|
|
1762
|
+
return {
|
|
1763
|
+
rail: "x402",
|
|
1764
|
+
idempotencyKey: idempotencyKey ?? buildX402IdempotencyKey(paymentRequired, option),
|
|
1765
|
+
paymentRequired,
|
|
1766
|
+
accepted: option,
|
|
1767
|
+
request,
|
|
1768
|
+
...mcpTransport ? { mcpTransport } : {},
|
|
1769
|
+
resourceUrl: paymentRequired.resource.url,
|
|
1770
|
+
description: paymentRequired.resource.description ?? option.description ?? null,
|
|
1771
|
+
mimeType: paymentRequired.resource.mimeType ?? option.mimeType ?? null,
|
|
1772
|
+
amountAtomic: x402AuthorizationAmount(option),
|
|
1773
|
+
amount: decimalFromUsdcAtomic(x402AuthorizationAmount(option)),
|
|
1774
|
+
token: token?.symbol ?? "USDC",
|
|
1775
|
+
// #1351: null when the asset is unrecognised on this network — the
|
|
1776
|
+
// `token` fallback above is a LABEL, not evidence of 6 decimals, and a
|
|
1777
|
+
// human-denominated cap must fail closed rather than convert against a
|
|
1778
|
+
// guess. Same resolution as `token`, so the two never disagree.
|
|
1779
|
+
decimals: token?.decimals ?? null,
|
|
1780
|
+
asset: option.asset,
|
|
1781
|
+
network: option.network,
|
|
1782
|
+
chainId: chainIdOrNull(option.network),
|
|
1783
|
+
merchantAddress: option.payTo,
|
|
1784
|
+
maxTimeoutSeconds: option.maxTimeoutSeconds
|
|
1785
|
+
};
|
|
1786
|
+
}
|
|
1787
|
+
function buildX402Receipt(input) {
|
|
1788
|
+
const fundingExplorerUrl = input.explorerUrl || explorerUrlOrEmpty(input.chainId, input.txHash);
|
|
1789
|
+
return {
|
|
1790
|
+
success: true,
|
|
1791
|
+
paymentId: input.paymentId,
|
|
1792
|
+
txHash: input.txHash,
|
|
1793
|
+
token: input.token,
|
|
1794
|
+
amount: input.amount,
|
|
1795
|
+
to: input.to,
|
|
1796
|
+
resourceUrl: input.resourceUrl,
|
|
1797
|
+
explorerUrl: input.explorerUrl,
|
|
1798
|
+
accepted: input.accepted,
|
|
1799
|
+
paymentHeader: input.paymentHeader,
|
|
1800
|
+
merchantTo: input.merchantTo ?? input.accepted.payTo,
|
|
1801
|
+
payer: input.payer,
|
|
1802
|
+
chainId: input.chainId,
|
|
1803
|
+
haven: {
|
|
1804
|
+
paymentId: input.paymentId,
|
|
1805
|
+
fundingTxHash: input.txHash,
|
|
1806
|
+
fundingExplorerUrl
|
|
1807
|
+
},
|
|
1808
|
+
merchant: {
|
|
1809
|
+
payTo: input.merchantTo ?? input.accepted.payTo
|
|
1810
|
+
},
|
|
1811
|
+
x402: {
|
|
1812
|
+
amount: x402AuthorizationAmount(input.accepted),
|
|
1813
|
+
token: input.token,
|
|
1814
|
+
network: input.accepted.network,
|
|
1815
|
+
asset: input.accepted.asset,
|
|
1816
|
+
resource: input.accepted.resource ?? input.resourceUrl
|
|
1817
|
+
}
|
|
1818
|
+
};
|
|
1819
|
+
}
|
|
1820
|
+
function buildX402ResumeState(input) {
|
|
1821
|
+
const token = resolveTokenFromAddress(input.accepted.asset, input.accepted.network);
|
|
1822
|
+
return {
|
|
1823
|
+
rail: "x402",
|
|
1824
|
+
paymentId: input.paymentId,
|
|
1825
|
+
idempotencyKey: input.idempotencyKey,
|
|
1826
|
+
paymentRequired: input.paymentRequired,
|
|
1827
|
+
accepted: input.accepted,
|
|
1828
|
+
url: input.request?.url ?? input.paymentRequired.resource.url,
|
|
1829
|
+
request: input.request,
|
|
1830
|
+
resourceUrl: input.paymentRequired.resource.url,
|
|
1831
|
+
description: input.paymentRequired.resource.description ?? input.accepted.description ?? null,
|
|
1832
|
+
amountAtomic: x402AuthorizationAmount(input.accepted),
|
|
1833
|
+
amount: decimalFromUsdcAtomic(x402AuthorizationAmount(input.accepted)),
|
|
1834
|
+
token: token?.symbol ?? "USDC",
|
|
1835
|
+
asset: input.accepted.asset,
|
|
1836
|
+
network: input.accepted.network,
|
|
1837
|
+
chainId: chainIdOrNull(input.accepted.network),
|
|
1838
|
+
merchantAddress: input.accepted.payTo
|
|
1839
|
+
};
|
|
1840
|
+
}
|
|
1841
|
+
function attachX402ResumeState(err, paymentRequired, accepted, idempotencyKey, request) {
|
|
1842
|
+
if (!(err instanceof HavenPaymentStateError)) return;
|
|
1843
|
+
if (err.state.rail !== "x402") return;
|
|
1844
|
+
err.resumeState = buildX402ResumeState({
|
|
1845
|
+
paymentId: err.state.paymentId,
|
|
1846
|
+
paymentRequired,
|
|
1847
|
+
accepted,
|
|
1848
|
+
idempotencyKey,
|
|
1849
|
+
request
|
|
1850
|
+
});
|
|
1851
|
+
}
|
|
1852
|
+
function attachResumeState(err, input) {
|
|
1853
|
+
attachX402ResumeState(
|
|
1854
|
+
err,
|
|
1855
|
+
input.paymentRequired,
|
|
1856
|
+
input.accepted,
|
|
1857
|
+
input.idempotencyKey,
|
|
1858
|
+
input.request
|
|
1859
|
+
);
|
|
1860
|
+
}
|
|
1861
|
+
function assertCanResumeX402(status, paymentRequired, option) {
|
|
1862
|
+
if (status.rail !== "x402") {
|
|
1863
|
+
throw new HavenPaymentStateError(
|
|
1864
|
+
`Payment ${status.paymentId} is ${status.rail}, not x402.`,
|
|
1865
|
+
409,
|
|
1866
|
+
status
|
|
1867
|
+
);
|
|
1868
|
+
}
|
|
1869
|
+
if (status.nextAction !== AgentPaymentNextAction.RetryOriginalX402Request) {
|
|
1870
|
+
throw new HavenPaymentStateError(status.message, paymentStateStatusCode(status.status, 409), status);
|
|
1871
|
+
}
|
|
1872
|
+
if (!status.txHash) {
|
|
1873
|
+
throw new HavenApiError(
|
|
1874
|
+
`x402 payment ${status.paymentId} is ready to retry but has no Haven transaction hash.`,
|
|
1875
|
+
502,
|
|
1876
|
+
status,
|
|
1877
|
+
status.paymentId
|
|
1878
|
+
);
|
|
1879
|
+
}
|
|
1880
|
+
if (status.resourceUrl && status.resourceUrl !== paymentRequired.resource.url) {
|
|
1881
|
+
throw new HavenApiError(
|
|
1882
|
+
"x402 resume request does not match the approved resource URL.",
|
|
1883
|
+
409,
|
|
1884
|
+
{ status, paymentRequired },
|
|
1885
|
+
status.paymentId
|
|
1886
|
+
);
|
|
1887
|
+
}
|
|
1888
|
+
if (status.merchantAddress && !sameAddress3(status.merchantAddress, option.payTo)) {
|
|
1889
|
+
throw new HavenApiError(
|
|
1890
|
+
"x402 resume request does not match the approved merchant.",
|
|
1891
|
+
409,
|
|
1892
|
+
{ status, selectedPayment: option },
|
|
1893
|
+
status.paymentId
|
|
1894
|
+
);
|
|
1895
|
+
}
|
|
1896
|
+
const optionChainId = chainIdFromNetwork(option.network);
|
|
1897
|
+
if (status.chainId && optionChainId && status.chainId !== optionChainId) {
|
|
1898
|
+
throw new HavenApiError(
|
|
1899
|
+
"x402 resume request does not match the approved network.",
|
|
1900
|
+
409,
|
|
1901
|
+
{ status, selectedPayment: option },
|
|
1902
|
+
status.paymentId
|
|
1903
|
+
);
|
|
1904
|
+
}
|
|
1905
|
+
if (status.token && status.token !== "USDC") {
|
|
1906
|
+
throw new HavenApiError(
|
|
1907
|
+
"x402 resume request does not match the approved token.",
|
|
1908
|
+
409,
|
|
1909
|
+
{ status, selectedPayment: option },
|
|
1910
|
+
status.paymentId
|
|
1911
|
+
);
|
|
1912
|
+
}
|
|
1913
|
+
const approvedAmount = status.amount ? normalizeDecimal(status.amount) : "";
|
|
1914
|
+
const requestedAmount = normalizeDecimal(decimalFromUsdcAtomic(x402AuthorizationAmount(option)));
|
|
1915
|
+
if (approvedAmount && approvedAmount !== requestedAmount) {
|
|
1916
|
+
throw new HavenApiError(
|
|
1917
|
+
"x402 resume request does not match the approved amount.",
|
|
1918
|
+
409,
|
|
1919
|
+
{ status, selectedPayment: option },
|
|
1920
|
+
status.paymentId
|
|
1921
|
+
);
|
|
1922
|
+
}
|
|
1923
|
+
}
|
|
1924
|
+
var X402FundingLeg = class {
|
|
1925
|
+
delegateKey;
|
|
1926
|
+
delegateAddress;
|
|
1927
|
+
x402Wallet;
|
|
1928
|
+
chainRpcs;
|
|
1929
|
+
post;
|
|
1930
|
+
signForData;
|
|
1931
|
+
assertSignableAuthorizationState;
|
|
1932
|
+
/**
|
|
1933
|
+
* Receipts keyed by idempotency key, held only as long as the underlying
|
|
1934
|
+
* EIP-3009 authorization is valid. The cache belongs to this module rather
|
|
1935
|
+
* than to the facade because its expiry is read out of the authorization
|
|
1936
|
+
* header itself — a 3009 artifact.
|
|
1937
|
+
*/
|
|
1938
|
+
receiptCache = /* @__PURE__ */ new Map();
|
|
1939
|
+
constructor(options) {
|
|
1940
|
+
this.delegateKey = options.delegateKey;
|
|
1941
|
+
this.delegateAddress = options.delegateAddress;
|
|
1942
|
+
this.x402Wallet = options.x402Wallet;
|
|
1943
|
+
this.chainRpcs = options.chainRpcs;
|
|
1944
|
+
this.post = options.post;
|
|
1945
|
+
this.signForData = options.signForData;
|
|
1946
|
+
this.assertSignableAuthorizationState = options.assertSignableAuthorizationState;
|
|
1947
|
+
}
|
|
1948
|
+
// ── Receipt cache ────────────────────────────────────────────────
|
|
1949
|
+
/** A still-valid cached receipt for this key, or undefined. */
|
|
1950
|
+
cachedReceipt(idempotencyKey) {
|
|
1951
|
+
const cached = this.receiptCache.get(idempotencyKey);
|
|
1952
|
+
if (cached && cached.expiresAt > Date.now()) return cached.receipt;
|
|
1953
|
+
return void 0;
|
|
1954
|
+
}
|
|
1955
|
+
cacheReceipt(idempotencyKey, paymentHeader, receipt) {
|
|
1956
|
+
const expiresAt = getPaymentHeaderValidBefore(paymentHeader);
|
|
1957
|
+
if (expiresAt > Date.now()) {
|
|
1958
|
+
this.receiptCache.set(idempotencyKey, { expiresAt, receipt });
|
|
1959
|
+
}
|
|
1960
|
+
}
|
|
1961
|
+
// ── Authorization ────────────────────────────────────────────────
|
|
1962
|
+
async authorize(paymentRequired, option, idempotencyKey) {
|
|
1963
|
+
const raw = await this.post("/x402", {
|
|
1964
|
+
url: paymentRequired.resource.url,
|
|
1965
|
+
payTo: this.delegateAddress,
|
|
1966
|
+
merchantPayTo: option.payTo,
|
|
1967
|
+
amount: x402AuthorizationAmount(option),
|
|
1968
|
+
asset: option.asset,
|
|
1969
|
+
network: option.network,
|
|
1970
|
+
description: paymentRequired.resource.description,
|
|
1971
|
+
idempotencyKey,
|
|
1972
|
+
// #1360: same explicit funding-leg declaration as createX402Intent —
|
|
1973
|
+
// this local-key path derives payTo from the key (never stale), but the
|
|
1974
|
+
// declaration keeps both writers of the 3009 shape loud-by-default.
|
|
1975
|
+
settlementScheme: "eip3009"
|
|
1976
|
+
});
|
|
1977
|
+
const state = paymentStateFromRaw("x402 payment", raw);
|
|
1978
|
+
const executedReplay = raw.success && raw.tx_hash ? "idempotency-collision" : state?.nextAction === AgentPaymentNextAction.RetryOriginalX402Request ? "approval-resume" : null;
|
|
1979
|
+
if (executedReplay) {
|
|
1980
|
+
const canFund = await this.delegateCanFund(
|
|
1981
|
+
raw.chain_id ?? state?.chainId ?? chainIdFromNetwork(option.network),
|
|
1982
|
+
option.asset,
|
|
1983
|
+
x402AuthorizationAmount(option)
|
|
1984
|
+
);
|
|
1985
|
+
const refuse = executedReplay === "idempotency-collision" ? canFund !== true : canFund === false;
|
|
1986
|
+
if (refuse) {
|
|
1987
|
+
const settledReceipt = state && executedReplay === "approval-resume" ? this.receiptFromStatus(paymentRequired, option, void 0, state) : this.receiptFromAuthorization(paymentRequired, option, void 0, raw);
|
|
1988
|
+
throw new X402AlreadySettledError(
|
|
1989
|
+
canFund === false ? "This x402 payment already settled \u2014 the delegate no longer holds the funds to authorize it again. To buy the same item a second time, pass a distinct `idempotencyKey`; the synthesised key intentionally collapses repeat calls for the same product within a 5-minute window so a retried request cannot pay twice." : "This x402 payment already settled, and whether the delegate can still fund a new authorization could not be verified (no `chainRpcs` entry for this chain). Refusing rather than issue an authorization that may be unfundable. To buy the same item a second time, pass a distinct `idempotencyKey`; to finish an interrupted payment, resume it by `paymentId`.",
|
|
1990
|
+
settledReceipt,
|
|
1991
|
+
canFund === false ? "settled" : "unverifiable"
|
|
1992
|
+
);
|
|
1993
|
+
}
|
|
1994
|
+
}
|
|
1995
|
+
const paymentHeader = await this.createPaymentHeader(paymentRequired, option);
|
|
1996
|
+
if (raw.success && raw.tx_hash) {
|
|
1997
|
+
const receipt2 = this.receiptFromAuthorization(paymentRequired, option, paymentHeader, raw);
|
|
1998
|
+
this.cacheReceipt(idempotencyKey, paymentHeader, receipt2);
|
|
1999
|
+
return receipt2;
|
|
2000
|
+
}
|
|
2001
|
+
if (state?.nextAction === AgentPaymentNextAction.RetryOriginalX402Request) {
|
|
2002
|
+
const receipt2 = this.receiptFromStatus(paymentRequired, option, paymentHeader, state);
|
|
2003
|
+
this.cacheReceipt(idempotencyKey, paymentHeader, receipt2);
|
|
2004
|
+
return receipt2;
|
|
2005
|
+
}
|
|
2006
|
+
this.assertSignableAuthorizationState("x402 payment", raw);
|
|
2007
|
+
if (!raw.sign_data?.hash) {
|
|
2008
|
+
throw new HavenApiError("No sign_hash returned from x402/authorize", 500, raw);
|
|
2009
|
+
}
|
|
2010
|
+
const sig = await this.signForData(raw.sign_data);
|
|
2011
|
+
const execResult = await this.post(
|
|
2012
|
+
`/payments/${raw.payment_id}/sign`,
|
|
2013
|
+
{ signature: sig }
|
|
2014
|
+
);
|
|
2015
|
+
if (execResult.status !== "confirmed") {
|
|
2016
|
+
throwPaymentStateError("x402 payment", execResult);
|
|
2017
|
+
}
|
|
2018
|
+
await this.waitForFundingTx(
|
|
2019
|
+
execResult.tx_hash,
|
|
2020
|
+
execResult.chain_id ?? chainIdFromNetwork(option.network)
|
|
2021
|
+
);
|
|
2022
|
+
const receipt = this.receiptFromAuthorization(paymentRequired, option, paymentHeader, raw, execResult);
|
|
2023
|
+
this.cacheReceipt(idempotencyKey, paymentHeader, receipt);
|
|
2024
|
+
return receipt;
|
|
2025
|
+
}
|
|
2026
|
+
// ── Header minting ───────────────────────────────────────────────
|
|
2027
|
+
async createPaymentHeader(paymentRequired, option) {
|
|
2028
|
+
if (!this.delegateKey) {
|
|
2029
|
+
throw new HavenSigningError("delegateKey is required to sign x402 payment headers.");
|
|
2030
|
+
}
|
|
2031
|
+
const account = privateKeyToAccount(this.delegateKey);
|
|
2032
|
+
const requirements = toStandardPaymentRequirements(paymentRequired, option);
|
|
2033
|
+
const header = await exact.evm.createPaymentHeader(
|
|
2034
|
+
account,
|
|
2035
|
+
paymentRequired.x402Version,
|
|
2036
|
+
requirements
|
|
2037
|
+
);
|
|
2038
|
+
if (paymentRequired.x402Version < 2) return header;
|
|
2039
|
+
const payment = decodeBase64Json(header);
|
|
2040
|
+
return encodeBase64Json({
|
|
2041
|
+
x402Version: paymentRequired.x402Version,
|
|
2042
|
+
accepted: option,
|
|
2043
|
+
payload: payment.payload
|
|
2044
|
+
});
|
|
2045
|
+
}
|
|
2046
|
+
// ── Receipt mapping ──────────────────────────────────────────────
|
|
2047
|
+
receiptFromAuthorization(paymentRequired, option, paymentHeader, raw, execResult) {
|
|
2048
|
+
const txHash = execResult?.tx_hash ?? raw.tx_hash ?? "";
|
|
2049
|
+
const chainId = execResult?.chain_id ?? raw.chain_id ?? chainIdFromNetwork(option.network);
|
|
2050
|
+
const token = execResult?.token ?? raw.token ?? "USDC";
|
|
2051
|
+
const amount = execResult?.amount ?? raw.amount ?? decimalFromUsdcAtomic(x402AuthorizationAmount(option));
|
|
2052
|
+
const to = execResult?.to ?? raw.to ?? this.delegateAddress ?? "";
|
|
2053
|
+
const explorerUrl = execResult?.explorer_url ?? raw.explorer_url ?? explorerUrlOrEmpty(chainId, txHash);
|
|
2054
|
+
const merchantTo = execResult?.merchant_to ?? raw.merchant_to ?? option.payTo;
|
|
2055
|
+
const payer = raw.payer ?? raw.safe_address ?? raw.sign_data?.components.safe;
|
|
2056
|
+
return buildX402Receipt({
|
|
2057
|
+
paymentId: raw.payment_id,
|
|
2058
|
+
txHash,
|
|
2059
|
+
token,
|
|
2060
|
+
amount,
|
|
2061
|
+
to,
|
|
2062
|
+
resourceUrl: paymentRequired.resource.url,
|
|
2063
|
+
explorerUrl,
|
|
2064
|
+
accepted: option,
|
|
2065
|
+
paymentHeader,
|
|
2066
|
+
merchantTo,
|
|
2067
|
+
payer,
|
|
2068
|
+
chainId
|
|
2069
|
+
});
|
|
2070
|
+
}
|
|
2071
|
+
receiptFromStatus(paymentRequired, option, paymentHeader, status) {
|
|
2072
|
+
if (!status.txHash) {
|
|
2073
|
+
throw new HavenApiError(
|
|
2074
|
+
`x402 payment ${status.paymentId} is ready to retry but has no Haven transaction hash.`,
|
|
2075
|
+
502,
|
|
2076
|
+
status,
|
|
2077
|
+
status.paymentId
|
|
2078
|
+
);
|
|
2079
|
+
}
|
|
2080
|
+
return buildX402Receipt({
|
|
2081
|
+
paymentId: status.paymentId,
|
|
2082
|
+
txHash: status.txHash,
|
|
2083
|
+
token: status.token || "USDC",
|
|
2084
|
+
amount: status.amount || decimalFromUsdcAtomic(x402AuthorizationAmount(option)),
|
|
2085
|
+
to: this.delegateAddress ?? "",
|
|
2086
|
+
resourceUrl: paymentRequired.resource.url,
|
|
2087
|
+
explorerUrl: explorerUrlOrEmpty(status.chainId, status.txHash),
|
|
2088
|
+
accepted: option,
|
|
2089
|
+
paymentHeader,
|
|
2090
|
+
merchantTo: status.merchantAddress ?? option.payTo,
|
|
2091
|
+
payer: this.x402Wallet,
|
|
2092
|
+
chainId: status.chainId || chainIdFromNetwork(option.network)
|
|
2093
|
+
});
|
|
2094
|
+
}
|
|
2095
|
+
// ── On-chain reads ───────────────────────────────────────────────
|
|
2096
|
+
/**
|
|
2097
|
+
* Wait for a funding tx to be mined with ≥1 confirmation before the
|
|
2098
|
+
* merchant retry, eliminating the race where the merchant's
|
|
2099
|
+
* `balanceOf(delegate)` runs before the funding block propagates.
|
|
2100
|
+
*
|
|
2101
|
+
* Skipped when `chainRpcs` does not include the chain; in that case Haven's
|
|
2102
|
+
* backend has already confirmed on-chain submission and callers accept the
|
|
2103
|
+
* small propagation window as a trade-off for not configuring an RPC URL.
|
|
2104
|
+
*/
|
|
2105
|
+
async waitForFundingTx(txHash, chainId, timeoutMs = 3e4) {
|
|
2106
|
+
if (!txHash || !chainId) return;
|
|
2107
|
+
const rpcUrl = this.chainRpcs[chainId];
|
|
2108
|
+
if (!rpcUrl) return;
|
|
2109
|
+
const provider = createJsonRpcProvider(rpcUrl);
|
|
2110
|
+
const onChainReceipt = await provider.waitForTransaction(txHash, 1, timeoutMs);
|
|
2111
|
+
if (!onChainReceipt || onChainReceipt.status !== 1) {
|
|
2112
|
+
throw new HavenApiError(
|
|
2113
|
+
"Funding tx did not confirm on-chain within the timeout window.",
|
|
2114
|
+
500,
|
|
2115
|
+
{ txHash, chainId }
|
|
2116
|
+
);
|
|
2117
|
+
}
|
|
2118
|
+
}
|
|
2119
|
+
/**
|
|
2120
|
+
* Can the delegate EOA still fund an authorization for `amountAtomic`?
|
|
2121
|
+
*
|
|
2122
|
+
* #1521: the only question that separates a legitimate resume (funding
|
|
2123
|
+
* confirmed, merchant never paid — the delegate still holds the money) from
|
|
2124
|
+
* a replayed settled payment (funding confirmed, merchant paid, delegate
|
|
2125
|
+
* spent). The intent's own `status: 'confirmed'` is identical in both.
|
|
2126
|
+
*
|
|
2127
|
+
* The balance is asked of the CHAIN rather than of Haven's bookkeeping on
|
|
2128
|
+
* purpose: the merchant-settlement evidence record is written by this SDK
|
|
2129
|
+
* *after* the merchant call, so a client that dies between the two leaves
|
|
2130
|
+
* the backend believing the merchant was never paid — the exact case the
|
|
2131
|
+
* discriminator has to get right. The chain cannot be behind in that way.
|
|
2132
|
+
*
|
|
2133
|
+
* Returns `null` — never a guess — when `chainRpcs` has no entry for the
|
|
2134
|
+
* chain or the read fails. Callers must treat that as "unverifiable", not
|
|
2135
|
+
* as "funded".
|
|
2136
|
+
*/
|
|
2137
|
+
async delegateCanFund(chainId, tokenAddress, amountAtomic, timeoutMs = 1e4) {
|
|
2138
|
+
if (!chainId || !this.delegateAddress) return null;
|
|
2139
|
+
const rpcUrl = this.chainRpcs[chainId];
|
|
2140
|
+
if (!rpcUrl) return null;
|
|
2141
|
+
try {
|
|
2142
|
+
const provider = createJsonRpcProvider(rpcUrl);
|
|
2143
|
+
const token = createErc20Contract(
|
|
2144
|
+
tokenAddress,
|
|
2145
|
+
["function balanceOf(address) view returns (uint256)"],
|
|
2146
|
+
provider
|
|
2147
|
+
);
|
|
2148
|
+
const balance = await Promise.race([
|
|
2149
|
+
token.balanceOf(this.delegateAddress),
|
|
2150
|
+
new Promise((resolve) => setTimeout(() => resolve(null), timeoutMs).unref?.())
|
|
2151
|
+
]);
|
|
2152
|
+
if (balance === null) return null;
|
|
2153
|
+
return balance >= BigInt(amountAtomic);
|
|
2154
|
+
} catch {
|
|
2155
|
+
return null;
|
|
2156
|
+
}
|
|
2157
|
+
}
|
|
2158
|
+
};
|
|
2159
|
+
function getPaymentHeaderValidBefore(paymentHeader) {
|
|
2160
|
+
try {
|
|
2161
|
+
const payment = decodeBase64Json(
|
|
2162
|
+
paymentHeader
|
|
2163
|
+
);
|
|
2164
|
+
const payload = payment.payload;
|
|
2165
|
+
const validBeforeSeconds = Number(payload.authorization?.validBefore);
|
|
2166
|
+
if (Number.isFinite(validBeforeSeconds)) return validBeforeSeconds * 1e3;
|
|
2167
|
+
} catch {
|
|
2168
|
+
}
|
|
2169
|
+
return 0;
|
|
2170
|
+
}
|
|
2171
|
+
|
|
2172
|
+
// src/x402-erc7710.ts
|
|
2173
|
+
var X402Erc7710 = class {
|
|
2174
|
+
delegateKey;
|
|
2175
|
+
post;
|
|
2176
|
+
signForData;
|
|
2177
|
+
getAgent;
|
|
2178
|
+
constructor(options) {
|
|
2179
|
+
this.delegateKey = options.delegateKey;
|
|
2180
|
+
this.post = options.post;
|
|
2181
|
+
this.signForData = options.signForData;
|
|
2182
|
+
this.getAgent = options.getAgent;
|
|
2183
|
+
}
|
|
2184
|
+
/**
|
|
2185
|
+
* Pay a merchant through **erc7710 direct settlement** (#1454, epic #1450).
|
|
2186
|
+
*
|
|
2187
|
+
* The whole point of this path is what it does NOT do. There is no funding
|
|
2188
|
+
* leg: the merchant redeems a delegation chain and pulls from the treasury
|
|
2189
|
+
* directly, so the delegate EOA never holds the money, no sweep can strand
|
|
2190
|
+
* it, and the #713 reconciliation class does not apply. It is also why this
|
|
2191
|
+
* method is SMALLER than the 3009 path — the backend assembles the merchant
|
|
2192
|
+
* `X-PAYMENT` header in `assembleSettlementPayload`, so the SDK builds no
|
|
2193
|
+
* header locally.
|
|
2194
|
+
*
|
|
2195
|
+
* authorize (payTo = the MERCHANT) → sign the child → settle → header
|
|
2196
|
+
*
|
|
2197
|
+
* The caller then retries the merchant with that header. **Nothing has
|
|
2198
|
+
* settled when this returns** — that is why it does not return an
|
|
2199
|
+
* `X402Receipt`.
|
|
2200
|
+
*
|
|
2201
|
+
* Requires a delegation-rail account. The backend enforces that
|
|
2202
|
+
* (`validateGenericSchemeRail`), and so does this method, before building a
|
|
2203
|
+
* request the backend would only reject: an error a client can explain is
|
|
2204
|
+
* worth more than a 400 it has to decode.
|
|
2205
|
+
*
|
|
2206
|
+
* **MCP callers must pass `options.resourceUrl`.** An in-band MCP 402
|
|
2207
|
+
* challenge frequently carries no `resource` object at all, so
|
|
2208
|
+
* `paymentRequired.resource?.url` is undefined and the backend answers
|
|
2209
|
+
* "Valid url is required". The QA scenario this path was ported from falls
|
|
2210
|
+
* back to the request URL for exactly that reason — the SDK cannot, because
|
|
2211
|
+
* it never saw the request. Pass it.
|
|
2212
|
+
*/
|
|
2213
|
+
async settle(paymentRequired, options = {}) {
|
|
2214
|
+
if (!this.delegateKey) {
|
|
2215
|
+
throw new HavenSigningError(
|
|
2216
|
+
"delegateKey is required for x402 payments. Pass it in the HavenClient config."
|
|
2217
|
+
);
|
|
2218
|
+
}
|
|
2219
|
+
const prepared = await this.prepare(paymentRequired, options);
|
|
2220
|
+
const signature = await this.signForData(prepared.signData);
|
|
2221
|
+
const paymentHeader = await this.submit(prepared.paymentId, signature);
|
|
2222
|
+
return { ...prepared.settlement, paymentHeader };
|
|
2223
|
+
}
|
|
2224
|
+
/**
|
|
2225
|
+
* The AUTHORIZE half of erc7710 settlement (#1456): select the scheme, build
|
|
2226
|
+
* the request, and return the child to be signed — without signing it.
|
|
2227
|
+
*
|
|
2228
|
+
* Split out because the hosted topology cannot use `settleX402Erc7710()`:
|
|
2229
|
+
* that method signs in-process with `delegateKey`, and hosted Haven does not
|
|
2230
|
+
* have one and must not. The hosted MCP server drives these two halves with
|
|
2231
|
+
* the LOCAL signer in between, so the key stays where it belongs and the
|
|
2232
|
+
* request shaping stays in one place rather than being reimplemented.
|
|
2233
|
+
*/
|
|
2234
|
+
async prepare(paymentRequired, options = {}) {
|
|
2235
|
+
const delegationRail = options.delegationRail ?? (await this.getAgent()).executionRail === "delegation";
|
|
2236
|
+
if (!delegationRail) {
|
|
2237
|
+
throw new HavenApiError(
|
|
2238
|
+
"erc7710 settlement requires a delegation-rail account; this one is not on it. Use authorizeX402() for the standard EIP-3009 path.",
|
|
2239
|
+
400
|
|
2240
|
+
);
|
|
2241
|
+
}
|
|
2242
|
+
const selection = selectX402SettlementScheme(paymentRequired.accepts, { delegationRail });
|
|
2243
|
+
if (!selection || selection.scheme !== "erc7710") {
|
|
2244
|
+
throw new HavenApiError(
|
|
2245
|
+
"This merchant does not advertise an erc7710 settlement option (no accepts[] entry carries extra.assetTransferMethod: 'erc7710'). Use authorizeX402() for the standard EIP-3009 path.",
|
|
2246
|
+
400
|
|
2247
|
+
);
|
|
2248
|
+
}
|
|
2249
|
+
const option = selection.option;
|
|
2250
|
+
const merchantPayTo = option.payTo;
|
|
2251
|
+
const amountAtomic = x402AuthorizationAmount(option);
|
|
2252
|
+
const raw = await this.post("/x402", {
|
|
2253
|
+
url: options.resourceUrl ?? paymentRequired.resource?.url,
|
|
2254
|
+
// payTo = the MERCHANT is what selects direct settlement server-side.
|
|
2255
|
+
// The explicit settlementScheme must AGREE with that shape (#1360) —
|
|
2256
|
+
// disagreement is a 400 by design, so that a stale delegate address
|
|
2257
|
+
// becomes a loud mismatch instead of a silent reroute to the 3009 leg.
|
|
2258
|
+
payTo: merchantPayTo,
|
|
2259
|
+
settlementScheme: "erc7710",
|
|
2260
|
+
amount: amountAtomic,
|
|
2261
|
+
asset: option.asset,
|
|
2262
|
+
network: option.network,
|
|
2263
|
+
// The v2 header echoes the accepted entry field-for-field, so the quoted
|
|
2264
|
+
// timeout must round-trip or the merchant rejects the echo (#1064).
|
|
2265
|
+
maxTimeoutSeconds: option.maxTimeoutSeconds,
|
|
2266
|
+
// #1058: forward the advertised facilitators verbatim — the child becomes
|
|
2267
|
+
// redeemable ONLY by them. `null` here means the merchant advertised none
|
|
2268
|
+
// (or an empty array, which the backend 400s on), so the field is OMITTED
|
|
2269
|
+
// rather than sent empty. See x402FacilitatorAddresses.
|
|
2270
|
+
...selection.facilitatorAddresses ? { facilitatorAddresses: selection.facilitatorAddresses } : {},
|
|
2271
|
+
// #1307/#1547: persisted so the settle leg can rehydrate the merchant
|
|
2272
|
+
// call by payment_id on this scheme too, not only on the 3009 bridge.
|
|
2273
|
+
...options.mcpCallContext ? { mcpCallContext: options.mcpCallContext } : {}
|
|
2274
|
+
});
|
|
2275
|
+
if (!raw.payment_id) {
|
|
2276
|
+
throw new HavenApiError("No payment_id returned from x402/authorize", 500, raw);
|
|
2277
|
+
}
|
|
2278
|
+
const signData = raw.sign_data;
|
|
2279
|
+
if (signData?.signature_scheme !== "eip712_delegation" || !signData.typed_data) {
|
|
2280
|
+
throw new HavenApiError(
|
|
2281
|
+
`x402/authorize did not return an erc7710 settlement child (signature_scheme was ${JSON.stringify(signData?.signature_scheme)}). Refusing to sign a payload this path did not ask for.`,
|
|
2282
|
+
500,
|
|
2283
|
+
raw
|
|
2284
|
+
);
|
|
2285
|
+
}
|
|
2286
|
+
return {
|
|
2287
|
+
paymentId: raw.payment_id,
|
|
2288
|
+
signData,
|
|
2289
|
+
settlement: {
|
|
2290
|
+
paymentId: raw.payment_id,
|
|
2291
|
+
merchantPayTo,
|
|
2292
|
+
amountAtomic,
|
|
2293
|
+
asset: option.asset,
|
|
2294
|
+
network: option.network,
|
|
2295
|
+
facilitatorAddresses: selection.facilitatorAddresses
|
|
2296
|
+
}
|
|
2297
|
+
};
|
|
2298
|
+
}
|
|
2299
|
+
/**
|
|
2300
|
+
* The SETTLE half (#1456): exchange the signed child for the merchant header.
|
|
2301
|
+
*
|
|
2302
|
+
* The SDK builds no header on this path — the backend assembles the MetaMask
|
|
2303
|
+
* erc7710 payload in `assembleSettlementPayload`. Whoever produced the
|
|
2304
|
+
* signature (an in-process delegate key, or the local edge signer over the
|
|
2305
|
+
* hosted boundary) is irrelevant here.
|
|
2306
|
+
*/
|
|
2307
|
+
async submit(paymentId, signature) {
|
|
2308
|
+
const settled = await this.post(
|
|
2309
|
+
`/x402/${paymentId}/settle`,
|
|
2310
|
+
{ signature }
|
|
2311
|
+
);
|
|
2312
|
+
if (!settled.payment_header) {
|
|
2313
|
+
throw new HavenApiError(
|
|
2314
|
+
"x402 settle returned no payment_header \u2014 the merchant cannot be retried.",
|
|
2315
|
+
500,
|
|
2316
|
+
settled
|
|
2317
|
+
);
|
|
2318
|
+
}
|
|
2319
|
+
return settled.payment_header;
|
|
2320
|
+
}
|
|
2321
|
+
};
|
|
2322
|
+
|
|
2323
|
+
// src/tool-adapter.ts
|
|
2324
|
+
function toolX402PaymentRequired(input) {
|
|
2325
|
+
return {
|
|
2326
|
+
x402Version: 2,
|
|
2327
|
+
resource: { url: input.url, description: input.description },
|
|
2328
|
+
accepts: [
|
|
2329
|
+
{
|
|
2330
|
+
scheme: "exact",
|
|
2331
|
+
network: input.network,
|
|
2332
|
+
amount: input.amount,
|
|
2333
|
+
asset: input.asset,
|
|
2334
|
+
payTo: input.payTo,
|
|
2335
|
+
maxTimeoutSeconds: 30
|
|
2336
|
+
}
|
|
2337
|
+
]
|
|
2338
|
+
};
|
|
2339
|
+
}
|
|
2340
|
+
function x402ToolReceipt(receipt) {
|
|
2341
|
+
return {
|
|
2342
|
+
success: true,
|
|
2343
|
+
payment_id: receipt.paymentId,
|
|
2344
|
+
tx_hash: receipt.txHash,
|
|
2345
|
+
token: receipt.token,
|
|
2346
|
+
amount: receipt.amount,
|
|
2347
|
+
to: receipt.to,
|
|
2348
|
+
resource_url: receipt.resourceUrl,
|
|
2349
|
+
explorer_url: receipt.explorerUrl,
|
|
2350
|
+
payment_header: receipt.paymentHeader,
|
|
2351
|
+
merchant_to: receipt.merchantTo,
|
|
2352
|
+
payer: receipt.payer,
|
|
2353
|
+
chain_id: receipt.chainId,
|
|
2354
|
+
haven: receipt.haven,
|
|
2355
|
+
merchant: receipt.merchant,
|
|
2356
|
+
x402: receipt.x402
|
|
2357
|
+
};
|
|
2358
|
+
}
|
|
2359
|
+
function toolError(err) {
|
|
2360
|
+
if (err instanceof HavenPaymentStateError) {
|
|
2361
|
+
return {
|
|
2362
|
+
success: false,
|
|
2363
|
+
payment_id: err.state.paymentId,
|
|
2364
|
+
kind: err.state.kind,
|
|
2365
|
+
rail: err.state.rail,
|
|
2366
|
+
status: err.state.status,
|
|
2367
|
+
phase: err.state.phase,
|
|
2368
|
+
next_action: err.state.nextAction,
|
|
2369
|
+
tx_hash: err.state.txHash,
|
|
2370
|
+
token: err.state.token,
|
|
2371
|
+
amount: err.state.amount,
|
|
2372
|
+
resource_url: err.state.resourceUrl,
|
|
2373
|
+
merchant_address: err.state.merchantAddress,
|
|
2374
|
+
amount_atomic: err.state.amountAtomic,
|
|
2375
|
+
asset: err.state.asset,
|
|
2376
|
+
network: err.state.network,
|
|
2377
|
+
description: err.state.description,
|
|
2378
|
+
idempotency_key: err.state.idempotencyKey,
|
|
2379
|
+
x402: err.state.x402 ? {
|
|
2380
|
+
amount_atomic: err.state.x402.amountAtomic,
|
|
2381
|
+
asset: err.state.x402.asset,
|
|
2382
|
+
network: err.state.x402.network,
|
|
2383
|
+
resource_url: err.state.x402.resourceUrl,
|
|
2384
|
+
merchant_address: err.state.x402.merchantAddress,
|
|
2385
|
+
description: err.state.x402.description,
|
|
2386
|
+
idempotency_key: err.state.x402.idempotencyKey
|
|
2387
|
+
} : void 0,
|
|
2388
|
+
mpp: err.state.mpp ? {
|
|
2389
|
+
amount_atomic: err.state.mpp.amountAtomic,
|
|
2390
|
+
asset: err.state.mpp.asset,
|
|
2391
|
+
network: err.state.mpp.network,
|
|
2392
|
+
resource_url: err.state.mpp.resourceUrl,
|
|
2393
|
+
merchant_address: err.state.mpp.merchantAddress,
|
|
2394
|
+
description: err.state.mpp.description,
|
|
2395
|
+
idempotency_key: err.state.mpp.idempotencyKey,
|
|
2396
|
+
challenge_id: err.state.mpp.challengeId
|
|
2397
|
+
} : void 0,
|
|
2398
|
+
resume_state: err.resumeState,
|
|
2399
|
+
expires_at: err.state.expiresAt,
|
|
2400
|
+
chain_id: err.state.chainId,
|
|
2401
|
+
message: err.state.message,
|
|
2402
|
+
error: err.message
|
|
2403
|
+
};
|
|
2404
|
+
}
|
|
2405
|
+
if (err instanceof HavenApiError) {
|
|
2406
|
+
return {
|
|
2407
|
+
success: false,
|
|
2408
|
+
status_code: err.statusCode,
|
|
2409
|
+
error: err.message,
|
|
2410
|
+
body: err.body
|
|
2411
|
+
};
|
|
2412
|
+
}
|
|
2413
|
+
return {
|
|
2414
|
+
success: false,
|
|
2415
|
+
error: err instanceof Error ? err.message : String(err)
|
|
2416
|
+
};
|
|
2417
|
+
}
|
|
2418
|
+
|
|
2419
|
+
// src/merchant-completion.ts
|
|
2420
|
+
var MERCHANT_BODY_SNIPPET_LIMIT = 1e3;
|
|
2421
|
+
var MerchantCompletion = class {
|
|
2422
|
+
post;
|
|
2423
|
+
merchantTransport;
|
|
2424
|
+
getPaymentStatus;
|
|
2425
|
+
getAgent;
|
|
2426
|
+
delegateAddress;
|
|
2427
|
+
x402Wallet;
|
|
2428
|
+
constructor(options) {
|
|
2429
|
+
this.post = options.post;
|
|
2430
|
+
this.merchantTransport = options.merchantTransport;
|
|
2431
|
+
this.getPaymentStatus = options.getPaymentStatus;
|
|
2432
|
+
this.getAgent = options.getAgent;
|
|
2433
|
+
this.delegateAddress = options.delegateAddress;
|
|
2434
|
+
this.x402Wallet = options.x402Wallet;
|
|
2435
|
+
}
|
|
2436
|
+
async retryRequest(url, initialInit, paymentRequired, receipt) {
|
|
2437
|
+
if (!receipt.accepted) {
|
|
2438
|
+
throw new HavenApiError("No accepted x402 option was recorded for payment retry", 500);
|
|
2439
|
+
}
|
|
2440
|
+
if (!receipt.paymentHeader) {
|
|
2441
|
+
throw new HavenApiError("No x402 payment header was returned for payment retry", 500);
|
|
2442
|
+
}
|
|
2443
|
+
const retryResponse = await this.merchantTransport.deliverPayment(
|
|
2444
|
+
url,
|
|
2445
|
+
initialInit,
|
|
2446
|
+
receipt.paymentHeader
|
|
2447
|
+
);
|
|
2448
|
+
if (!retryResponse.ok) {
|
|
2449
|
+
const merchant = await captureMerchantResponse(retryResponse);
|
|
2450
|
+
await this.recordRetryRejected({
|
|
2451
|
+
rail: "x402",
|
|
2452
|
+
paymentId: receipt.paymentId,
|
|
2453
|
+
txHash: receipt.txHash,
|
|
2454
|
+
resourceUrl: receipt.resourceUrl,
|
|
2455
|
+
merchant,
|
|
2456
|
+
details: {
|
|
2457
|
+
merchant_to: receipt.merchantTo,
|
|
2458
|
+
delegate_to: receipt.to
|
|
2459
|
+
}
|
|
2460
|
+
});
|
|
2461
|
+
throw new HavenApiError(
|
|
2462
|
+
"x402 retry failed after Haven funded the delegate wallet; reconciliation may be required.",
|
|
2463
|
+
merchant.merchant_status,
|
|
2464
|
+
{
|
|
2465
|
+
marker: "x402_retry_rejected_after_funding",
|
|
2466
|
+
payment_id: receipt.paymentId,
|
|
2467
|
+
tx_hash: receipt.txHash,
|
|
2468
|
+
resource_url: receipt.resourceUrl,
|
|
2469
|
+
merchant_to: receipt.merchantTo,
|
|
2470
|
+
delegate_to: receipt.to,
|
|
2471
|
+
...merchant
|
|
2472
|
+
}
|
|
2473
|
+
);
|
|
2474
|
+
}
|
|
2475
|
+
const merchantSettlement = parseMerchantSettlement(retryResponse.headers.get("PAYMENT-RESPONSE"));
|
|
2476
|
+
if (receipt.merchant && merchantSettlement.settlementTxHash) {
|
|
2477
|
+
receipt.merchant.settlementTxHash = merchantSettlement.settlementTxHash;
|
|
2478
|
+
receipt.merchant.settlementExplorerUrl = buildExplorerUrl(
|
|
2479
|
+
receipt.chainId,
|
|
2480
|
+
merchantSettlement.settlementTxHash
|
|
2481
|
+
);
|
|
2482
|
+
}
|
|
2483
|
+
await this.reportEvidence({
|
|
2484
|
+
paymentId: receipt.paymentId,
|
|
2485
|
+
rail: "x402",
|
|
2486
|
+
txHash: receipt.txHash,
|
|
2487
|
+
resourceUrl: receipt.resourceUrl,
|
|
2488
|
+
merchantStatus: retryResponse.status,
|
|
2489
|
+
challengePayload: paymentRequired,
|
|
2490
|
+
selectedPayment: receipt.accepted,
|
|
2491
|
+
paymentProofHeaderName: "X-PAYMENT",
|
|
2492
|
+
paymentProofHeader: receipt.paymentHeader,
|
|
2493
|
+
protocolReceiptHeaderName: "PAYMENT-RESPONSE",
|
|
2494
|
+
protocolReceiptHeader: retryResponse.headers.get("PAYMENT-RESPONSE") ?? void 0
|
|
2495
|
+
});
|
|
2496
|
+
await this.reportMerchantReceipt(receipt.paymentId, retryResponse);
|
|
2497
|
+
return retryResponse;
|
|
2498
|
+
}
|
|
2499
|
+
/**
|
|
2500
|
+
* #956: capture the merchant's OWN receipt when the paid response carries
|
|
2501
|
+
* one, and report it to Haven so the reporting feed can attach it next to
|
|
2502
|
+
* the Haven-generated payment evidence (#498). Two supported signals on the
|
|
2503
|
+
* paid response:
|
|
2504
|
+
*
|
|
2505
|
+
* x-receipt-json: base64-encoded JSON receipt document (inline)
|
|
2506
|
+
* x-receipt-url: https URL to the receipt document (reference)
|
|
2507
|
+
*
|
|
2508
|
+
* Strictly best-effort: absence is the normal case, and no failure here may
|
|
2509
|
+
* ever affect the completed payment — the response is already paid for.
|
|
2510
|
+
*/
|
|
2511
|
+
async reportMerchantReceipt(paymentId, response) {
|
|
2512
|
+
try {
|
|
2513
|
+
const inlineB64 = response.headers.get("x-receipt-json");
|
|
2514
|
+
const url = response.headers.get("x-receipt-url");
|
|
2515
|
+
if (!inlineB64 && !url) return;
|
|
2516
|
+
let body = null;
|
|
2517
|
+
if (inlineB64) {
|
|
2518
|
+
if (inlineB64.length > Math.ceil(64 * 1024 * 4 / 3)) return;
|
|
2519
|
+
const decoded = JSON.parse(Buffer.from(inlineB64, "base64").toString("utf8"));
|
|
2520
|
+
if (decoded && typeof decoded === "object") body = { json: decoded };
|
|
2521
|
+
} else if (url && url.startsWith("https://") && url.length <= 2048) {
|
|
2522
|
+
body = { url };
|
|
2523
|
+
}
|
|
2524
|
+
if (!body) return;
|
|
2525
|
+
await this.post(`/machine-payments/${paymentId}/merchant-receipt`, body);
|
|
2526
|
+
} catch {
|
|
2527
|
+
}
|
|
2528
|
+
}
|
|
2529
|
+
async resolveCompletionContext(input) {
|
|
2530
|
+
const status = await this.getPaymentStatus(input.paymentId);
|
|
2531
|
+
if (status.rail !== "x402") {
|
|
2532
|
+
throw new HavenPaymentStateError(
|
|
2533
|
+
`Payment ${status.paymentId} is ${status.rail}, not x402.`,
|
|
2534
|
+
409,
|
|
2535
|
+
status
|
|
2536
|
+
);
|
|
2537
|
+
}
|
|
2538
|
+
const readyForMerchantCompletion = input.noFundingLeg ? status.kind === "payment_intent" && status.status === "submitted" : status.nextAction === AgentPaymentNextAction.RetryOriginalX402Request || status.kind === "payment_intent" && status.status === "confirmed" && status.phase === AgentPaymentPhase.PaymentConfirmed && status.nextAction === AgentPaymentNextAction.None;
|
|
2539
|
+
if (!readyForMerchantCompletion) {
|
|
2540
|
+
throw new HavenPaymentStateError(status.message, paymentStateStatusCode(status.status, 409), status);
|
|
2541
|
+
}
|
|
2542
|
+
if (!input.noFundingLeg && !status.txHash) {
|
|
2543
|
+
throw new HavenApiError(
|
|
2544
|
+
`x402 payment ${status.paymentId} is ready for merchant completion but has no Haven transaction hash.`,
|
|
2545
|
+
502,
|
|
2546
|
+
status,
|
|
2547
|
+
status.paymentId
|
|
2548
|
+
);
|
|
2549
|
+
}
|
|
2550
|
+
const approvedResourceUrl = status.resourceUrl ?? status.x402?.resourceUrl ?? null;
|
|
2551
|
+
if (approvedResourceUrl && approvedResourceUrl !== input.url) {
|
|
2552
|
+
throw new HavenApiError(
|
|
2553
|
+
"x402 merchant completion does not match the approved resource URL.",
|
|
2554
|
+
409,
|
|
2555
|
+
{ status, url: input.url },
|
|
2556
|
+
status.paymentId
|
|
2557
|
+
);
|
|
2558
|
+
}
|
|
2559
|
+
return {
|
|
2560
|
+
paymentId: status.paymentId,
|
|
2561
|
+
txHash: status.txHash,
|
|
2562
|
+
resourceUrl: approvedResourceUrl ?? input.url,
|
|
2563
|
+
merchantAddress: status.merchantAddress ?? status.x402?.merchantAddress ?? null
|
|
2564
|
+
};
|
|
2565
|
+
}
|
|
2566
|
+
async resolveWalletForMerchantCall() {
|
|
2567
|
+
const localWallet = x402PayerAddress(this.delegateAddress, this.x402Wallet);
|
|
2568
|
+
if (localWallet) return localWallet;
|
|
2569
|
+
try {
|
|
2570
|
+
const agent = await this.getAgent();
|
|
2571
|
+
return agent.delegateAddress ?? void 0;
|
|
2572
|
+
} catch {
|
|
2573
|
+
return void 0;
|
|
2574
|
+
}
|
|
2575
|
+
}
|
|
2576
|
+
// #1328: authorizeMachinePayment / authorizeMppDemoPayment / resumeAuthorizedMpp
|
|
2577
|
+
// / resumeMppPayment / fetchWithMachinePayment / retryMppRequest (the
|
|
2578
|
+
// MACHINE-PAYMENT-CHALLENGE / mpp_demo client surface) are retired — the
|
|
2579
|
+
// backend's POST /machine-payments/authorize refuses unconditionally now,
|
|
2580
|
+
// and MACHINE-PAYMENT-CHALLENGE was never produced by any other Haven
|
|
2581
|
+
// surface. Use the x402 flow (authorizeX402 / fetch / quoteX402 / payX402Quote)
|
|
2582
|
+
// for agent-to-merchant payments.
|
|
2583
|
+
async recordRetryRejected(input) {
|
|
2584
|
+
try {
|
|
2585
|
+
await this.post("/machine-payments/reconciliation-events", {
|
|
2586
|
+
paymentId: input.paymentId,
|
|
2587
|
+
rail: input.rail,
|
|
2588
|
+
eventType: "merchant_retry_rejected_after_payment",
|
|
2589
|
+
txHash: input.txHash,
|
|
2590
|
+
reason: `Merchant returned HTTP ${input.merchant.merchant_status} after Haven payment confirmation`,
|
|
2591
|
+
details: {
|
|
2592
|
+
resource_url: input.resourceUrl,
|
|
2593
|
+
retry_status: input.merchant.merchant_status,
|
|
2594
|
+
retry_body: input.merchant.merchant_body.slice(0, MERCHANT_BODY_SNIPPET_LIMIT) || null,
|
|
2595
|
+
...input.details
|
|
2596
|
+
}
|
|
2597
|
+
});
|
|
2598
|
+
} catch {
|
|
2599
|
+
}
|
|
2600
|
+
}
|
|
2601
|
+
async reportEvidence(input) {
|
|
2602
|
+
try {
|
|
2603
|
+
await this.post("/machine-payments/evidence", {
|
|
2604
|
+
paymentId: input.paymentId,
|
|
2605
|
+
rail: input.rail,
|
|
2606
|
+
txHash: input.txHash,
|
|
2607
|
+
resourceUrl: input.resourceUrl,
|
|
2608
|
+
merchantStatus: input.merchantStatus,
|
|
2609
|
+
challengePayload: input.challengePayload,
|
|
2610
|
+
selectedPayment: input.selectedPayment,
|
|
2611
|
+
paymentProofHeaderName: input.paymentProofHeaderName,
|
|
2612
|
+
paymentProofHeader: input.paymentProofHeader,
|
|
2613
|
+
protocolReceiptHeaderName: input.protocolReceiptHeaderName,
|
|
2614
|
+
protocolReceiptHeader: input.protocolReceiptHeader,
|
|
2615
|
+
protocolReceiptPayload: input.protocolReceiptHeader ? parseProtocolReceiptHeader(input.protocolReceiptHeader) : void 0
|
|
2616
|
+
});
|
|
2617
|
+
} catch {
|
|
2618
|
+
}
|
|
2619
|
+
}
|
|
2620
|
+
};
|
|
2621
|
+
function parseMerchantSettlement(header) {
|
|
2622
|
+
if (!header) return {};
|
|
2623
|
+
const parsed = parseProtocolReceiptHeader(header);
|
|
2624
|
+
const tx = typeof parsed?.transaction === "string" ? parsed.transaction : typeof parsed?.txHash === "string" ? parsed.txHash : typeof parsed?.tx_hash === "string" ? parsed.tx_hash : null;
|
|
2625
|
+
return { settlementTxHash: tx };
|
|
2626
|
+
}
|
|
2627
|
+
function parseProtocolReceiptHeader(value) {
|
|
2628
|
+
try {
|
|
2629
|
+
return decodeBase64Json(value);
|
|
2630
|
+
} catch {
|
|
2631
|
+
try {
|
|
2632
|
+
return JSON.parse(value);
|
|
2633
|
+
} catch {
|
|
2634
|
+
return void 0;
|
|
2635
|
+
}
|
|
2636
|
+
}
|
|
2637
|
+
}
|
|
2638
|
+
|
|
2639
|
+
// src/client.ts
|
|
2640
|
+
var DEFAULT_CONFIRMATION_TIMEOUT = 9e4;
|
|
2641
|
+
var DEFAULT_POLLING_INTERVAL = 3e3;
|
|
1140
2642
|
function x402TypedDataDigest(typedData) {
|
|
1141
2643
|
if (!typedData || typeof typedData !== "object") return void 0;
|
|
1142
2644
|
try {
|
|
@@ -1167,48 +2669,78 @@ function mapCatalogEntry(entry) {
|
|
|
1167
2669
|
};
|
|
1168
2670
|
}
|
|
1169
2671
|
var HavenClient = class {
|
|
1170
|
-
apiKey;
|
|
1171
2672
|
delegateKey;
|
|
1172
|
-
|
|
2673
|
+
havenApi;
|
|
2674
|
+
accountReads;
|
|
2675
|
+
delegateSweep;
|
|
1173
2676
|
x402Wallet;
|
|
1174
|
-
|
|
1175
|
-
merchantTimeout;
|
|
2677
|
+
merchantTransport;
|
|
1176
2678
|
confirmationTimeout;
|
|
1177
2679
|
pollingInterval;
|
|
1178
2680
|
chainRpcs;
|
|
1179
2681
|
inFlightX402 = /* @__PURE__ */ new Map();
|
|
1180
|
-
x402ReceiptCache = /* @__PURE__ */ new Map();
|
|
1181
2682
|
/**
|
|
1182
|
-
*
|
|
1183
|
-
*
|
|
1184
|
-
* scoping so concurrent requests don't race on shared mutable state.
|
|
2683
|
+
* The EIP-3009 funding-leg lifecycle (#1618). The facade holds a reference
|
|
2684
|
+
* and delegates; it does not reimplement any of it.
|
|
1185
2685
|
*/
|
|
1186
|
-
|
|
2686
|
+
fundingLeg;
|
|
1187
2687
|
/**
|
|
1188
|
-
*
|
|
1189
|
-
*
|
|
1190
|
-
* overlapping async work — like two MCP tool dispatches in flight at
|
|
1191
|
-
* the same time — see their own headers without stepping on each other.
|
|
2688
|
+
* The erc7710 direct-settlement lifecycle (#1619). Separate from the funding
|
|
2689
|
+
* leg on purpose: this scheme has no funding leg to share.
|
|
1192
2690
|
*/
|
|
1193
|
-
|
|
1194
|
-
/**
|
|
1195
|
-
|
|
2691
|
+
erc7710;
|
|
2692
|
+
/**
|
|
2693
|
+
* Merchant delivery and the evidence trail behind it (#1620). Scheme-neutral
|
|
2694
|
+
* on purpose — both settlement schemes finish through the same door.
|
|
2695
|
+
*/
|
|
2696
|
+
merchantCompletion;
|
|
1196
2697
|
/** Delegate address derived from the private key (if provided) */
|
|
1197
2698
|
delegateAddress;
|
|
1198
2699
|
constructor(config) {
|
|
1199
|
-
this.apiKey = config.apiKey;
|
|
1200
2700
|
this.delegateKey = config.delegateKey;
|
|
1201
|
-
this.
|
|
2701
|
+
this.havenApi = new HavenApiTransport(config);
|
|
2702
|
+
this.accountReads = new AccountReads({
|
|
2703
|
+
transport: this.havenApi,
|
|
2704
|
+
getPaymentStatus: (paymentId) => this.getPaymentStatus(paymentId)
|
|
2705
|
+
});
|
|
2706
|
+
this.delegateSweep = new DelegateSweepApi({
|
|
2707
|
+
transport: this.havenApi,
|
|
2708
|
+
delegateKey: config.delegateKey,
|
|
2709
|
+
chainRpcs: config.chainRpcs ?? {},
|
|
2710
|
+
getAgent: () => this.getAgent(),
|
|
2711
|
+
buildExplorerUrl: (chainId, hash) => buildExplorerUrl(chainId, hash)
|
|
2712
|
+
});
|
|
1202
2713
|
this.x402Wallet = config.x402Wallet;
|
|
1203
|
-
this.
|
|
1204
|
-
this.merchantTimeout = config.merchantTimeout ?? DEFAULT_MERCHANT_TIMEOUT;
|
|
2714
|
+
this.merchantTransport = new McpMerchantTransport({ merchantTimeout: config.merchantTimeout });
|
|
1205
2715
|
this.confirmationTimeout = config.confirmationTimeout ?? DEFAULT_CONFIRMATION_TIMEOUT;
|
|
1206
2716
|
this.pollingInterval = config.pollingInterval ?? DEFAULT_POLLING_INTERVAL;
|
|
1207
2717
|
this.chainRpcs = config.chainRpcs ?? {};
|
|
1208
|
-
this.defaultHeaders = { ...config.defaultHeaders ?? {} };
|
|
1209
2718
|
if (this.delegateKey) {
|
|
1210
2719
|
this.delegateAddress = addressFromKey(this.delegateKey);
|
|
1211
2720
|
}
|
|
2721
|
+
this.fundingLeg = new X402FundingLeg({
|
|
2722
|
+
delegateKey: this.delegateKey,
|
|
2723
|
+
delegateAddress: this.delegateAddress,
|
|
2724
|
+
x402Wallet: this.x402Wallet,
|
|
2725
|
+
chainRpcs: this.chainRpcs,
|
|
2726
|
+
post: (path, body) => this.post(path, body),
|
|
2727
|
+
signForData: (signData) => this.signForData(signData),
|
|
2728
|
+
assertSignableAuthorizationState: (label, raw) => this.throwIfNonSignableAuthorizationState(label, raw)
|
|
2729
|
+
});
|
|
2730
|
+
this.merchantCompletion = new MerchantCompletion({
|
|
2731
|
+
post: (path, body) => this.post(path, body),
|
|
2732
|
+
merchantTransport: this.merchantTransport,
|
|
2733
|
+
getPaymentStatus: (paymentId) => this.getPaymentStatus(paymentId),
|
|
2734
|
+
getAgent: () => this.getAgent(),
|
|
2735
|
+
delegateAddress: this.delegateAddress,
|
|
2736
|
+
x402Wallet: this.x402Wallet
|
|
2737
|
+
});
|
|
2738
|
+
this.erc7710 = new X402Erc7710({
|
|
2739
|
+
delegateKey: this.delegateKey,
|
|
2740
|
+
post: (path, body) => this.post(path, body),
|
|
2741
|
+
signForData: (signData) => this.signForData(signData),
|
|
2742
|
+
getAgent: () => this.getAgent()
|
|
2743
|
+
});
|
|
1212
2744
|
}
|
|
1213
2745
|
/**
|
|
1214
2746
|
* Run `fn` with extra Haven-API headers scoped to the async work it
|
|
@@ -1226,7 +2758,7 @@ var HavenClient = class {
|
|
|
1226
2758
|
* context.
|
|
1227
2759
|
*/
|
|
1228
2760
|
withRequestContext(headers, fn) {
|
|
1229
|
-
return this.
|
|
2761
|
+
return this.havenApi.withRequestContext(headers, fn);
|
|
1230
2762
|
}
|
|
1231
2763
|
// ── High-Level API ───────────────────────────────────────────────
|
|
1232
2764
|
/**
|
|
@@ -1262,7 +2794,7 @@ var HavenClient = class {
|
|
|
1262
2794
|
...request.idempotencyKey ? { idempotency_key: request.idempotencyKey } : {}
|
|
1263
2795
|
});
|
|
1264
2796
|
if (raw.status === "pending_approval") {
|
|
1265
|
-
|
|
2797
|
+
throwPaymentStateError("Payment", raw);
|
|
1266
2798
|
}
|
|
1267
2799
|
return {
|
|
1268
2800
|
paymentId: raw.payment_id,
|
|
@@ -1325,7 +2857,7 @@ var HavenClient = class {
|
|
|
1325
2857
|
...new TextEncoder().encode(JSON.stringify(paymentRequired)).length <= 65536 ? { paymentRequired } : {}
|
|
1326
2858
|
});
|
|
1327
2859
|
if (raw.status !== "pending_signature") {
|
|
1328
|
-
|
|
2860
|
+
throwPaymentStateError("x402 payment", raw);
|
|
1329
2861
|
}
|
|
1330
2862
|
if (!raw.sign_data?.hash) {
|
|
1331
2863
|
throw new HavenApiError("No sign_hash returned from x402/authorize", 500, raw);
|
|
@@ -1446,7 +2978,7 @@ var HavenClient = class {
|
|
|
1446
2978
|
*/
|
|
1447
2979
|
async getPayment(paymentId) {
|
|
1448
2980
|
const raw = await this.get(`/payments/${paymentId}`);
|
|
1449
|
-
return
|
|
2981
|
+
return mapPaymentResult(raw, buildExplorerUrl);
|
|
1450
2982
|
}
|
|
1451
2983
|
/**
|
|
1452
2984
|
* Get agent-actionable status for a payment intent or approval request.
|
|
@@ -1456,137 +2988,35 @@ var HavenClient = class {
|
|
|
1456
2988
|
*/
|
|
1457
2989
|
async getPaymentStatus(paymentId) {
|
|
1458
2990
|
const raw = await this.get(`/machine-payments/${paymentId}/status`);
|
|
1459
|
-
return
|
|
2991
|
+
return mapPaymentStatusResult(raw);
|
|
1460
2992
|
}
|
|
1461
2993
|
/**
|
|
1462
2994
|
* Get the agent identity tied to this API key.
|
|
1463
2995
|
*/
|
|
1464
2996
|
async getAgent() {
|
|
1465
|
-
|
|
1466
|
-
const request = this.fetchAgent();
|
|
1467
|
-
this.agentInFlight = request;
|
|
1468
|
-
request.finally(() => {
|
|
1469
|
-
this.agentInFlight = null;
|
|
1470
|
-
}).catch(() => {
|
|
1471
|
-
});
|
|
1472
|
-
return request;
|
|
1473
|
-
}
|
|
1474
|
-
agentInFlight = null;
|
|
1475
|
-
async fetchAgent() {
|
|
1476
|
-
const raw = await this.get("/machine-payments/agent");
|
|
1477
|
-
return {
|
|
1478
|
-
id: raw.id,
|
|
1479
|
-
name: raw.name,
|
|
1480
|
-
status: raw.status,
|
|
1481
|
-
safeAddress: raw.safe_address,
|
|
1482
|
-
delegateAddress: raw.delegate_address,
|
|
1483
|
-
chainId: raw.chain_id,
|
|
1484
|
-
// Defensive normalization, not trust: the backend contract is exactly
|
|
1485
|
-
// 'legacy' | 'delegation' (#1306), but an older/mismatched backend
|
|
1486
|
-
// during a rollout window should degrade to the wider legacy bucket
|
|
1487
|
-
// rather than propagate an unrecognized string.
|
|
1488
|
-
executionRail: raw.execution_rail === "delegation" ? "delegation" : "legacy"
|
|
1489
|
-
};
|
|
1490
|
-
}
|
|
1491
|
-
/**
|
|
1492
|
-
* One-shot "am I ready?" bootstrap: identity + live spend authority + a
|
|
1493
|
-
* readiness signal, in a single call. Folds {@link getAgent} and
|
|
1494
|
-
* {@link getAllowances} together and derives a {@link HavenAgentReadiness}
|
|
1495
|
-
* so an agent can answer "who am I and can I pay right now" at session start
|
|
1496
|
-
* without two round trips and manual assembly.
|
|
1497
|
-
*/
|
|
1498
|
-
async getAgentSummary() {
|
|
1499
|
-
const [agent, allowanceSummary] = await Promise.all([
|
|
1500
|
-
this.getAgent(),
|
|
1501
|
-
this.getAllowances()
|
|
1502
|
-
]);
|
|
1503
|
-
const allowances = allowanceSummary.allowances.map((a) => {
|
|
1504
|
-
const token = resolveTokenFromAddress(a.tokenAddress);
|
|
1505
|
-
const remainingDisplay = token ? `${formatAtomicAmount(safeBigInt(a.onchain.remaining), token.decimals)} ${a.tokenSymbol}` : `${a.onchain.remaining} ${a.tokenSymbol} (atomic; unknown decimals)`;
|
|
1506
|
-
return {
|
|
1507
|
-
tokenSymbol: a.tokenSymbol,
|
|
1508
|
-
remainingAtomic: a.onchain.remaining,
|
|
1509
|
-
remainingDisplay,
|
|
1510
|
-
configuredAmount: a.configuredAmount,
|
|
1511
|
-
resetPeriodMin: a.resetPeriodMin,
|
|
1512
|
-
isResetPending: a.onchain.isResetPending
|
|
1513
|
-
};
|
|
1514
|
-
});
|
|
1515
|
-
const readiness = deriveReadiness(agent.status, allowances);
|
|
1516
|
-
return { ...agent, readiness, spend_authority_readiness: readiness, allowances };
|
|
2997
|
+
return this.accountReads.getAgent();
|
|
1517
2998
|
}
|
|
1518
2999
|
/**
|
|
1519
|
-
*
|
|
1520
|
-
*
|
|
1521
|
-
*
|
|
1522
|
-
*
|
|
1523
|
-
*
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
`chainRpcs[${chainId}] must be configured to sweep the delegate wallet.`,
|
|
1540
|
-
422
|
|
1541
|
-
);
|
|
1542
|
-
}
|
|
1543
|
-
const provider = createJsonRpcProvider(rpcUrl);
|
|
1544
|
-
const wallet = createWallet(this.delegateKey, provider);
|
|
1545
|
-
const ERC20_TRANSFER_ABI = ["function balanceOf(address) view returns (uint256)", "function transfer(address to, uint256 amount) returns (bool)"];
|
|
1546
|
-
const transfers = [];
|
|
1547
|
-
const usdcAddress = CHAIN_USDC[chainId];
|
|
1548
|
-
if (usdcAddress) {
|
|
1549
|
-
const usdcContract = createErc20Contract(usdcAddress, ERC20_TRANSFER_ABI, wallet);
|
|
1550
|
-
const usdcBalance = await usdcContract.balanceOf(delegateAddress);
|
|
1551
|
-
if (usdcBalance > 0n) {
|
|
1552
|
-
const tx = await usdcContract.transfer(safeAddress, usdcBalance);
|
|
1553
|
-
const receipt = await tx.wait(1);
|
|
1554
|
-
const txHash = receipt?.hash ?? tx.hash;
|
|
1555
|
-
transfers.push({
|
|
1556
|
-
asset: "USDC",
|
|
1557
|
-
amount: formatAtomicAmount(usdcBalance, 6),
|
|
1558
|
-
amountAtomic: usdcBalance.toString(),
|
|
1559
|
-
txHash,
|
|
1560
|
-
explorerUrl: buildExplorerUrl(chainId, txHash)
|
|
1561
|
-
});
|
|
1562
|
-
}
|
|
1563
|
-
}
|
|
1564
|
-
const ethBalance = await provider.getBalance(delegateAddress);
|
|
1565
|
-
if (ethBalance > 0n) {
|
|
1566
|
-
const fee = await provider.getFeeData();
|
|
1567
|
-
const effectiveGasPrice = fee.maxFeePerGas ?? fee.gasPrice ?? 1000000n;
|
|
1568
|
-
const gasLimit = 21000n;
|
|
1569
|
-
const gasCost = effectiveGasPrice * gasLimit * 2n;
|
|
1570
|
-
const ethToSend = ethBalance > gasCost ? ethBalance - gasCost : 0n;
|
|
1571
|
-
if (ethToSend > 0n) {
|
|
1572
|
-
const tx = await wallet.sendTransaction({ to: safeAddress, value: ethToSend });
|
|
1573
|
-
const receipt = await tx.wait(1);
|
|
1574
|
-
const txHash = receipt?.hash ?? tx.hash;
|
|
1575
|
-
transfers.push({
|
|
1576
|
-
asset: "ETH",
|
|
1577
|
-
amount: formatAtomicAmount(ethToSend, 18),
|
|
1578
|
-
amountAtomic: ethToSend.toString(),
|
|
1579
|
-
txHash,
|
|
1580
|
-
explorerUrl: buildExplorerUrl(chainId, txHash)
|
|
1581
|
-
});
|
|
1582
|
-
}
|
|
1583
|
-
}
|
|
1584
|
-
return {
|
|
1585
|
-
fromAddress: delegateAddress,
|
|
1586
|
-
toAddress: safeAddress,
|
|
1587
|
-
chainId,
|
|
1588
|
-
transfers
|
|
1589
|
-
};
|
|
3000
|
+
* One-shot "am I ready?" bootstrap: identity + live spend authority + a
|
|
3001
|
+
* readiness signal, in a single call. Folds {@link getAgent} and
|
|
3002
|
+
* {@link getAllowances} together and derives a {@link HavenAgentReadiness}
|
|
3003
|
+
* so an agent can answer "who am I and can I pay right now" at session start
|
|
3004
|
+
* without two round trips and manual assembly.
|
|
3005
|
+
*/
|
|
3006
|
+
async getAgentSummary() {
|
|
3007
|
+
return this.accountReads.getAgentSummary();
|
|
3008
|
+
}
|
|
3009
|
+
/**
|
|
3010
|
+
* Sweep stranded USDC and ETH from the delegate EOA back to the originating Safe.
|
|
3011
|
+
*
|
|
3012
|
+
* The delegate key held by this client signs and submits the transfer transactions
|
|
3013
|
+
* directly — Haven's backend never handles the key or constructs signed txs
|
|
3014
|
+
* (CASP/MiCA Red Line #2). Funds always go to the Safe linked to this agent.
|
|
3015
|
+
*
|
|
3016
|
+
* Requires `chainRpcs` to be set for the agent's chain in `HavenClientConfig`.
|
|
3017
|
+
*/
|
|
3018
|
+
async sweepDelegate() {
|
|
3019
|
+
return this.delegateSweep.sweepDelegate();
|
|
1590
3020
|
}
|
|
1591
3021
|
/**
|
|
1592
3022
|
* Hosted (keyless) split-signer sweep — step 1 of 2.
|
|
@@ -1597,7 +3027,7 @@ var HavenClient = class {
|
|
|
1597
3027
|
* edge signer's `haven_sign_sweep_delegate`. No key is required on this client.
|
|
1598
3028
|
*/
|
|
1599
3029
|
async prepareSweep() {
|
|
1600
|
-
return this.
|
|
3030
|
+
return this.delegateSweep.prepareSweep();
|
|
1601
3031
|
}
|
|
1602
3032
|
/**
|
|
1603
3033
|
* Hosted (keyless) split-signer sweep — step 2 of 2.
|
|
@@ -1607,40 +3037,13 @@ var HavenClient = class {
|
|
|
1607
3037
|
* the key.
|
|
1608
3038
|
*/
|
|
1609
3039
|
async submitSweep(authorization, signature) {
|
|
1610
|
-
return this.
|
|
1611
|
-
authorization,
|
|
1612
|
-
signature
|
|
1613
|
-
});
|
|
3040
|
+
return this.delegateSweep.submitSweep(authorization, signature);
|
|
1614
3041
|
}
|
|
1615
3042
|
/**
|
|
1616
3043
|
* Get configured and on-chain allowances for the authenticated agent.
|
|
1617
3044
|
*/
|
|
1618
3045
|
async getAllowances() {
|
|
1619
|
-
|
|
1620
|
-
return {
|
|
1621
|
-
agentId: raw.agent_id,
|
|
1622
|
-
safeAddress: raw.safe_address,
|
|
1623
|
-
delegateAddress: raw.delegate_address,
|
|
1624
|
-
chainId: raw.chain_id,
|
|
1625
|
-
allowances: raw.allowances.map((allowance) => ({
|
|
1626
|
-
id: allowance.id,
|
|
1627
|
-
tokenAddress: allowance.token_address,
|
|
1628
|
-
tokenSymbol: allowance.token_symbol,
|
|
1629
|
-
configuredAmount: allowance.configured_amount,
|
|
1630
|
-
resetPeriodMin: allowance.reset_period_min,
|
|
1631
|
-
onchain: {
|
|
1632
|
-
amount: allowance.onchain.amount,
|
|
1633
|
-
spent: allowance.onchain.spent,
|
|
1634
|
-
remaining: allowance.onchain.remaining,
|
|
1635
|
-
effectiveSpent: allowance.onchain.effective_spent,
|
|
1636
|
-
resetTimeMin: allowance.onchain.reset_time_min,
|
|
1637
|
-
lastResetMin: allowance.onchain.last_reset_min,
|
|
1638
|
-
nonce: allowance.onchain.nonce,
|
|
1639
|
-
isResetPending: allowance.onchain.is_reset_pending,
|
|
1640
|
-
remainingIsFromChain: allowance.onchain.remaining_is_from_chain
|
|
1641
|
-
}
|
|
1642
|
-
}))
|
|
1643
|
-
};
|
|
3046
|
+
return this.accountReads.getAllowances();
|
|
1644
3047
|
}
|
|
1645
3048
|
/**
|
|
1646
3049
|
* Post-purchase allowance/budget summary for a settled payment (#1310).
|
|
@@ -1671,62 +3074,7 @@ var HavenClient = class {
|
|
|
1671
3074
|
* phrase it as guaranteed-fresh.
|
|
1672
3075
|
*/
|
|
1673
3076
|
async getPostPurchaseAllowanceSummary(paymentId) {
|
|
1674
|
-
|
|
1675
|
-
payment: null,
|
|
1676
|
-
allowance: null,
|
|
1677
|
-
warnings: [
|
|
1678
|
-
{
|
|
1679
|
-
code: AgentPaymentWarningCode.AllowanceCheckUnavailable,
|
|
1680
|
-
message: `Could not read the post-purchase allowance/budget for payment ${paymentId} (${detail}). The payment itself succeeded \u2014 the on-chain policy remains the actual spend gate; this only affects the remaining-budget figure reported here.`
|
|
1681
|
-
}
|
|
1682
|
-
]
|
|
1683
|
-
});
|
|
1684
|
-
const [statusResult, agentResult, allowanceResult] = await Promise.allSettled([
|
|
1685
|
-
this.getPaymentStatus(paymentId),
|
|
1686
|
-
this.getAgent(),
|
|
1687
|
-
this.getAllowances()
|
|
1688
|
-
]);
|
|
1689
|
-
if (statusResult.status === "rejected") {
|
|
1690
|
-
return unavailable(statusResult.reason instanceof Error ? statusResult.reason.message : String(statusResult.reason));
|
|
1691
|
-
}
|
|
1692
|
-
const status = statusResult.value;
|
|
1693
|
-
if (agentResult.status === "rejected") {
|
|
1694
|
-
return { ...unavailable(agentResult.reason instanceof Error ? agentResult.reason.message : String(agentResult.reason)), payment: status };
|
|
1695
|
-
}
|
|
1696
|
-
if (allowanceResult.status === "rejected") {
|
|
1697
|
-
return { ...unavailable(allowanceResult.reason instanceof Error ? allowanceResult.reason.message : String(allowanceResult.reason)), payment: status };
|
|
1698
|
-
}
|
|
1699
|
-
try {
|
|
1700
|
-
const tokenAddress = status.asset ?? status.x402?.asset ?? null;
|
|
1701
|
-
if (!tokenAddress) {
|
|
1702
|
-
return { ...unavailable("the settled payment does not carry a resolvable token address"), payment: status };
|
|
1703
|
-
}
|
|
1704
|
-
const rail = agentResult.value.executionRail;
|
|
1705
|
-
const source = rail === "delegation" ? "active_delegations" : "allowance_module";
|
|
1706
|
-
const match = allowanceResult.value.allowances.find(
|
|
1707
|
-
(a) => a.tokenAddress.toLowerCase() === tokenAddress.toLowerCase()
|
|
1708
|
-
);
|
|
1709
|
-
if (!match) {
|
|
1710
|
-
return { ...unavailable("no allowance/budget row matches the settled token"), payment: status };
|
|
1711
|
-
}
|
|
1712
|
-
const token = resolveTokenFromAddress(match.tokenAddress);
|
|
1713
|
-
const remainingDisplay = token ? `${formatAtomicAmount(safeBigInt(match.onchain.remaining), token.decimals)} ${match.tokenSymbol}` : void 0;
|
|
1714
|
-
return {
|
|
1715
|
-
payment: status,
|
|
1716
|
-
allowance: {
|
|
1717
|
-
rail,
|
|
1718
|
-
remaining_atomic: match.onchain.remaining,
|
|
1719
|
-
...remainingDisplay ? { remaining_display: remainingDisplay } : {},
|
|
1720
|
-
token_symbol: match.tokenSymbol,
|
|
1721
|
-
token_address: match.tokenAddress,
|
|
1722
|
-
reset_period: match.resetPeriodMin,
|
|
1723
|
-
source
|
|
1724
|
-
},
|
|
1725
|
-
warnings: []
|
|
1726
|
-
};
|
|
1727
|
-
} catch (err) {
|
|
1728
|
-
return unavailable(err instanceof Error ? err.message : String(err));
|
|
1729
|
-
}
|
|
3077
|
+
return this.accountReads.getPostPurchaseAllowanceSummary(paymentId);
|
|
1730
3078
|
}
|
|
1731
3079
|
/**
|
|
1732
3080
|
* `haven_get_payment_status` convenience: fetch status and, for a
|
|
@@ -1783,9 +3131,7 @@ var HavenClient = class {
|
|
|
1783
3131
|
* List recent machine-payment receipts/evidence for bookkeeping.
|
|
1784
3132
|
*/
|
|
1785
3133
|
async listReceipts(options = {}) {
|
|
1786
|
-
|
|
1787
|
-
const raw = await this.get(`/machine-payments/receipts${query}`);
|
|
1788
|
-
return raw.receipts.map((receipt) => this.mapPaymentReceipt(receipt));
|
|
3134
|
+
return this.accountReads.listReceipts(options);
|
|
1789
3135
|
}
|
|
1790
3136
|
/**
|
|
1791
3137
|
* Fetch the verifiable receipt bundle for a settled payment and verify it
|
|
@@ -1794,10 +3140,7 @@ var HavenClient = class {
|
|
|
1794
3140
|
* authorisation, so the result is trustworthy even if the backend lied.
|
|
1795
3141
|
*/
|
|
1796
3142
|
async getReceipt(paymentId) {
|
|
1797
|
-
|
|
1798
|
-
`/payments/${paymentId}/receipt`
|
|
1799
|
-
);
|
|
1800
|
-
return { receipt, verification: verifyPaymentReceipt(receipt) };
|
|
3143
|
+
return this.accountReads.getReceipt(paymentId);
|
|
1801
3144
|
}
|
|
1802
3145
|
/**
|
|
1803
3146
|
* Rehydrate the x402 resume-state bundle for a payment id (#1328: the MPP
|
|
@@ -1850,17 +3193,16 @@ var HavenClient = class {
|
|
|
1850
3193
|
);
|
|
1851
3194
|
}
|
|
1852
3195
|
const idempotencyKey = options.idempotencyKey ?? buildX402IdempotencyKey(paymentRequired, option);
|
|
1853
|
-
const cached = this.
|
|
1854
|
-
if (cached
|
|
3196
|
+
const cached = this.fundingLeg.cachedReceipt(idempotencyKey);
|
|
3197
|
+
if (cached) return cached;
|
|
1855
3198
|
const inFlight = this.inFlightX402.get(idempotencyKey);
|
|
1856
3199
|
if (inFlight) return inFlight;
|
|
1857
|
-
const promise = this.
|
|
3200
|
+
const promise = this.fundingLeg.authorize(paymentRequired, option, idempotencyKey);
|
|
1858
3201
|
this.inFlightX402.set(idempotencyKey, promise);
|
|
1859
3202
|
try {
|
|
1860
3203
|
return await promise;
|
|
1861
3204
|
} catch (err) {
|
|
1862
|
-
|
|
1863
|
-
rail: "x402",
|
|
3205
|
+
attachResumeState(err, {
|
|
1864
3206
|
paymentRequired,
|
|
1865
3207
|
accepted: option,
|
|
1866
3208
|
idempotencyKey
|
|
@@ -1875,9 +3217,9 @@ var HavenClient = class {
|
|
|
1875
3217
|
* payment or approval request.
|
|
1876
3218
|
*/
|
|
1877
3219
|
async quoteX402(url, init, options = {}) {
|
|
1878
|
-
const initialInit =
|
|
1879
|
-
const request =
|
|
1880
|
-
const response = await this.
|
|
3220
|
+
const initialInit = withX402Wallet(init, x402PayerAddress(this.delegateAddress, this.x402Wallet));
|
|
3221
|
+
const request = snapshotX402Request(url, initialInit);
|
|
3222
|
+
const response = await this.merchantTransport.fetch(url, initialInit);
|
|
1881
3223
|
if (response.status !== 402) {
|
|
1882
3224
|
throw new X402UnexpectedStatusError(
|
|
1883
3225
|
`Expected an x402 quote response with HTTP 402, got HTTP ${response.status}.`,
|
|
@@ -1888,8 +3230,8 @@ var HavenClient = class {
|
|
|
1888
3230
|
throw new HavenApiError("quoteX402 only supports standard x402 Payment Required responses.", 400);
|
|
1889
3231
|
}
|
|
1890
3232
|
const paymentRequired = await parsePaymentRequiredResponse(response);
|
|
1891
|
-
const mcpTransport = await this.
|
|
1892
|
-
return
|
|
3233
|
+
const mcpTransport = await this.merchantTransport.detect(url, paymentRequired, response);
|
|
3234
|
+
return buildX402Quote(paymentRequired, request, options.idempotencyKey, mcpTransport);
|
|
1893
3235
|
}
|
|
1894
3236
|
/**
|
|
1895
3237
|
* Probe an MCP tool for its x402 quote without creating a payment.
|
|
@@ -1902,8 +3244,8 @@ var HavenClient = class {
|
|
|
1902
3244
|
* callers that need a plain x402 endpoint must use {@link quoteX402}.
|
|
1903
3245
|
*/
|
|
1904
3246
|
async quoteMcpX402(url, init, options = {}) {
|
|
1905
|
-
const wallet = await this.
|
|
1906
|
-
const sessionId = await this.
|
|
3247
|
+
const wallet = await this.merchantCompletion.resolveWalletForMerchantCall();
|
|
3248
|
+
const sessionId = await this.merchantTransport.initialize(url, init, wallet);
|
|
1907
3249
|
if (!sessionId) {
|
|
1908
3250
|
throw new HavenApiError(
|
|
1909
3251
|
"The merchant did not establish an MCP session before the x402 quote. No payment was created.",
|
|
@@ -1911,238 +3253,71 @@ var HavenClient = class {
|
|
|
1911
3253
|
{ mcpSessionNotEstablished: true }
|
|
1912
3254
|
);
|
|
1913
3255
|
}
|
|
1914
|
-
let requestInit =
|
|
1915
|
-
requestInit = this.
|
|
3256
|
+
let requestInit = withX402Wallet(init, wallet);
|
|
3257
|
+
requestInit = this.merchantTransport.withSessionHeaders(requestInit, sessionId);
|
|
1916
3258
|
const quote = await this.quoteX402(url, requestInit, options);
|
|
1917
3259
|
return {
|
|
1918
3260
|
...quote,
|
|
1919
3261
|
mcpTransport: quote.mcpTransport ?? { handshakeRequired: true, source: "path" }
|
|
1920
3262
|
};
|
|
1921
3263
|
}
|
|
1922
|
-
/**
|
|
1923
|
-
* Pay a previously inspected x402 quote and retry the exact captured request.
|
|
1924
|
-
*/
|
|
1925
|
-
async payX402Quote(quote, options = {}) {
|
|
1926
|
-
const idempotencyKey = options.idempotencyKey ?? quote.idempotencyKey;
|
|
1927
|
-
try {
|
|
1928
|
-
const receipt = await this.authorizeX402(quote.paymentRequired, { idempotencyKey });
|
|
1929
|
-
return this.
|
|
1930
|
-
quote.request.url,
|
|
1931
|
-
|
|
1932
|
-
quote.paymentRequired,
|
|
1933
|
-
receipt
|
|
1934
|
-
);
|
|
1935
|
-
} catch (err) {
|
|
1936
|
-
this.attachResumeState(err, {
|
|
1937
|
-
rail: "x402",
|
|
1938
|
-
paymentRequired: quote.paymentRequired,
|
|
1939
|
-
accepted: quote.accepted,
|
|
1940
|
-
idempotencyKey,
|
|
1941
|
-
request: quote.request
|
|
1942
|
-
});
|
|
1943
|
-
throw err;
|
|
1944
|
-
}
|
|
1945
|
-
}
|
|
1946
|
-
async authorizeStandardX402(paymentRequired, option, idempotencyKey) {
|
|
1947
|
-
const raw = await this.post("/x402", {
|
|
1948
|
-
url: paymentRequired.resource.url,
|
|
1949
|
-
payTo: this.delegateAddress,
|
|
1950
|
-
merchantPayTo: option.payTo,
|
|
1951
|
-
amount: x402AuthorizationAmount(option),
|
|
1952
|
-
asset: option.asset,
|
|
1953
|
-
network: option.network,
|
|
1954
|
-
description: paymentRequired.resource.description,
|
|
1955
|
-
idempotencyKey,
|
|
1956
|
-
// #1360: same explicit funding-leg declaration as createX402Intent —
|
|
1957
|
-
// this local-key path derives payTo from the key (never stale), but the
|
|
1958
|
-
// declaration keeps both writers of the 3009 shape loud-by-default.
|
|
1959
|
-
settlementScheme: "eip3009"
|
|
1960
|
-
});
|
|
1961
|
-
const state = this.paymentStateFromRaw("x402 payment", raw);
|
|
1962
|
-
const executedReplay = raw.success && raw.tx_hash ? "idempotency-collision" : state?.nextAction === AgentPaymentNextAction.RetryOriginalX402Request ? "approval-resume" : null;
|
|
1963
|
-
if (executedReplay) {
|
|
1964
|
-
const canFund = await this.delegateCanFund(
|
|
1965
|
-
raw.chain_id ?? state?.chainId ?? chainIdFromNetwork(option.network),
|
|
1966
|
-
option.asset,
|
|
1967
|
-
x402AuthorizationAmount(option)
|
|
1968
|
-
);
|
|
1969
|
-
const refuse = executedReplay === "idempotency-collision" ? canFund !== true : canFund === false;
|
|
1970
|
-
if (refuse) {
|
|
1971
|
-
const settledReceipt = state && executedReplay === "approval-resume" ? this.mapX402ReceiptFromStatus(paymentRequired, option, void 0, state) : this.mapX402ReceiptFromAuthorization(paymentRequired, option, void 0, raw);
|
|
1972
|
-
throw new X402AlreadySettledError(
|
|
1973
|
-
canFund === false ? "This x402 payment already settled \u2014 the delegate no longer holds the funds to authorize it again. To buy the same item a second time, pass a distinct `idempotencyKey`; the synthesised key intentionally collapses repeat calls for the same product within a 5-minute window so a retried request cannot pay twice." : "This x402 payment already settled, and whether the delegate can still fund a new authorization could not be verified (no `chainRpcs` entry for this chain). Refusing rather than issue an authorization that may be unfundable. To buy the same item a second time, pass a distinct `idempotencyKey`; to finish an interrupted payment, resume it by `paymentId`.",
|
|
1974
|
-
settledReceipt,
|
|
1975
|
-
canFund === false ? "settled" : "unverifiable"
|
|
1976
|
-
);
|
|
1977
|
-
}
|
|
1978
|
-
}
|
|
1979
|
-
const paymentHeader = await this.createStandardX402Header(paymentRequired, option);
|
|
1980
|
-
if (raw.success && raw.tx_hash) {
|
|
1981
|
-
const receipt2 = this.mapX402ReceiptFromAuthorization(paymentRequired, option, paymentHeader, raw);
|
|
1982
|
-
this.cacheX402Receipt(idempotencyKey, paymentHeader, receipt2);
|
|
1983
|
-
return receipt2;
|
|
1984
|
-
}
|
|
1985
|
-
if (state?.nextAction === AgentPaymentNextAction.RetryOriginalX402Request) {
|
|
1986
|
-
const receipt2 = this.mapX402ReceiptFromStatus(paymentRequired, option, paymentHeader, state);
|
|
1987
|
-
this.cacheX402Receipt(idempotencyKey, paymentHeader, receipt2);
|
|
1988
|
-
return receipt2;
|
|
1989
|
-
}
|
|
1990
|
-
this.throwIfNonSignableAuthorizationState("x402 payment", raw);
|
|
1991
|
-
if (!raw.sign_data?.hash) {
|
|
1992
|
-
throw new HavenApiError("No sign_hash returned from x402/authorize", 500, raw);
|
|
1993
|
-
}
|
|
1994
|
-
const sig = await this.signForData(raw.sign_data);
|
|
1995
|
-
const execResult = await this.post(
|
|
1996
|
-
`/payments/${raw.payment_id}/sign`,
|
|
1997
|
-
{ signature: sig }
|
|
1998
|
-
);
|
|
1999
|
-
if (execResult.status !== "confirmed") {
|
|
2000
|
-
this.throwPaymentStateError("x402 payment", execResult);
|
|
2001
|
-
}
|
|
2002
|
-
await this.waitForFundingTx(
|
|
2003
|
-
execResult.tx_hash,
|
|
2004
|
-
execResult.chain_id ?? chainIdFromNetwork(option.network)
|
|
2005
|
-
);
|
|
2006
|
-
const receipt = this.mapX402ReceiptFromAuthorization(paymentRequired, option, paymentHeader, raw, execResult);
|
|
2007
|
-
this.cacheX402Receipt(idempotencyKey, paymentHeader, receipt);
|
|
2008
|
-
return receipt;
|
|
2009
|
-
}
|
|
2010
|
-
/**
|
|
2011
|
-
* Pay a merchant through **erc7710 direct settlement** (#1454, epic #1450).
|
|
2012
|
-
*
|
|
2013
|
-
* The whole point of this path is what it does NOT do. There is no funding
|
|
2014
|
-
* leg: the merchant redeems a delegation chain and pulls from the treasury
|
|
2015
|
-
* directly, so the delegate EOA never holds the money, no sweep can strand
|
|
2016
|
-
* it, and the #713 reconciliation class does not apply. It is also why this
|
|
2017
|
-
* method is SMALLER than the 3009 path — the backend assembles the merchant
|
|
2018
|
-
* `X-PAYMENT` header in `assembleSettlementPayload`, so the SDK builds no
|
|
2019
|
-
* header locally.
|
|
2020
|
-
*
|
|
2021
|
-
* authorize (payTo = the MERCHANT) → sign the child → settle → header
|
|
2022
|
-
*
|
|
2023
|
-
* The caller then retries the merchant with that header. **Nothing has
|
|
2024
|
-
* settled when this returns** — that is why it does not return an
|
|
2025
|
-
* `X402Receipt`.
|
|
2026
|
-
*
|
|
2027
|
-
* Requires a delegation-rail account. The backend enforces that
|
|
2028
|
-
* (`validateGenericSchemeRail`), and so does this method, before building a
|
|
2029
|
-
* request the backend would only reject: an error a client can explain is
|
|
2030
|
-
* worth more than a 400 it has to decode.
|
|
2031
|
-
*
|
|
2032
|
-
* **MCP callers must pass `options.resourceUrl`.** An in-band MCP 402
|
|
2033
|
-
* challenge frequently carries no `resource` object at all, so
|
|
2034
|
-
* `paymentRequired.resource?.url` is undefined and the backend answers
|
|
2035
|
-
* "Valid url is required". The QA scenario this path was ported from falls
|
|
2036
|
-
* back to the request URL for exactly that reason — the SDK cannot, because
|
|
2037
|
-
* it never saw the request. Pass it.
|
|
2038
|
-
*/
|
|
2039
|
-
async settleX402Erc7710(paymentRequired, options = {}) {
|
|
2040
|
-
if (!this.delegateKey) {
|
|
2041
|
-
throw new HavenSigningError(
|
|
2042
|
-
"delegateKey is required for x402 payments. Pass it in the HavenClient config."
|
|
2043
|
-
);
|
|
2044
|
-
}
|
|
2045
|
-
const prepared = await this.prepareX402Erc7710(paymentRequired, options);
|
|
2046
|
-
const signature = await this.signForData(prepared.signData);
|
|
2047
|
-
const paymentHeader = await this.submitX402Erc7710(prepared.paymentId, signature);
|
|
2048
|
-
return { ...prepared.settlement, paymentHeader };
|
|
2049
|
-
}
|
|
2050
|
-
/**
|
|
2051
|
-
* The AUTHORIZE half of erc7710 settlement (#1456): select the scheme, build
|
|
2052
|
-
* the request, and return the child to be signed — without signing it.
|
|
2053
|
-
*
|
|
2054
|
-
* Split out because the hosted topology cannot use `settleX402Erc7710()`:
|
|
2055
|
-
* that method signs in-process with `delegateKey`, and hosted Haven does not
|
|
2056
|
-
* have one and must not. The hosted MCP server drives these two halves with
|
|
2057
|
-
* the LOCAL signer in between, so the key stays where it belongs and the
|
|
2058
|
-
* request shaping stays in one place rather than being reimplemented.
|
|
2059
|
-
*/
|
|
2060
|
-
async prepareX402Erc7710(paymentRequired, options = {}) {
|
|
2061
|
-
const delegationRail = options.delegationRail ?? (await this.getAgent()).executionRail === "delegation";
|
|
2062
|
-
if (!delegationRail) {
|
|
2063
|
-
throw new HavenApiError(
|
|
2064
|
-
"erc7710 settlement requires a delegation-rail account; this one is not on it. Use authorizeX402() for the standard EIP-3009 path.",
|
|
2065
|
-
400
|
|
2066
|
-
);
|
|
2067
|
-
}
|
|
2068
|
-
const selection = selectX402SettlementScheme(paymentRequired.accepts, { delegationRail });
|
|
2069
|
-
if (!selection || selection.scheme !== "erc7710") {
|
|
2070
|
-
throw new HavenApiError(
|
|
2071
|
-
"This merchant does not advertise an erc7710 settlement option (no accepts[] entry carries extra.assetTransferMethod: 'erc7710'). Use authorizeX402() for the standard EIP-3009 path.",
|
|
2072
|
-
400
|
|
2073
|
-
);
|
|
2074
|
-
}
|
|
2075
|
-
const option = selection.option;
|
|
2076
|
-
const merchantPayTo = option.payTo;
|
|
2077
|
-
const amountAtomic = x402AuthorizationAmount(option);
|
|
2078
|
-
const raw = await this.post("/x402", {
|
|
2079
|
-
url: options.resourceUrl ?? paymentRequired.resource?.url,
|
|
2080
|
-
// payTo = the MERCHANT is what selects direct settlement server-side.
|
|
2081
|
-
// The explicit settlementScheme must AGREE with that shape (#1360) —
|
|
2082
|
-
// disagreement is a 400 by design, so that a stale delegate address
|
|
2083
|
-
// becomes a loud mismatch instead of a silent reroute to the 3009 leg.
|
|
2084
|
-
payTo: merchantPayTo,
|
|
2085
|
-
settlementScheme: "erc7710",
|
|
2086
|
-
amount: amountAtomic,
|
|
2087
|
-
asset: option.asset,
|
|
2088
|
-
network: option.network,
|
|
2089
|
-
// The v2 header echoes the accepted entry field-for-field, so the quoted
|
|
2090
|
-
// timeout must round-trip or the merchant rejects the echo (#1064).
|
|
2091
|
-
maxTimeoutSeconds: option.maxTimeoutSeconds,
|
|
2092
|
-
// #1058: forward the advertised facilitators verbatim — the child becomes
|
|
2093
|
-
// redeemable ONLY by them. `null` here means the merchant advertised none
|
|
2094
|
-
// (or an empty array, which the backend 400s on), so the field is OMITTED
|
|
2095
|
-
// rather than sent empty. See x402FacilitatorAddresses.
|
|
2096
|
-
...selection.facilitatorAddresses ? { facilitatorAddresses: selection.facilitatorAddresses } : {},
|
|
2097
|
-
// #1307/#1547: persisted so the settle leg can rehydrate the merchant
|
|
2098
|
-
// call by payment_id on this scheme too, not only on the 3009 bridge.
|
|
2099
|
-
...options.mcpCallContext ? { mcpCallContext: options.mcpCallContext } : {}
|
|
2100
|
-
});
|
|
2101
|
-
if (!raw.payment_id) {
|
|
2102
|
-
throw new HavenApiError("No payment_id returned from x402/authorize", 500, raw);
|
|
2103
|
-
}
|
|
2104
|
-
const signData = raw.sign_data;
|
|
2105
|
-
if (signData?.signature_scheme !== "eip712_delegation" || !signData.typed_data) {
|
|
2106
|
-
throw new HavenApiError(
|
|
2107
|
-
`x402/authorize did not return an erc7710 settlement child (signature_scheme was ${JSON.stringify(signData?.signature_scheme)}). Refusing to sign a payload this path did not ask for.`,
|
|
2108
|
-
500,
|
|
2109
|
-
raw
|
|
3264
|
+
/**
|
|
3265
|
+
* Pay a previously inspected x402 quote and retry the exact captured request.
|
|
3266
|
+
*/
|
|
3267
|
+
async payX402Quote(quote, options = {}) {
|
|
3268
|
+
const idempotencyKey = options.idempotencyKey ?? quote.idempotencyKey;
|
|
3269
|
+
try {
|
|
3270
|
+
const receipt = await this.authorizeX402(quote.paymentRequired, { idempotencyKey });
|
|
3271
|
+
return this.merchantCompletion.retryRequest(
|
|
3272
|
+
quote.request.url,
|
|
3273
|
+
requestInitFromSnapshot(quote.request),
|
|
3274
|
+
quote.paymentRequired,
|
|
3275
|
+
receipt
|
|
2110
3276
|
);
|
|
3277
|
+
} catch (err) {
|
|
3278
|
+
attachResumeState(err, {
|
|
3279
|
+
paymentRequired: quote.paymentRequired,
|
|
3280
|
+
accepted: quote.accepted,
|
|
3281
|
+
idempotencyKey,
|
|
3282
|
+
request: quote.request
|
|
3283
|
+
});
|
|
3284
|
+
throw err;
|
|
2111
3285
|
}
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
3286
|
+
}
|
|
3287
|
+
/**
|
|
3288
|
+
* Pay a merchant through **erc7710 direct settlement** (#1454, epic #1450).
|
|
3289
|
+
*
|
|
3290
|
+
* **Nothing has settled when this returns** — that is why it does not return
|
|
3291
|
+
* an `X402Receipt`; the caller still has to retry the merchant with the
|
|
3292
|
+
* header. **MCP callers must pass `options.resourceUrl`**, because an in-band
|
|
3293
|
+
* MCP 402 challenge frequently carries no `resource` object at all.
|
|
3294
|
+
*
|
|
3295
|
+
* Both caveats, and why this scheme has no funding leg, are explained where
|
|
3296
|
+
* the lifecycle lives: `x402-erc7710.ts` (#1619).
|
|
3297
|
+
*/
|
|
3298
|
+
async settleX402Erc7710(paymentRequired, options = {}) {
|
|
3299
|
+
return this.erc7710.settle(paymentRequired, options);
|
|
3300
|
+
}
|
|
3301
|
+
/**
|
|
3302
|
+
* The AUTHORIZE half of erc7710 settlement (#1456): select the scheme, build
|
|
3303
|
+
* the request, and return the child to be signed — without signing it.
|
|
3304
|
+
*
|
|
3305
|
+
* Split out because the hosted topology cannot use `settleX402Erc7710()`:
|
|
3306
|
+
* that method signs in-process with `delegateKey`, and hosted Haven does not
|
|
3307
|
+
* have one and must not.
|
|
3308
|
+
*/
|
|
3309
|
+
async prepareX402Erc7710(paymentRequired, options = {}) {
|
|
3310
|
+
return this.erc7710.prepare(paymentRequired, options);
|
|
2124
3311
|
}
|
|
2125
3312
|
/**
|
|
2126
3313
|
* The SETTLE half (#1456): exchange the signed child for the merchant header.
|
|
2127
3314
|
*
|
|
2128
3315
|
* The SDK builds no header on this path — the backend assembles the MetaMask
|
|
2129
|
-
* erc7710 payload
|
|
2130
|
-
*
|
|
2131
|
-
* hosted boundary) is irrelevant here.
|
|
3316
|
+
* erc7710 payload. Whoever produced the signature (an in-process delegate
|
|
3317
|
+
* key, or the local edge signer over the hosted boundary) is irrelevant.
|
|
2132
3318
|
*/
|
|
2133
3319
|
async submitX402Erc7710(paymentId, signature) {
|
|
2134
|
-
|
|
2135
|
-
`/x402/${paymentId}/settle`,
|
|
2136
|
-
{ signature }
|
|
2137
|
-
);
|
|
2138
|
-
if (!settled.payment_header) {
|
|
2139
|
-
throw new HavenApiError(
|
|
2140
|
-
"x402 settle returned no payment_header \u2014 the merchant cannot be retried.",
|
|
2141
|
-
500,
|
|
2142
|
-
settled
|
|
2143
|
-
);
|
|
2144
|
-
}
|
|
2145
|
-
return settled.payment_header;
|
|
3320
|
+
return this.erc7710.submit(paymentId, signature);
|
|
2146
3321
|
}
|
|
2147
3322
|
async resumeAuthorizedX402(input) {
|
|
2148
3323
|
if (!this.delegateKey) {
|
|
@@ -2161,11 +3336,11 @@ var HavenClient = class {
|
|
|
2161
3336
|
);
|
|
2162
3337
|
}
|
|
2163
3338
|
const idempotencyKey = input.idempotencyKey ?? buildX402IdempotencyKey(input.paymentRequired, option);
|
|
2164
|
-
const cached = this.
|
|
2165
|
-
if (cached
|
|
3339
|
+
const cached = this.fundingLeg.cachedReceipt(idempotencyKey);
|
|
3340
|
+
if (cached) return cached;
|
|
2166
3341
|
const status = await this.getPaymentStatus(input.paymentId);
|
|
2167
|
-
|
|
2168
|
-
const canFund = await this.delegateCanFund(
|
|
3342
|
+
assertCanResumeX402(status, input.paymentRequired, option);
|
|
3343
|
+
const canFund = await this.fundingLeg.delegateCanFund(
|
|
2169
3344
|
status.chainId ?? chainIdFromNetwork(option.network),
|
|
2170
3345
|
option.asset,
|
|
2171
3346
|
x402AuthorizationAmount(option)
|
|
@@ -2173,20 +3348,20 @@ var HavenClient = class {
|
|
|
2173
3348
|
if (canFund === false) {
|
|
2174
3349
|
throw new X402AlreadySettledError(
|
|
2175
3350
|
`x402 payment ${status.paymentId} has already settled \u2014 the delegate no longer holds the funds to authorize it again, so there is nothing left to resume.`,
|
|
2176
|
-
this.
|
|
3351
|
+
this.fundingLeg.receiptFromStatus(input.paymentRequired, option, void 0, status),
|
|
2177
3352
|
"settled"
|
|
2178
3353
|
);
|
|
2179
3354
|
}
|
|
2180
|
-
const paymentHeader = await this.
|
|
2181
|
-
const receipt = this.
|
|
2182
|
-
this.
|
|
3355
|
+
const paymentHeader = await this.fundingLeg.createPaymentHeader(input.paymentRequired, option);
|
|
3356
|
+
const receipt = this.fundingLeg.receiptFromStatus(input.paymentRequired, option, paymentHeader, status);
|
|
3357
|
+
this.fundingLeg.cacheReceipt(idempotencyKey, paymentHeader, receipt);
|
|
2183
3358
|
return receipt;
|
|
2184
3359
|
}
|
|
2185
3360
|
async resumeX402Payment(input) {
|
|
2186
3361
|
const inputInit = "init" in input ? input.init : void 0;
|
|
2187
|
-
const initialInit =
|
|
2188
|
-
inputInit ?? (input.request ?
|
|
2189
|
-
this.
|
|
3362
|
+
const initialInit = withX402Wallet(
|
|
3363
|
+
inputInit ?? (input.request ? requestInitFromSnapshot(input.request) : void 0),
|
|
3364
|
+
x402PayerAddress(this.delegateAddress, this.x402Wallet)
|
|
2190
3365
|
);
|
|
2191
3366
|
let paymentRequired = input.paymentRequired;
|
|
2192
3367
|
const url = input.url ?? input.request?.url;
|
|
@@ -2194,7 +3369,7 @@ var HavenClient = class {
|
|
|
2194
3369
|
if (!url) {
|
|
2195
3370
|
throw new HavenApiError("x402 resume requires the original URL or a captured request snapshot.", 400);
|
|
2196
3371
|
}
|
|
2197
|
-
const response = await this.
|
|
3372
|
+
const response = await this.merchantTransport.fetch(url, initialInit);
|
|
2198
3373
|
if (response.status !== 402) {
|
|
2199
3374
|
throw new HavenApiError("Expected the original x402 request to return HTTP 402 before resuming.", 400);
|
|
2200
3375
|
}
|
|
@@ -2205,7 +3380,7 @@ var HavenClient = class {
|
|
|
2205
3380
|
paymentRequired,
|
|
2206
3381
|
idempotencyKey: input.idempotencyKey
|
|
2207
3382
|
});
|
|
2208
|
-
return this.
|
|
3383
|
+
return this.merchantCompletion.retryRequest(url ?? paymentRequired.resource.url, initialInit, paymentRequired, receipt);
|
|
2209
3384
|
}
|
|
2210
3385
|
/**
|
|
2211
3386
|
* Fetch wrapper that automatically handles HTTP 402 responses.
|
|
@@ -2219,268 +3394,57 @@ var HavenClient = class {
|
|
|
2219
3394
|
* ```
|
|
2220
3395
|
*
|
|
2221
3396
|
* **MCP-over-x402 auto-handshake (issue #315):** when the endpoint is
|
|
2222
|
-
* MCP-shaped — the URL path ends in `/mcp`, or the 402 body carries a
|
|
2223
|
-
* Coinbase Bazaar `extensions.bazaar` block — the SDK runs the MCP
|
|
2224
|
-
* `initialize` handshake, threads the resulting `mcp-session-id`,
|
|
2225
|
-
* `Accept: application/json, text/event-stream`, and `x402-wallet` headers
|
|
2226
|
-
* through every request, and collapses SSE responses to the JSON-RPC
|
|
2227
|
-
* `result`. The caller just passes `(url, { body })` and never sees the
|
|
2228
|
-
* protocol plumbing. A non-MCP server (handshake error / no session id)
|
|
2229
|
-
* falls back to standard x402 behaviour.
|
|
2230
|
-
*
|
|
2231
|
-
* Requires `delegateKey` to be set in the client config.
|
|
2232
|
-
*/
|
|
2233
|
-
async fetch(url, init, options = {}) {
|
|
2234
|
-
let mcpSessionId;
|
|
2235
|
-
if (isMcpUrl(url)) {
|
|
2236
|
-
mcpSessionId = await this.mcpInitialize(url, init);
|
|
2237
|
-
}
|
|
2238
|
-
let requestInit = this.withX402Wallet(init, this.x402PayerAddress());
|
|
2239
|
-
if (mcpSessionId) requestInit = this.withMcpHeaders(requestInit, mcpSessionId);
|
|
2240
|
-
const response = await this.merchantFetch(url, requestInit);
|
|
2241
|
-
if (response.status !== 402) {
|
|
2242
|
-
return mcpSessionId ? this.surfaceMcpResult(response) : response;
|
|
2243
|
-
}
|
|
2244
|
-
let paymentRequired;
|
|
2245
|
-
try {
|
|
2246
|
-
paymentRequired = await parsePaymentRequiredResponse(response);
|
|
2247
|
-
} catch {
|
|
2248
|
-
return response;
|
|
2249
|
-
}
|
|
2250
|
-
if (!mcpSessionId && await responseHasBazaarExtension(response)) {
|
|
2251
|
-
mcpSessionId = await this.mcpInitialize(url, init);
|
|
2252
|
-
if (mcpSessionId) requestInit = this.withMcpHeaders(requestInit, mcpSessionId);
|
|
2253
|
-
}
|
|
2254
|
-
const request = this.snapshotX402Request(url, requestInit);
|
|
2255
|
-
const option = selectStandardPaymentOption(paymentRequired.accepts);
|
|
2256
|
-
const idempotencyKey = options.idempotencyKey ?? (option ? buildX402IdempotencyKey(paymentRequired, option) : void 0);
|
|
2257
|
-
let receipt;
|
|
2258
|
-
try {
|
|
2259
|
-
receipt = await this.authorizeX402(paymentRequired, options);
|
|
2260
|
-
} catch (err) {
|
|
2261
|
-
if (option && idempotencyKey) {
|
|
2262
|
-
this.attachResumeState(err, {
|
|
2263
|
-
rail: "x402",
|
|
2264
|
-
paymentRequired,
|
|
2265
|
-
accepted: option,
|
|
2266
|
-
idempotencyKey,
|
|
2267
|
-
request
|
|
2268
|
-
});
|
|
2269
|
-
}
|
|
2270
|
-
throw err;
|
|
2271
|
-
}
|
|
2272
|
-
const retryResponse = await this.retryX402Request(url, requestInit, paymentRequired, receipt);
|
|
2273
|
-
return mcpSessionId ? this.surfaceMcpResult(retryResponse) : retryResponse;
|
|
2274
|
-
}
|
|
2275
|
-
// ── MCP-over-x402 transport helpers (issue #315) ─────────────────
|
|
2276
|
-
/**
|
|
2277
|
-
* Run the MCP `initialize` handshake against a Streamable-HTTP endpoint and
|
|
2278
|
-
* return the `mcp-session-id` the server assigns.
|
|
2279
|
-
*
|
|
2280
|
-
* Returns `undefined` whenever the endpoint is not actually an MCP server —
|
|
2281
|
-
* a transport/HTTP error, a missing session id, or a JSON-RPC error in the
|
|
2282
|
-
* handshake response — so the caller can fall back to plain x402.
|
|
2283
|
-
*/
|
|
2284
|
-
async mcpInitialize(url, init, wallet = this.x402PayerAddress()) {
|
|
2285
|
-
try {
|
|
2286
|
-
const headers = new Headers(init?.headers);
|
|
2287
|
-
headers.set("Content-Type", "application/json");
|
|
2288
|
-
headers.set("Accept", MCP_ACCEPT);
|
|
2289
|
-
if (wallet && !headers.has("x402-wallet")) headers.set("x402-wallet", wallet);
|
|
2290
|
-
const response = await this.merchantFetch(url, {
|
|
2291
|
-
method: "POST",
|
|
2292
|
-
headers,
|
|
2293
|
-
body: JSON.stringify({
|
|
2294
|
-
jsonrpc: "2.0",
|
|
2295
|
-
id: ++this.mcpRequestId,
|
|
2296
|
-
method: "initialize",
|
|
2297
|
-
params: {
|
|
2298
|
-
protocolVersion: MCP_PROTOCOL_VERSION,
|
|
2299
|
-
capabilities: {},
|
|
2300
|
-
clientInfo: MCP_CLIENT_INFO
|
|
2301
|
-
}
|
|
2302
|
-
})
|
|
2303
|
-
});
|
|
2304
|
-
if (!response.ok) return void 0;
|
|
2305
|
-
const sessionId = response.headers.get("mcp-session-id");
|
|
2306
|
-
if (!sessionId) return void 0;
|
|
2307
|
-
const message = await this.readMcpMessage(response);
|
|
2308
|
-
if (message && "error" in message) return void 0;
|
|
2309
|
-
await this.mcpNotifyInitialized(url, init, sessionId, wallet);
|
|
2310
|
-
return sessionId;
|
|
2311
|
-
} catch {
|
|
2312
|
-
return void 0;
|
|
2313
|
-
}
|
|
2314
|
-
}
|
|
2315
|
-
/**
|
|
2316
|
-
* Send the MCP `notifications/initialized` notification that completes the
|
|
2317
|
-
* lifecycle handshake. Best-effort: the session is already established, so a
|
|
2318
|
-
* failed notification must not abort the payment.
|
|
2319
|
-
*/
|
|
2320
|
-
async mcpNotifyInitialized(url, init, sessionId, wallet = this.x402PayerAddress()) {
|
|
2321
|
-
try {
|
|
2322
|
-
const headers = new Headers(init?.headers);
|
|
2323
|
-
headers.set("Content-Type", "application/json");
|
|
2324
|
-
headers.set("Accept", MCP_ACCEPT);
|
|
2325
|
-
headers.set("mcp-session-id", sessionId);
|
|
2326
|
-
if (wallet && !headers.has("x402-wallet")) headers.set("x402-wallet", wallet);
|
|
2327
|
-
await this.merchantFetch(
|
|
2328
|
-
url,
|
|
2329
|
-
{
|
|
2330
|
-
method: "POST",
|
|
2331
|
-
headers,
|
|
2332
|
-
body: JSON.stringify({ jsonrpc: "2.0", method: "notifications/initialized" })
|
|
2333
|
-
},
|
|
2334
|
-
NOTIFY_TIMEOUT
|
|
2335
|
-
);
|
|
2336
|
-
} catch {
|
|
2337
|
-
}
|
|
2338
|
-
}
|
|
2339
|
-
/** Read a single JSON-RPC message from an MCP response (JSON or SSE body). */
|
|
2340
|
-
async readMcpMessage(response) {
|
|
2341
|
-
let text;
|
|
2342
|
-
try {
|
|
2343
|
-
text = await response.clone().text();
|
|
2344
|
-
} catch {
|
|
2345
|
-
return void 0;
|
|
2346
|
-
}
|
|
2347
|
-
if ((response.headers.get("content-type") ?? "").includes("text/event-stream")) {
|
|
2348
|
-
return selectJsonRpcResult(parseSseJsonRpcMessages(text));
|
|
2349
|
-
}
|
|
2350
|
-
try {
|
|
2351
|
-
return JSON.parse(text);
|
|
2352
|
-
} catch {
|
|
2353
|
-
return void 0;
|
|
2354
|
-
}
|
|
2355
|
-
}
|
|
2356
|
-
/** Add the MCP transport headers (session id + SSE Accept) to a request. */
|
|
2357
|
-
withMcpHeaders(init, sessionId) {
|
|
2358
|
-
const headers = new Headers(init?.headers);
|
|
2359
|
-
headers.set("mcp-session-id", sessionId);
|
|
2360
|
-
headers.set("Accept", MCP_ACCEPT);
|
|
2361
|
-
return { ...init, headers };
|
|
2362
|
-
}
|
|
2363
|
-
/**
|
|
2364
|
-
* Collapse an MCP SSE response into a plain JSON response carrying the
|
|
2365
|
-
* JSON-RPC `result`, so callers of `fetch()` never see raw SSE framing.
|
|
2366
|
-
* Non-SSE responses pass through untouched.
|
|
2367
|
-
*/
|
|
2368
|
-
async surfaceMcpResult(response) {
|
|
2369
|
-
if (!(response.headers.get("content-type") ?? "").includes("text/event-stream")) {
|
|
2370
|
-
return response;
|
|
2371
|
-
}
|
|
2372
|
-
let text;
|
|
2373
|
-
try {
|
|
2374
|
-
text = await response.clone().text();
|
|
2375
|
-
} catch {
|
|
2376
|
-
return response;
|
|
2377
|
-
}
|
|
2378
|
-
const message = selectJsonRpcResult(parseSseJsonRpcMessages(text));
|
|
2379
|
-
if (!message) return response;
|
|
2380
|
-
const body = "result" in message ? message.result : message;
|
|
2381
|
-
const headers = new Headers(response.headers);
|
|
2382
|
-
headers.set("content-type", "application/json");
|
|
2383
|
-
headers.delete("content-length");
|
|
2384
|
-
headers.delete("mcp-session-id");
|
|
2385
|
-
return new Response(JSON.stringify(body), {
|
|
2386
|
-
status: response.status,
|
|
2387
|
-
statusText: response.statusText,
|
|
2388
|
-
headers
|
|
2389
|
-
});
|
|
2390
|
-
}
|
|
2391
|
-
async retryX402Request(url, initialInit, paymentRequired, receipt) {
|
|
2392
|
-
if (!receipt.accepted) {
|
|
2393
|
-
throw new HavenApiError("No accepted x402 option was recorded for payment retry", 500);
|
|
2394
|
-
}
|
|
2395
|
-
if (!receipt.paymentHeader) {
|
|
2396
|
-
throw new HavenApiError("No x402 payment header was returned for payment retry", 500);
|
|
2397
|
-
}
|
|
2398
|
-
const retryHeaders = new Headers(initialInit?.headers);
|
|
2399
|
-
retryHeaders.set("X-PAYMENT", receipt.paymentHeader);
|
|
2400
|
-
const retryResponse = await this.merchantFetch(url, {
|
|
2401
|
-
...initialInit,
|
|
2402
|
-
headers: retryHeaders
|
|
2403
|
-
});
|
|
2404
|
-
if (!retryResponse.ok) {
|
|
2405
|
-
const merchant = await captureMerchantResponse(retryResponse);
|
|
2406
|
-
await this.recordMerchantRetryRejected({
|
|
2407
|
-
rail: "x402",
|
|
2408
|
-
paymentId: receipt.paymentId,
|
|
2409
|
-
txHash: receipt.txHash,
|
|
2410
|
-
resourceUrl: receipt.resourceUrl,
|
|
2411
|
-
merchant,
|
|
2412
|
-
details: {
|
|
2413
|
-
merchant_to: receipt.merchantTo,
|
|
2414
|
-
delegate_to: receipt.to
|
|
2415
|
-
}
|
|
2416
|
-
});
|
|
2417
|
-
throw new HavenApiError(
|
|
2418
|
-
"x402 retry failed after Haven funded the delegate wallet; reconciliation may be required.",
|
|
2419
|
-
merchant.merchant_status,
|
|
2420
|
-
{
|
|
2421
|
-
marker: "x402_retry_rejected_after_funding",
|
|
2422
|
-
payment_id: receipt.paymentId,
|
|
2423
|
-
tx_hash: receipt.txHash,
|
|
2424
|
-
resource_url: receipt.resourceUrl,
|
|
2425
|
-
merchant_to: receipt.merchantTo,
|
|
2426
|
-
delegate_to: receipt.to,
|
|
2427
|
-
...merchant
|
|
2428
|
-
}
|
|
2429
|
-
);
|
|
2430
|
-
}
|
|
2431
|
-
const merchantSettlement = parseMerchantSettlement(retryResponse.headers.get("PAYMENT-RESPONSE"));
|
|
2432
|
-
if (receipt.merchant && merchantSettlement.settlementTxHash) {
|
|
2433
|
-
receipt.merchant.settlementTxHash = merchantSettlement.settlementTxHash;
|
|
2434
|
-
receipt.merchant.settlementExplorerUrl = buildExplorerUrl(
|
|
2435
|
-
receipt.chainId,
|
|
2436
|
-
merchantSettlement.settlementTxHash
|
|
2437
|
-
);
|
|
2438
|
-
}
|
|
2439
|
-
await this.reportMachinePaymentEvidence({
|
|
2440
|
-
paymentId: receipt.paymentId,
|
|
2441
|
-
rail: "x402",
|
|
2442
|
-
txHash: receipt.txHash,
|
|
2443
|
-
resourceUrl: receipt.resourceUrl,
|
|
2444
|
-
merchantStatus: retryResponse.status,
|
|
2445
|
-
challengePayload: paymentRequired,
|
|
2446
|
-
selectedPayment: receipt.accepted,
|
|
2447
|
-
paymentProofHeaderName: "X-PAYMENT",
|
|
2448
|
-
paymentProofHeader: receipt.paymentHeader,
|
|
2449
|
-
protocolReceiptHeaderName: "PAYMENT-RESPONSE",
|
|
2450
|
-
protocolReceiptHeader: retryResponse.headers.get("PAYMENT-RESPONSE") ?? void 0
|
|
2451
|
-
});
|
|
2452
|
-
await this.reportMerchantReceipt(receipt.paymentId, retryResponse);
|
|
2453
|
-
return retryResponse;
|
|
2454
|
-
}
|
|
2455
|
-
/**
|
|
2456
|
-
* #956: capture the merchant's OWN receipt when the paid response carries
|
|
2457
|
-
* one, and report it to Haven so the reporting feed can attach it next to
|
|
2458
|
-
* the Haven-generated payment evidence (#498). Two supported signals on the
|
|
2459
|
-
* paid response:
|
|
2460
|
-
*
|
|
2461
|
-
* x-receipt-json: base64-encoded JSON receipt document (inline)
|
|
2462
|
-
* x-receipt-url: https URL to the receipt document (reference)
|
|
3397
|
+
* MCP-shaped — the URL path ends in `/mcp`, or the 402 body carries a
|
|
3398
|
+
* Coinbase Bazaar `extensions.bazaar` block — the SDK runs the MCP
|
|
3399
|
+
* `initialize` handshake, threads the resulting `mcp-session-id`,
|
|
3400
|
+
* `Accept: application/json, text/event-stream`, and `x402-wallet` headers
|
|
3401
|
+
* through every request, and collapses SSE responses to the JSON-RPC
|
|
3402
|
+
* `result`. The caller just passes `(url, { body })` and never sees the
|
|
3403
|
+
* protocol plumbing. A non-MCP server (handshake error / no session id)
|
|
3404
|
+
* falls back to standard x402 behaviour.
|
|
2463
3405
|
*
|
|
2464
|
-
*
|
|
2465
|
-
* ever affect the completed payment — the response is already paid for.
|
|
3406
|
+
* Requires `delegateKey` to be set in the client config.
|
|
2466
3407
|
*/
|
|
2467
|
-
async
|
|
3408
|
+
async fetch(url, init, options = {}) {
|
|
3409
|
+
let mcpSessionId;
|
|
3410
|
+
if (this.merchantTransport.isMcpUrl(url)) {
|
|
3411
|
+
mcpSessionId = await this.merchantTransport.initialize(url, init);
|
|
3412
|
+
}
|
|
3413
|
+
let requestInit = withX402Wallet(init, x402PayerAddress(this.delegateAddress, this.x402Wallet));
|
|
3414
|
+
if (mcpSessionId) requestInit = this.merchantTransport.withSessionHeaders(requestInit, mcpSessionId);
|
|
3415
|
+
const response = await this.merchantTransport.fetch(url, requestInit);
|
|
3416
|
+
if (response.status !== 402) {
|
|
3417
|
+
return mcpSessionId ? this.merchantTransport.surfaceResult(response) : response;
|
|
3418
|
+
}
|
|
3419
|
+
let paymentRequired;
|
|
2468
3420
|
try {
|
|
2469
|
-
|
|
2470
|
-
const url = response.headers.get("x-receipt-url");
|
|
2471
|
-
if (!inlineB64 && !url) return;
|
|
2472
|
-
let body = null;
|
|
2473
|
-
if (inlineB64) {
|
|
2474
|
-
if (inlineB64.length > Math.ceil(64 * 1024 * 4 / 3)) return;
|
|
2475
|
-
const decoded = JSON.parse(Buffer.from(inlineB64, "base64").toString("utf8"));
|
|
2476
|
-
if (decoded && typeof decoded === "object") body = { json: decoded };
|
|
2477
|
-
} else if (url && url.startsWith("https://") && url.length <= 2048) {
|
|
2478
|
-
body = { url };
|
|
2479
|
-
}
|
|
2480
|
-
if (!body) return;
|
|
2481
|
-
await this.post(`/machine-payments/${paymentId}/merchant-receipt`, body);
|
|
3421
|
+
paymentRequired = await parsePaymentRequiredResponse(response);
|
|
2482
3422
|
} catch {
|
|
3423
|
+
return response;
|
|
3424
|
+
}
|
|
3425
|
+
if (!mcpSessionId && await this.merchantTransport.hasBazaarExtension(response)) {
|
|
3426
|
+
mcpSessionId = await this.merchantTransport.initialize(url, init);
|
|
3427
|
+
if (mcpSessionId) requestInit = this.merchantTransport.withSessionHeaders(requestInit, mcpSessionId);
|
|
3428
|
+
}
|
|
3429
|
+
const request = snapshotX402Request(url, requestInit);
|
|
3430
|
+
const option = selectStandardPaymentOption(paymentRequired.accepts);
|
|
3431
|
+
const idempotencyKey = options.idempotencyKey ?? (option ? buildX402IdempotencyKey(paymentRequired, option) : void 0);
|
|
3432
|
+
let receipt;
|
|
3433
|
+
try {
|
|
3434
|
+
receipt = await this.authorizeX402(paymentRequired, options);
|
|
3435
|
+
} catch (err) {
|
|
3436
|
+
if (option && idempotencyKey) {
|
|
3437
|
+
attachResumeState(err, {
|
|
3438
|
+
paymentRequired,
|
|
3439
|
+
accepted: option,
|
|
3440
|
+
idempotencyKey,
|
|
3441
|
+
request
|
|
3442
|
+
});
|
|
3443
|
+
}
|
|
3444
|
+
throw err;
|
|
2483
3445
|
}
|
|
3446
|
+
const retryResponse = await this.merchantCompletion.retryRequest(url, requestInit, paymentRequired, receipt);
|
|
3447
|
+
return mcpSessionId ? this.merchantTransport.surfaceResult(retryResponse) : retryResponse;
|
|
2484
3448
|
}
|
|
2485
3449
|
/**
|
|
2486
3450
|
* Deliver an already-signed x402 payment header to the merchant and return
|
|
@@ -2505,7 +3469,7 @@ var HavenClient = class {
|
|
|
2505
3469
|
* and before delivering the X-PAYMENT header, so the merchant's
|
|
2506
3470
|
* balanceOf(delegate) / transferWithAuthorization verification sees the funded
|
|
2507
3471
|
* balance — otherwise it rejects with "Payment verification failed". The
|
|
2508
|
-
* SDK's local path already does this (see
|
|
3472
|
+
* SDK's local path already does this (see `X402FundingLeg.authorize`); the hosted
|
|
2509
3473
|
* split flow regressed when the 5→3 collapse removed the incidental
|
|
2510
3474
|
* inter-call latency that used to mask it.
|
|
2511
3475
|
*
|
|
@@ -2522,28 +3486,25 @@ var HavenClient = class {
|
|
|
2522
3486
|
*/
|
|
2523
3487
|
async ensureFundingConfirmed(paymentId, fundingTxHash) {
|
|
2524
3488
|
const status = await this.getPaymentStatus(paymentId);
|
|
2525
|
-
await this.waitForFundingTx(fundingTxHash ?? status.txHash ?? void 0, status.chainId);
|
|
3489
|
+
await this.fundingLeg.waitForFundingTx(fundingTxHash ?? status.txHash ?? void 0, status.chainId);
|
|
2526
3490
|
}
|
|
2527
3491
|
async completeX402MerchantCall(input) {
|
|
2528
|
-
const evidenceContext = await this.
|
|
3492
|
+
const evidenceContext = await this.merchantCompletion.resolveCompletionContext({
|
|
2529
3493
|
paymentId: input.paymentId,
|
|
2530
3494
|
url: input.url,
|
|
2531
3495
|
noFundingLeg: input.noFundingLeg === true
|
|
2532
3496
|
});
|
|
2533
3497
|
const fundingTxHash = evidenceContext.txHash;
|
|
2534
|
-
const shouldHandshakeMcp = isMcpUrl(input.url) || input.mcpTransport?.handshakeRequired === true;
|
|
2535
|
-
const x402Wallet = shouldHandshakeMcp ? await this.
|
|
3498
|
+
const shouldHandshakeMcp = this.merchantTransport.isMcpUrl(input.url) || input.mcpTransport?.handshakeRequired === true;
|
|
3499
|
+
const x402Wallet = shouldHandshakeMcp ? await this.merchantCompletion.resolveWalletForMerchantCall() : x402PayerAddress(this.delegateAddress, this.x402Wallet);
|
|
2536
3500
|
let mcpSessionId;
|
|
2537
3501
|
if (shouldHandshakeMcp) {
|
|
2538
|
-
mcpSessionId = await this.
|
|
3502
|
+
mcpSessionId = await this.merchantTransport.initialize(input.url, input.init, x402Wallet);
|
|
2539
3503
|
}
|
|
2540
|
-
let requestInit =
|
|
2541
|
-
if (mcpSessionId) requestInit = this.
|
|
2542
|
-
const
|
|
2543
|
-
|
|
2544
|
-
requestInit = { ...requestInit, headers };
|
|
2545
|
-
const response = await this.merchantFetch(input.url, requestInit);
|
|
2546
|
-
const surfaced = mcpSessionId ? await this.surfaceMcpResult(response) : response;
|
|
3504
|
+
let requestInit = withX402Wallet(input.init, x402Wallet) ?? {};
|
|
3505
|
+
if (mcpSessionId) requestInit = this.merchantTransport.withSessionHeaders(requestInit, mcpSessionId);
|
|
3506
|
+
const response = await this.merchantTransport.deliverPayment(input.url, requestInit, input.paymentHeader);
|
|
3507
|
+
const surfaced = mcpSessionId ? await this.merchantTransport.surfaceResult(response) : response;
|
|
2547
3508
|
const protocolReceiptHeader = surfaced.headers.get("PAYMENT-RESPONSE") ?? void 0;
|
|
2548
3509
|
const settlement = parseMerchantSettlement(protocolReceiptHeader ?? null);
|
|
2549
3510
|
const text = await surfaced.text();
|
|
@@ -2555,7 +3516,7 @@ var HavenClient = class {
|
|
|
2555
3516
|
}
|
|
2556
3517
|
if (!surfaced.ok) {
|
|
2557
3518
|
if (!input.noFundingLeg && fundingTxHash) {
|
|
2558
|
-
await this.
|
|
3519
|
+
await this.merchantCompletion.recordRetryRejected({
|
|
2559
3520
|
rail: "x402",
|
|
2560
3521
|
paymentId: evidenceContext.paymentId,
|
|
2561
3522
|
txHash: fundingTxHash,
|
|
@@ -2573,7 +3534,7 @@ var HavenClient = class {
|
|
|
2573
3534
|
}
|
|
2574
3535
|
} else {
|
|
2575
3536
|
if (!input.noFundingLeg && fundingTxHash) {
|
|
2576
|
-
await this.
|
|
3537
|
+
await this.merchantCompletion.reportEvidence({
|
|
2577
3538
|
paymentId: evidenceContext.paymentId,
|
|
2578
3539
|
rail: "x402",
|
|
2579
3540
|
txHash: fundingTxHash,
|
|
@@ -2585,7 +3546,7 @@ var HavenClient = class {
|
|
|
2585
3546
|
protocolReceiptHeader
|
|
2586
3547
|
});
|
|
2587
3548
|
}
|
|
2588
|
-
await this.reportMerchantReceipt(evidenceContext.paymentId, surfaced);
|
|
3549
|
+
await this.merchantCompletion.reportMerchantReceipt(evidenceContext.paymentId, surfaced);
|
|
2589
3550
|
}
|
|
2590
3551
|
return {
|
|
2591
3552
|
status: surfaced.status,
|
|
@@ -2615,271 +3576,11 @@ var HavenClient = class {
|
|
|
2615
3576
|
arguments: raw.arguments ?? {},
|
|
2616
3577
|
...raw.mcp_transport ? {
|
|
2617
3578
|
mcpTransport: {
|
|
2618
|
-
handshakeRequired: raw.mcp_transport.handshake_required,
|
|
2619
|
-
source: raw.mcp_transport.source
|
|
2620
|
-
}
|
|
2621
|
-
} : {}
|
|
2622
|
-
};
|
|
2623
|
-
}
|
|
2624
|
-
async resolveX402MerchantCompletionContext(input) {
|
|
2625
|
-
const status = await this.getPaymentStatus(input.paymentId);
|
|
2626
|
-
if (status.rail !== "x402") {
|
|
2627
|
-
throw new HavenPaymentStateError(
|
|
2628
|
-
`Payment ${status.paymentId} is ${status.rail}, not x402.`,
|
|
2629
|
-
409,
|
|
2630
|
-
status
|
|
2631
|
-
);
|
|
2632
|
-
}
|
|
2633
|
-
const readyForMerchantCompletion = input.noFundingLeg ? status.kind === "payment_intent" && status.status === "submitted" : status.nextAction === AgentPaymentNextAction.RetryOriginalX402Request || status.kind === "payment_intent" && status.status === "confirmed" && status.phase === AgentPaymentPhase.PaymentConfirmed && status.nextAction === AgentPaymentNextAction.None;
|
|
2634
|
-
if (!readyForMerchantCompletion) {
|
|
2635
|
-
throw new HavenPaymentStateError(status.message, PAYMENT_STATE_STATUS_CODES[status.status] ?? 409, status);
|
|
2636
|
-
}
|
|
2637
|
-
if (!input.noFundingLeg && !status.txHash) {
|
|
2638
|
-
throw new HavenApiError(
|
|
2639
|
-
`x402 payment ${status.paymentId} is ready for merchant completion but has no Haven transaction hash.`,
|
|
2640
|
-
502,
|
|
2641
|
-
status,
|
|
2642
|
-
status.paymentId
|
|
2643
|
-
);
|
|
2644
|
-
}
|
|
2645
|
-
const approvedResourceUrl = status.resourceUrl ?? status.x402?.resourceUrl ?? null;
|
|
2646
|
-
if (approvedResourceUrl && approvedResourceUrl !== input.url) {
|
|
2647
|
-
throw new HavenApiError(
|
|
2648
|
-
"x402 merchant completion does not match the approved resource URL.",
|
|
2649
|
-
409,
|
|
2650
|
-
{ status, url: input.url },
|
|
2651
|
-
status.paymentId
|
|
2652
|
-
);
|
|
2653
|
-
}
|
|
2654
|
-
return {
|
|
2655
|
-
paymentId: status.paymentId,
|
|
2656
|
-
txHash: status.txHash,
|
|
2657
|
-
resourceUrl: approvedResourceUrl ?? input.url,
|
|
2658
|
-
merchantAddress: status.merchantAddress ?? status.x402?.merchantAddress ?? null
|
|
2659
|
-
};
|
|
2660
|
-
}
|
|
2661
|
-
async resolveX402WalletForMerchantCall() {
|
|
2662
|
-
const localWallet = this.x402PayerAddress();
|
|
2663
|
-
if (localWallet) return localWallet;
|
|
2664
|
-
try {
|
|
2665
|
-
const agent = await this.getAgent();
|
|
2666
|
-
return agent.delegateAddress ?? void 0;
|
|
2667
|
-
} catch {
|
|
2668
|
-
return void 0;
|
|
2669
|
-
}
|
|
2670
|
-
}
|
|
2671
|
-
// #1328: authorizeMachinePayment / authorizeMppDemoPayment / resumeAuthorizedMpp
|
|
2672
|
-
// / resumeMppPayment / fetchWithMachinePayment / retryMppRequest (the
|
|
2673
|
-
// MACHINE-PAYMENT-CHALLENGE / mpp_demo client surface) are retired — the
|
|
2674
|
-
// backend's POST /machine-payments/authorize refuses unconditionally now,
|
|
2675
|
-
// and MACHINE-PAYMENT-CHALLENGE was never produced by any other Haven
|
|
2676
|
-
// surface. Use the x402 flow (authorizeX402 / fetch / quoteX402 / payX402Quote)
|
|
2677
|
-
// for agent-to-merchant payments.
|
|
2678
|
-
assertCanResumeX402(status, paymentRequired, option) {
|
|
2679
|
-
if (status.rail !== "x402") {
|
|
2680
|
-
throw new HavenPaymentStateError(
|
|
2681
|
-
`Payment ${status.paymentId} is ${status.rail}, not x402.`,
|
|
2682
|
-
409,
|
|
2683
|
-
status
|
|
2684
|
-
);
|
|
2685
|
-
}
|
|
2686
|
-
if (status.nextAction !== AgentPaymentNextAction.RetryOriginalX402Request) {
|
|
2687
|
-
throw new HavenPaymentStateError(status.message, PAYMENT_STATE_STATUS_CODES[status.status] ?? 409, status);
|
|
2688
|
-
}
|
|
2689
|
-
if (!status.txHash) {
|
|
2690
|
-
throw new HavenApiError(
|
|
2691
|
-
`x402 payment ${status.paymentId} is ready to retry but has no Haven transaction hash.`,
|
|
2692
|
-
502,
|
|
2693
|
-
status,
|
|
2694
|
-
status.paymentId
|
|
2695
|
-
);
|
|
2696
|
-
}
|
|
2697
|
-
if (status.resourceUrl && status.resourceUrl !== paymentRequired.resource.url) {
|
|
2698
|
-
throw new HavenApiError(
|
|
2699
|
-
"x402 resume request does not match the approved resource URL.",
|
|
2700
|
-
409,
|
|
2701
|
-
{ status, paymentRequired },
|
|
2702
|
-
status.paymentId
|
|
2703
|
-
);
|
|
2704
|
-
}
|
|
2705
|
-
if (status.merchantAddress && !sameAddress3(status.merchantAddress, option.payTo)) {
|
|
2706
|
-
throw new HavenApiError(
|
|
2707
|
-
"x402 resume request does not match the approved merchant.",
|
|
2708
|
-
409,
|
|
2709
|
-
{ status, selectedPayment: option },
|
|
2710
|
-
status.paymentId
|
|
2711
|
-
);
|
|
2712
|
-
}
|
|
2713
|
-
const optionChainId = chainIdFromNetwork(option.network);
|
|
2714
|
-
if (status.chainId && optionChainId && status.chainId !== optionChainId) {
|
|
2715
|
-
throw new HavenApiError(
|
|
2716
|
-
"x402 resume request does not match the approved network.",
|
|
2717
|
-
409,
|
|
2718
|
-
{ status, selectedPayment: option },
|
|
2719
|
-
status.paymentId
|
|
2720
|
-
);
|
|
2721
|
-
}
|
|
2722
|
-
if (status.token && status.token !== "USDC") {
|
|
2723
|
-
throw new HavenApiError(
|
|
2724
|
-
"x402 resume request does not match the approved token.",
|
|
2725
|
-
409,
|
|
2726
|
-
{ status, selectedPayment: option },
|
|
2727
|
-
status.paymentId
|
|
2728
|
-
);
|
|
2729
|
-
}
|
|
2730
|
-
const approvedAmount = status.amount ? normalizeDecimal(status.amount) : "";
|
|
2731
|
-
const requestedAmount = normalizeDecimal(decimalFromUsdcAtomic(x402AuthorizationAmount(option)));
|
|
2732
|
-
if (approvedAmount && approvedAmount !== requestedAmount) {
|
|
2733
|
-
throw new HavenApiError(
|
|
2734
|
-
"x402 resume request does not match the approved amount.",
|
|
2735
|
-
409,
|
|
2736
|
-
{ status, selectedPayment: option },
|
|
2737
|
-
status.paymentId
|
|
2738
|
-
);
|
|
2739
|
-
}
|
|
2740
|
-
}
|
|
2741
|
-
mapX402ReceiptFromAuthorization(paymentRequired, option, paymentHeader, raw, execResult) {
|
|
2742
|
-
const txHash = execResult?.tx_hash ?? raw.tx_hash ?? "";
|
|
2743
|
-
const chainId = execResult?.chain_id ?? raw.chain_id ?? chainIdFromNetwork(option.network);
|
|
2744
|
-
const token = execResult?.token ?? raw.token ?? "USDC";
|
|
2745
|
-
const amount = execResult?.amount ?? raw.amount ?? decimalFromUsdcAtomic(x402AuthorizationAmount(option));
|
|
2746
|
-
const to = execResult?.to ?? raw.to ?? this.delegateAddress ?? "";
|
|
2747
|
-
const explorerUrl = execResult?.explorer_url ?? raw.explorer_url ?? explorerUrlOrEmpty(chainId, txHash);
|
|
2748
|
-
const merchantTo = execResult?.merchant_to ?? raw.merchant_to ?? option.payTo;
|
|
2749
|
-
const payer = raw.payer ?? raw.safe_address ?? raw.sign_data?.components.safe;
|
|
2750
|
-
return this.buildX402Receipt({
|
|
2751
|
-
paymentId: raw.payment_id,
|
|
2752
|
-
txHash,
|
|
2753
|
-
token,
|
|
2754
|
-
amount,
|
|
2755
|
-
to,
|
|
2756
|
-
resourceUrl: paymentRequired.resource.url,
|
|
2757
|
-
explorerUrl,
|
|
2758
|
-
accepted: option,
|
|
2759
|
-
paymentHeader,
|
|
2760
|
-
merchantTo,
|
|
2761
|
-
payer,
|
|
2762
|
-
chainId
|
|
2763
|
-
});
|
|
2764
|
-
}
|
|
2765
|
-
mapX402ReceiptFromStatus(paymentRequired, option, paymentHeader, status) {
|
|
2766
|
-
if (!status.txHash) {
|
|
2767
|
-
throw new HavenApiError(
|
|
2768
|
-
`x402 payment ${status.paymentId} is ready to retry but has no Haven transaction hash.`,
|
|
2769
|
-
502,
|
|
2770
|
-
status,
|
|
2771
|
-
status.paymentId
|
|
2772
|
-
);
|
|
2773
|
-
}
|
|
2774
|
-
return this.buildX402Receipt({
|
|
2775
|
-
paymentId: status.paymentId,
|
|
2776
|
-
txHash: status.txHash,
|
|
2777
|
-
token: status.token || "USDC",
|
|
2778
|
-
amount: status.amount || decimalFromUsdcAtomic(x402AuthorizationAmount(option)),
|
|
2779
|
-
to: this.delegateAddress ?? "",
|
|
2780
|
-
resourceUrl: paymentRequired.resource.url,
|
|
2781
|
-
explorerUrl: explorerUrlOrEmpty(status.chainId, status.txHash),
|
|
2782
|
-
accepted: option,
|
|
2783
|
-
paymentHeader,
|
|
2784
|
-
merchantTo: status.merchantAddress ?? option.payTo,
|
|
2785
|
-
payer: this.x402Wallet,
|
|
2786
|
-
chainId: status.chainId || chainIdFromNetwork(option.network)
|
|
2787
|
-
});
|
|
2788
|
-
}
|
|
2789
|
-
buildX402Receipt(input) {
|
|
2790
|
-
const fundingExplorerUrl = input.explorerUrl || explorerUrlOrEmpty(input.chainId, input.txHash);
|
|
2791
|
-
return {
|
|
2792
|
-
success: true,
|
|
2793
|
-
paymentId: input.paymentId,
|
|
2794
|
-
txHash: input.txHash,
|
|
2795
|
-
token: input.token,
|
|
2796
|
-
amount: input.amount,
|
|
2797
|
-
to: input.to,
|
|
2798
|
-
resourceUrl: input.resourceUrl,
|
|
2799
|
-
explorerUrl: input.explorerUrl,
|
|
2800
|
-
accepted: input.accepted,
|
|
2801
|
-
paymentHeader: input.paymentHeader,
|
|
2802
|
-
merchantTo: input.merchantTo ?? input.accepted.payTo,
|
|
2803
|
-
payer: input.payer,
|
|
2804
|
-
chainId: input.chainId,
|
|
2805
|
-
haven: {
|
|
2806
|
-
paymentId: input.paymentId,
|
|
2807
|
-
fundingTxHash: input.txHash,
|
|
2808
|
-
fundingExplorerUrl
|
|
2809
|
-
},
|
|
2810
|
-
merchant: {
|
|
2811
|
-
payTo: input.merchantTo ?? input.accepted.payTo
|
|
2812
|
-
},
|
|
2813
|
-
x402: {
|
|
2814
|
-
amount: x402AuthorizationAmount(input.accepted),
|
|
2815
|
-
token: input.token,
|
|
2816
|
-
network: input.accepted.network,
|
|
2817
|
-
asset: input.accepted.asset,
|
|
2818
|
-
resource: input.accepted.resource ?? input.resourceUrl
|
|
2819
|
-
}
|
|
2820
|
-
};
|
|
2821
|
-
}
|
|
2822
|
-
async createStandardX402Header(paymentRequired, option) {
|
|
2823
|
-
if (!this.delegateKey) {
|
|
2824
|
-
throw new HavenSigningError("delegateKey is required to sign x402 payment headers.");
|
|
2825
|
-
}
|
|
2826
|
-
const account = privateKeyToAccount(this.delegateKey);
|
|
2827
|
-
const requirements = toStandardPaymentRequirements(paymentRequired, option);
|
|
2828
|
-
const header = await exact.evm.createPaymentHeader(
|
|
2829
|
-
account,
|
|
2830
|
-
paymentRequired.x402Version,
|
|
2831
|
-
requirements
|
|
2832
|
-
);
|
|
2833
|
-
if (paymentRequired.x402Version < 2) return header;
|
|
2834
|
-
const payment = decodeBase64Json(header);
|
|
2835
|
-
return encodeBase64Json({
|
|
2836
|
-
x402Version: paymentRequired.x402Version,
|
|
2837
|
-
accepted: option,
|
|
2838
|
-
payload: payment.payload
|
|
2839
|
-
});
|
|
2840
|
-
}
|
|
2841
|
-
cacheX402Receipt(idempotencyKey, paymentHeader, receipt) {
|
|
2842
|
-
const expiresAt = getPaymentHeaderValidBefore(paymentHeader);
|
|
2843
|
-
if (expiresAt > Date.now()) {
|
|
2844
|
-
this.x402ReceiptCache.set(idempotencyKey, { expiresAt, receipt });
|
|
2845
|
-
}
|
|
2846
|
-
}
|
|
2847
|
-
async recordMerchantRetryRejected(input) {
|
|
2848
|
-
try {
|
|
2849
|
-
await this.post("/machine-payments/reconciliation-events", {
|
|
2850
|
-
paymentId: input.paymentId,
|
|
2851
|
-
rail: input.rail,
|
|
2852
|
-
eventType: "merchant_retry_rejected_after_payment",
|
|
2853
|
-
txHash: input.txHash,
|
|
2854
|
-
reason: `Merchant returned HTTP ${input.merchant.merchant_status} after Haven payment confirmation`,
|
|
2855
|
-
details: {
|
|
2856
|
-
resource_url: input.resourceUrl,
|
|
2857
|
-
retry_status: input.merchant.merchant_status,
|
|
2858
|
-
retry_body: input.merchant.merchant_body.slice(0, MERCHANT_BODY_SNIPPET_LIMIT) || null,
|
|
2859
|
-
...input.details
|
|
2860
|
-
}
|
|
2861
|
-
});
|
|
2862
|
-
} catch {
|
|
2863
|
-
}
|
|
2864
|
-
}
|
|
2865
|
-
async reportMachinePaymentEvidence(input) {
|
|
2866
|
-
try {
|
|
2867
|
-
await this.post("/machine-payments/evidence", {
|
|
2868
|
-
paymentId: input.paymentId,
|
|
2869
|
-
rail: input.rail,
|
|
2870
|
-
txHash: input.txHash,
|
|
2871
|
-
resourceUrl: input.resourceUrl,
|
|
2872
|
-
merchantStatus: input.merchantStatus,
|
|
2873
|
-
challengePayload: input.challengePayload,
|
|
2874
|
-
selectedPayment: input.selectedPayment,
|
|
2875
|
-
paymentProofHeaderName: input.paymentProofHeaderName,
|
|
2876
|
-
paymentProofHeader: input.paymentProofHeader,
|
|
2877
|
-
protocolReceiptHeaderName: input.protocolReceiptHeaderName,
|
|
2878
|
-
protocolReceiptHeader: input.protocolReceiptHeader,
|
|
2879
|
-
protocolReceiptPayload: input.protocolReceiptHeader ? parseProtocolReceiptHeader(input.protocolReceiptHeader) : void 0
|
|
2880
|
-
});
|
|
2881
|
-
} catch {
|
|
2882
|
-
}
|
|
3579
|
+
handshakeRequired: raw.mcp_transport.handshake_required,
|
|
3580
|
+
source: raw.mcp_transport.source
|
|
3581
|
+
}
|
|
3582
|
+
} : {}
|
|
3583
|
+
};
|
|
2883
3584
|
}
|
|
2884
3585
|
/**
|
|
2885
3586
|
* Wait for a funding tx to be mined with ≥1 confirmation before the
|
|
@@ -2890,264 +3591,9 @@ var HavenClient = class {
|
|
|
2890
3591
|
* backend has already confirmed on-chain submission and callers accept the
|
|
2891
3592
|
* small propagation window as a trade-off for not configuring an RPC URL.
|
|
2892
3593
|
*/
|
|
2893
|
-
async waitForFundingTx(txHash, chainId, timeoutMs = 3e4) {
|
|
2894
|
-
if (!txHash || !chainId) return;
|
|
2895
|
-
const rpcUrl = this.chainRpcs[chainId];
|
|
2896
|
-
if (!rpcUrl) return;
|
|
2897
|
-
const provider = createJsonRpcProvider(rpcUrl);
|
|
2898
|
-
const onChainReceipt = await provider.waitForTransaction(txHash, 1, timeoutMs);
|
|
2899
|
-
if (!onChainReceipt || onChainReceipt.status !== 1) {
|
|
2900
|
-
throw new HavenApiError(
|
|
2901
|
-
"Funding tx did not confirm on-chain within the timeout window.",
|
|
2902
|
-
500,
|
|
2903
|
-
{ txHash, chainId }
|
|
2904
|
-
);
|
|
2905
|
-
}
|
|
2906
|
-
}
|
|
2907
|
-
/**
|
|
2908
|
-
* Can the delegate EOA still fund an authorization for `amountAtomic`?
|
|
2909
|
-
*
|
|
2910
|
-
* #1521: the only question that separates a legitimate resume (funding
|
|
2911
|
-
* confirmed, merchant never paid — the delegate still holds the money) from
|
|
2912
|
-
* a replayed settled payment (funding confirmed, merchant paid, delegate
|
|
2913
|
-
* spent). The intent's own `status: 'confirmed'` is identical in both.
|
|
2914
|
-
*
|
|
2915
|
-
* The balance is asked of the CHAIN rather than of Haven's bookkeeping on
|
|
2916
|
-
* purpose: the merchant-settlement evidence record is written by this SDK
|
|
2917
|
-
* *after* the merchant call, so a client that dies between the two leaves
|
|
2918
|
-
* the backend believing the merchant was never paid — the exact case the
|
|
2919
|
-
* discriminator has to get right. The chain cannot be behind in that way.
|
|
2920
|
-
*
|
|
2921
|
-
* Returns `null` — never a guess — when `chainRpcs` has no entry for the
|
|
2922
|
-
* chain or the read fails. Callers must treat that as "unverifiable", not
|
|
2923
|
-
* as "funded".
|
|
2924
|
-
*/
|
|
2925
|
-
async delegateCanFund(chainId, tokenAddress, amountAtomic, timeoutMs = 1e4) {
|
|
2926
|
-
if (!chainId || !this.delegateAddress) return null;
|
|
2927
|
-
const rpcUrl = this.chainRpcs[chainId];
|
|
2928
|
-
if (!rpcUrl) return null;
|
|
2929
|
-
try {
|
|
2930
|
-
const provider = createJsonRpcProvider(rpcUrl);
|
|
2931
|
-
const token = createErc20Contract(
|
|
2932
|
-
tokenAddress,
|
|
2933
|
-
["function balanceOf(address) view returns (uint256)"],
|
|
2934
|
-
provider
|
|
2935
|
-
);
|
|
2936
|
-
const balance = await Promise.race([
|
|
2937
|
-
token.balanceOf(this.delegateAddress),
|
|
2938
|
-
new Promise((resolve) => setTimeout(() => resolve(null), timeoutMs).unref?.())
|
|
2939
|
-
]);
|
|
2940
|
-
if (balance === null) return null;
|
|
2941
|
-
return balance >= BigInt(amountAtomic);
|
|
2942
|
-
} catch {
|
|
2943
|
-
return null;
|
|
2944
|
-
}
|
|
2945
|
-
}
|
|
2946
3594
|
throwIfNonSignableAuthorizationState(label, raw) {
|
|
2947
3595
|
if (raw.status === "pending_signature") return;
|
|
2948
|
-
|
|
2949
|
-
}
|
|
2950
|
-
throwPaymentStateError(label, raw) {
|
|
2951
|
-
const statusCode = PAYMENT_STATE_STATUS_CODES[raw.status] ?? 502;
|
|
2952
|
-
const state = this.paymentStateFromRaw(label, raw);
|
|
2953
|
-
if (state) {
|
|
2954
|
-
throw new HavenPaymentStateError(state.message, statusCode, state, raw);
|
|
2955
|
-
}
|
|
2956
|
-
if (raw.status === "pending_approval") {
|
|
2957
|
-
throw new HavenApiError(
|
|
2958
|
-
`${label} exceeds the on-chain allowance and was queued for owner approval (payment_id: ${raw.payment_id}).`,
|
|
2959
|
-
statusCode,
|
|
2960
|
-
raw
|
|
2961
|
-
);
|
|
2962
|
-
}
|
|
2963
|
-
if (raw.status === "expired") {
|
|
2964
|
-
throw new HavenApiError(
|
|
2965
|
-
`${label} expired before it could be completed (payment_id: ${raw.payment_id}).`,
|
|
2966
|
-
statusCode,
|
|
2967
|
-
raw
|
|
2968
|
-
);
|
|
2969
|
-
}
|
|
2970
|
-
const paymentId = raw.payment_id ? ` (payment_id: ${raw.payment_id})` : "";
|
|
2971
|
-
const message = raw.error ?? `${label} ${raw.status}${paymentId}`;
|
|
2972
|
-
throw new HavenApiError(message, statusCode, raw);
|
|
2973
|
-
}
|
|
2974
|
-
paymentStateFromRaw(label, raw) {
|
|
2975
|
-
if (!raw.payment_id || !raw.status) return null;
|
|
2976
|
-
const phase = raw.phase ?? phaseForStatus(raw.status);
|
|
2977
|
-
const nextAction = raw.next_action ?? nextActionForStatus(raw.status);
|
|
2978
|
-
if (!phase || !nextAction) return null;
|
|
2979
|
-
const amount = raw.amount ?? raw.requested ?? "";
|
|
2980
|
-
const token = raw.token ?? "";
|
|
2981
|
-
const message = raw.message ?? raw.error ?? messageForState(label, raw.status, raw.payment_id, nextAction);
|
|
2982
|
-
return {
|
|
2983
|
-
paymentId: raw.payment_id,
|
|
2984
|
-
kind: raw.kind === "payment_intent" ? "payment_intent" : "approval_request",
|
|
2985
|
-
rail: raw.rail ?? "direct",
|
|
2986
|
-
status: raw.status === "pending" ? "pending_approval" : raw.status,
|
|
2987
|
-
phase,
|
|
2988
|
-
nextAction,
|
|
2989
|
-
amount,
|
|
2990
|
-
token,
|
|
2991
|
-
resourceUrl: raw.resource_url ?? null,
|
|
2992
|
-
merchantAddress: raw.merchant_address ?? raw.merchant_to ?? null,
|
|
2993
|
-
txHash: raw.tx_hash ?? null,
|
|
2994
|
-
expiresAt: raw.expires_at ?? "",
|
|
2995
|
-
chainId: raw.chain_id ?? 0,
|
|
2996
|
-
message,
|
|
2997
|
-
amountAtomic: raw.amount_atomic ?? raw.x402?.amount_atomic ?? raw.mpp?.amount_atomic ?? null,
|
|
2998
|
-
asset: raw.asset ?? raw.x402?.asset ?? raw.mpp?.asset ?? null,
|
|
2999
|
-
network: raw.network ?? raw.x402?.network ?? raw.mpp?.network ?? null,
|
|
3000
|
-
description: raw.description ?? raw.x402?.description ?? raw.mpp?.description ?? null,
|
|
3001
|
-
idempotencyKey: raw.idempotency_key ?? raw.x402?.idempotency_key ?? raw.mpp?.idempotency_key ?? null,
|
|
3002
|
-
x402: raw.x402 ? {
|
|
3003
|
-
amountAtomic: raw.x402.amount_atomic ?? raw.amount_atomic ?? null,
|
|
3004
|
-
asset: raw.x402.asset ?? raw.asset ?? null,
|
|
3005
|
-
network: raw.x402.network ?? raw.network ?? null,
|
|
3006
|
-
resourceUrl: raw.x402.resource_url ?? raw.resource_url ?? null,
|
|
3007
|
-
merchantAddress: raw.x402.merchant_address ?? raw.merchant_address ?? raw.merchant_to ?? null,
|
|
3008
|
-
description: raw.x402.description ?? raw.description ?? null,
|
|
3009
|
-
idempotencyKey: raw.x402.idempotency_key ?? raw.idempotency_key ?? null
|
|
3010
|
-
} : void 0,
|
|
3011
|
-
mpp: raw.mpp ? {
|
|
3012
|
-
amountAtomic: raw.mpp.amount_atomic ?? raw.amount_atomic ?? null,
|
|
3013
|
-
asset: raw.mpp.asset ?? raw.asset ?? null,
|
|
3014
|
-
network: raw.mpp.network ?? raw.network ?? null,
|
|
3015
|
-
resourceUrl: raw.mpp.resource_url ?? raw.resource_url ?? null,
|
|
3016
|
-
merchantAddress: raw.mpp.merchant_address ?? raw.merchant_address ?? raw.merchant_to ?? null,
|
|
3017
|
-
description: raw.mpp.description ?? raw.description ?? null,
|
|
3018
|
-
idempotencyKey: raw.mpp.idempotency_key ?? raw.idempotency_key ?? null,
|
|
3019
|
-
challengeId: raw.mpp.challenge_id ?? raw.challenge_id ?? null
|
|
3020
|
-
} : void 0
|
|
3021
|
-
};
|
|
3022
|
-
}
|
|
3023
|
-
x402PayerAddress() {
|
|
3024
|
-
return this.delegateAddress ?? this.x402Wallet;
|
|
3025
|
-
}
|
|
3026
|
-
snapshotX402Request(url, init) {
|
|
3027
|
-
return {
|
|
3028
|
-
url,
|
|
3029
|
-
method: init?.method ?? "GET",
|
|
3030
|
-
headers: Array.from(new Headers(init?.headers).entries()),
|
|
3031
|
-
body: this.snapshotRequestBody(init?.body)
|
|
3032
|
-
};
|
|
3033
|
-
}
|
|
3034
|
-
snapshotRequestBody(body) {
|
|
3035
|
-
if (body == null) return void 0;
|
|
3036
|
-
if (typeof body === "string") return body;
|
|
3037
|
-
if (body instanceof URLSearchParams) return body.toString();
|
|
3038
|
-
throw new HavenApiError(
|
|
3039
|
-
"Quote helpers can only capture resumable request bodies that are strings or URLSearchParams. For streams, blobs, or binary bodies, preserve the original request yourself and call the matching resume method with fresh init.",
|
|
3040
|
-
400
|
|
3041
|
-
);
|
|
3042
|
-
}
|
|
3043
|
-
requestInitFromSnapshot(request) {
|
|
3044
|
-
return {
|
|
3045
|
-
method: request.method,
|
|
3046
|
-
headers: request.headers,
|
|
3047
|
-
body: request.body
|
|
3048
|
-
};
|
|
3049
|
-
}
|
|
3050
|
-
withX402Wallet(init, wallet = this.x402PayerAddress()) {
|
|
3051
|
-
if (!wallet) return init;
|
|
3052
|
-
const headers = new Headers(init?.headers);
|
|
3053
|
-
if (!headers.has("x402-wallet")) {
|
|
3054
|
-
headers.set("x402-wallet", wallet);
|
|
3055
|
-
}
|
|
3056
|
-
return {
|
|
3057
|
-
...init,
|
|
3058
|
-
headers
|
|
3059
|
-
};
|
|
3060
|
-
}
|
|
3061
|
-
buildX402Quote(paymentRequired, request, idempotencyKey, mcpTransport) {
|
|
3062
|
-
const option = selectStandardPaymentOption(paymentRequired.accepts);
|
|
3063
|
-
if (!option) {
|
|
3064
|
-
throw new HavenApiError(
|
|
3065
|
-
"No compatible payment option found in x402 requirements. Haven supports standard x402 exact payments on Base USDC.",
|
|
3066
|
-
400
|
|
3067
|
-
);
|
|
3068
|
-
}
|
|
3069
|
-
const token = resolveTokenFromAddress(option.asset, option.network);
|
|
3070
|
-
return {
|
|
3071
|
-
rail: "x402",
|
|
3072
|
-
idempotencyKey: idempotencyKey ?? buildX402IdempotencyKey(paymentRequired, option),
|
|
3073
|
-
paymentRequired,
|
|
3074
|
-
accepted: option,
|
|
3075
|
-
request,
|
|
3076
|
-
...mcpTransport ? { mcpTransport } : {},
|
|
3077
|
-
resourceUrl: paymentRequired.resource.url,
|
|
3078
|
-
description: paymentRequired.resource.description ?? option.description ?? null,
|
|
3079
|
-
mimeType: paymentRequired.resource.mimeType ?? option.mimeType ?? null,
|
|
3080
|
-
amountAtomic: x402AuthorizationAmount(option),
|
|
3081
|
-
amount: decimalFromUsdcAtomic(x402AuthorizationAmount(option)),
|
|
3082
|
-
token: token?.symbol ?? "USDC",
|
|
3083
|
-
// #1351: null when the asset is unrecognised on this network — the
|
|
3084
|
-
// `token` fallback above is a LABEL, not evidence of 6 decimals, and a
|
|
3085
|
-
// human-denominated cap must fail closed rather than convert against a
|
|
3086
|
-
// guess. Same resolution as `token`, so the two never disagree.
|
|
3087
|
-
decimals: token?.decimals ?? null,
|
|
3088
|
-
asset: option.asset,
|
|
3089
|
-
network: option.network,
|
|
3090
|
-
chainId: chainIdOrNull(option.network),
|
|
3091
|
-
merchantAddress: option.payTo,
|
|
3092
|
-
maxTimeoutSeconds: option.maxTimeoutSeconds
|
|
3093
|
-
};
|
|
3094
|
-
}
|
|
3095
|
-
async detectX402McpTransport(url, paymentRequired, response) {
|
|
3096
|
-
if (isMcpUrl(url)) {
|
|
3097
|
-
return { handshakeRequired: true, source: "path" };
|
|
3098
|
-
}
|
|
3099
|
-
if (paymentRequired.extensions?.bazaar != null) {
|
|
3100
|
-
return { handshakeRequired: true, source: "bazaar" };
|
|
3101
|
-
}
|
|
3102
|
-
if (await responseHasBazaarExtension(response)) {
|
|
3103
|
-
return { handshakeRequired: true, source: "bazaar" };
|
|
3104
|
-
}
|
|
3105
|
-
return void 0;
|
|
3106
|
-
}
|
|
3107
|
-
buildX402ResumeState(input) {
|
|
3108
|
-
const token = resolveTokenFromAddress(input.accepted.asset, input.accepted.network);
|
|
3109
|
-
return {
|
|
3110
|
-
rail: "x402",
|
|
3111
|
-
paymentId: input.paymentId,
|
|
3112
|
-
idempotencyKey: input.idempotencyKey,
|
|
3113
|
-
paymentRequired: input.paymentRequired,
|
|
3114
|
-
accepted: input.accepted,
|
|
3115
|
-
url: input.request?.url ?? input.paymentRequired.resource.url,
|
|
3116
|
-
request: input.request,
|
|
3117
|
-
resourceUrl: input.paymentRequired.resource.url,
|
|
3118
|
-
description: input.paymentRequired.resource.description ?? input.accepted.description ?? null,
|
|
3119
|
-
amountAtomic: x402AuthorizationAmount(input.accepted),
|
|
3120
|
-
amount: decimalFromUsdcAtomic(x402AuthorizationAmount(input.accepted)),
|
|
3121
|
-
token: token?.symbol ?? "USDC",
|
|
3122
|
-
asset: input.accepted.asset,
|
|
3123
|
-
network: input.accepted.network,
|
|
3124
|
-
chainId: chainIdOrNull(input.accepted.network),
|
|
3125
|
-
merchantAddress: input.accepted.payTo
|
|
3126
|
-
};
|
|
3127
|
-
}
|
|
3128
|
-
// #1328: attachResumeState's 'mpp' branch (buildMppQuote / buildMppResumeState
|
|
3129
|
-
// / attachMppResumeState) is retired along with the rest of the MPP-demo
|
|
3130
|
-
// client surface — every remaining caller passes rail: 'x402' only, so this
|
|
3131
|
-
// is now a direct alias for attachX402ResumeState rather than a dispatcher.
|
|
3132
|
-
attachResumeState(err, input) {
|
|
3133
|
-
this.attachX402ResumeState(
|
|
3134
|
-
err,
|
|
3135
|
-
input.paymentRequired,
|
|
3136
|
-
input.accepted,
|
|
3137
|
-
input.idempotencyKey,
|
|
3138
|
-
input.request
|
|
3139
|
-
);
|
|
3140
|
-
}
|
|
3141
|
-
attachX402ResumeState(err, paymentRequired, accepted, idempotencyKey, request) {
|
|
3142
|
-
if (!(err instanceof HavenPaymentStateError)) return;
|
|
3143
|
-
if (err.state.rail !== "x402") return;
|
|
3144
|
-
err.resumeState = this.buildX402ResumeState({
|
|
3145
|
-
paymentId: err.state.paymentId,
|
|
3146
|
-
paymentRequired,
|
|
3147
|
-
accepted,
|
|
3148
|
-
idempotencyKey,
|
|
3149
|
-
request
|
|
3150
|
-
});
|
|
3596
|
+
throwPaymentStateError(label, raw);
|
|
3151
3597
|
}
|
|
3152
3598
|
// ── Tool Execution (for agent frameworks) ────────────────────────
|
|
3153
3599
|
/**
|
|
@@ -3179,19 +3625,19 @@ var HavenClient = class {
|
|
|
3179
3625
|
error: result.errorMessage
|
|
3180
3626
|
};
|
|
3181
3627
|
} catch (err) {
|
|
3182
|
-
return
|
|
3628
|
+
return toolError(err);
|
|
3183
3629
|
}
|
|
3184
3630
|
}
|
|
3185
3631
|
if (toolName === "authorize_x402_payment") {
|
|
3186
3632
|
const { url, payTo, amount, asset, network, description, idempotencyKey } = input;
|
|
3187
3633
|
try {
|
|
3188
3634
|
const receipt = await this.authorizeX402(
|
|
3189
|
-
|
|
3635
|
+
toolX402PaymentRequired({ url, payTo, amount, asset, network, description }),
|
|
3190
3636
|
{ idempotencyKey }
|
|
3191
3637
|
);
|
|
3192
|
-
return
|
|
3638
|
+
return x402ToolReceipt(receipt);
|
|
3193
3639
|
} catch (err) {
|
|
3194
|
-
return
|
|
3640
|
+
return toolError(err);
|
|
3195
3641
|
}
|
|
3196
3642
|
}
|
|
3197
3643
|
if (toolName === "resume_x402_payment") {
|
|
@@ -3199,12 +3645,12 @@ var HavenClient = class {
|
|
|
3199
3645
|
try {
|
|
3200
3646
|
const receipt = await this.resumeAuthorizedX402({
|
|
3201
3647
|
paymentId: payment_id,
|
|
3202
|
-
paymentRequired:
|
|
3648
|
+
paymentRequired: toolX402PaymentRequired({ url, payTo, amount, asset, network, description }),
|
|
3203
3649
|
idempotencyKey
|
|
3204
3650
|
});
|
|
3205
|
-
return
|
|
3651
|
+
return x402ToolReceipt(receipt);
|
|
3206
3652
|
} catch (err) {
|
|
3207
|
-
return
|
|
3653
|
+
return toolError(err);
|
|
3208
3654
|
}
|
|
3209
3655
|
}
|
|
3210
3656
|
if (toolName === "get_payment_status") {
|
|
@@ -3239,302 +3685,17 @@ var HavenClient = class {
|
|
|
3239
3685
|
}
|
|
3240
3686
|
throw new Error(`Unknown tool: ${toolName}`);
|
|
3241
3687
|
}
|
|
3242
|
-
toolX402PaymentRequired(input) {
|
|
3243
|
-
return {
|
|
3244
|
-
x402Version: 2,
|
|
3245
|
-
resource: { url: input.url, description: input.description },
|
|
3246
|
-
accepts: [
|
|
3247
|
-
{
|
|
3248
|
-
scheme: "exact",
|
|
3249
|
-
network: input.network,
|
|
3250
|
-
amount: input.amount,
|
|
3251
|
-
asset: input.asset,
|
|
3252
|
-
payTo: input.payTo,
|
|
3253
|
-
maxTimeoutSeconds: 30
|
|
3254
|
-
}
|
|
3255
|
-
]
|
|
3256
|
-
};
|
|
3257
|
-
}
|
|
3258
|
-
x402ToolReceipt(receipt) {
|
|
3259
|
-
return {
|
|
3260
|
-
success: true,
|
|
3261
|
-
payment_id: receipt.paymentId,
|
|
3262
|
-
tx_hash: receipt.txHash,
|
|
3263
|
-
token: receipt.token,
|
|
3264
|
-
amount: receipt.amount,
|
|
3265
|
-
to: receipt.to,
|
|
3266
|
-
resource_url: receipt.resourceUrl,
|
|
3267
|
-
explorer_url: receipt.explorerUrl,
|
|
3268
|
-
payment_header: receipt.paymentHeader,
|
|
3269
|
-
merchant_to: receipt.merchantTo,
|
|
3270
|
-
payer: receipt.payer,
|
|
3271
|
-
chain_id: receipt.chainId,
|
|
3272
|
-
haven: receipt.haven,
|
|
3273
|
-
merchant: receipt.merchant,
|
|
3274
|
-
x402: receipt.x402
|
|
3275
|
-
};
|
|
3276
|
-
}
|
|
3277
|
-
toolError(err) {
|
|
3278
|
-
if (err instanceof HavenPaymentStateError) {
|
|
3279
|
-
return {
|
|
3280
|
-
success: false,
|
|
3281
|
-
payment_id: err.state.paymentId,
|
|
3282
|
-
kind: err.state.kind,
|
|
3283
|
-
rail: err.state.rail,
|
|
3284
|
-
status: err.state.status,
|
|
3285
|
-
phase: err.state.phase,
|
|
3286
|
-
next_action: err.state.nextAction,
|
|
3287
|
-
tx_hash: err.state.txHash,
|
|
3288
|
-
token: err.state.token,
|
|
3289
|
-
amount: err.state.amount,
|
|
3290
|
-
resource_url: err.state.resourceUrl,
|
|
3291
|
-
merchant_address: err.state.merchantAddress,
|
|
3292
|
-
amount_atomic: err.state.amountAtomic,
|
|
3293
|
-
asset: err.state.asset,
|
|
3294
|
-
network: err.state.network,
|
|
3295
|
-
description: err.state.description,
|
|
3296
|
-
idempotency_key: err.state.idempotencyKey,
|
|
3297
|
-
x402: err.state.x402 ? {
|
|
3298
|
-
amount_atomic: err.state.x402.amountAtomic,
|
|
3299
|
-
asset: err.state.x402.asset,
|
|
3300
|
-
network: err.state.x402.network,
|
|
3301
|
-
resource_url: err.state.x402.resourceUrl,
|
|
3302
|
-
merchant_address: err.state.x402.merchantAddress,
|
|
3303
|
-
description: err.state.x402.description,
|
|
3304
|
-
idempotency_key: err.state.x402.idempotencyKey
|
|
3305
|
-
} : void 0,
|
|
3306
|
-
mpp: err.state.mpp ? {
|
|
3307
|
-
amount_atomic: err.state.mpp.amountAtomic,
|
|
3308
|
-
asset: err.state.mpp.asset,
|
|
3309
|
-
network: err.state.mpp.network,
|
|
3310
|
-
resource_url: err.state.mpp.resourceUrl,
|
|
3311
|
-
merchant_address: err.state.mpp.merchantAddress,
|
|
3312
|
-
description: err.state.mpp.description,
|
|
3313
|
-
idempotency_key: err.state.mpp.idempotencyKey,
|
|
3314
|
-
challenge_id: err.state.mpp.challengeId
|
|
3315
|
-
} : void 0,
|
|
3316
|
-
resume_state: err.resumeState,
|
|
3317
|
-
expires_at: err.state.expiresAt,
|
|
3318
|
-
chain_id: err.state.chainId,
|
|
3319
|
-
message: err.state.message,
|
|
3320
|
-
error: err.message
|
|
3321
|
-
};
|
|
3322
|
-
}
|
|
3323
|
-
if (err instanceof HavenApiError) {
|
|
3324
|
-
return {
|
|
3325
|
-
success: false,
|
|
3326
|
-
status_code: err.statusCode,
|
|
3327
|
-
error: err.message,
|
|
3328
|
-
body: err.body
|
|
3329
|
-
};
|
|
3330
|
-
}
|
|
3331
|
-
return {
|
|
3332
|
-
success: false,
|
|
3333
|
-
error: err instanceof Error ? err.message : String(err)
|
|
3334
|
-
};
|
|
3335
|
-
}
|
|
3336
3688
|
// ── HTTP Helpers ─────────────────────────────────────────────────
|
|
3337
3689
|
async post(path, body) {
|
|
3338
|
-
return this.
|
|
3690
|
+
return this.havenApi.post(path, body);
|
|
3339
3691
|
}
|
|
3340
3692
|
async get(path) {
|
|
3341
|
-
return this.
|
|
3342
|
-
}
|
|
3343
|
-
/**
|
|
3344
|
-
* #1300: every MERCHANT-facing fetch goes through here. Haven API calls
|
|
3345
|
-
* have always been bounded (request() below); the merchant probes/retries
|
|
3346
|
-
* called globalThis.fetch bare, so a slow-loris merchant could hold a tool
|
|
3347
|
-
* call open forever. A caller-supplied signal still applies (combined via
|
|
3348
|
-
* AbortSignal.any); a timeout abort surfaces as a clear HavenApiError 504
|
|
3349
|
-
* naming the URL rather than a bare AbortError.
|
|
3350
|
-
*/
|
|
3351
|
-
async merchantFetch(url, init = {}, timeoutMs = this.merchantTimeout) {
|
|
3352
|
-
const timeoutSignal = AbortSignal.timeout(timeoutMs);
|
|
3353
|
-
const signal = init.signal ? AbortSignal.any([init.signal, timeoutSignal]) : timeoutSignal;
|
|
3354
|
-
try {
|
|
3355
|
-
return await globalThis.fetch(url, { ...init, signal });
|
|
3356
|
-
} catch (err) {
|
|
3357
|
-
if (timeoutSignal.aborted) {
|
|
3358
|
-
throw new MerchantTimeoutError(`Merchant request timed out after ${timeoutMs}ms: ${url}`);
|
|
3359
|
-
}
|
|
3360
|
-
throw err;
|
|
3361
|
-
}
|
|
3362
|
-
}
|
|
3363
|
-
async request(method, path, body) {
|
|
3364
|
-
const url = `${this.baseUrl}${path}`;
|
|
3365
|
-
const controller = new AbortController();
|
|
3366
|
-
const timeout = setTimeout(() => controller.abort(), this.requestTimeout);
|
|
3367
|
-
try {
|
|
3368
|
-
const contextHeaders = this.requestContext.getStore()?.headers ?? {};
|
|
3369
|
-
const res = await fetch(url, {
|
|
3370
|
-
method,
|
|
3371
|
-
headers: {
|
|
3372
|
-
"Content-Type": "application/json",
|
|
3373
|
-
"Authorization": `Bearer ${this.apiKey}`,
|
|
3374
|
-
...this.defaultHeaders,
|
|
3375
|
-
...contextHeaders
|
|
3376
|
-
},
|
|
3377
|
-
body: body ? JSON.stringify(body) : void 0,
|
|
3378
|
-
signal: controller.signal
|
|
3379
|
-
});
|
|
3380
|
-
const data = await res.json();
|
|
3381
|
-
if (!res.ok) {
|
|
3382
|
-
const record = data;
|
|
3383
|
-
const errorText = typeof record.error === "string" ? record.error : void 0;
|
|
3384
|
-
const rawDetails = record.details ?? record.detail;
|
|
3385
|
-
const detailsText = typeof rawDetails === "string" ? rawDetails : rawDetails != null ? JSON.stringify(rawDetails) : void 0;
|
|
3386
|
-
const message = errorText && detailsText ? `${errorText}: ${detailsText}` : errorText ?? detailsText ?? `API request failed`;
|
|
3387
|
-
throw new HavenApiError(message, res.status, data);
|
|
3388
|
-
}
|
|
3389
|
-
return data;
|
|
3390
|
-
} catch (err) {
|
|
3391
|
-
if (err instanceof HavenApiError) throw err;
|
|
3392
|
-
if (err instanceof Error && err.name === "AbortError") {
|
|
3393
|
-
throw new HavenApiError(`Request to ${path} timed out`, 408);
|
|
3394
|
-
}
|
|
3395
|
-
throw new HavenApiError(
|
|
3396
|
-
`Request to ${path} failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
3397
|
-
0
|
|
3398
|
-
);
|
|
3399
|
-
} finally {
|
|
3400
|
-
clearTimeout(timeout);
|
|
3401
|
-
}
|
|
3402
|
-
}
|
|
3403
|
-
// ── Mapping Helpers ──────────────────────────────────────────────
|
|
3404
|
-
mapPaymentResult(raw) {
|
|
3405
|
-
return {
|
|
3406
|
-
paymentId: raw.payment_id,
|
|
3407
|
-
status: raw.status,
|
|
3408
|
-
token: raw.token,
|
|
3409
|
-
amount: raw.amount,
|
|
3410
|
-
to: raw.to,
|
|
3411
|
-
txHash: raw.tx_hash,
|
|
3412
|
-
errorMessage: raw.error_message,
|
|
3413
|
-
explorerUrl: raw.explorer_url ?? (raw.tx_hash ? buildExplorerUrl(raw.chain_id, raw.tx_hash) : null),
|
|
3414
|
-
fee: raw.fee ? {
|
|
3415
|
-
amount: raw.fee.amount,
|
|
3416
|
-
token: raw.fee.token,
|
|
3417
|
-
basisPoints: raw.fee.basis_points,
|
|
3418
|
-
applied: raw.fee.applied
|
|
3419
|
-
} : null,
|
|
3420
|
-
createdAt: raw.created_at,
|
|
3421
|
-
signedAt: raw.signed_at,
|
|
3422
|
-
submittedAt: raw.submitted_at,
|
|
3423
|
-
confirmedAt: raw.confirmed_at,
|
|
3424
|
-
expiresAt: raw.expires_at
|
|
3425
|
-
};
|
|
3426
|
-
}
|
|
3427
|
-
mapPaymentStatusResult(raw) {
|
|
3428
|
-
return {
|
|
3429
|
-
paymentId: raw.payment_id,
|
|
3430
|
-
kind: raw.kind,
|
|
3431
|
-
rail: raw.rail,
|
|
3432
|
-
status: raw.status,
|
|
3433
|
-
phase: raw.phase,
|
|
3434
|
-
nextAction: raw.next_action,
|
|
3435
|
-
amount: raw.amount,
|
|
3436
|
-
token: raw.token,
|
|
3437
|
-
resourceUrl: raw.resource_url,
|
|
3438
|
-
merchantAddress: raw.merchant_address,
|
|
3439
|
-
payerAddress: raw.payer_address ?? null,
|
|
3440
|
-
txHash: raw.tx_hash,
|
|
3441
|
-
expiresAt: raw.expires_at,
|
|
3442
|
-
chainId: raw.chain_id,
|
|
3443
|
-
message: raw.message,
|
|
3444
|
-
fee: raw.fee ? {
|
|
3445
|
-
amount: raw.fee.amount,
|
|
3446
|
-
token: raw.fee.token,
|
|
3447
|
-
basisPoints: raw.fee.basis_points,
|
|
3448
|
-
applied: raw.fee.applied
|
|
3449
|
-
} : null,
|
|
3450
|
-
amountAtomic: raw.amount_atomic ?? raw.x402?.amount_atomic ?? null,
|
|
3451
|
-
asset: raw.asset ?? raw.x402?.asset ?? null,
|
|
3452
|
-
network: raw.network ?? raw.x402?.network ?? null,
|
|
3453
|
-
description: raw.description ?? raw.x402?.description ?? null,
|
|
3454
|
-
idempotencyKey: raw.idempotency_key ?? raw.x402?.idempotency_key ?? null,
|
|
3455
|
-
x402: raw.x402 ? {
|
|
3456
|
-
amountAtomic: raw.x402.amount_atomic ?? raw.amount_atomic ?? null,
|
|
3457
|
-
asset: raw.x402.asset ?? raw.asset ?? null,
|
|
3458
|
-
network: raw.x402.network ?? raw.network ?? null,
|
|
3459
|
-
resourceUrl: raw.x402.resource_url ?? raw.resource_url,
|
|
3460
|
-
merchantAddress: raw.x402.merchant_address ?? raw.merchant_address,
|
|
3461
|
-
description: raw.x402.description ?? raw.description ?? null,
|
|
3462
|
-
idempotencyKey: raw.x402.idempotency_key ?? raw.idempotency_key ?? null
|
|
3463
|
-
} : void 0
|
|
3464
|
-
};
|
|
3465
|
-
}
|
|
3466
|
-
mapPaymentReceipt(raw) {
|
|
3467
|
-
const receipt = {
|
|
3468
|
-
id: raw.id,
|
|
3469
|
-
paymentId: raw.payment_id,
|
|
3470
|
-
rail: raw.rail,
|
|
3471
|
-
proofStatus: raw.proof_status,
|
|
3472
|
-
txHash: raw.tx_hash,
|
|
3473
|
-
chainId: raw.chain_id,
|
|
3474
|
-
resourceUrl: raw.resource_url,
|
|
3475
|
-
merchantAddress: raw.merchant_address,
|
|
3476
|
-
payerAddress: raw.payer_address,
|
|
3477
|
-
settlementAddress: raw.settlement_address,
|
|
3478
|
-
tokenSymbol: raw.token_symbol,
|
|
3479
|
-
tokenAddress: raw.token_address,
|
|
3480
|
-
amountRaw: raw.amount_raw,
|
|
3481
|
-
amount: raw.amount_human,
|
|
3482
|
-
challengeId: raw.challenge_id,
|
|
3483
|
-
idempotencyKey: raw.idempotency_key,
|
|
3484
|
-
challengePayload: raw.challenge_payload,
|
|
3485
|
-
selectedPayment: raw.selected_payment,
|
|
3486
|
-
paymentProofHeaderName: raw.payment_proof_header_name,
|
|
3487
|
-
protocolReceiptHeaderName: raw.protocol_receipt_header_name,
|
|
3488
|
-
protocolReceiptPayload: raw.protocol_receipt_payload,
|
|
3489
|
-
merchantStatus: raw.merchant_status,
|
|
3490
|
-
confirmedAt: raw.confirmed_at,
|
|
3491
|
-
createdAt: raw.created_at,
|
|
3492
|
-
updatedAt: raw.updated_at
|
|
3493
|
-
};
|
|
3494
|
-
if ("payment_intent_id" in raw) {
|
|
3495
|
-
receipt.paymentIntentId = raw.payment_intent_id ?? null;
|
|
3496
|
-
}
|
|
3497
|
-
if ("approval_request_id" in raw) {
|
|
3498
|
-
receipt.approvalRequestId = raw.approval_request_id ?? null;
|
|
3499
|
-
}
|
|
3500
|
-
return receipt;
|
|
3693
|
+
return this.havenApi.get(path);
|
|
3501
3694
|
}
|
|
3502
3695
|
};
|
|
3503
3696
|
function sleep(ms) {
|
|
3504
3697
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
3505
3698
|
}
|
|
3506
|
-
function getPaymentHeaderValidBefore(paymentHeader) {
|
|
3507
|
-
try {
|
|
3508
|
-
const payment = decodeBase64Json(
|
|
3509
|
-
paymentHeader
|
|
3510
|
-
);
|
|
3511
|
-
const payload = payment.payload;
|
|
3512
|
-
const validBeforeSeconds = Number(payload.authorization?.validBefore);
|
|
3513
|
-
if (Number.isFinite(validBeforeSeconds)) return validBeforeSeconds * 1e3;
|
|
3514
|
-
} catch {
|
|
3515
|
-
}
|
|
3516
|
-
return 0;
|
|
3517
|
-
}
|
|
3518
|
-
function parseProtocolReceiptHeader(value) {
|
|
3519
|
-
try {
|
|
3520
|
-
return decodeBase64Json(value);
|
|
3521
|
-
} catch {
|
|
3522
|
-
try {
|
|
3523
|
-
return JSON.parse(value);
|
|
3524
|
-
} catch {
|
|
3525
|
-
return void 0;
|
|
3526
|
-
}
|
|
3527
|
-
}
|
|
3528
|
-
}
|
|
3529
|
-
async function captureMerchantResponse(response) {
|
|
3530
|
-
const merchant_body = await response.text().catch(() => "");
|
|
3531
|
-
return {
|
|
3532
|
-
merchant_status: response.status,
|
|
3533
|
-
merchant_status_text: response.statusText,
|
|
3534
|
-
merchant_headers: Object.fromEntries(response.headers.entries()),
|
|
3535
|
-
merchant_body
|
|
3536
|
-
};
|
|
3537
|
-
}
|
|
3538
3699
|
|
|
3539
3700
|
// src/tool-descriptions.ts
|
|
3540
3701
|
function composeDescription(d) {
|