@mysten-incubation/suipay-mcp 0.3.0-dev.34 → 0.3.0-dev.36
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/README.md +18 -11
- package/dist/bin/suipay.js +2 -2
- package/dist/{chunk-TCPF3ONF.js → chunk-CEZ3NFAR.js} +1 -1
- package/dist/{chunk-5S4MJN2A.js → chunk-FUMH3XYC.js} +430 -79
- package/dist/{http-DRQ838c6.d.ts → http-BwtYzkbN.d.ts} +85 -13
- package/dist/http.d.ts +1 -1
- package/dist/http.js +7 -3
- package/dist/index.d.ts +2 -2
- package/dist/index.js +4 -2
- package/package.json +3 -3
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// src/http.ts
|
|
2
2
|
import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js";
|
|
3
|
+
import { ErrorCode } from "@modelcontextprotocol/sdk/types.js";
|
|
3
4
|
|
|
4
5
|
// src/server.ts
|
|
5
6
|
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
@@ -246,6 +247,31 @@ var ACCOUNT_CODES = {
|
|
|
246
247
|
nextAction: "fix_request",
|
|
247
248
|
user: { title: "Agent address not valid", message: "Enter a valid agent address and try again." }
|
|
248
249
|
},
|
|
250
|
+
"account.deposit_cap_exceeded": {
|
|
251
|
+
surfaces: ["gateway", "sdk", "browser"],
|
|
252
|
+
status: 403,
|
|
253
|
+
title: "Deposit would exceed the vault beta cap",
|
|
254
|
+
paymentState: "not_charged",
|
|
255
|
+
retry: "never",
|
|
256
|
+
nextAction: "fix_request",
|
|
257
|
+
user: {
|
|
258
|
+
title: "Over the beta vault limit",
|
|
259
|
+
message: "This deposit would put your vault over the Mainnet Beta cap. Deposit less, or withdraw first."
|
|
260
|
+
}
|
|
261
|
+
},
|
|
262
|
+
"account.deposits_paused": {
|
|
263
|
+
surfaces: ["gateway", "sdk", "browser"],
|
|
264
|
+
status: 403,
|
|
265
|
+
title: "Deposits are paused",
|
|
266
|
+
paymentState: "not_charged",
|
|
267
|
+
retry: "after_delay",
|
|
268
|
+
retryAfterSeconds: 30,
|
|
269
|
+
nextAction: "retry_later",
|
|
270
|
+
user: {
|
|
271
|
+
title: "Deposits paused",
|
|
272
|
+
message: "New deposits are paused right now. Withdrawals and spending still work. Try again after deposits resume."
|
|
273
|
+
}
|
|
274
|
+
},
|
|
249
275
|
"account.finalize_mismatch": {
|
|
250
276
|
surfaces: ["gateway"],
|
|
251
277
|
status: 409,
|
|
@@ -2316,7 +2342,7 @@ var SERVICE_CODES = {
|
|
|
2316
2342
|
// Also `browser`: a directory page that only got a pass/fail boolean off
|
|
2317
2343
|
// a server-side fetch restates this exact condition locally — the user
|
|
2318
2344
|
// copy is equally true regardless of which layer noticed.
|
|
2319
|
-
surfaces: ["gateway", "console_server", "mcp_hosted", "browser"],
|
|
2345
|
+
surfaces: ["gateway", "seller", "console_server", "mcp_hosted", "browser"],
|
|
2320
2346
|
status: 503,
|
|
2321
2347
|
title: "Service temporarily unavailable",
|
|
2322
2348
|
paymentState: "not_charged",
|
|
@@ -2325,6 +2351,16 @@ var SERVICE_CODES = {
|
|
|
2325
2351
|
nextAction: "retry_later",
|
|
2326
2352
|
user: { title: "Temporarily unavailable", message: "This isn't available right now. Try again in a moment." }
|
|
2327
2353
|
},
|
|
2354
|
+
"service.halted": {
|
|
2355
|
+
surfaces: ["gateway", "seller", "console_server", "mcp_hosted", "browser"],
|
|
2356
|
+
status: 503,
|
|
2357
|
+
title: "Services halted temporarily",
|
|
2358
|
+
paymentState: "not_charged",
|
|
2359
|
+
retry: "after_delay",
|
|
2360
|
+
retryAfterSeconds: 5,
|
|
2361
|
+
nextAction: "retry_later",
|
|
2362
|
+
user: { title: "Services halted temporarily", message: "Try again in a moment." }
|
|
2363
|
+
},
|
|
2328
2364
|
"service.not_ready": {
|
|
2329
2365
|
surfaces: ["gateway", "seller", "console_server"],
|
|
2330
2366
|
status: 503,
|
|
@@ -3023,6 +3059,22 @@ function parseErrorPayload(input) {
|
|
|
3023
3059
|
}
|
|
3024
3060
|
return null;
|
|
3025
3061
|
}
|
|
3062
|
+
var DEFINITE_PRE_BROADCAST_REFUSALS = /* @__PURE__ */ new Set([
|
|
3063
|
+
"auth.spend_not_whitelisted",
|
|
3064
|
+
// Confirmed operator spend pause: gateway refuses before broadcast after
|
|
3065
|
+
// first-use bind or a chain_failed + ESpendPaused abort. Generic
|
|
3066
|
+
// `service.unavailable` stays out — session-revocation and other pre-check
|
|
3067
|
+
// paths can emit it after a lost execute may already have broadcast.
|
|
3068
|
+
"service.halted",
|
|
3069
|
+
// Confirmed operator deposit pause on dry-run / chain_failed + EDepositPaused.
|
|
3070
|
+
"account.deposits_paused"
|
|
3071
|
+
]);
|
|
3072
|
+
function isValidatedProblemRefusal(parsed, allowedCodes) {
|
|
3073
|
+
return parsed !== null && parsed.source === "problem" && parsed.paymentState === "not_charged" && isErrorCode(parsed.code) && allowedCodes.has(parsed.code) && definitionOf(parsed.code).status === parsed.status;
|
|
3074
|
+
}
|
|
3075
|
+
function isDefinitePreBroadcastRefusal(parsed) {
|
|
3076
|
+
return isValidatedProblemRefusal(parsed, DEFINITE_PRE_BROADCAST_REFUSALS);
|
|
3077
|
+
}
|
|
3026
3078
|
function readX402Failure(headers) {
|
|
3027
3079
|
for (const name of X402_RESPONSE_HEADERS) {
|
|
3028
3080
|
const raw = readHeader(headers, name);
|
|
@@ -3226,6 +3278,8 @@ var CONTEXT_ALLOWLIST = /* @__PURE__ */ new Set([
|
|
|
3226
3278
|
"sessionDecision",
|
|
3227
3279
|
"settleCode",
|
|
3228
3280
|
"signedRequest",
|
|
3281
|
+
// ADR-0103: v1/v2 dual-accept drain counter on verified delegate/bootstrap lines.
|
|
3282
|
+
"protocolVersion",
|
|
3229
3283
|
"signerCode",
|
|
3230
3284
|
"consentCode",
|
|
3231
3285
|
"publicReason",
|
|
@@ -4305,22 +4359,38 @@ function createSpendAccountChain(options) {
|
|
|
4305
4359
|
const tx = new Transaction2();
|
|
4306
4360
|
tx.setSender(input.owner);
|
|
4307
4361
|
const funding = await fundingCoin(tx, input.owner, input.coinType, BigInt(input.funding));
|
|
4308
|
-
|
|
4309
|
-
|
|
4310
|
-
|
|
4311
|
-
|
|
4312
|
-
|
|
4362
|
+
if (input.exclusionDirectoryId) {
|
|
4363
|
+
tx.moveCall({
|
|
4364
|
+
target: target("create_account_limited"),
|
|
4365
|
+
typeArguments: [input.coinType],
|
|
4366
|
+
arguments: [tx.object(input.exclusionDirectoryId), funding]
|
|
4367
|
+
});
|
|
4368
|
+
} else {
|
|
4369
|
+
tx.moveCall({
|
|
4370
|
+
target: target("create_account"),
|
|
4371
|
+
typeArguments: [input.coinType],
|
|
4372
|
+
arguments: [funding]
|
|
4373
|
+
});
|
|
4374
|
+
}
|
|
4313
4375
|
return kindBytes(tx);
|
|
4314
4376
|
},
|
|
4315
4377
|
async buildTopUpKind(input) {
|
|
4316
4378
|
const tx = new Transaction2();
|
|
4317
4379
|
tx.setSender(input.owner);
|
|
4318
4380
|
const funding = await fundingCoin(tx, input.owner, input.coinType, BigInt(input.amount));
|
|
4319
|
-
|
|
4320
|
-
|
|
4321
|
-
|
|
4322
|
-
|
|
4323
|
-
|
|
4381
|
+
if (input.exclusionDirectoryId) {
|
|
4382
|
+
tx.moveCall({
|
|
4383
|
+
target: target("fund_account_limited"),
|
|
4384
|
+
typeArguments: [input.coinType],
|
|
4385
|
+
arguments: [tx.object(input.exclusionDirectoryId), tx.object(input.poolObjectId), funding]
|
|
4386
|
+
});
|
|
4387
|
+
} else {
|
|
4388
|
+
tx.moveCall({
|
|
4389
|
+
target: target("fund_account"),
|
|
4390
|
+
typeArguments: [input.coinType],
|
|
4391
|
+
arguments: [tx.object(input.poolObjectId), funding]
|
|
4392
|
+
});
|
|
4393
|
+
}
|
|
4324
4394
|
return kindBytes(tx);
|
|
4325
4395
|
},
|
|
4326
4396
|
async buildWithdrawKind(input) {
|
|
@@ -5591,6 +5661,8 @@ function addressBalanceGasExpiration(input) {
|
|
|
5591
5661
|
// ../sdk/dist/delegated-signer/pay-gas.js
|
|
5592
5662
|
var DEFAULT_GAS_BUDGET = 10000000n;
|
|
5593
5663
|
var DEFAULT_GAS_PRICE = 1000n;
|
|
5664
|
+
var MAX_SPONSOR_GAS_BUDGET = 50000000n;
|
|
5665
|
+
var MAX_SPONSOR_GAS_PRICE = 10000n;
|
|
5594
5666
|
var X402_EXPIRATION_EPOCHS = 32n;
|
|
5595
5667
|
async function readChainBound(read) {
|
|
5596
5668
|
try {
|
|
@@ -5633,8 +5705,16 @@ function assertPayGas(data, delegate, policy = {}) {
|
|
|
5633
5705
|
if (policy.mode === "agent") {
|
|
5634
5706
|
if (!pinned)
|
|
5635
5707
|
throw new Error("agent gas requires caller-owned gas inputs");
|
|
5636
|
-
} else
|
|
5637
|
-
|
|
5708
|
+
} else {
|
|
5709
|
+
if (owner === agent) {
|
|
5710
|
+
throw new Error("sponsored transaction gas owner must not be the delegate");
|
|
5711
|
+
}
|
|
5712
|
+
if (actual.budget == null || actual.price == null) {
|
|
5713
|
+
throw new Error("sponsored transaction gas budget and price must be pinned");
|
|
5714
|
+
}
|
|
5715
|
+
if (BigInt(actual.budget) > MAX_SPONSOR_GAS_BUDGET || BigInt(actual.price) > MAX_SPONSOR_GAS_PRICE) {
|
|
5716
|
+
throw new Error("sponsored transaction gas budget or price exceeds the cap");
|
|
5717
|
+
}
|
|
5638
5718
|
}
|
|
5639
5719
|
if (pinned) {
|
|
5640
5720
|
const expectedOwner = normalizeSuiAddress3(pinned.owner ?? delegate);
|
|
@@ -6517,6 +6597,8 @@ var X402_ERROR_REASON = {
|
|
|
6517
6597
|
INSUFFICIENT_FUNDS: "insufficient_funds",
|
|
6518
6598
|
PAYER_MISMATCH: "invalid_payload",
|
|
6519
6599
|
SETTLEMENT_REJECTED: "invalid_transaction_state",
|
|
6600
|
+
SPEND_PAUSED: "unexpected_settle_error",
|
|
6601
|
+
SPEND_PAUSE_UNAVAILABLE: "unexpected_settle_error",
|
|
6520
6602
|
SETTLEMENT_MISMATCH: "unexpected_settle_error",
|
|
6521
6603
|
AMBIGUOUS: "settlement_pending",
|
|
6522
6604
|
VERSION_UNSUPPORTED: "invalid_x402_version",
|
|
@@ -7866,6 +7948,12 @@ async function signSponsorPersonalMessagePayload(input) {
|
|
|
7866
7948
|
return input.signPersonalMessage(encoded);
|
|
7867
7949
|
}
|
|
7868
7950
|
|
|
7951
|
+
// ../sdk/dist/protocol/redirect.js
|
|
7952
|
+
var NO_REDIRECT = { redirect: "manual" };
|
|
7953
|
+
function isRedirectResponse(res) {
|
|
7954
|
+
return res.type === "opaqueredirect" || res.status >= 300 && res.status < 400;
|
|
7955
|
+
}
|
|
7956
|
+
|
|
7869
7957
|
// ../sdk/dist/protocol/offer-keys.js
|
|
7870
7958
|
var SUIPAY_OFFER_KEYS_PATH = "/.well-known/suipay-offer-keys.json";
|
|
7871
7959
|
var DEFAULT_OFFER_KEY_CACHE_TTL_MS = 5 * 60 * 1e3;
|
|
@@ -7914,10 +8002,13 @@ async function fetchGatewayOfferKeys(gatewayUrl, options = {}) {
|
|
|
7914
8002
|
const targetUrl = `${normalizedBase}${SUIPAY_OFFER_KEYS_PATH}`;
|
|
7915
8003
|
let response;
|
|
7916
8004
|
try {
|
|
7917
|
-
response = await fetchImpl(targetUrl);
|
|
8005
|
+
response = await fetchImpl(targetUrl, NO_REDIRECT);
|
|
7918
8006
|
} catch (err) {
|
|
7919
8007
|
throw new OfferKeysFetchError(`failed to fetch gateway offer keys: ${err instanceof Error ? err.message : String(err)}`);
|
|
7920
8008
|
}
|
|
8009
|
+
if (isRedirectResponse(response)) {
|
|
8010
|
+
throw new OfferKeysFetchError("failed to fetch gateway offer keys: redirect refused", response.status);
|
|
8011
|
+
}
|
|
7921
8012
|
if (response.status === 404) {
|
|
7922
8013
|
throw new OfferKeysGatewayUnavailableError();
|
|
7923
8014
|
}
|
|
@@ -8019,6 +8110,29 @@ import { isValidPersonalMessageSignature as isValidPersonalMessageSignature2, ve
|
|
|
8019
8110
|
import { normalizeSuiAddress as normalizeSuiAddress6 } from "@mysten/sui/utils";
|
|
8020
8111
|
var SIGNED_REQUEST_DOMAIN = "suipay:signed-request:v1";
|
|
8021
8112
|
var BOOTSTRAP_REQUEST_DOMAIN = "suipay:bootstrap-request:v1";
|
|
8113
|
+
var SIGNED_REQUEST_DOMAIN_V2 = "suipay:signed-request:v2";
|
|
8114
|
+
var BOOTSTRAP_REQUEST_DOMAIN_V2 = "suipay:bootstrap-request:v2";
|
|
8115
|
+
function canonicalOrigin(urlOrOrigin) {
|
|
8116
|
+
if (typeof urlOrOrigin !== "string" || urlOrOrigin.trim() === "") {
|
|
8117
|
+
throw new Error("origin must be a non-empty http(s) URL");
|
|
8118
|
+
}
|
|
8119
|
+
let url;
|
|
8120
|
+
try {
|
|
8121
|
+
url = new URL(urlOrOrigin.trim());
|
|
8122
|
+
} catch {
|
|
8123
|
+
throw new Error("origin must be an absolute http(s) URL");
|
|
8124
|
+
}
|
|
8125
|
+
if (url.protocol !== "https:" && url.protocol !== "http:") {
|
|
8126
|
+
throw new Error("origin must use http or https");
|
|
8127
|
+
}
|
|
8128
|
+
if (url.username !== "" || url.password !== "") {
|
|
8129
|
+
throw new Error("origin must not carry userinfo");
|
|
8130
|
+
}
|
|
8131
|
+
if (url.hostname === "" || url.origin === "null") {
|
|
8132
|
+
throw new Error("origin must name a host");
|
|
8133
|
+
}
|
|
8134
|
+
return url.origin;
|
|
8135
|
+
}
|
|
8022
8136
|
var SIGNED_REQUEST_MAX_AGE_MS = 3e5;
|
|
8023
8137
|
var SIGNED_REQUEST_MAX_FUTURE_SKEW_MS = 3e4;
|
|
8024
8138
|
var SIGNED_REQUEST_NONCE_TTL_MS = SIGNED_REQUEST_MAX_AGE_MS + 2 * SIGNED_REQUEST_MAX_FUTURE_SKEW_MS;
|
|
@@ -8043,6 +8157,61 @@ function hexToBytes32(hex) {
|
|
|
8043
8157
|
}
|
|
8044
8158
|
return Uint8Array.from(Buffer.from(h, "hex"));
|
|
8045
8159
|
}
|
|
8160
|
+
var utf8VecBytes = (s) => bcs4.vector(bcs4.u8()).serialize([...Buffer.from(s, "utf8")]).toBytes();
|
|
8161
|
+
var addressBytes = (a) => bcs4.Address.serialize(normalizeSuiAddress6(a)).toBytes();
|
|
8162
|
+
function assertCanonicalOrigin(origin) {
|
|
8163
|
+
if (typeof origin !== "string" || canonicalOrigin(origin) !== origin) {
|
|
8164
|
+
throw new Error("origin must be a canonical http(s) origin");
|
|
8165
|
+
}
|
|
8166
|
+
return origin;
|
|
8167
|
+
}
|
|
8168
|
+
function encodeSignedRequestV2(payload) {
|
|
8169
|
+
if (payload.domain !== SIGNED_REQUEST_DOMAIN_V2) {
|
|
8170
|
+
throw new Error(`domain must be ${SIGNED_REQUEST_DOMAIN_V2}`);
|
|
8171
|
+
}
|
|
8172
|
+
const origin = assertCanonicalOrigin(payload.origin);
|
|
8173
|
+
const parts = [
|
|
8174
|
+
utf8VecBytes(payload.domain),
|
|
8175
|
+
utf8VecBytes(payload.network),
|
|
8176
|
+
addressBytes(payload.packageId),
|
|
8177
|
+
utf8VecBytes(origin),
|
|
8178
|
+
addressBytes(payload.grantObjectId),
|
|
8179
|
+
addressBytes(payload.delegate),
|
|
8180
|
+
utf8VecBytes(payload.method),
|
|
8181
|
+
utf8VecBytes(payload.path),
|
|
8182
|
+
utf8VecBytes(payload.query),
|
|
8183
|
+
bcs4.vector(bcs4.u8()).serialize([...hexToBytes32(payload.bodyHash)]).toBytes(),
|
|
8184
|
+
bcs4.u64().serialize(BigInt(payload.timestampMs)).toBytes(),
|
|
8185
|
+
utf8VecBytes(payload.nonce)
|
|
8186
|
+
];
|
|
8187
|
+
return Buffer.concat(parts.map((p) => Buffer.from(p)));
|
|
8188
|
+
}
|
|
8189
|
+
function encodeBootstrapRequestV2(payload) {
|
|
8190
|
+
if (payload.domain !== BOOTSTRAP_REQUEST_DOMAIN_V2) {
|
|
8191
|
+
throw new Error(`domain must be ${BOOTSTRAP_REQUEST_DOMAIN_V2}`);
|
|
8192
|
+
}
|
|
8193
|
+
const origin = assertCanonicalOrigin(payload.origin);
|
|
8194
|
+
const parts = [
|
|
8195
|
+
utf8VecBytes(payload.domain),
|
|
8196
|
+
utf8VecBytes(payload.network),
|
|
8197
|
+
addressBytes(payload.packageId),
|
|
8198
|
+
utf8VecBytes(origin),
|
|
8199
|
+
addressBytes(payload.delegate),
|
|
8200
|
+
utf8VecBytes(payload.method),
|
|
8201
|
+
utf8VecBytes(payload.path),
|
|
8202
|
+
utf8VecBytes(payload.query),
|
|
8203
|
+
bcs4.vector(bcs4.u8()).serialize([...hexToBytes32(payload.bodyHash)]).toBytes(),
|
|
8204
|
+
bcs4.u64().serialize(BigInt(payload.timestampMs)).toBytes(),
|
|
8205
|
+
utf8VecBytes(payload.nonce)
|
|
8206
|
+
];
|
|
8207
|
+
return Buffer.concat(parts.map((p) => Buffer.from(p)));
|
|
8208
|
+
}
|
|
8209
|
+
function encodeAnySignedRequest(payload) {
|
|
8210
|
+
return payload.domain === SIGNED_REQUEST_DOMAIN_V2 ? encodeSignedRequestV2(payload) : encodeSignedRequest(payload);
|
|
8211
|
+
}
|
|
8212
|
+
function encodeAnyBootstrapRequest(payload) {
|
|
8213
|
+
return payload.domain === BOOTSTRAP_REQUEST_DOMAIN_V2 ? encodeBootstrapRequestV2(payload) : encodeBootstrapRequest(payload);
|
|
8214
|
+
}
|
|
8046
8215
|
function encodeBootstrapRequest(payload) {
|
|
8047
8216
|
if (payload.domain !== BOOTSTRAP_REQUEST_DOMAIN) {
|
|
8048
8217
|
throw new Error(`domain must be ${BOOTSTRAP_REQUEST_DOMAIN}`);
|
|
@@ -8175,10 +8344,10 @@ function validateNonceFormat(nonce) {
|
|
|
8175
8344
|
return bytes >= SIGNED_REQUEST_NONCE_MIN_ENTROPY_BYTES;
|
|
8176
8345
|
}
|
|
8177
8346
|
async function signSignedRequest(input) {
|
|
8178
|
-
return input.signPersonalMessage(
|
|
8347
|
+
return input.signPersonalMessage(encodeAnySignedRequest(input.payload));
|
|
8179
8348
|
}
|
|
8180
8349
|
async function signBootstrapRequest(input) {
|
|
8181
|
-
return input.signPersonalMessage(
|
|
8350
|
+
return input.signPersonalMessage(encodeAnyBootstrapRequest(input.payload));
|
|
8182
8351
|
}
|
|
8183
8352
|
|
|
8184
8353
|
// ../sdk/dist/agent-sdk/signed-headers.js
|
|
@@ -8204,6 +8373,17 @@ function splitPathAndQuery(target) {
|
|
|
8204
8373
|
return { path: noFragment.slice(0, q), query: noFragment.slice(q + 1) };
|
|
8205
8374
|
}
|
|
8206
8375
|
}
|
|
8376
|
+
function assertTargetOrigin(url, signedOrigin) {
|
|
8377
|
+
let target;
|
|
8378
|
+
try {
|
|
8379
|
+
target = new URL(url);
|
|
8380
|
+
} catch {
|
|
8381
|
+
return;
|
|
8382
|
+
}
|
|
8383
|
+
if (canonicalOrigin(target.href) !== canonicalOrigin(signedOrigin)) {
|
|
8384
|
+
throw new Error("request URL origin does not match the signed origin");
|
|
8385
|
+
}
|
|
8386
|
+
}
|
|
8207
8387
|
function buildSignedRequestPayload(context, request, fields) {
|
|
8208
8388
|
if (!validateNonceFormat(fields.nonce)) {
|
|
8209
8389
|
throw new Error("nonce must be base64url with at least 128 bits of entropy");
|
|
@@ -8212,9 +8392,10 @@ function buildSignedRequestPayload(context, request, fields) {
|
|
|
8212
8392
|
throw new Error("timestampMs must be a positive integer (epoch ms)");
|
|
8213
8393
|
}
|
|
8214
8394
|
return {
|
|
8215
|
-
domain:
|
|
8395
|
+
domain: SIGNED_REQUEST_DOMAIN_V2,
|
|
8216
8396
|
network: context.network,
|
|
8217
8397
|
packageId: context.packageId,
|
|
8398
|
+
origin: canonicalOrigin(context.origin),
|
|
8218
8399
|
grantObjectId: context.grantObjectId,
|
|
8219
8400
|
delegate: context.delegateAddress,
|
|
8220
8401
|
method: request.method.toUpperCase(),
|
|
@@ -8251,6 +8432,7 @@ async function signSessionRequest(input) {
|
|
|
8251
8432
|
};
|
|
8252
8433
|
}
|
|
8253
8434
|
async function signSessionRequestForUrl(input) {
|
|
8435
|
+
assertTargetOrigin(input.url, input.context.origin);
|
|
8254
8436
|
const { path, query } = splitPathAndQuery(input.url);
|
|
8255
8437
|
return signSessionRequest({
|
|
8256
8438
|
context: input.context,
|
|
@@ -8272,9 +8454,10 @@ function buildBootstrapRequestPayload(context, request, fields) {
|
|
|
8272
8454
|
throw new Error("timestampMs must be a positive integer (epoch ms)");
|
|
8273
8455
|
}
|
|
8274
8456
|
return {
|
|
8275
|
-
domain:
|
|
8457
|
+
domain: BOOTSTRAP_REQUEST_DOMAIN_V2,
|
|
8276
8458
|
network: context.network,
|
|
8277
8459
|
packageId: context.packageId,
|
|
8460
|
+
origin: canonicalOrigin(context.origin),
|
|
8278
8461
|
delegate: context.delegateAddress,
|
|
8279
8462
|
method: request.method.toUpperCase(),
|
|
8280
8463
|
path: canonicalizePath(request.path),
|
|
@@ -8310,6 +8493,7 @@ async function signBootstrapSessionRequest(input) {
|
|
|
8310
8493
|
};
|
|
8311
8494
|
}
|
|
8312
8495
|
async function signBootstrapSessionRequestForUrl(input) {
|
|
8496
|
+
assertTargetOrigin(input.url, input.context.origin);
|
|
8313
8497
|
const { path, query } = splitPathAndQuery(input.url);
|
|
8314
8498
|
return signBootstrapSessionRequest({
|
|
8315
8499
|
context: input.context,
|
|
@@ -8328,6 +8512,9 @@ async function signBootstrapSessionRequestForUrl(input) {
|
|
|
8328
8512
|
function classifyReadFailure(status) {
|
|
8329
8513
|
return status >= 500 ? "unavailable" : "rejected";
|
|
8330
8514
|
}
|
|
8515
|
+
function redirectRefused() {
|
|
8516
|
+
return { ok: false, status: 0, error: "unavailable", code: "request.redirect_refused" };
|
|
8517
|
+
}
|
|
8331
8518
|
function joinUrl(gatewayUrl, basePath, suffix) {
|
|
8332
8519
|
const origin = gatewayUrl.replace(/\/$/, "");
|
|
8333
8520
|
let base = basePath.replace(/\/$/, "");
|
|
@@ -8362,7 +8549,7 @@ function createReader(options) {
|
|
|
8362
8549
|
let signed;
|
|
8363
8550
|
try {
|
|
8364
8551
|
signed = await signSessionRequestForUrl({
|
|
8365
|
-
context: options.context,
|
|
8552
|
+
context: { ...options.context, origin: canonicalOrigin(options.gatewayUrl) },
|
|
8366
8553
|
method: "GET",
|
|
8367
8554
|
url,
|
|
8368
8555
|
signPersonalMessage: options.signPersonalMessage,
|
|
@@ -8375,11 +8562,14 @@ function createReader(options) {
|
|
|
8375
8562
|
try {
|
|
8376
8563
|
res = await fetchImpl(url, {
|
|
8377
8564
|
method: "GET",
|
|
8378
|
-
headers: { ...signed.headers, accept: "application/json" }
|
|
8565
|
+
headers: { ...signed.headers, accept: "application/json" },
|
|
8566
|
+
...NO_REDIRECT
|
|
8379
8567
|
});
|
|
8380
8568
|
} catch {
|
|
8381
8569
|
return { ok: false, status: 0, error: "unavailable" };
|
|
8382
8570
|
}
|
|
8571
|
+
if (isRedirectResponse(res))
|
|
8572
|
+
return redirectRefused();
|
|
8383
8573
|
const body = await readJsonBody(res);
|
|
8384
8574
|
if (res.status >= 200 && res.status < 300) {
|
|
8385
8575
|
return { ok: true, status: res.status, data: body };
|
|
@@ -8461,6 +8651,12 @@ async function readPublicSessionState(getObjectJson, grantObjectId) {
|
|
|
8461
8651
|
revoked: asBool(g.revoked)
|
|
8462
8652
|
};
|
|
8463
8653
|
}
|
|
8654
|
+
function isRevokedBootstrap(data) {
|
|
8655
|
+
if (!data || typeof data !== "object")
|
|
8656
|
+
return false;
|
|
8657
|
+
const dto = data;
|
|
8658
|
+
return Array.isArray(dto.grants) && dto.grants.length === 0 && dto.grantStatus === "revoked";
|
|
8659
|
+
}
|
|
8464
8660
|
async function bootstrapPayerSessions(options) {
|
|
8465
8661
|
const fetchImpl = options.fetchImpl ?? fetch;
|
|
8466
8662
|
const basePath = options.basePath ?? "/v1/delegate";
|
|
@@ -8468,7 +8664,7 @@ async function bootstrapPayerSessions(options) {
|
|
|
8468
8664
|
let signed;
|
|
8469
8665
|
try {
|
|
8470
8666
|
signed = await signBootstrapSessionRequestForUrl({
|
|
8471
|
-
context: options.context,
|
|
8667
|
+
context: { ...options.context, origin: canonicalOrigin(options.gatewayUrl) },
|
|
8472
8668
|
method: "GET",
|
|
8473
8669
|
url,
|
|
8474
8670
|
signPersonalMessage: options.signPersonalMessage,
|
|
@@ -8481,11 +8677,14 @@ async function bootstrapPayerSessions(options) {
|
|
|
8481
8677
|
try {
|
|
8482
8678
|
res = await fetchImpl(url, {
|
|
8483
8679
|
method: "GET",
|
|
8484
|
-
headers: { ...signed.headers, accept: "application/json" }
|
|
8680
|
+
headers: { ...signed.headers, accept: "application/json" },
|
|
8681
|
+
...NO_REDIRECT
|
|
8485
8682
|
});
|
|
8486
8683
|
} catch {
|
|
8487
8684
|
return { ok: false, status: 0, error: "unavailable" };
|
|
8488
8685
|
}
|
|
8686
|
+
if (isRedirectResponse(res))
|
|
8687
|
+
return redirectRefused();
|
|
8489
8688
|
const body = await readJsonBody(res);
|
|
8490
8689
|
if (res.status >= 200 && res.status < 300) {
|
|
8491
8690
|
return { ok: true, status: res.status, data: body };
|
|
@@ -8782,14 +8981,33 @@ function gatewayFailure(answer, local, extras) {
|
|
|
8782
8981
|
txDigest: error.txDigest ?? extras.txDigest
|
|
8783
8982
|
});
|
|
8784
8983
|
}
|
|
8785
|
-
var
|
|
8786
|
-
|
|
8787
|
-
|
|
8788
|
-
|
|
8789
|
-
|
|
8790
|
-
|
|
8984
|
+
var PRE_SIGN_SPONSOR_REFUSALS = /* @__PURE__ */ new Set([
|
|
8985
|
+
...DEFINITE_PRE_BROADCAST_REFUSALS,
|
|
8986
|
+
"auth.spend_daily_limit_reached",
|
|
8987
|
+
// Unreadable pause / other pre-sign unavailability: safe on challenge and
|
|
8988
|
+
// sponsor create only. Execute keeps this ambiguous (see DEFINITE set).
|
|
8989
|
+
"service.unavailable",
|
|
8990
|
+
"limit.rate_limited",
|
|
8991
|
+
"limit.gas_quota_exceeded",
|
|
8992
|
+
"sponsor.would_abort",
|
|
8993
|
+
"sponsor.kind_rejected",
|
|
8994
|
+
"sponsor.target_not_allowed",
|
|
8995
|
+
"account.grant_revoked",
|
|
8996
|
+
"account.grant_paused",
|
|
8997
|
+
"account.grant_expired",
|
|
8998
|
+
"account.grant_not_active"
|
|
8999
|
+
]);
|
|
9000
|
+
function isPreSignSponsorRefusal(parsed) {
|
|
9001
|
+
return isValidatedProblemRefusal(parsed, PRE_SIGN_SPONSOR_REFUSALS);
|
|
9002
|
+
}
|
|
9003
|
+
var BYO_SPONSOR_EXECUTE_PRE_BROADCAST_REFUSALS = /* @__PURE__ */ new Set([
|
|
9004
|
+
"auth.spend_daily_limit_reached",
|
|
9005
|
+
"sponsor.sponsorship_not_found",
|
|
9006
|
+
"sponsor.unavailable"
|
|
9007
|
+
]);
|
|
9008
|
+
async function sponsorProblemRefusal(response, paymentId, admit) {
|
|
8791
9009
|
const parsed = parseErrorPayload(await readGatewayAnswer(response));
|
|
8792
|
-
if (parsed
|
|
9010
|
+
if (!admit(parsed)) {
|
|
8793
9011
|
return null;
|
|
8794
9012
|
}
|
|
8795
9013
|
const detail = parsed.detail?.trim();
|
|
@@ -9447,6 +9665,11 @@ async function payDelegatedSpendAccount(args) {
|
|
|
9447
9665
|
detail: "the gateway could not be reached; nothing was signed"
|
|
9448
9666
|
});
|
|
9449
9667
|
}
|
|
9668
|
+
if (isRedirectResponse(first)) {
|
|
9669
|
+
return resultError("request.redirect_refused", "not_charged", {
|
|
9670
|
+
detail: "the gateway answered with a redirect; nothing was signed"
|
|
9671
|
+
});
|
|
9672
|
+
}
|
|
9450
9673
|
if (first.status !== 402) {
|
|
9451
9674
|
let body2;
|
|
9452
9675
|
try {
|
|
@@ -9886,9 +10109,18 @@ async function payDelegatedSpendAccount(args) {
|
|
|
9886
10109
|
grantId: resolved.grantObjectId,
|
|
9887
10110
|
network: session.network,
|
|
9888
10111
|
packageId: session.packageId
|
|
9889
|
-
})
|
|
10112
|
+
}),
|
|
10113
|
+
...NO_REDIRECT
|
|
9890
10114
|
});
|
|
10115
|
+
if (isRedirectResponse(challengeRes)) {
|
|
10116
|
+
return abortBeforeSign("SPONSOR_UNAVAILABLE", PUBLIC_DETAIL.SPONSOR_UNAVAILABLE);
|
|
10117
|
+
}
|
|
9891
10118
|
if (!challengeRes.ok) {
|
|
10119
|
+
const refused = await sponsorProblemRefusal(challengeRes, paymentId, isPreSignSponsorRefusal);
|
|
10120
|
+
if (refused) {
|
|
10121
|
+
await releaseReservation(ledger, ownedPaymentId, log);
|
|
10122
|
+
return refused;
|
|
10123
|
+
}
|
|
9892
10124
|
return abortBeforeSign("SPONSOR_UNAVAILABLE", PUBLIC_DETAIL.SPONSOR_UNAVAILABLE);
|
|
9893
10125
|
}
|
|
9894
10126
|
const body2 = await challengeRes.json();
|
|
@@ -9956,10 +10188,14 @@ async function payDelegatedSpendAccount(args) {
|
|
|
9956
10188
|
packageId: challenge.packageId,
|
|
9957
10189
|
requestId: paymentId
|
|
9958
10190
|
}
|
|
9959
|
-
})
|
|
10191
|
+
}),
|
|
10192
|
+
...NO_REDIRECT
|
|
9960
10193
|
});
|
|
10194
|
+
if (isRedirectResponse(createRes)) {
|
|
10195
|
+
return abortBeforeSign("SPONSOR_UNAVAILABLE", PUBLIC_DETAIL.SPONSOR_UNAVAILABLE);
|
|
10196
|
+
}
|
|
9961
10197
|
if (!createRes.ok) {
|
|
9962
|
-
const refused = await
|
|
10198
|
+
const refused = await sponsorProblemRefusal(createRes, paymentId, isPreSignSponsorRefusal);
|
|
9963
10199
|
if (refused) {
|
|
9964
10200
|
await releaseReservation(ledger, ownedPaymentId, log);
|
|
9965
10201
|
return refused;
|
|
@@ -10053,10 +10289,11 @@ async function payDelegatedSpendAccount(args) {
|
|
|
10053
10289
|
signature,
|
|
10054
10290
|
executeToken: sponsoredExecuteToken,
|
|
10055
10291
|
...sponsoredBackendToken ? { sponsorBackendToken: sponsoredBackendToken } : {}
|
|
10056
|
-
})
|
|
10292
|
+
}),
|
|
10293
|
+
...NO_REDIRECT
|
|
10057
10294
|
});
|
|
10058
10295
|
if (!execRes.ok) {
|
|
10059
|
-
const refused = await
|
|
10296
|
+
const refused = isRedirectResponse(execRes) ? null : await sponsorProblemRefusal(execRes, paymentId, (parsed) => isDefinitePreBroadcastRefusal(parsed) || isValidatedProblemRefusal(parsed, BYO_SPONSOR_EXECUTE_PRE_BROADCAST_REFUSALS));
|
|
10060
10297
|
if (refused) {
|
|
10061
10298
|
await releaseReservation(ledger, ownedPaymentId, log);
|
|
10062
10299
|
return refused;
|
|
@@ -10165,6 +10402,8 @@ async function payDelegatedSpendAccount(args) {
|
|
|
10165
10402
|
let second;
|
|
10166
10403
|
try {
|
|
10167
10404
|
second = await fetchImpl(resourceUrl, paidRequestInit(request, { [MPP_CREDENTIAL_HEADER]: proof }));
|
|
10405
|
+
if (isRedirectResponse(second))
|
|
10406
|
+
throw new Error("proof presentation refused a redirect");
|
|
10168
10407
|
} catch (err) {
|
|
10169
10408
|
logInternal(log, { code: "client.gateway_unreachable", msg: "proof presentation failed" }, err);
|
|
10170
10409
|
return completeAttempt(ledger, paymentId, ambiguous("PROOF_PRESENTATION_FAILED", PUBLIC_DETAIL.PROOF_PRESENTATION_FAILED, {
|
|
@@ -10346,6 +10585,9 @@ async function presentX402Credential(ctx, header) {
|
|
|
10346
10585
|
let response;
|
|
10347
10586
|
try {
|
|
10348
10587
|
response = await ctx.fetchImpl(ctx.resourceUrl, paidRequestInit(ctx.request, { [X402_PAYMENT_HEADER]: header }));
|
|
10588
|
+
if (isRedirectResponse(response)) {
|
|
10589
|
+
throw new Error("x402 credential presentation refused a redirect");
|
|
10590
|
+
}
|
|
10349
10591
|
} catch (err) {
|
|
10350
10592
|
logInternal(ctx.deps.log, { code: "client.gateway_unreachable", msg: "x402 credential presentation failed" }, err);
|
|
10351
10593
|
return { kind: "unresolved", txDigest };
|
|
@@ -10372,7 +10614,8 @@ async function presentX402Credential(ctx, header) {
|
|
|
10372
10614
|
const settledReceipt = receipt && receipt.challengeId === ctx.offer.challengeId ? receipt : null;
|
|
10373
10615
|
const pending = decoded !== null && isX402Failure(decoded) && decoded.errorReason === "settlement_pending";
|
|
10374
10616
|
const answer = await readGatewayAnswer(response);
|
|
10375
|
-
const
|
|
10617
|
+
const parsed = parseErrorPayload(answer);
|
|
10618
|
+
const stated = parsed?.paymentState;
|
|
10376
10619
|
if (settledReceipt || pending || stated === "charged" || stated === "unknown") {
|
|
10377
10620
|
return {
|
|
10378
10621
|
kind: "undelivered",
|
|
@@ -10381,6 +10624,12 @@ async function presentX402Credential(ctx, header) {
|
|
|
10381
10624
|
txDigest: settledReceipt?.txDigest ?? txDigest
|
|
10382
10625
|
};
|
|
10383
10626
|
}
|
|
10627
|
+
if (parsed?.source === "problem" && (parsed.code === "service.halted" || parsed.code === "service.unavailable") && answer.body.paymentState === "not_charged" && decoded && isX402Failure(decoded) && decoded.errorReason === "unexpected_settle_error") {
|
|
10628
|
+
txDigest = parsed.txDigest ?? txDigest;
|
|
10629
|
+
if (attempt === 1 && !txDigest)
|
|
10630
|
+
return { kind: "refused", answer };
|
|
10631
|
+
return { kind: "unresolved", txDigest };
|
|
10632
|
+
}
|
|
10384
10633
|
if (attempt < attempts)
|
|
10385
10634
|
await sleep(X402_PRESENT_DELAY_MS);
|
|
10386
10635
|
}
|
|
@@ -10596,6 +10845,17 @@ function preparedSharedObjectId(input) {
|
|
|
10596
10845
|
return null;
|
|
10597
10846
|
}
|
|
10598
10847
|
}
|
|
10848
|
+
function preparedMoveCallInput(data, args, position) {
|
|
10849
|
+
const index = preparedInputIndex(args[position]);
|
|
10850
|
+
return index === null ? void 0 : data.inputs?.[index];
|
|
10851
|
+
}
|
|
10852
|
+
function readPreparedSettlementPayment(data, args, accessMode) {
|
|
10853
|
+
return {
|
|
10854
|
+
poolObjectId: preparedSharedObjectId(preparedMoveCallInput(data, args, 2)),
|
|
10855
|
+
grantObjectId: preparedSharedObjectId(preparedMoveCallInput(data, args, 3)),
|
|
10856
|
+
amount: preparedPureU64(preparedMoveCallInput(data, args, accessMode === "open" ? 5 : 6))
|
|
10857
|
+
};
|
|
10858
|
+
}
|
|
10599
10859
|
async function verifyPreparedSettlementKind(input) {
|
|
10600
10860
|
const accessMode = input.expected.accessMode ?? "open";
|
|
10601
10861
|
if (accessMode !== "scoped" && accessMode !== "open") {
|
|
@@ -10620,20 +10880,13 @@ async function verifyPreparedSettlementKind(input) {
|
|
|
10620
10880
|
throw new Error(`prepared Move target is ${got}, expected ${wanted}`);
|
|
10621
10881
|
}
|
|
10622
10882
|
const args = cmd.MoveCall.arguments ?? [];
|
|
10623
|
-
const
|
|
10624
|
-
const
|
|
10625
|
-
const idx = preparedInputIndex(args[argIdx]);
|
|
10626
|
-
if (idx === null || idx < 0 || idx >= inputs.length)
|
|
10627
|
-
return void 0;
|
|
10628
|
-
return inputs[idx];
|
|
10629
|
-
};
|
|
10630
|
-
const poolObjectId = preparedSharedObjectId(resolve2(2));
|
|
10883
|
+
const resolve2 = (argIdx) => preparedMoveCallInput(data, args, argIdx);
|
|
10884
|
+
const { poolObjectId, grantObjectId, amount } = readPreparedSettlementPayment(data, args, accessMode);
|
|
10631
10885
|
if (input.expected.poolObjectId) {
|
|
10632
10886
|
if (!poolObjectId || !sameAddress2(poolObjectId, input.expected.poolObjectId)) {
|
|
10633
10887
|
throw new Error(`prepared pool ${poolObjectId ?? "(missing)"} does not match expected pool ${input.expected.poolObjectId}`);
|
|
10634
10888
|
}
|
|
10635
10889
|
}
|
|
10636
|
-
const grantObjectId = preparedSharedObjectId(resolve2(3));
|
|
10637
10890
|
if (!grantObjectId) {
|
|
10638
10891
|
throw new Error("prepared transaction does not name a grant object");
|
|
10639
10892
|
}
|
|
@@ -10645,7 +10898,6 @@ async function verifyPreparedSettlementKind(input) {
|
|
|
10645
10898
|
if (!recipient || !sameAddress2(recipient, input.expected.recipient)) {
|
|
10646
10899
|
throw new Error(`prepared recipient ${recipient ?? "(missing)"} does not match the requested recipient`);
|
|
10647
10900
|
}
|
|
10648
|
-
const amount = preparedPureU64(resolve2(5));
|
|
10649
10901
|
let expectedAmount;
|
|
10650
10902
|
try {
|
|
10651
10903
|
expectedAmount = BigInt(input.expected.amount);
|
|
@@ -10670,7 +10922,6 @@ async function verifyPreparedSettlementKind(input) {
|
|
|
10670
10922
|
throw new Error("prepared target_hash does not match expected targetHash");
|
|
10671
10923
|
}
|
|
10672
10924
|
}
|
|
10673
|
-
const amount = preparedPureU64(resolve2(6));
|
|
10674
10925
|
let expectedAmount;
|
|
10675
10926
|
try {
|
|
10676
10927
|
expectedAmount = BigInt(input.expected.amount);
|
|
@@ -10815,10 +11066,10 @@ import { normalizeSuiAddress as normalizeSuiAddress8 } from "@mysten/sui/utils";
|
|
|
10815
11066
|
import { existsSync, readFileSync } from "fs";
|
|
10816
11067
|
import { dirname, join } from "path";
|
|
10817
11068
|
import { fileURLToPath } from "url";
|
|
10818
|
-
var MCP_LOCAL_GATEWAY_URL = "https://gateway-
|
|
10819
|
-
var MCP_LOCAL_CONSOLE_URL = "https://suipay-
|
|
10820
|
-
var MCP_DEV_GATEWAY_URL = "https://gateway-
|
|
10821
|
-
var MCP_DEV_CONSOLE_URL = "https://suipay-
|
|
11069
|
+
var MCP_LOCAL_GATEWAY_URL = "https://suipay-gateway-staging.mystenlabs.com";
|
|
11070
|
+
var MCP_LOCAL_CONSOLE_URL = "https://suipay-staging.mystenlabs.com";
|
|
11071
|
+
var MCP_DEV_GATEWAY_URL = "https://suipay-gateway-staging.mystenlabs.com";
|
|
11072
|
+
var MCP_DEV_CONSOLE_URL = "https://suipay-staging.mystenlabs.com";
|
|
10822
11073
|
var MCP_PROD_GATEWAY_URL = "https://gateway.suipay.dev";
|
|
10823
11074
|
var MCP_PROD_CONSOLE_URL = "https://www.sui.io/agentpayments";
|
|
10824
11075
|
var MCP_CHANNEL_DEFAULTS = {
|
|
@@ -10882,12 +11133,14 @@ function mcpCredentialsPath() {
|
|
|
10882
11133
|
}
|
|
10883
11134
|
var MCP_TARGET_PRESETS = {
|
|
10884
11135
|
dev: {
|
|
10885
|
-
consoleUrl:
|
|
10886
|
-
gatewayUrl:
|
|
11136
|
+
consoleUrl: MCP_DEV_CONSOLE_URL,
|
|
11137
|
+
gatewayUrl: MCP_DEV_GATEWAY_URL
|
|
10887
11138
|
},
|
|
10888
11139
|
local: {
|
|
10889
|
-
|
|
10890
|
-
|
|
11140
|
+
// Assemble so the npm release bake (string-replace of contiguous local
|
|
11141
|
+
// origins across bundled dist) cannot rewrite `--local` onto a hosted pair.
|
|
11142
|
+
consoleUrl: ["http://localhost", 3e3].join(":"),
|
|
11143
|
+
gatewayUrl: ["http://127.0.0.1", 4340].join(":")
|
|
10891
11144
|
}
|
|
10892
11145
|
};
|
|
10893
11146
|
var PACKAGE_ID_ENV_NAMES = [
|
|
@@ -12518,7 +12771,8 @@ async function fetchGatewayDeployment(options) {
|
|
|
12518
12771
|
try {
|
|
12519
12772
|
res = await fetchImpl(`${origin}${base}/deployment`, {
|
|
12520
12773
|
method: "GET",
|
|
12521
|
-
headers: { accept: "application/json" }
|
|
12774
|
+
headers: { accept: "application/json" },
|
|
12775
|
+
...NO_REDIRECT
|
|
12522
12776
|
});
|
|
12523
12777
|
} catch {
|
|
12524
12778
|
return {
|
|
@@ -12526,6 +12780,12 @@ async function fetchGatewayDeployment(options) {
|
|
|
12526
12780
|
error: manifestError(MANIFEST_ERROR_CODES.MALFORMED_MANIFEST, "gateway deployment identity could not be reached", "client.gateway_unreachable")
|
|
12527
12781
|
};
|
|
12528
12782
|
}
|
|
12783
|
+
if (isRedirectResponse(res)) {
|
|
12784
|
+
return {
|
|
12785
|
+
ok: false,
|
|
12786
|
+
error: manifestError(MANIFEST_ERROR_CODES.MALFORMED_MANIFEST, "gateway deployment identity refused a redirect", "request.redirect_refused")
|
|
12787
|
+
};
|
|
12788
|
+
}
|
|
12529
12789
|
if (res.status < 200 || res.status >= 300) {
|
|
12530
12790
|
return {
|
|
12531
12791
|
ok: false,
|
|
@@ -12659,6 +12919,15 @@ async function resolvePayerSession(options) {
|
|
|
12659
12919
|
});
|
|
12660
12920
|
if (whole.ok)
|
|
12661
12921
|
return { ok: true, value: { ...whole.value, deployment } };
|
|
12922
|
+
if (whole.error.reason === MANIFEST_ERROR_CODES.NO_GRANTS) {
|
|
12923
|
+
if (isRevokedBootstrap(bootstrap.data)) {
|
|
12924
|
+
return {
|
|
12925
|
+
ok: false,
|
|
12926
|
+
error: manifestError(MANIFEST_ERROR_CODES.NO_GRANTS, "this delegate key has been revoked", "account.grant_revoked")
|
|
12927
|
+
};
|
|
12928
|
+
}
|
|
12929
|
+
return whole;
|
|
12930
|
+
}
|
|
12662
12931
|
const data = bootstrap.data;
|
|
12663
12932
|
const rawGrants = Array.isArray(data.grants) ? data.grants : [];
|
|
12664
12933
|
const usable = [];
|
|
@@ -12691,7 +12960,9 @@ async function resolvePayerSession(options) {
|
|
|
12691
12960
|
if (usable.length === 0) {
|
|
12692
12961
|
const scoped = blocked.find((b) => b.error.reason === MANIFEST_ERROR_CODES.MISSING_POLICY_BINDING);
|
|
12693
12962
|
const pick2 = scoped ?? blocked[0];
|
|
12694
|
-
|
|
12963
|
+
if (pick2)
|
|
12964
|
+
return { ok: false, error: explainBlockedGrant(pick2.raw, pick2.error) };
|
|
12965
|
+
return whole;
|
|
12695
12966
|
}
|
|
12696
12967
|
const resolved = manifestToSession({
|
|
12697
12968
|
manifest: { ...data, grants: usable },
|
|
@@ -12772,10 +13043,16 @@ async function verifyGrantOnChain(input) {
|
|
|
12772
13043
|
return notVerified(`grant ${input.grantObjectId} draws on a different pool than the one resolved`);
|
|
12773
13044
|
}
|
|
12774
13045
|
if (state.revoked === true) {
|
|
12775
|
-
return
|
|
13046
|
+
return {
|
|
13047
|
+
ok: false,
|
|
13048
|
+
error: manifestError(MANIFEST_ERROR_CODES.GRANT_NOT_VERIFIED, `grant ${input.grantObjectId} is revoked on chain`, "account.grant_revoked")
|
|
13049
|
+
};
|
|
12776
13050
|
}
|
|
12777
13051
|
if (state.paused === true) {
|
|
12778
|
-
return
|
|
13052
|
+
return {
|
|
13053
|
+
ok: false,
|
|
13054
|
+
error: manifestError(MANIFEST_ERROR_CODES.GRANT_NOT_VERIFIED, `grant ${input.grantObjectId} is paused on chain`, "account.grant_paused")
|
|
13055
|
+
};
|
|
12779
13056
|
}
|
|
12780
13057
|
return { ok: true, value: null };
|
|
12781
13058
|
}
|
|
@@ -13488,7 +13765,7 @@ function verifySpendAccountPayReconstructs(bytes, expected) {
|
|
|
13488
13765
|
const auth = defaultAuthorityForRail("delegate", expected.packageId);
|
|
13489
13766
|
const wanted = `${normalizeSuiAddress17(auth.packageId)}::${auth.module}::${auth.payFunction}`;
|
|
13490
13767
|
const cmd = data.commands[0];
|
|
13491
|
-
if (cmd
|
|
13768
|
+
if (cmd?.$kind !== "MoveCall") {
|
|
13492
13769
|
throw new Error("sponsored transaction is not a MoveCall");
|
|
13493
13770
|
}
|
|
13494
13771
|
const got = `${normalizeSuiAddress17(String(cmd.MoveCall.package))}::${cmd.MoveCall.module}::${cmd.MoveCall.function}`;
|
|
@@ -13509,9 +13786,20 @@ function verifySpendAccountPayReconstructs(bytes, expected) {
|
|
|
13509
13786
|
"sponsored transaction inputs do not match the intended payment (pool/grant/amount/target tampered)"
|
|
13510
13787
|
);
|
|
13511
13788
|
}
|
|
13512
|
-
|
|
13513
|
-
|
|
13514
|
-
|
|
13789
|
+
const payment = readPreparedSettlementPayment(data, cmd.MoveCall.arguments, "scoped");
|
|
13790
|
+
if (payment.poolObjectId !== normalizeSuiAddress17(expected.poolObjectId)) {
|
|
13791
|
+
throw new Error("sponsored transaction pool object does not match the intended payment");
|
|
13792
|
+
}
|
|
13793
|
+
if (payment.grantObjectId !== normalizeSuiAddress17(expected.grantObjectId)) {
|
|
13794
|
+
throw new Error("sponsored transaction grant object does not match the intended payment");
|
|
13795
|
+
}
|
|
13796
|
+
if (payment.amount !== BigInt(expected.amount)) {
|
|
13797
|
+
throw new Error("sponsored transaction amount does not match the intended payment");
|
|
13798
|
+
}
|
|
13799
|
+
assertPayGas(data, expected.delegate, {
|
|
13800
|
+
mode: "sponsored",
|
|
13801
|
+
...expected.expiration ? { expiration: expected.expiration } : {}
|
|
13802
|
+
});
|
|
13515
13803
|
}
|
|
13516
13804
|
async function prepareSpendAccountPayment(args) {
|
|
13517
13805
|
const { session, cfg } = args;
|
|
@@ -13634,7 +13922,7 @@ async function prepareSpendAccountPayment(args) {
|
|
|
13634
13922
|
authority: session.snapshot,
|
|
13635
13923
|
assertLive: session.assertLive
|
|
13636
13924
|
};
|
|
13637
|
-
const directorySwitch = cfg.exclusionDirectoryObjectId ? cfg.exclusionDirectoryObjectId : deps.resolveExclusionRegistry ? null : await publishedExclusionDirectoryId(cfg.gatewayUrl);
|
|
13925
|
+
const directorySwitch = cfg.exclusionDirectoryObjectId ? cfg.exclusionDirectoryObjectId : deps.resolveExclusionRegistry ? null : await publishedExclusionDirectoryId(cfg.gatewayUrl, deps.fetchImpl);
|
|
13638
13926
|
const resolveExclusionRegistry2 = deps.resolveExclusionRegistry ?? (directorySwitch ? (() => {
|
|
13639
13927
|
const reader = createGrpcExclusionRegistryReader(rpcClient(cfg));
|
|
13640
13928
|
return async ({ directoryId, packageId, recipient, challengeId, amount, coinType }) => resolveExclusionRegistry({
|
|
@@ -14467,12 +14755,13 @@ async function delegateProofHeaders(resourceUrl, method, key, fetchImpl) {
|
|
|
14467
14755
|
res = await fetchImpl(new URL("/v1/delegate-control/challenges", resourceUrl), {
|
|
14468
14756
|
method: "POST",
|
|
14469
14757
|
headers: { "content-type": "application/json" },
|
|
14470
|
-
body: JSON.stringify({ delegateAddress: key.address })
|
|
14758
|
+
body: JSON.stringify({ delegateAddress: key.address }),
|
|
14759
|
+
...NO_REDIRECT
|
|
14471
14760
|
});
|
|
14472
14761
|
} catch {
|
|
14473
14762
|
return null;
|
|
14474
14763
|
}
|
|
14475
|
-
if (!res.ok)
|
|
14764
|
+
if (isRedirectResponse(res) || !res.ok)
|
|
14476
14765
|
return null;
|
|
14477
14766
|
let issued;
|
|
14478
14767
|
try {
|
|
@@ -14523,12 +14812,12 @@ async function quote(resourceUrl, probeBody, options = {}) {
|
|
|
14523
14812
|
try {
|
|
14524
14813
|
res = await doFetch(resourceUrl, {
|
|
14525
14814
|
...probeInit(method, probeBody, proofHeaders),
|
|
14526
|
-
|
|
14815
|
+
...NO_REDIRECT
|
|
14527
14816
|
});
|
|
14528
14817
|
} catch {
|
|
14529
14818
|
throw refuse("client.gateway_unreachable", `cannot reach ${resourceUrl.host}`);
|
|
14530
14819
|
}
|
|
14531
|
-
if (res
|
|
14820
|
+
if (isRedirectResponse(res)) {
|
|
14532
14821
|
throw refuse("request.redirect_refused", "quote refused a redirect");
|
|
14533
14822
|
}
|
|
14534
14823
|
if (res.status !== 402) {
|
|
@@ -14866,8 +15155,11 @@ async function pay(args, cfg, deps = {}) {
|
|
|
14866
15155
|
if (!local) return mcpFailure("auth.login_required", AUTH_REQUIRED);
|
|
14867
15156
|
return payStdioWithLocalKey({ request, intent, cfg, deps });
|
|
14868
15157
|
}
|
|
14869
|
-
function stdioGatewayUrl(cfg) {
|
|
14870
|
-
|
|
15158
|
+
function stdioGatewayUrl(cfg, storedUrl = loadLocalSigningKeyRecord()?.gatewayUrl) {
|
|
15159
|
+
if (storedUrl?.replace(/\/+$/, "") === "http://localhost:4340" && cfg.gatewayUrl.replace(/\/+$/, "") === MCP_TARGET_PRESETS.local.gatewayUrl) {
|
|
15160
|
+
return cfg.gatewayUrl;
|
|
15161
|
+
}
|
|
15162
|
+
return storedUrl ?? cfg.gatewayUrl;
|
|
14871
15163
|
}
|
|
14872
15164
|
function isExpiredBootstrapGrant(expiresAtMs, nowMs) {
|
|
14873
15165
|
if (expiresAtMs === void 0 || expiresAtMs === null || expiresAtMs === "" || expiresAtMs === "0" || expiresAtMs === 0) {
|
|
@@ -14936,7 +15228,13 @@ async function payStdioWithLocalKey(args) {
|
|
|
14936
15228
|
);
|
|
14937
15229
|
}
|
|
14938
15230
|
const resource = new URL(request.url);
|
|
14939
|
-
|
|
15231
|
+
const gatewayUrl = stdioGatewayUrl(cfg);
|
|
15232
|
+
let gatewayOrigin;
|
|
15233
|
+
try {
|
|
15234
|
+
gatewayOrigin = new URL(gatewayUrl).origin;
|
|
15235
|
+
} catch {
|
|
15236
|
+
}
|
|
15237
|
+
if (resource.username || resource.password || resource.origin !== gatewayOrigin) {
|
|
14940
15238
|
return mcpFailure("account.policy_rejected", "pay URL must use the configured gateway; nothing was signed");
|
|
14941
15239
|
}
|
|
14942
15240
|
const built = await stdioPayerAndReader(cfg, deps);
|
|
@@ -15353,7 +15651,7 @@ async function suipayLogin(args, cfg, deps = {}) {
|
|
|
15353
15651
|
}
|
|
15354
15652
|
const endpoints = {
|
|
15355
15653
|
consoleUrl: cfg.consoleUrl,
|
|
15356
|
-
gatewayUrl: stored?.gatewayUrl ?? cfg.gatewayUrl
|
|
15654
|
+
gatewayUrl: stdioGatewayUrl(cfg, stored?.gatewayUrl ?? cfg.gatewayUrl)
|
|
15357
15655
|
};
|
|
15358
15656
|
if (local) {
|
|
15359
15657
|
const live = await checkLiveDelegateGrant({
|
|
@@ -15378,7 +15676,7 @@ async function suipayLogin(args, cfg, deps = {}) {
|
|
|
15378
15676
|
reused: true,
|
|
15379
15677
|
target: target ?? cfg.target,
|
|
15380
15678
|
delegateAddress: identity.delegateAddress,
|
|
15381
|
-
gatewayUrl:
|
|
15679
|
+
gatewayUrl: endpoints.gatewayUrl,
|
|
15382
15680
|
grantObjectId: live.grantObjectId,
|
|
15383
15681
|
message: "Existing spend-account grant is live for this key. No browser round-trip."
|
|
15384
15682
|
});
|
|
@@ -15479,8 +15777,7 @@ async function suipayLoginStatus(_args = {}, cfg, deps = {}) {
|
|
|
15479
15777
|
});
|
|
15480
15778
|
}
|
|
15481
15779
|
const delegateAddress = keypair.toSuiAddress();
|
|
15482
|
-
const
|
|
15483
|
-
const gatewayUrl = stored?.gatewayUrl ?? cfg.gatewayUrl;
|
|
15780
|
+
const gatewayUrl = stdioGatewayUrl(cfg);
|
|
15484
15781
|
const cacheKey = `${gatewayUrl}:${delegateAddress}`;
|
|
15485
15782
|
const now = Date.now();
|
|
15486
15783
|
const cacheTtlMs = deps.cacheTtlMs ?? DEFAULT_LOGIN_STATUS_CACHE_TTL_MS;
|
|
@@ -15731,7 +16028,13 @@ function createServer2(cfg, auth, trace, options = {}) {
|
|
|
15731
16028
|
}
|
|
15732
16029
|
switch (name) {
|
|
15733
16030
|
case "access_context":
|
|
15734
|
-
return await accessContext(
|
|
16031
|
+
return await accessContext(
|
|
16032
|
+
{
|
|
16033
|
+
auth,
|
|
16034
|
+
...options.fetchImpl ? { fetchImpl: options.fetchImpl } : {}
|
|
16035
|
+
},
|
|
16036
|
+
cfg
|
|
16037
|
+
);
|
|
15735
16038
|
case "pay": {
|
|
15736
16039
|
const agentHeld = auth?.spendAccountPay ? resolveAgentHeld(cfg, auth.spendAccountPay) : null;
|
|
15737
16040
|
const fileArgs = {
|
|
@@ -15760,6 +16063,7 @@ function createServer2(cfg, auth, trace, options = {}) {
|
|
|
15760
16063
|
transport,
|
|
15761
16064
|
...trace ? { trace } : {},
|
|
15762
16065
|
...agentHeld ? { agentHeld } : {},
|
|
16066
|
+
...options.fetchImpl ? { fetchImpl: options.fetchImpl } : {},
|
|
15763
16067
|
...options.resolveExclusionRegistry ? { resolveExclusionRegistry: options.resolveExclusionRegistry } : {}
|
|
15764
16068
|
}
|
|
15765
16069
|
);
|
|
@@ -15769,7 +16073,12 @@ function createServer2(cfg, auth, trace, options = {}) {
|
|
|
15769
16073
|
{
|
|
15770
16074
|
query: typeof args.query === "string" ? args.query : void 0
|
|
15771
16075
|
},
|
|
15772
|
-
cfg
|
|
16076
|
+
cfg,
|
|
16077
|
+
{
|
|
16078
|
+
auth,
|
|
16079
|
+
transport,
|
|
16080
|
+
...options.fetchImpl ? { fetchImpl: options.fetchImpl } : {}
|
|
16081
|
+
}
|
|
15773
16082
|
);
|
|
15774
16083
|
case "receipts":
|
|
15775
16084
|
return await receipts(
|
|
@@ -15777,7 +16086,10 @@ function createServer2(cfg, auth, trace, options = {}) {
|
|
|
15777
16086
|
challengeId: typeof args.challengeId === "string" ? args.challengeId : void 0
|
|
15778
16087
|
},
|
|
15779
16088
|
cfg,
|
|
15780
|
-
{
|
|
16089
|
+
{
|
|
16090
|
+
auth,
|
|
16091
|
+
...options.fetchImpl ? { fetchImpl: options.fetchImpl } : {}
|
|
16092
|
+
}
|
|
15781
16093
|
);
|
|
15782
16094
|
case "suipay_login": {
|
|
15783
16095
|
if (auth) {
|
|
@@ -15816,7 +16128,10 @@ function createServer2(cfg, auth, trace, options = {}) {
|
|
|
15816
16128
|
"suipay_login_status is for local stdio only; hosted /mcp never holds a spendable key"
|
|
15817
16129
|
);
|
|
15818
16130
|
}
|
|
15819
|
-
return await suipayLoginStatus(args, cfg, {
|
|
16131
|
+
return await suipayLoginStatus(args, cfg, {
|
|
16132
|
+
auth,
|
|
16133
|
+
...options.fetchImpl ? { fetchImpl: options.fetchImpl } : {}
|
|
16134
|
+
});
|
|
15820
16135
|
case "suipay_logout":
|
|
15821
16136
|
if (auth) {
|
|
15822
16137
|
return mcpFailure(
|
|
@@ -15844,7 +16159,7 @@ var EMPTY_ENVELOPE = {
|
|
|
15844
16159
|
requestId: null,
|
|
15845
16160
|
clientName: null
|
|
15846
16161
|
};
|
|
15847
|
-
var DEFAULT_MAX_OBSERVED_BYTES =
|
|
16162
|
+
var DEFAULT_MAX_OBSERVED_BYTES = 1024 * 1024;
|
|
15848
16163
|
var DEFAULT_RUN_SETUP_TIMEOUT_MS = 500;
|
|
15849
16164
|
function canonicalMcpRequestId(id) {
|
|
15850
16165
|
if (typeof id === "number") {
|
|
@@ -16295,7 +16610,41 @@ async function traceMcpHttpRequest(input) {
|
|
|
16295
16610
|
}
|
|
16296
16611
|
|
|
16297
16612
|
// src/http.ts
|
|
16613
|
+
async function countMcpToolCalls(req) {
|
|
16614
|
+
if (req.method !== "POST") return 0;
|
|
16615
|
+
let text2;
|
|
16616
|
+
try {
|
|
16617
|
+
text2 = await req.clone().text();
|
|
16618
|
+
} catch {
|
|
16619
|
+
return 0;
|
|
16620
|
+
}
|
|
16621
|
+
let parsed;
|
|
16622
|
+
try {
|
|
16623
|
+
parsed = JSON.parse(text2);
|
|
16624
|
+
} catch {
|
|
16625
|
+
return 0;
|
|
16626
|
+
}
|
|
16627
|
+
const messages = Array.isArray(parsed) ? parsed : [parsed];
|
|
16628
|
+
let count = 0;
|
|
16629
|
+
for (const message of messages) {
|
|
16630
|
+
if (message && typeof message === "object" && message.method === "tools/call") {
|
|
16631
|
+
count += 1;
|
|
16632
|
+
}
|
|
16633
|
+
}
|
|
16634
|
+
return count;
|
|
16635
|
+
}
|
|
16636
|
+
function tooManyToolCalls() {
|
|
16637
|
+
return new Response(
|
|
16638
|
+
JSON.stringify({
|
|
16639
|
+
jsonrpc: "2.0",
|
|
16640
|
+
id: null,
|
|
16641
|
+
error: { code: ErrorCode.InvalidRequest, message: "a request may contain at most one tools/call" }
|
|
16642
|
+
}),
|
|
16643
|
+
{ status: 400, headers: { "content-type": "application/json" } }
|
|
16644
|
+
);
|
|
16645
|
+
}
|
|
16298
16646
|
async function handleSuipayMcpHttpRequest(req, cfg, auth, trace, options) {
|
|
16647
|
+
if (await countMcpToolCalls(req) > 1) return tooManyToolCalls();
|
|
16299
16648
|
return traceMcpHttpRequest({
|
|
16300
16649
|
request: req,
|
|
16301
16650
|
runtime: trace ?? null,
|
|
@@ -16340,5 +16689,7 @@ export {
|
|
|
16340
16689
|
canonicalMcpRequestId,
|
|
16341
16690
|
inspectMcpEnvelope,
|
|
16342
16691
|
traceMcpHttpRequest,
|
|
16692
|
+
countMcpToolCalls,
|
|
16693
|
+
tooManyToolCalls,
|
|
16343
16694
|
handleSuipayMcpHttpRequest
|
|
16344
16695
|
};
|