@mysten-incubation/suipay-buyer 0.3.0-dev.4 → 0.3.0-dev.6

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.d.ts CHANGED
@@ -385,6 +385,48 @@ interface CreatePolicyResponse {
385
385
  interface CreateProfileResponse {
386
386
  profile: AgentProfile;
387
387
  }
388
+ /**
389
+ * Discriminated union for prepared consent grant transactions.
390
+ * Returned by POST /v1/oauth/sessions/:id/grants/prepare.
391
+ */
392
+ type PreparedConsentGrant = {
393
+ kind: 'sponsored';
394
+ connectionGrantId: string;
395
+ connectionGrantIds?: string[];
396
+ asset: string;
397
+ coinType: string;
398
+ target: string;
399
+ sender: string;
400
+ bytes: string;
401
+ digest: string;
402
+ transactionKindBytes?: string;
403
+ completed?: false;
404
+ } | {
405
+ kind: 'unsponsored';
406
+ connectionGrantId: string;
407
+ connectionGrantIds?: string[];
408
+ asset: string;
409
+ coinType: string;
410
+ target: string;
411
+ sender: string;
412
+ transactionKindBytes: string;
413
+ bytes?: undefined;
414
+ digest?: undefined;
415
+ completed?: false;
416
+ } | {
417
+ kind: 'completed';
418
+ connectionGrantId: string;
419
+ connectionGrantIds?: string[];
420
+ asset: string;
421
+ coinType: string;
422
+ target: string;
423
+ sender: string;
424
+ bytes?: string;
425
+ digest?: string;
426
+ grantObjectId?: string;
427
+ completed: true;
428
+ transactionKindBytes?: string;
429
+ };
388
430
  /** @deprecated Renamed to {@link DelegatedSignerSession}. */
389
431
  type DelegatedSignerGrant = DelegatedSignerSession;
390
432
  /** @deprecated Renamed to {@link PreparedSessionTxKind}. */
@@ -1763,4 +1805,4 @@ interface VerifyPreparedSettlementInput {
1763
1805
  */
1764
1806
  declare function verifyPreparedPayment(input: VerifyPreparedSettlementInput): Promise<PreparedSettlement>;
1765
1807
 
1766
- export { type AccountApi, type AccountsApi, type AgentConnectionManifest, type AgentGrantBinding, type AgentProfile, type AgentSessionBinding, type AgentsApi, type AssetScopeInput, type BuyerAccount, type BuyerClient, type BuyerClientError, type BuyerClientOptions, type BuyerConnection, type BuyerPolicy, type BuyerResult, type CatalogApi, type CatalogService, type CreateAndActivateResult, type CreateDelegatedSignerInput, type CreateDelegatedSignerResponse, type CreatePolicyInput, type CreatePolicyResponse, type CreateProfileInput, type CreateProfileResponse, type DashboardBridgeResolvers, type DashboardConnectionManifest, type DelegatedSigner, type DelegatedSignerGrant, type DelegatedSignerGrantStatus, type DelegatedSignerSession, type DelegatedSignerStatus, type DelegatedSignersApi, ErrorCode, type ExecuteIntentKind, type ExecuteIntentResult, type FinalizeGrantResponse, type FinalizeIntentResult, type FinalizeSessionResponse, type FundHistoryItem, type GrantsApi, type ManifestError, type ManifestResult, NextAction, type PatchProfileInput, type PayInput, type PayResult, type Payer, type PayerOptions, type PaymentMethod, type PaymentReceipt, type PaymentRow, PaymentState, type PaymentStatusValue, type PaymentsApi, type PoliciesApi, type PolicyStatus, type PolicyTarget, type PrepareCreateGrantInput, type PrepareCreateSessionInput, type PreparedGrantTxKind, type PreparedIntent, type PreparedRevokeTxKind, type PreparedSessionTxKind, type ProfileStatus, type ProfilesApi, type ReceiptsApi, type RegistryApi, type RegistryService, type RevokeDelegatedSignerResponse, type ServiceRail, type ServicesApi, type SessionsApi, type SettlementRow, type SettlementsApi, type SpendAccount, type SpendAccountStatus, type SpendAccountsApi, type SponsoredTx, type TransactionSigner, type TrustSignal, createBuyer, createBuyerControlPlane, createMemoryLedger, createPayer, createReader, dashboardManifestToAgentManifest, manifestToSession, parseAgentManifest, personalMessageSigner, verifyPreparedPayment };
1808
+ export { type AccountApi, type AccountsApi, type AgentConnectionManifest, type AgentGrantBinding, type AgentProfile, type AgentSessionBinding, type AgentsApi, type AssetScopeInput, type BuyerAccount, type BuyerClient, type BuyerClientError, type BuyerClientOptions, type BuyerConnection, type BuyerPolicy, type BuyerResult, type CatalogApi, type CatalogService, type CreateAndActivateResult, type CreateDelegatedSignerInput, type CreateDelegatedSignerResponse, type CreatePolicyInput, type CreatePolicyResponse, type CreateProfileInput, type CreateProfileResponse, type DashboardBridgeResolvers, type DashboardConnectionManifest, type DelegatedSigner, type DelegatedSignerGrant, type DelegatedSignerGrantStatus, type DelegatedSignerSession, type DelegatedSignerStatus, type DelegatedSignersApi, ErrorCode, type ExecuteIntentKind, type ExecuteIntentResult, type FinalizeGrantResponse, type FinalizeIntentResult, type FinalizeSessionResponse, type FundHistoryItem, type GrantsApi, type ManifestError, type ManifestResult, NextAction, type PatchProfileInput, type PayInput, type PayResult, type Payer, type PayerOptions, type PaymentMethod, type PaymentReceipt, type PaymentRow, PaymentState, type PaymentStatusValue, type PaymentsApi, type PoliciesApi, type PolicyStatus, type PolicyTarget, type PrepareCreateGrantInput, type PrepareCreateSessionInput, type PreparedConsentGrant, type PreparedGrantTxKind, type PreparedIntent, type PreparedRevokeTxKind, type PreparedSessionTxKind, type ProfileStatus, type ProfilesApi, type ReceiptsApi, type RegistryApi, type RegistryService, type RevokeDelegatedSignerResponse, type ServiceRail, type ServicesApi, type SessionsApi, type SettlementRow, type SettlementsApi, type SpendAccount, type SpendAccountStatus, type SpendAccountsApi, type SponsoredTx, type TransactionSigner, type TrustSignal, createBuyer, createBuyerControlPlane, createMemoryLedger, createPayer, createReader, dashboardManifestToAgentManifest, manifestToSession, parseAgentManifest, personalMessageSigner, verifyPreparedPayment };
package/dist/index.js CHANGED
@@ -514,7 +514,7 @@ function verifyOwnerTransaction(input) {
514
514
  if (k === "TransferObjects" || k === "Publish" || k === "Upgrade") {
515
515
  throw new TransactionVerificationError("unsupportedOperation");
516
516
  }
517
- if (k === "MakeMoveVec" && input.operation.kind !== "create_policy" && input.operation.kind !== "replace_grant") {
517
+ if (k === "MakeMoveVec" && input.operation.kind !== "create_policy" && input.operation.kind !== "replace_grant" && input.operation.kind !== "create_grant_consent_batch") {
518
518
  throw new TransactionVerificationError("unsupportedOperation");
519
519
  }
520
520
  }
@@ -958,87 +958,253 @@ function verifyOwnerTransaction(input) {
958
958
  allowedAssets.add(assetNorm);
959
959
  const existing = assetGroups.get(assetNorm) ?? {
960
960
  sumTotalCap: 0n,
961
- policyIds: []
961
+ policyIds: [],
962
+ policies: []
962
963
  };
963
964
  existing.sumTotalCap += capAmt;
964
965
  existing.policyIds.push(p.id);
966
+ existing.policies.push(p);
965
967
  assetGroups.set(assetNorm, existing);
966
968
  }
967
- const distinctAssetCount = assetGroups.size;
968
- const batchN = commands.length;
969
- if (batchN > distinctAssetCount) {
970
- throw new TransactionVerificationError("caps");
971
- }
972
- const seenCoins = /* @__PURE__ */ new Set();
973
- const seenPools = /* @__PURE__ */ new Set();
974
- for (let i = 0; i < batchN; i++) {
975
- const cmd = commands[i];
976
- if (cmd.$kind !== "MoveCall") {
977
- throw new TransactionVerificationError("unsupportedOperation");
978
- }
979
- const mc = cmd.MoveCall;
980
- if (mc.module !== "spend_account" || mc.function !== "create_policy_grant") {
981
- throw new TransactionVerificationError("target");
982
- }
983
- const mcTypes = mc.typeArguments;
984
- if (mcTypes.length !== 1) {
985
- throw new TransactionVerificationError("coinType");
986
- }
987
- const cmdCoinType = safeNormalizeStructTag(mcTypes[0]);
988
- if (!cmdCoinType || !allowedAssets.has(cmdCoinType)) {
989
- throw new TransactionVerificationError("coinType");
990
- }
991
- if (seenCoins.has(cmdCoinType)) {
992
- throw new TransactionVerificationError("coinType");
993
- }
994
- seenCoins.add(cmdCoinType);
995
- const groupInfo = assetGroups.get(cmdCoinType);
996
- const args = mc.arguments;
997
- if (args.length !== 6) {
998
- throw new TransactionVerificationError("target");
999
- }
1000
- const poolInputIdx = markInput(args[0]);
1001
- const poolId = getObjectIdFromInput(data.inputs[poolInputIdx]);
1002
- if (!poolId) {
1003
- throw new TransactionVerificationError("pool");
1004
- }
1005
- if (seenPools.has(poolId)) {
1006
- throw new TransactionVerificationError("pool");
1007
- }
1008
- seenPools.add(poolId);
1009
- const delInputIdx = markInput(args[1]);
1010
- const delBytes = getPureBytes(data.inputs[delInputIdx]);
1011
- if (!delBytes || parseAddress(delBytes) !== safeNormalizeAddress(op.delegate.address)) {
1012
- throw new TransactionVerificationError("delegate");
1013
- }
1014
- const capInputIdx = markInput(args[2]);
1015
- const capBytes = getPureBytes(data.inputs[capInputIdx]);
1016
- if (!capBytes) {
1017
- throw new TransactionVerificationError("caps");
969
+ const isCombined = commands.length >= 5 && commands[0]?.$kind === "MoveCall" && commands[0].MoveCall?.module === "spend_account" && commands[0].MoveCall?.function === "new_policy_target";
970
+ if (!isCombined) {
971
+ const hasDraftPolicy = uniquePolicies.some((p) => p.status === "draft" || p.onChainPolicyId == null && p.targets && p.targets.length > 0);
972
+ if (hasDraftPolicy) {
973
+ throw new TransactionVerificationError("policyId");
1018
974
  }
1019
- const txSessionCap = parseOptionU64(capBytes);
1020
- const maxInputIdx = markInput(args[3]);
1021
- const maxBytes = getPureBytes(data.inputs[maxInputIdx]);
1022
- if (!maxBytes) {
975
+ const distinctAssetCount = assetGroups.size;
976
+ const batchN = commands.length;
977
+ if (batchN > distinctAssetCount) {
1023
978
  throw new TransactionVerificationError("caps");
1024
979
  }
1025
- const txMaxPerPayment = parseOptionU64(maxBytes);
1026
- if (txSessionCap === null || txMaxPerPayment !== txSessionCap) {
1027
- throw new TransactionVerificationError("caps");
980
+ const seenCoins = /* @__PURE__ */ new Set();
981
+ const seenPools = /* @__PURE__ */ new Set();
982
+ for (let i = 0; i < batchN; i++) {
983
+ const cmd = commands[i];
984
+ if (cmd.$kind !== "MoveCall") {
985
+ throw new TransactionVerificationError("unsupportedOperation");
986
+ }
987
+ const mc = cmd.MoveCall;
988
+ if (mc.module !== "spend_account" || mc.function !== "create_policy_grant") {
989
+ throw new TransactionVerificationError("target");
990
+ }
991
+ const mcTypes = mc.typeArguments;
992
+ if (mcTypes.length !== 1) {
993
+ throw new TransactionVerificationError("coinType");
994
+ }
995
+ const cmdCoinType2 = safeNormalizeStructTag(mcTypes[0]);
996
+ if (!cmdCoinType2 || !allowedAssets.has(cmdCoinType2)) {
997
+ throw new TransactionVerificationError("coinType");
998
+ }
999
+ if (seenCoins.has(cmdCoinType2)) {
1000
+ throw new TransactionVerificationError("coinType");
1001
+ }
1002
+ seenCoins.add(cmdCoinType2);
1003
+ const groupInfo2 = assetGroups.get(cmdCoinType2);
1004
+ const args = mc.arguments;
1005
+ if (args.length !== 6) {
1006
+ throw new TransactionVerificationError("target");
1007
+ }
1008
+ const poolInputIdx2 = markInput(args[0]);
1009
+ const poolId2 = getObjectIdFromInput(data.inputs[poolInputIdx2]);
1010
+ if (!poolId2) {
1011
+ throw new TransactionVerificationError("pool");
1012
+ }
1013
+ if (seenPools.has(poolId2)) {
1014
+ throw new TransactionVerificationError("pool");
1015
+ }
1016
+ seenPools.add(poolId2);
1017
+ const delInputIdx2 = markInput(args[1]);
1018
+ const delBytes2 = getPureBytes(data.inputs[delInputIdx2]);
1019
+ if (!delBytes2 || parseAddress(delBytes2) !== safeNormalizeAddress(op.delegate.address)) {
1020
+ throw new TransactionVerificationError("delegate");
1021
+ }
1022
+ const capInputIdx2 = markInput(args[2]);
1023
+ const capBytes2 = getPureBytes(data.inputs[capInputIdx2]);
1024
+ if (!capBytes2) {
1025
+ throw new TransactionVerificationError("caps");
1026
+ }
1027
+ const txSessionCap2 = parseOptionU64(capBytes2);
1028
+ const maxInputIdx2 = markInput(args[3]);
1029
+ const maxBytes2 = getPureBytes(data.inputs[maxInputIdx2]);
1030
+ if (!maxBytes2) {
1031
+ throw new TransactionVerificationError("caps");
1032
+ }
1033
+ const txMaxPerPayment2 = parseOptionU64(maxBytes2);
1034
+ if (txSessionCap2 === null || txMaxPerPayment2 !== txSessionCap2) {
1035
+ throw new TransactionVerificationError("caps");
1036
+ }
1037
+ if (txSessionCap2 > groupInfo2.sumTotalCap) {
1038
+ throw new TransactionVerificationError("caps");
1039
+ }
1040
+ assertPureU64(args[4], 0n, "expiry");
1041
+ const pIdsInputIdx = markInput(args[5]);
1042
+ const pIdsBytes = getPureBytes(data.inputs[pIdsInputIdx]);
1043
+ if (!pIdsBytes) {
1044
+ throw new TransactionVerificationError("policyId");
1045
+ }
1046
+ const parsedPids = bcs.vector(bcs.u64()).parse(pIdsBytes);
1047
+ const expectedOnChainIds = groupInfo2.policies.map((p) => p.onChainPolicyId != null ? String(p.onChainPolicyId) : null).filter((id) => id != null);
1048
+ if (expectedOnChainIds.length === 0 || expectedOnChainIds.length !== groupInfo2.policies.length || parsedPids.length !== expectedOnChainIds.length) {
1049
+ throw new TransactionVerificationError("policyId");
1050
+ }
1051
+ for (let idx = 0; idx < parsedPids.length; idx++) {
1052
+ if (String(parsedPids[idx]) !== expectedOnChainIds[idx]) {
1053
+ throw new TransactionVerificationError("policyId");
1054
+ }
1055
+ }
1028
1056
  }
1029
- if (txSessionCap > groupInfo.sumTotalCap) {
1030
- throw new TransactionVerificationError("caps");
1057
+ break;
1058
+ }
1059
+ if (uniquePolicies.length !== 1) {
1060
+ throw new TransactionVerificationError("targets");
1061
+ }
1062
+ const draftPolicy = uniquePolicies[0];
1063
+ let N = 0;
1064
+ while (N < commands.length && commands[N]?.$kind === "MoveCall" && commands[N].MoveCall?.module === "spend_account" && commands[N].MoveCall?.function === "new_policy_target") {
1065
+ N++;
1066
+ }
1067
+ if (!draftPolicy.targets || draftPolicy.targets.length !== N) {
1068
+ throw new TransactionVerificationError("targets");
1069
+ }
1070
+ if (commands.length !== N + 4) {
1071
+ throw new TransactionVerificationError("targets");
1072
+ }
1073
+ for (let i = 0; i < N; i++) {
1074
+ const c = commands[i];
1075
+ const mc = c.MoveCall;
1076
+ if (mc.arguments.length !== 3) {
1077
+ throw new TransactionVerificationError("target");
1031
1078
  }
1032
- assertPureU64(args[4], 0n, "expiry");
1033
- const pIdsInputIdx = markInput(args[5]);
1034
- const pIdsBytes = getPureBytes(data.inputs[pIdsInputIdx]);
1035
- if (!pIdsBytes) {
1036
- throw new TransactionVerificationError("policyId");
1079
+ const expectedTarget = draftPolicy.targets[i];
1080
+ const hashIdx = markInput(mc.arguments[0]);
1081
+ const hashBytes = getPureBytes(data.inputs[hashIdx]);
1082
+ const hash = hashBytes ? bcs.vector(bcs.u8()).parse(hashBytes) : null;
1083
+ const got = hash ? [...hash].map((byte) => byte.toString(16).padStart(2, "0")).join("") : "";
1084
+ const want = expectedTarget.targetHash.trim().toLowerCase().replace(/^0x/, "");
1085
+ if (!hash || got !== want)
1086
+ throw new TransactionVerificationError("target");
1087
+ const recipientIdx = markInput(mc.arguments[1]);
1088
+ const recipientBytes = getPureBytes(data.inputs[recipientIdx]);
1089
+ if (!recipientBytes || parseAddress(recipientBytes) !== safeNormalizeAddress(expectedTarget.recipient)) {
1090
+ throw new TransactionVerificationError("target");
1037
1091
  }
1038
- if (parseVectorU64Len(pIdsBytes) > groupInfo.policyIds.length) {
1039
- throw new TransactionVerificationError("policyId");
1092
+ assertPureU64(mc.arguments[2], BigInt(expectedTarget.maxPerPayment), "target");
1093
+ }
1094
+ const vecCmd = commands[N];
1095
+ if (vecCmd.$kind !== "MakeMoveVec") {
1096
+ throw new TransactionVerificationError("unsupportedOperation");
1097
+ }
1098
+ const targetElements = vecCmd.MakeMoveVec?.elements;
1099
+ if (!Array.isArray(targetElements) || targetElements.length !== N) {
1100
+ throw new TransactionVerificationError("targets");
1101
+ }
1102
+ for (let i = 0; i < N; i++) {
1103
+ const el = markResult(targetElements[i], "targets");
1104
+ if (el.cmdIndex !== i || el.resIndex !== 0) {
1105
+ throw new TransactionVerificationError("targets");
1040
1106
  }
1041
1107
  }
1108
+ const createPolicyCmd = commands[N + 1];
1109
+ if (createPolicyCmd.$kind !== "MoveCall" || createPolicyCmd.MoveCall?.module !== "spend_account" || createPolicyCmd.MoveCall?.function !== "create_spend_policy") {
1110
+ throw new TransactionVerificationError("target");
1111
+ }
1112
+ const policyTypes = createPolicyCmd.MoveCall.typeArguments;
1113
+ if (!policyTypes || policyTypes.length !== 1) {
1114
+ throw new TransactionVerificationError("coinType");
1115
+ }
1116
+ const cmdCoinType = safeNormalizeStructTag(policyTypes[0]);
1117
+ if (!cmdCoinType || !allowedAssets.has(cmdCoinType)) {
1118
+ throw new TransactionVerificationError("coinType");
1119
+ }
1120
+ const groupInfo = assetGroups.get(cmdCoinType);
1121
+ const policyArgs = createPolicyCmd.MoveCall.arguments;
1122
+ if (!policyArgs || policyArgs.length !== 5) {
1123
+ throw new TransactionVerificationError("target");
1124
+ }
1125
+ const poolInputIdx = markInput(policyArgs[0]);
1126
+ const poolId = getObjectIdFromInput(data.inputs[poolInputIdx]);
1127
+ if (!poolId) {
1128
+ throw new TransactionVerificationError("pool");
1129
+ }
1130
+ const targetsRes = markResult(policyArgs[1], "unsupportedOperation");
1131
+ if (targetsRes.cmdIndex !== N || targetsRes.resIndex !== 0) {
1132
+ throw new TransactionVerificationError("unsupportedOperation");
1133
+ }
1134
+ const totalCapInputIdx = markInput(policyArgs[2]);
1135
+ const totalCapBytes = getPureBytes(data.inputs[totalCapInputIdx]);
1136
+ if (!totalCapBytes)
1137
+ throw new TransactionVerificationError("caps");
1138
+ const policyTotalCap = parseU64(totalCapBytes);
1139
+ if (policyTotalCap <= 0n || policyTotalCap > groupInfo.sumTotalCap || policyTotalCap !== BigInt(draftPolicy.totalCap)) {
1140
+ throw new TransactionVerificationError("caps");
1141
+ }
1142
+ const maxPerPaymentInputIdx = markInput(policyArgs[3]);
1143
+ const maxPerPaymentBytes = getPureBytes(data.inputs[maxPerPaymentInputIdx]);
1144
+ if (!maxPerPaymentBytes)
1145
+ throw new TransactionVerificationError("caps");
1146
+ const policyMaxPerPayment = parseU64(maxPerPaymentBytes);
1147
+ if (policyMaxPerPayment <= 0n || policyMaxPerPayment > policyTotalCap || draftPolicy.maxPerPayment == null || policyMaxPerPayment !== BigInt(draftPolicy.maxPerPayment)) {
1148
+ throw new TransactionVerificationError("caps");
1149
+ }
1150
+ assertPureU64(policyArgs[4], callerExpiryU64(draftPolicy.expiresAtMs), "expiry");
1151
+ const idVecCmd = commands[N + 2];
1152
+ if (idVecCmd.$kind !== "MakeMoveVec" || !Array.isArray(idVecCmd.MakeMoveVec?.elements) || idVecCmd.MakeMoveVec.elements.length !== 1) {
1153
+ throw new TransactionVerificationError("policyId");
1154
+ }
1155
+ const policyRes = markResult(idVecCmd.MakeMoveVec.elements[0], "policyId");
1156
+ if (policyRes.cmdIndex !== N + 1 || policyRes.resIndex !== 0) {
1157
+ throw new TransactionVerificationError("policyId");
1158
+ }
1159
+ const grantCmd = commands[N + 3];
1160
+ if (grantCmd.$kind !== "MoveCall" || grantCmd.MoveCall?.module !== "spend_account" || grantCmd.MoveCall?.function !== "create_policy_grant") {
1161
+ throw new TransactionVerificationError("target");
1162
+ }
1163
+ const grantTypes = grantCmd.MoveCall.typeArguments;
1164
+ if (!grantTypes || grantTypes.length !== 1) {
1165
+ throw new TransactionVerificationError("coinType");
1166
+ }
1167
+ const grantCoinType = safeNormalizeStructTag(grantTypes[0]);
1168
+ if (grantCoinType !== cmdCoinType) {
1169
+ throw new TransactionVerificationError("coinType");
1170
+ }
1171
+ const grantArgs = grantCmd.MoveCall.arguments;
1172
+ if (!grantArgs || grantArgs.length !== 6) {
1173
+ throw new TransactionVerificationError("target");
1174
+ }
1175
+ const grantPoolInputIdx = markInput(grantArgs[0]);
1176
+ const grantPoolId = getObjectIdFromInput(data.inputs[grantPoolInputIdx]);
1177
+ if (!grantPoolId || grantPoolId !== poolId) {
1178
+ throw new TransactionVerificationError("pool");
1179
+ }
1180
+ const delInputIdx = markInput(grantArgs[1]);
1181
+ const delBytes = getPureBytes(data.inputs[delInputIdx]);
1182
+ if (!delBytes || parseAddress(delBytes) !== safeNormalizeAddress(op.delegate.address)) {
1183
+ throw new TransactionVerificationError("delegate");
1184
+ }
1185
+ const capInputIdx = markInput(grantArgs[2]);
1186
+ const capBytes = getPureBytes(data.inputs[capInputIdx]);
1187
+ if (!capBytes) {
1188
+ throw new TransactionVerificationError("caps");
1189
+ }
1190
+ const txSessionCap = parseOptionU64(capBytes);
1191
+ const maxInputIdx = markInput(grantArgs[3]);
1192
+ const maxBytes = getPureBytes(data.inputs[maxInputIdx]);
1193
+ if (!maxBytes) {
1194
+ throw new TransactionVerificationError("caps");
1195
+ }
1196
+ const txMaxPerPayment = parseOptionU64(maxBytes);
1197
+ if (txSessionCap === null || txMaxPerPayment !== txSessionCap) {
1198
+ throw new TransactionVerificationError("caps");
1199
+ }
1200
+ if (txSessionCap > groupInfo.sumTotalCap) {
1201
+ throw new TransactionVerificationError("caps");
1202
+ }
1203
+ assertPureU64(grantArgs[4], 0n, "expiry");
1204
+ const grantVecRes = markResult(grantArgs[5], "policyId");
1205
+ if (grantVecRes.cmdIndex !== N + 2 || grantVecRes.resIndex !== 0) {
1206
+ throw new TransactionVerificationError("policyId");
1207
+ }
1042
1208
  break;
1043
1209
  }
1044
1210
  case "create_grant_open": {
@@ -2981,6 +3147,70 @@ function createSpendAccountChain(options) {
2981
3147
  });
2982
3148
  return kindBytes(tx);
2983
3149
  },
3150
+ async buildCreatePolicyAndGrantKind(input) {
3151
+ if (!input.policy.targets?.length) {
3152
+ throw new Error("create_spend_policy requires at least one target");
3153
+ }
3154
+ if (!input.policy.totalCap || BigInt(input.policy.totalCap) <= 0n) {
3155
+ throw new Error("create_spend_policy requires a non-zero total_cap");
3156
+ }
3157
+ if (!input.policy.maxPerPayment || BigInt(input.policy.maxPerPayment) <= 0n) {
3158
+ throw new Error("create_spend_policy requires a non-zero max_per_payment");
3159
+ }
3160
+ if (!input.grant.sessionCap || BigInt(input.grant.sessionCap) <= 0n) {
3161
+ throw new Error("create_grant requires a non-zero session_cap");
3162
+ }
3163
+ if (!input.grant.maxPerPayment || BigInt(input.grant.maxPerPayment) <= 0n) {
3164
+ throw new Error("create_grant requires a non-zero max_per_payment");
3165
+ }
3166
+ const tx = new Transaction2();
3167
+ tx.setSender(input.owner);
3168
+ const targetResults = input.policy.targets.map((t) => {
3169
+ const hash = targetHashBytes(t.targetHash);
3170
+ return tx.moveCall({
3171
+ target: target("new_policy_target"),
3172
+ arguments: [
3173
+ tx.pure.vector("u8", Array.from(hash)),
3174
+ tx.pure.address(t.recipient),
3175
+ tx.pure.u64(BigInt(t.maxPerPayment))
3176
+ ]
3177
+ });
3178
+ });
3179
+ const targetsVec = tx.makeMoveVec({
3180
+ type: `${packageId}::${MODULE}::PolicyTarget`,
3181
+ elements: targetResults
3182
+ });
3183
+ const policyExpires = input.policy.expiresAtMs === void 0 || input.policy.expiresAtMs === null ? 0n : BigInt(input.policy.expiresAtMs);
3184
+ const policyIdResult = tx.moveCall({
3185
+ target: target("create_spend_policy"),
3186
+ typeArguments: [input.coinType],
3187
+ arguments: [
3188
+ tx.object(input.poolObjectId),
3189
+ targetsVec,
3190
+ tx.pure.u64(BigInt(input.policy.totalCap)),
3191
+ tx.pure.u64(BigInt(input.policy.maxPerPayment)),
3192
+ tx.pure.u64(policyExpires)
3193
+ ]
3194
+ });
3195
+ const policyIdsVec = tx.makeMoveVec({
3196
+ type: "u64",
3197
+ elements: [policyIdResult]
3198
+ });
3199
+ const grantExpires = input.grant.expiresAtMs === void 0 || input.grant.expiresAtMs === null ? 0n : BigInt(input.grant.expiresAtMs);
3200
+ tx.moveCall({
3201
+ target: target("create_policy_grant"),
3202
+ typeArguments: [input.coinType],
3203
+ arguments: [
3204
+ tx.object(input.poolObjectId),
3205
+ tx.pure.address(input.delegate),
3206
+ tx.pure.option("u64", BigInt(input.grant.sessionCap)),
3207
+ tx.pure.option("u64", BigInt(input.grant.maxPerPayment)),
3208
+ tx.pure.u64(grantExpires),
3209
+ policyIdsVec
3210
+ ]
3211
+ });
3212
+ return kindBytes(tx);
3213
+ },
2984
3214
  async buildPausePolicyKind(input) {
2985
3215
  const tx = new Transaction2();
2986
3216
  tx.setSender(input.owner);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mysten-incubation/suipay-buyer",
3
- "version": "0.3.0-dev.4",
3
+ "version": "0.3.0-dev.6",
4
4
  "type": "module",
5
5
  "description": "SuiPay buyer SDK — delegated spend-account pay plus a typed /v1 control-plane client.",
6
6
  "license": "MIT",
@@ -40,8 +40,8 @@
40
40
  "tsup": "^8.0.0",
41
41
  "typescript": "^6.0.3",
42
42
  "vitest": "^4.1.10",
43
- "@cmdoss/suipay-sdk": "0.1.0",
44
- "@cmdoss/suipay-core": "0.1.0"
43
+ "@cmdoss/suipay-core": "0.1.0",
44
+ "@cmdoss/suipay-sdk": "0.1.0"
45
45
  },
46
46
  "scripts": {
47
47
  "build": "tsup",