@mysten-incubation/suipay-buyer 0.4.0-dev.10 → 0.4.0-dev.12
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 +8 -0
- package/dist/{chunk-RWUAPD7K.js → chunk-FWLPQH4N.js} +301 -16
- package/dist/index.d.ts +21 -12
- package/dist/index.js +292 -68
- package/dist/{native-transfer-DWs55Mse.d.ts → native-transfer-D0UoteMk.d.ts} +83 -4
- package/dist/native-transfer.d.ts +1 -1
- package/dist/native-transfer.js +3 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -198,6 +198,14 @@ the policy's terms as `expected` (and the signer as `options.signer`), or use
|
|
|
198
198
|
`policies.createAndActivate`. `expected.asset` fixes the coin type: a policy
|
|
199
199
|
the gateway reports for another asset is refused before anything is signed.
|
|
200
200
|
|
|
201
|
+
`buildMcpPaymentPayload(paymentRequired, signer, intent)`,
|
|
202
|
+
`createNativeTransferSigner({ client, sender, sign, intent })`, and
|
|
203
|
+
`buildNativeTransferBytes({ client, sender, accept, intent })` require a buyer
|
|
204
|
+
payment intent. Set `network`, `asset`, and `maxAmount` in base units from your
|
|
205
|
+
payment policy. Set `recipient` to pin the payee. Choose these values before
|
|
206
|
+
reading the server's offer. Native transfer helpers also check the intent
|
|
207
|
+
against the client's network.
|
|
208
|
+
|
|
201
209
|
`createAndActivate` and the pool / policy / grant action helpers run the
|
|
202
210
|
prepare → sponsor → sign → execute → finalize round-trip. After execute
|
|
203
211
|
is sent, a lost confirmation is `chain.tx_unconfirmed` (money state
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/native-transfer.ts
|
|
2
2
|
import { Transaction } from "@mysten/sui/transactions";
|
|
3
|
-
import { fromBase64, normalizeStructTag, normalizeSuiAddress, toBase64 } from "@mysten/sui/utils";
|
|
3
|
+
import { fromBase64, normalizeStructTag as normalizeStructTag2, normalizeSuiAddress as normalizeSuiAddress2, toBase64 } from "@mysten/sui/utils";
|
|
4
4
|
|
|
5
5
|
// ../core/dist/errors/types.js
|
|
6
6
|
var PAYMENT_STATES = ["not_charged", "charged", "unknown"];
|
|
@@ -43,6 +43,31 @@ var ACCOUNT_CODES = {
|
|
|
43
43
|
nextAction: "fix_request",
|
|
44
44
|
user: { title: "Agent address not valid", message: "Enter a valid agent address and try again." }
|
|
45
45
|
},
|
|
46
|
+
"account.deposit_cap_exceeded": {
|
|
47
|
+
surfaces: ["gateway", "sdk", "browser"],
|
|
48
|
+
status: 403,
|
|
49
|
+
title: "Deposit would exceed the vault beta cap",
|
|
50
|
+
paymentState: "not_charged",
|
|
51
|
+
retry: "never",
|
|
52
|
+
nextAction: "fix_request",
|
|
53
|
+
user: {
|
|
54
|
+
title: "Over the beta vault limit",
|
|
55
|
+
message: "This deposit would put your vault over the Mainnet Beta cap. Deposit less, or withdraw first."
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"account.deposits_paused": {
|
|
59
|
+
surfaces: ["gateway", "sdk", "browser"],
|
|
60
|
+
status: 403,
|
|
61
|
+
title: "Deposits are paused",
|
|
62
|
+
paymentState: "not_charged",
|
|
63
|
+
retry: "after_delay",
|
|
64
|
+
retryAfterSeconds: 30,
|
|
65
|
+
nextAction: "retry_later",
|
|
66
|
+
user: {
|
|
67
|
+
title: "Deposits paused",
|
|
68
|
+
message: "New deposits are paused right now. Withdrawals and spending still work. Try again after deposits resume."
|
|
69
|
+
}
|
|
70
|
+
},
|
|
46
71
|
"account.finalize_mismatch": {
|
|
47
72
|
surfaces: ["gateway"],
|
|
48
73
|
status: 409,
|
|
@@ -2113,7 +2138,7 @@ var SERVICE_CODES = {
|
|
|
2113
2138
|
// Also `browser`: a directory page that only got a pass/fail boolean off
|
|
2114
2139
|
// a server-side fetch restates this exact condition locally — the user
|
|
2115
2140
|
// copy is equally true regardless of which layer noticed.
|
|
2116
|
-
surfaces: ["gateway", "console_server", "mcp_hosted", "browser"],
|
|
2141
|
+
surfaces: ["gateway", "seller", "console_server", "mcp_hosted", "browser"],
|
|
2117
2142
|
status: 503,
|
|
2118
2143
|
title: "Service temporarily unavailable",
|
|
2119
2144
|
paymentState: "not_charged",
|
|
@@ -2122,6 +2147,16 @@ var SERVICE_CODES = {
|
|
|
2122
2147
|
nextAction: "retry_later",
|
|
2123
2148
|
user: { title: "Temporarily unavailable", message: "This isn't available right now. Try again in a moment." }
|
|
2124
2149
|
},
|
|
2150
|
+
"service.halted": {
|
|
2151
|
+
surfaces: ["gateway", "seller", "console_server", "mcp_hosted", "browser"],
|
|
2152
|
+
status: 503,
|
|
2153
|
+
title: "Services halted temporarily",
|
|
2154
|
+
paymentState: "not_charged",
|
|
2155
|
+
retry: "after_delay",
|
|
2156
|
+
retryAfterSeconds: 5,
|
|
2157
|
+
nextAction: "retry_later",
|
|
2158
|
+
user: { title: "Services halted temporarily", message: "Try again in a moment." }
|
|
2159
|
+
},
|
|
2125
2160
|
"service.not_ready": {
|
|
2126
2161
|
surfaces: ["gateway", "seller", "console_server"],
|
|
2127
2162
|
status: 503,
|
|
@@ -2820,6 +2855,22 @@ function parseErrorPayload(input) {
|
|
|
2820
2855
|
}
|
|
2821
2856
|
return null;
|
|
2822
2857
|
}
|
|
2858
|
+
var DEFINITE_PRE_BROADCAST_REFUSALS = /* @__PURE__ */ new Set([
|
|
2859
|
+
"auth.spend_not_whitelisted",
|
|
2860
|
+
// Confirmed operator spend pause: gateway refuses before broadcast after
|
|
2861
|
+
// first-use bind or a chain_failed + ESpendPaused abort. Generic
|
|
2862
|
+
// `service.unavailable` stays out — session-revocation and other pre-check
|
|
2863
|
+
// paths can emit it after a lost execute may already have broadcast.
|
|
2864
|
+
"service.halted",
|
|
2865
|
+
// Confirmed operator deposit pause on dry-run / chain_failed + EDepositPaused.
|
|
2866
|
+
"account.deposits_paused"
|
|
2867
|
+
]);
|
|
2868
|
+
function isValidatedProblemRefusal(parsed, allowedCodes) {
|
|
2869
|
+
return parsed !== null && parsed.source === "problem" && parsed.paymentState === "not_charged" && isErrorCode(parsed.code) && allowedCodes.has(parsed.code) && definitionOf(parsed.code).status === parsed.status;
|
|
2870
|
+
}
|
|
2871
|
+
function isDefinitePreBroadcastRefusal(parsed) {
|
|
2872
|
+
return isValidatedProblemRefusal(parsed, DEFINITE_PRE_BROADCAST_REFUSALS);
|
|
2873
|
+
}
|
|
2823
2874
|
function readX402Failure(headers) {
|
|
2824
2875
|
for (const name of X402_RESPONSE_HEADERS) {
|
|
2825
2876
|
const raw = readHeader(headers, name);
|
|
@@ -3023,6 +3074,8 @@ var CONTEXT_ALLOWLIST = /* @__PURE__ */ new Set([
|
|
|
3023
3074
|
"sessionDecision",
|
|
3024
3075
|
"settleCode",
|
|
3025
3076
|
"signedRequest",
|
|
3077
|
+
// ADR-0103: v1/v2 dual-accept drain counter on verified delegate/bootstrap lines.
|
|
3078
|
+
"protocolVersion",
|
|
3026
3079
|
"signerCode",
|
|
3027
3080
|
"consentCode",
|
|
3028
3081
|
"publicReason",
|
|
@@ -3347,13 +3400,146 @@ function toErrorFields(value) {
|
|
|
3347
3400
|
return { code: "service.internal", paymentState: "unknown", retryable: false, nextAction: "contact_support" };
|
|
3348
3401
|
}
|
|
3349
3402
|
|
|
3403
|
+
// src/mcp.ts
|
|
3404
|
+
import { isValidSuiAddress, normalizeStructTag, normalizeSuiAddress } from "@mysten/sui/utils";
|
|
3405
|
+
var SUI_ADDRESS_WIRE = /^0x[0-9a-fA-F]{1,64}$/;
|
|
3406
|
+
var BURN_ADDRESS = normalizeSuiAddress("0x0");
|
|
3407
|
+
function parseStrictSuiAddress(raw) {
|
|
3408
|
+
if (typeof raw !== "string" || !SUI_ADDRESS_WIRE.test(raw)) return null;
|
|
3409
|
+
const normalized = normalizeSuiAddress(raw);
|
|
3410
|
+
if (!isValidSuiAddress(normalized) || normalized === BURN_ADDRESS) return null;
|
|
3411
|
+
return normalized;
|
|
3412
|
+
}
|
|
3413
|
+
var INTENT_REFUSAL_CODES = {
|
|
3414
|
+
invalid: "client.config_invalid",
|
|
3415
|
+
mismatch: "payment.method_unsupported",
|
|
3416
|
+
limit: "account.limit_exceeded"
|
|
3417
|
+
};
|
|
3418
|
+
function intentRefusalToError(refusal) {
|
|
3419
|
+
return SuiPayError.of(INTENT_REFUSAL_CODES[refusal.kind], "not_charged", { detail: refusal.detail });
|
|
3420
|
+
}
|
|
3421
|
+
function invalid(detail) {
|
|
3422
|
+
return { ok: false, refusal: { kind: "invalid", detail } };
|
|
3423
|
+
}
|
|
3424
|
+
function resolveBuyerIntent(intent) {
|
|
3425
|
+
if (typeof intent !== "object" || intent === null) {
|
|
3426
|
+
return invalid("a buyer payment intent (network, maxAmount, asset) is required");
|
|
3427
|
+
}
|
|
3428
|
+
const raw = intent;
|
|
3429
|
+
if (typeof raw.network !== "string" || !raw.network.startsWith("sui:") || raw.network.length <= 4) {
|
|
3430
|
+
return invalid("intent.network must be a sui:* network");
|
|
3431
|
+
}
|
|
3432
|
+
let maxAmount;
|
|
3433
|
+
if (typeof raw.maxAmount === "bigint") {
|
|
3434
|
+
maxAmount = raw.maxAmount;
|
|
3435
|
+
} else if (typeof raw.maxAmount === "string" && /^[0-9]+$/.test(raw.maxAmount)) {
|
|
3436
|
+
maxAmount = BigInt(raw.maxAmount);
|
|
3437
|
+
} else {
|
|
3438
|
+
return invalid("intent.maxAmount must be a bigint or an integer string");
|
|
3439
|
+
}
|
|
3440
|
+
if (maxAmount <= 0n) return invalid("intent.maxAmount must be positive");
|
|
3441
|
+
if (typeof raw.asset !== "string") return invalid("intent.asset must be a coin type");
|
|
3442
|
+
let asset;
|
|
3443
|
+
try {
|
|
3444
|
+
asset = normalizeStructTag(raw.asset);
|
|
3445
|
+
} catch {
|
|
3446
|
+
return invalid("intent.asset must be a coin type");
|
|
3447
|
+
}
|
|
3448
|
+
let recipient;
|
|
3449
|
+
if (raw.recipient !== void 0) {
|
|
3450
|
+
const parsed = parseStrictSuiAddress(raw.recipient);
|
|
3451
|
+
if (!parsed) return invalid("intent.recipient must be a Sui address");
|
|
3452
|
+
recipient = parsed;
|
|
3453
|
+
}
|
|
3454
|
+
return {
|
|
3455
|
+
ok: true,
|
|
3456
|
+
intent: { network: raw.network, maxAmount, asset, ...recipient ? { recipient } : {} }
|
|
3457
|
+
};
|
|
3458
|
+
}
|
|
3459
|
+
function checkAcceptAgainstIntent(accept, intent) {
|
|
3460
|
+
if (accept.scheme !== "exact") {
|
|
3461
|
+
return { kind: "mismatch", detail: `accept scheme ${accept.scheme} is not exact` };
|
|
3462
|
+
}
|
|
3463
|
+
if (accept.network !== intent.network) {
|
|
3464
|
+
return { kind: "mismatch", detail: `accept network ${accept.network} is not the intended ${intent.network}` };
|
|
3465
|
+
}
|
|
3466
|
+
let asset;
|
|
3467
|
+
try {
|
|
3468
|
+
asset = normalizeStructTag(accept.asset);
|
|
3469
|
+
} catch {
|
|
3470
|
+
return { kind: "mismatch", detail: `accept asset ${accept.asset} is not a coin type` };
|
|
3471
|
+
}
|
|
3472
|
+
if (asset !== intent.asset) {
|
|
3473
|
+
return { kind: "mismatch", detail: `accept asset ${asset} is not the intended ${intent.asset}` };
|
|
3474
|
+
}
|
|
3475
|
+
if (typeof accept.amount !== "string" || !/^[0-9]+$/.test(accept.amount)) {
|
|
3476
|
+
return { kind: "mismatch", detail: "accept amount is not an integer string" };
|
|
3477
|
+
}
|
|
3478
|
+
const amount = BigInt(accept.amount);
|
|
3479
|
+
if (amount <= 0n) {
|
|
3480
|
+
return { kind: "mismatch", detail: "accept amount must be positive" };
|
|
3481
|
+
}
|
|
3482
|
+
if (amount > intent.maxAmount) {
|
|
3483
|
+
return {
|
|
3484
|
+
kind: "limit",
|
|
3485
|
+
detail: `accept amount ${accept.amount} exceeds the intended maximum ${intent.maxAmount}`
|
|
3486
|
+
};
|
|
3487
|
+
}
|
|
3488
|
+
const payTo = parseStrictSuiAddress(accept.payTo);
|
|
3489
|
+
if (!payTo) {
|
|
3490
|
+
return { kind: "mismatch", detail: "accept payTo must be a Sui address" };
|
|
3491
|
+
}
|
|
3492
|
+
if (intent.recipient !== void 0 && payTo !== intent.recipient) {
|
|
3493
|
+
return { kind: "mismatch", detail: `accept payTo ${accept.payTo} is not the intended recipient` };
|
|
3494
|
+
}
|
|
3495
|
+
return null;
|
|
3496
|
+
}
|
|
3497
|
+
async function buildMcpPaymentPayload(paymentRequired, signer, intent) {
|
|
3498
|
+
const resolved = resolveBuyerIntent(intent);
|
|
3499
|
+
if (!resolved.ok) throw intentRefusalToError(resolved.refusal);
|
|
3500
|
+
const candidates = paymentRequired.accepts.filter(
|
|
3501
|
+
(a) => a.scheme === "exact" && a.network === resolved.intent.network
|
|
3502
|
+
);
|
|
3503
|
+
let firstRefusal = null;
|
|
3504
|
+
let accept;
|
|
3505
|
+
for (const candidate of candidates) {
|
|
3506
|
+
const refusal = checkAcceptAgainstIntent(candidate, resolved.intent);
|
|
3507
|
+
if (!refusal) {
|
|
3508
|
+
accept = candidate;
|
|
3509
|
+
break;
|
|
3510
|
+
}
|
|
3511
|
+
firstRefusal ??= refusal;
|
|
3512
|
+
}
|
|
3513
|
+
if (!accept) {
|
|
3514
|
+
if (firstRefusal) throw intentRefusalToError(firstRefusal);
|
|
3515
|
+
throw SuiPayError.of("payment.method_unsupported", "not_charged", {
|
|
3516
|
+
detail: `no exact accept on ${resolved.intent.network} in PaymentRequired`
|
|
3517
|
+
});
|
|
3518
|
+
}
|
|
3519
|
+
const signed = await signer.buildNativeTransfer(accept);
|
|
3520
|
+
const payload = {
|
|
3521
|
+
x402Version: 2,
|
|
3522
|
+
resource: paymentRequired.resource,
|
|
3523
|
+
accepted: accept,
|
|
3524
|
+
payload: {
|
|
3525
|
+
transaction: signed.transaction,
|
|
3526
|
+
signature: signed.signature
|
|
3527
|
+
}
|
|
3528
|
+
};
|
|
3529
|
+
return { payload, meta: { "x402/payment": payload } };
|
|
3530
|
+
}
|
|
3531
|
+
|
|
3350
3532
|
// src/native-transfer.ts
|
|
3351
|
-
var
|
|
3533
|
+
var DEFAULT_NATIVE_MAX_GAS_BUDGET = 10000000n;
|
|
3534
|
+
var SUI_COIN_TYPE = normalizeStructTag2("0x2::sui::SUI");
|
|
3352
3535
|
var NATIVE_COMMAND_KINDS = /* @__PURE__ */ new Set(["SplitCoins", "MergeCoins", "TransferObjects"]);
|
|
3353
3536
|
var MAX_PAYMENT_COINS = 200;
|
|
3354
3537
|
var COIN_PAGE_LIMIT = 50;
|
|
3355
3538
|
var NATIVE_TRANSFER_CODES = {
|
|
3356
3539
|
UNSUPPORTED_ACCEPT: "payment.method_unsupported",
|
|
3540
|
+
INTENT_INVALID: INTENT_REFUSAL_CODES.invalid,
|
|
3541
|
+
INTENT_MISMATCH: INTENT_REFUSAL_CODES.mismatch,
|
|
3542
|
+
INTENT_LIMIT_EXCEEDED: INTENT_REFUSAL_CODES.limit,
|
|
3357
3543
|
INSUFFICIENT_BALANCE: "account.funds_insufficient",
|
|
3358
3544
|
TOO_MANY_COINS: "account.too_many_coins",
|
|
3359
3545
|
BUILD_FAILED: "client.build_failed",
|
|
@@ -3383,6 +3569,29 @@ var NativeTransferError = class extends SuiPayError {
|
|
|
3383
3569
|
this.reason = reason;
|
|
3384
3570
|
}
|
|
3385
3571
|
};
|
|
3572
|
+
var INTENT_REASONS = {
|
|
3573
|
+
invalid: "INTENT_INVALID",
|
|
3574
|
+
mismatch: "INTENT_MISMATCH",
|
|
3575
|
+
limit: "INTENT_LIMIT_EXCEEDED"
|
|
3576
|
+
};
|
|
3577
|
+
function refuse(refusal) {
|
|
3578
|
+
throw new NativeTransferError(INTENT_REASONS[refusal.kind], refusal.detail);
|
|
3579
|
+
}
|
|
3580
|
+
function resolveIntentFor(client, intent) {
|
|
3581
|
+
const resolved = resolveBuyerIntent(intent);
|
|
3582
|
+
if (!resolved.ok) return refuse(resolved.refusal);
|
|
3583
|
+
const clientNetwork = client?.network;
|
|
3584
|
+
if (typeof clientNetwork !== "string" || clientNetwork.length === 0) {
|
|
3585
|
+
return refuse({ kind: "invalid", detail: "the client does not expose a network to pin the intent to" });
|
|
3586
|
+
}
|
|
3587
|
+
if (`sui:${clientNetwork}` !== resolved.intent.network) {
|
|
3588
|
+
return refuse({
|
|
3589
|
+
kind: "mismatch",
|
|
3590
|
+
detail: `client network sui:${clientNetwork} is not the intended ${resolved.intent.network}`
|
|
3591
|
+
});
|
|
3592
|
+
}
|
|
3593
|
+
return resolved.intent;
|
|
3594
|
+
}
|
|
3386
3595
|
function parseAmount(raw) {
|
|
3387
3596
|
if (!/^[0-9]+$/.test(raw)) {
|
|
3388
3597
|
throw new NativeTransferError("UNSUPPORTED_ACCEPT", `amount ${raw} is not an integer string`);
|
|
@@ -3395,7 +3604,7 @@ function parseAmount(raw) {
|
|
|
3395
3604
|
}
|
|
3396
3605
|
function normalizeAsset(asset) {
|
|
3397
3606
|
try {
|
|
3398
|
-
return
|
|
3607
|
+
return normalizeStructTag2(asset);
|
|
3399
3608
|
} catch {
|
|
3400
3609
|
throw new NativeTransferError("UNSUPPORTED_ACCEPT", `asset ${asset} is not a coin type`);
|
|
3401
3610
|
}
|
|
@@ -3457,7 +3666,7 @@ function readAddress(arg, inputs) {
|
|
|
3457
3666
|
if (raw?.length !== 32) return void 0;
|
|
3458
3667
|
let hex = "";
|
|
3459
3668
|
for (const b of raw) hex += b.toString(16).padStart(2, "0");
|
|
3460
|
-
return
|
|
3669
|
+
return normalizeSuiAddress2(`0x${hex}`);
|
|
3461
3670
|
}
|
|
3462
3671
|
function readU64(arg, inputs) {
|
|
3463
3672
|
const raw = pureBytes(arg, inputs);
|
|
@@ -3474,12 +3683,31 @@ function splitIndexOf(arg) {
|
|
|
3474
3683
|
return void 0;
|
|
3475
3684
|
}
|
|
3476
3685
|
function inspectNativeTransfer(bytes, expected) {
|
|
3686
|
+
const sender = parseStrictSuiAddress(expected.sender);
|
|
3687
|
+
if (!sender) return "expected sender is not a Sui address";
|
|
3688
|
+
const payTo = parseStrictSuiAddress(expected.payTo);
|
|
3689
|
+
if (!payTo) return "expected payTo is not a Sui address";
|
|
3690
|
+
if (expected.maxGasBudget !== void 0 && (typeof expected.maxGasBudget !== "bigint" || expected.maxGasBudget <= 0n)) {
|
|
3691
|
+
return "maxGasBudget must be a positive bigint";
|
|
3692
|
+
}
|
|
3477
3693
|
const data = Transaction.from(bytes).getData();
|
|
3478
3694
|
const commands = data.commands;
|
|
3479
3695
|
const inputs = data.inputs;
|
|
3480
|
-
if (!data.sender ||
|
|
3696
|
+
if (!data.sender || normalizeSuiAddress2(data.sender) !== sender) {
|
|
3481
3697
|
return "transaction sender is not the payer";
|
|
3482
3698
|
}
|
|
3699
|
+
const gasOwner = data.gasData?.owner;
|
|
3700
|
+
if (!gasOwner || normalizeSuiAddress2(gasOwner) !== sender) {
|
|
3701
|
+
return "gas is not paid by the payer";
|
|
3702
|
+
}
|
|
3703
|
+
const budget = data.gasData?.budget;
|
|
3704
|
+
if (budget === null || budget === void 0 || !/^[0-9]+$/.test(String(budget)) || BigInt(budget) <= 0n) {
|
|
3705
|
+
return "transaction gas budget is missing or not a positive integer";
|
|
3706
|
+
}
|
|
3707
|
+
const maxGasBudget = expected.maxGasBudget ?? DEFAULT_NATIVE_MAX_GAS_BUDGET;
|
|
3708
|
+
if (BigInt(budget) > maxGasBudget) {
|
|
3709
|
+
return `gas budget ${budget} exceeds the allowed ${maxGasBudget}`;
|
|
3710
|
+
}
|
|
3483
3711
|
for (const command of commands) {
|
|
3484
3712
|
if (!NATIVE_COMMAND_KINDS.has(command.$kind)) {
|
|
3485
3713
|
return `command ${command.$kind} is not a native transfer`;
|
|
@@ -3487,15 +3715,15 @@ function inspectNativeTransfer(bytes, expected) {
|
|
|
3487
3715
|
}
|
|
3488
3716
|
const external = commands.filter((c) => {
|
|
3489
3717
|
if (c.$kind !== "TransferObjects" || !c.TransferObjects) return false;
|
|
3490
|
-
return readAddress(c.TransferObjects.address, inputs) !==
|
|
3718
|
+
return readAddress(c.TransferObjects.address, inputs) !== sender;
|
|
3491
3719
|
});
|
|
3492
3720
|
if (external.length !== 1) {
|
|
3493
3721
|
return `transaction has ${external.length} external recipients; exactly one is allowed`;
|
|
3494
3722
|
}
|
|
3495
3723
|
const transfer = external[0].TransferObjects;
|
|
3496
3724
|
const recipient = readAddress(transfer.address, inputs);
|
|
3497
|
-
if (recipient !==
|
|
3498
|
-
return `transaction pays ${recipient ?? "an unreadable address"}, not ${
|
|
3725
|
+
if (recipient !== payTo) {
|
|
3726
|
+
return `transaction pays ${recipient ?? "an unreadable address"}, not ${payTo}`;
|
|
3499
3727
|
}
|
|
3500
3728
|
if (transfer.objects.length !== 1) {
|
|
3501
3729
|
return "the payment transfer must move exactly one coin";
|
|
@@ -3514,18 +3742,55 @@ function inspectNativeTransfer(bytes, expected) {
|
|
|
3514
3742
|
}
|
|
3515
3743
|
return null;
|
|
3516
3744
|
}
|
|
3745
|
+
function requirePositiveBigint(value, label) {
|
|
3746
|
+
if (typeof value !== "bigint" || value <= 0n) {
|
|
3747
|
+
throw new NativeTransferError("INTENT_INVALID", `${label} must be a positive bigint`);
|
|
3748
|
+
}
|
|
3749
|
+
return value;
|
|
3750
|
+
}
|
|
3751
|
+
function parseGasInteger(raw, label) {
|
|
3752
|
+
try {
|
|
3753
|
+
const value = typeof raw === "bigint" ? raw : BigInt(raw);
|
|
3754
|
+
if (value <= 0n) {
|
|
3755
|
+
throw new NativeTransferError("INTENT_INVALID", `${label} must be a positive integer`);
|
|
3756
|
+
}
|
|
3757
|
+
return value;
|
|
3758
|
+
} catch (err) {
|
|
3759
|
+
if (err instanceof NativeTransferError) throw err;
|
|
3760
|
+
throw new NativeTransferError("INTENT_INVALID", `${label} must be an integer`, { cause: err });
|
|
3761
|
+
}
|
|
3762
|
+
}
|
|
3763
|
+
function resolveMaxGasBudget(input, pinnedBudget) {
|
|
3764
|
+
const explicit = input.maxGasBudget !== void 0 ? requirePositiveBigint(input.maxGasBudget, "maxGasBudget") : void 0;
|
|
3765
|
+
if (pinnedBudget !== void 0 && explicit !== void 0) {
|
|
3766
|
+
return pinnedBudget < explicit ? pinnedBudget : explicit;
|
|
3767
|
+
}
|
|
3768
|
+
return explicit ?? pinnedBudget ?? DEFAULT_NATIVE_MAX_GAS_BUDGET;
|
|
3769
|
+
}
|
|
3517
3770
|
async function buildNativeTransferBytes(input) {
|
|
3518
3771
|
const { accept, client } = input;
|
|
3772
|
+
const intent = resolveIntentFor(client, input.intent);
|
|
3773
|
+
const outside = checkAcceptAgainstIntent(accept, intent);
|
|
3774
|
+
if (outside) return refuse(outside);
|
|
3519
3775
|
if (accept.scheme !== "exact" || !accept.network.startsWith("sui:")) {
|
|
3520
3776
|
throw new NativeTransferError(
|
|
3521
3777
|
"UNSUPPORTED_ACCEPT",
|
|
3522
3778
|
`accept is ${accept.scheme} on ${accept.network}; only exact on sui:* is supported`
|
|
3523
3779
|
);
|
|
3524
3780
|
}
|
|
3525
|
-
const sender =
|
|
3526
|
-
|
|
3781
|
+
const sender = parseStrictSuiAddress(input.sender);
|
|
3782
|
+
if (!sender) {
|
|
3783
|
+
throw new NativeTransferError("INTENT_INVALID", "sender must be a Sui address");
|
|
3784
|
+
}
|
|
3785
|
+
const payTo = parseStrictSuiAddress(accept.payTo);
|
|
3786
|
+
if (!payTo) {
|
|
3787
|
+
throw new NativeTransferError("UNSUPPORTED_ACCEPT", "accept payTo must be a Sui address");
|
|
3788
|
+
}
|
|
3527
3789
|
const amount = parseAmount(accept.amount);
|
|
3528
3790
|
const asset = normalizeAsset(accept.asset);
|
|
3791
|
+
const pinnedBudget = input.gas ? parseGasInteger(input.gas.budget, "gas budget") : void 0;
|
|
3792
|
+
const pinnedPrice = input.gas ? parseGasInteger(input.gas.price, "gas price") : void 0;
|
|
3793
|
+
const maxGasBudget = resolveMaxGasBudget(input, pinnedBudget);
|
|
3529
3794
|
const tx = new Transaction();
|
|
3530
3795
|
tx.setSender(sender);
|
|
3531
3796
|
if (asset === SUI_COIN_TYPE) {
|
|
@@ -3547,8 +3812,8 @@ async function buildNativeTransferBytes(input) {
|
|
|
3547
3812
|
}
|
|
3548
3813
|
if (input.gas) {
|
|
3549
3814
|
tx.setGasPayment(input.gas.payment);
|
|
3550
|
-
tx.setGasPrice(
|
|
3551
|
-
tx.setGasBudget(
|
|
3815
|
+
tx.setGasPrice(pinnedPrice);
|
|
3816
|
+
tx.setGasBudget(pinnedBudget);
|
|
3552
3817
|
}
|
|
3553
3818
|
let bytes;
|
|
3554
3819
|
try {
|
|
@@ -3556,7 +3821,12 @@ async function buildNativeTransferBytes(input) {
|
|
|
3556
3821
|
} catch (err) {
|
|
3557
3822
|
throw new NativeTransferError("BUILD_FAILED", "could not build the transfer", { cause: err });
|
|
3558
3823
|
}
|
|
3559
|
-
const problem = inspectNativeTransfer(bytes, {
|
|
3824
|
+
const problem = inspectNativeTransfer(bytes, {
|
|
3825
|
+
sender,
|
|
3826
|
+
payTo,
|
|
3827
|
+
amount,
|
|
3828
|
+
maxGasBudget
|
|
3829
|
+
});
|
|
3560
3830
|
if (problem) throw new NativeTransferError("NOT_NATIVE", problem);
|
|
3561
3831
|
return {
|
|
3562
3832
|
transaction: toBase64(bytes),
|
|
@@ -3568,13 +3838,23 @@ async function buildNativeTransferBytes(input) {
|
|
|
3568
3838
|
};
|
|
3569
3839
|
}
|
|
3570
3840
|
function createNativeTransferSigner(input) {
|
|
3841
|
+
resolveIntentFor(input.client, input.intent);
|
|
3842
|
+
const sender = parseStrictSuiAddress(input.sender);
|
|
3843
|
+
if (!sender) {
|
|
3844
|
+
throw new NativeTransferError("INTENT_INVALID", "sender must be a Sui address");
|
|
3845
|
+
}
|
|
3846
|
+
if (input.maxGasBudget !== void 0) {
|
|
3847
|
+
requirePositiveBigint(input.maxGasBudget, "maxGasBudget");
|
|
3848
|
+
}
|
|
3571
3849
|
return {
|
|
3572
3850
|
async buildNativeTransfer(accept) {
|
|
3573
3851
|
const built = await buildNativeTransferBytes({
|
|
3574
3852
|
client: input.client,
|
|
3575
|
-
sender
|
|
3853
|
+
sender,
|
|
3576
3854
|
accept,
|
|
3577
|
-
|
|
3855
|
+
intent: input.intent,
|
|
3856
|
+
...input.gas ? { gas: input.gas } : {},
|
|
3857
|
+
...input.maxGasBudget !== void 0 ? { maxGasBudget: input.maxGasBudget } : {}
|
|
3578
3858
|
});
|
|
3579
3859
|
const signature = await input.sign(built.bytes);
|
|
3580
3860
|
return { transaction: built.transaction, signature };
|
|
@@ -3590,9 +3870,14 @@ export {
|
|
|
3590
3870
|
deriveNextAction,
|
|
3591
3871
|
stricterPaymentState,
|
|
3592
3872
|
parseErrorPayload,
|
|
3873
|
+
DEFINITE_PRE_BROADCAST_REFUSALS,
|
|
3874
|
+
isValidatedProblemRefusal,
|
|
3875
|
+
isDefinitePreBroadcastRefusal,
|
|
3593
3876
|
logError,
|
|
3594
3877
|
SuiPayError,
|
|
3595
3878
|
toErrorFields,
|
|
3879
|
+
buildMcpPaymentPayload,
|
|
3880
|
+
DEFAULT_NATIVE_MAX_GAS_BUDGET,
|
|
3596
3881
|
NativeTransferError,
|
|
3597
3882
|
inspectNativeTransfer,
|
|
3598
3883
|
buildNativeTransferBytes,
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { E as ErrorCode, P as PaymentState, N as NextAction, R as Receipt, C as CodeFor, D as DelegationScope, O as Offer } from './native-transfer-
|
|
2
|
-
export { B as BuildNativeTransferInput, M as McpPaymentSigner,
|
|
1
|
+
import { E as ErrorCode, P as PaymentState, N as NextAction, R as Receipt, C as CodeFor, D as DelegationScope, O as Offer } from './native-transfer-D0UoteMk.js';
|
|
2
|
+
export { B as BuildNativeTransferInput, a as BuyerPaymentIntent, M as McpPaymentSigner, b as NativeTransferAccept, c as NativeTransferBuild, d as NativeTransferError, e as NativeTransferGas, f as NativeTransferSignerInput, S as SuiPayError, g as SuiPayErrorFields, h as buildMcpPaymentPayload, i as buildNativeTransferBytes, j as createNativeTransferSigner, k as inspectNativeTransfer, t as toErrorFields } from './native-transfer-D0UoteMk.js';
|
|
3
3
|
import { SuiGrpcClient } from '@mysten/sui/grpc';
|
|
4
4
|
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
|
|
5
5
|
import { KeyObject } from 'node:crypto';
|
|
@@ -834,7 +834,7 @@ interface DelegatePoliciesDTO {
|
|
|
834
834
|
* Agent signing helper — emit the four `x-suipay-*` signed-request headers.
|
|
835
835
|
*
|
|
836
836
|
* A delegate agent proves identity on every private, grant-scoped request by
|
|
837
|
-
* signing the canonical `
|
|
837
|
+
* signing the canonical `SignedRequestV2` payload with a Sui personal-message
|
|
838
838
|
* signature. This helper is the client-side counterpart to the gateway verifier:
|
|
839
839
|
* it canonicalizes the path/query and hashes the body EXACTLY as the verifier
|
|
840
840
|
* will (reusing the P0 codec's `canonicalizePath` / `canonicalizeQuery` /
|
|
@@ -846,24 +846,30 @@ interface DelegatePoliciesDTO {
|
|
|
846
846
|
* bound via {@link personalMessageSigner}). Money never moves on this signature;
|
|
847
847
|
* it authorizes only the grant-scoped reads the epic defines.
|
|
848
848
|
*
|
|
849
|
-
*
|
|
850
|
-
* `tests/agent-sdk-signed-headers.test.ts`,
|
|
851
|
-
*
|
|
852
|
-
* pins.
|
|
849
|
+
* The signed bytes are pinned by the v2 vector in
|
|
850
|
+
* `tests/agent-sdk-signed-headers.test.ts`, built from the same field values
|
|
851
|
+
* as the v1 cross-language vector (`tests/parity/signed-request-golden.test.ts`).
|
|
853
852
|
*/
|
|
854
853
|
|
|
855
854
|
/** Sui personal-message signer: returns the serialized signature (base64). */
|
|
856
855
|
type SignPersonalMessage = (message: Uint8Array) => Promise<string>;
|
|
857
856
|
/**
|
|
858
|
-
* The deployment + grant identity every signed request binds. `network
|
|
859
|
-
* `packageId` MUST equal the gateway's server-authoritative
|
|
860
|
-
* MUST equal the grant's registered on-chain delegate (the
|
|
857
|
+
* The deployment + grant identity every signed request binds. `network`,
|
|
858
|
+
* `packageId`, and `origin` MUST equal the gateway's server-authoritative
|
|
859
|
+
* config; `delegate` MUST equal the grant's registered on-chain delegate (the
|
|
860
|
+
* signer's address).
|
|
861
861
|
*/
|
|
862
862
|
interface SessionSignerContext {
|
|
863
863
|
/** e.g. `sui:testnet` — must equal the gateway config. */
|
|
864
864
|
network: string;
|
|
865
865
|
/** Deployed package id — must equal the gateway config. */
|
|
866
866
|
packageId: string;
|
|
867
|
+
/**
|
|
868
|
+
* The gateway the request is sent to — any http(s) URL on it; only its
|
|
869
|
+
* {@link canonicalOrigin} is signed. Must equal the gateway's configured base
|
|
870
|
+
* URL origin, so the signature cannot be replayed at another gateway.
|
|
871
|
+
*/
|
|
872
|
+
origin: string;
|
|
867
873
|
/** On-chain grant object id — the authorization scope. */
|
|
868
874
|
grantObjectId: string;
|
|
869
875
|
/** Registered delegate Sui address (the signer). */
|
|
@@ -939,8 +945,11 @@ type PayerReadResult<T> = {
|
|
|
939
945
|
interface PayerReadClientOptions {
|
|
940
946
|
/** Gateway origin, e.g. `https://gateway.suipay.dev` (no trailing slash needed). */
|
|
941
947
|
gatewayUrl: string;
|
|
942
|
-
/**
|
|
943
|
-
|
|
948
|
+
/**
|
|
949
|
+
* Deployment + grant identity every signed read binds. The signed origin is
|
|
950
|
+
* always `gatewayUrl`'s: a read binds the gateway it is sent to.
|
|
951
|
+
*/
|
|
952
|
+
context: Omit<SessionSignerContext, 'origin'>;
|
|
944
953
|
/** Local personal-message signer; the key never leaves it. */
|
|
945
954
|
signPersonalMessage: SignPersonalMessage;
|
|
946
955
|
/** Injectable fetch (tests / custom transports). Defaults to global `fetch`. */
|