@x402/aptos 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/README.md +74 -0
  2. package/dist/cjs/exact/client/index.d.ts +29 -0
  3. package/dist/cjs/exact/client/index.js +141 -0
  4. package/dist/cjs/exact/client/index.js.map +1 -0
  5. package/dist/cjs/exact/facilitator/index.d.ts +52 -0
  6. package/dist/cjs/exact/facilitator/index.js +373 -0
  7. package/dist/cjs/exact/facilitator/index.js.map +1 -0
  8. package/dist/cjs/exact/server/index.d.ts +67 -0
  9. package/dist/cjs/exact/server/index.js +143 -0
  10. package/dist/cjs/exact/server/index.js.map +1 -0
  11. package/dist/cjs/index.d.ts +116 -0
  12. package/dist/cjs/index.js +269 -0
  13. package/dist/cjs/index.js.map +1 -0
  14. package/dist/cjs/signer-DPT9P1NX.d.ts +59 -0
  15. package/dist/cjs/signer-DfwN1I5I.d.ts +59 -0
  16. package/dist/esm/chunk-6BMAMLXJ.mjs +93 -0
  17. package/dist/esm/chunk-6BMAMLXJ.mjs.map +1 -0
  18. package/dist/esm/chunk-D4UVBSUH.mjs +53 -0
  19. package/dist/esm/chunk-D4UVBSUH.mjs.map +1 -0
  20. package/dist/esm/chunk-FG4ANPDN.mjs +46 -0
  21. package/dist/esm/chunk-FG4ANPDN.mjs.map +1 -0
  22. package/dist/esm/exact/client/index.d.mts +29 -0
  23. package/dist/esm/exact/client/index.mjs +9 -0
  24. package/dist/esm/exact/client/index.mjs.map +1 -0
  25. package/dist/esm/exact/facilitator/index.d.mts +52 -0
  26. package/dist/esm/exact/facilitator/index.mjs +294 -0
  27. package/dist/esm/exact/facilitator/index.mjs.map +1 -0
  28. package/dist/esm/exact/server/index.d.mts +67 -0
  29. package/dist/esm/exact/server/index.mjs +116 -0
  30. package/dist/esm/exact/server/index.mjs.map +1 -0
  31. package/dist/esm/index.d.mts +116 -0
  32. package/dist/esm/index.mjs +105 -0
  33. package/dist/esm/index.mjs.map +1 -0
  34. package/dist/esm/signer-DPT9P1NX.d.mts +59 -0
  35. package/package.json +95 -0
@@ -0,0 +1,116 @@
1
+ export { ExactAptosScheme } from './exact/client/index.js';
2
+ import { Network, Aptos, SimpleTransaction, AccountAuthenticator, EntryFunction, TransactionPayload, TransactionPayloadEntryFunction } from '@aptos-labs/ts-sdk';
3
+ export { C as ClientAptosConfig, a as ClientAptosSigner, F as FacilitatorAptosSigner, c as createClientSigner, t as toFacilitatorAptosSigner } from './signer-DPT9P1NX.js';
4
+ import '@x402/core/types';
5
+
6
+ /**
7
+ * Exact Aptos payload structure containing a base64 encoded transaction
8
+ */
9
+ type ExactAptosPayload = {
10
+ /**
11
+ * Base64 encoded JSON containing transaction and senderAuthenticator byte arrays
12
+ */
13
+ transaction: string;
14
+ };
15
+ /**
16
+ * Decoded Aptos payment payload structure
17
+ */
18
+ type DecodedAptosPayload = {
19
+ /**
20
+ * Transaction bytes as number array
21
+ */
22
+ transaction: number[];
23
+ /**
24
+ * Sender authenticator bytes as number array
25
+ */
26
+ senderAuthenticator: number[];
27
+ };
28
+
29
+ /**
30
+ * CAIP-2 network identifier for Aptos Mainnet
31
+ */
32
+ declare const APTOS_MAINNET_CAIP2 = "aptos:1";
33
+ /**
34
+ * CAIP-2 network identifier for Aptos Testnet
35
+ */
36
+ declare const APTOS_TESTNET_CAIP2 = "aptos:2";
37
+ /**
38
+ * Regex pattern for validating Aptos addresses
39
+ * Matches 64 hex characters with 0x prefix
40
+ */
41
+ declare const APTOS_ADDRESS_REGEX: RegExp;
42
+ /**
43
+ * The primary fungible store transfer function
44
+ */
45
+ declare const TRANSFER_FUNCTION = "0x1::primary_fungible_store::transfer";
46
+ /**
47
+ * Maximum gas amount allowed for sponsored transactions to prevent gas draining attacks.
48
+ * The Aptos SDK defaults to 200000 for simple transactions, so we allow some headroom.
49
+ */
50
+ declare const MAX_GAS_AMOUNT = 500000n;
51
+ /**
52
+ * Maps CAIP-2 network identifiers to Aptos chain IDs.
53
+ *
54
+ * @param network - The CAIP-2 network identifier (e.g., "aptos:1")
55
+ * @returns The corresponding chain ID
56
+ */
57
+ declare function getAptosChainId(network: string): number;
58
+ /**
59
+ * Default USDC fungible asset metadata address on mainnet.
60
+ */
61
+ declare const USDC_MAINNET_FA = "0xbae207659db88bea0cbead6da0ed00aac12edcdda169e591cd41c94180b46f3b";
62
+ /**
63
+ * Default USDC fungible asset metadata address on testnet.
64
+ */
65
+ declare const USDC_TESTNET_FA = "0x69091fbab5f7d635ee7ac5098cf0c1efbe31d68fec0f2cd565e8d168daf52832";
66
+ /**
67
+ * Maps CAIP-2 network identifiers to Aptos SDK Network enum.
68
+ *
69
+ * @param network - The CAIP-2 network identifier (e.g., "aptos:1")
70
+ * @returns The corresponding Aptos SDK Network enum value
71
+ */
72
+ declare function getAptosNetwork(network: string): Network;
73
+ /**
74
+ * Gets the default RPC URL for the given Aptos network.
75
+ *
76
+ * @param network - The Aptos SDK Network enum value
77
+ * @returns The default RPC URL for the network
78
+ */
79
+ declare function getAptosRpcUrl(network: Network): string;
80
+
81
+ /**
82
+ * Deserialize an Aptos transaction and authenticator from the payment payload.
83
+ *
84
+ * @param transactionBase64 - The base64 encoded transaction payload
85
+ * @returns The deserialized transaction and authenticator
86
+ */
87
+ declare function deserializeAptosPayment(transactionBase64: string): {
88
+ transaction: SimpleTransaction;
89
+ senderAuthenticator: AccountAuthenticator;
90
+ entryFunction?: EntryFunction;
91
+ };
92
+ /**
93
+ * Checks if it's an entry function payload.
94
+ *
95
+ * @param payload - The payload to check
96
+ * @returns True if it's an entry function payload
97
+ */
98
+ declare function isEntryFunctionPayload(payload: TransactionPayload): payload is TransactionPayloadEntryFunction;
99
+ /**
100
+ * Create an Aptos SDK client for the given network
101
+ *
102
+ * @param network - CAIP-2 network identifier (e.g., "aptos:1")
103
+ * @param rpcUrl - Optional custom RPC URL
104
+ * @returns Aptos SDK client
105
+ */
106
+ declare function createAptosClient(network: string, rpcUrl?: string): Aptos;
107
+ /**
108
+ * Encode an Aptos payment payload to base64
109
+ *
110
+ * @param transactionBytes - The serialized transaction bytes
111
+ * @param authenticatorBytes - The serialized authenticator bytes
112
+ * @returns Base64 encoded payload
113
+ */
114
+ declare function encodeAptosPayload(transactionBytes: Uint8Array, authenticatorBytes: Uint8Array): string;
115
+
116
+ export { APTOS_ADDRESS_REGEX, APTOS_MAINNET_CAIP2, APTOS_TESTNET_CAIP2, type DecodedAptosPayload, type ExactAptosPayload, MAX_GAS_AMOUNT, TRANSFER_FUNCTION, USDC_MAINNET_FA, USDC_TESTNET_FA, createAptosClient, deserializeAptosPayment, encodeAptosPayload, getAptosChainId, getAptosNetwork, getAptosRpcUrl, isEntryFunctionPayload };
@@ -0,0 +1,269 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ APTOS_ADDRESS_REGEX: () => APTOS_ADDRESS_REGEX,
24
+ APTOS_MAINNET_CAIP2: () => APTOS_MAINNET_CAIP2,
25
+ APTOS_TESTNET_CAIP2: () => APTOS_TESTNET_CAIP2,
26
+ ExactAptosScheme: () => ExactAptosScheme,
27
+ MAX_GAS_AMOUNT: () => MAX_GAS_AMOUNT,
28
+ TRANSFER_FUNCTION: () => TRANSFER_FUNCTION,
29
+ USDC_MAINNET_FA: () => USDC_MAINNET_FA,
30
+ USDC_TESTNET_FA: () => USDC_TESTNET_FA,
31
+ createAptosClient: () => createAptosClient,
32
+ createClientSigner: () => createClientSigner,
33
+ deserializeAptosPayment: () => deserializeAptosPayment,
34
+ encodeAptosPayload: () => encodeAptosPayload,
35
+ getAptosChainId: () => getAptosChainId,
36
+ getAptosNetwork: () => getAptosNetwork,
37
+ getAptosRpcUrl: () => getAptosRpcUrl,
38
+ isEntryFunctionPayload: () => isEntryFunctionPayload,
39
+ toFacilitatorAptosSigner: () => toFacilitatorAptosSigner
40
+ });
41
+ module.exports = __toCommonJS(src_exports);
42
+
43
+ // src/exact/client/scheme.ts
44
+ var import_ts_sdk3 = require("@aptos-labs/ts-sdk");
45
+
46
+ // src/constants.ts
47
+ var import_ts_sdk = require("@aptos-labs/ts-sdk");
48
+ var APTOS_MAINNET_CAIP2 = "aptos:1";
49
+ var APTOS_TESTNET_CAIP2 = "aptos:2";
50
+ var APTOS_ADDRESS_REGEX = /^0x[a-fA-F0-9]{64}$/;
51
+ var TRANSFER_FUNCTION = "0x1::primary_fungible_store::transfer";
52
+ var MAX_GAS_AMOUNT = 500000n;
53
+ function getAptosChainId(network) {
54
+ switch (network) {
55
+ case APTOS_MAINNET_CAIP2:
56
+ return 1;
57
+ case APTOS_TESTNET_CAIP2:
58
+ return 2;
59
+ default:
60
+ throw new Error(`Unsupported Aptos network: ${network}`);
61
+ }
62
+ }
63
+ var USDC_MAINNET_FA = "0xbae207659db88bea0cbead6da0ed00aac12edcdda169e591cd41c94180b46f3b";
64
+ var USDC_TESTNET_FA = "0x69091fbab5f7d635ee7ac5098cf0c1efbe31d68fec0f2cd565e8d168daf52832";
65
+ function getAptosNetwork(network) {
66
+ switch (network) {
67
+ case APTOS_MAINNET_CAIP2:
68
+ return import_ts_sdk.Network.MAINNET;
69
+ case APTOS_TESTNET_CAIP2:
70
+ return import_ts_sdk.Network.TESTNET;
71
+ default:
72
+ throw new Error(`Unsupported Aptos network: ${network}`);
73
+ }
74
+ }
75
+ function getAptosRpcUrl(network) {
76
+ return import_ts_sdk.NetworkToNodeAPI[network];
77
+ }
78
+
79
+ // src/utils.ts
80
+ var import_ts_sdk2 = require("@aptos-labs/ts-sdk");
81
+ function deserializeAptosPayment(transactionBase64) {
82
+ const decoded = Buffer.from(transactionBase64, "base64").toString("utf8");
83
+ const parsed = JSON.parse(decoded);
84
+ const transactionBytes = Uint8Array.from(parsed.transaction);
85
+ const transaction = import_ts_sdk2.SimpleTransaction.deserialize(new import_ts_sdk2.Deserializer(transactionBytes));
86
+ const authBytes = Uint8Array.from(parsed.senderAuthenticator);
87
+ const senderAuthenticator = import_ts_sdk2.AccountAuthenticator.deserialize(new import_ts_sdk2.Deserializer(authBytes));
88
+ if (!isEntryFunctionPayload(transaction.rawTransaction.payload)) {
89
+ return { transaction, senderAuthenticator };
90
+ }
91
+ const entryFunction = transaction.rawTransaction.payload.entryFunction;
92
+ return { transaction, senderAuthenticator, entryFunction };
93
+ }
94
+ function isEntryFunctionPayload(payload) {
95
+ return "entryFunction" in payload;
96
+ }
97
+ function createAptosClient(network, rpcUrl) {
98
+ const aptosNetwork = getAptosNetwork(network);
99
+ const fullnodeUrl = rpcUrl || getAptosRpcUrl(aptosNetwork);
100
+ const config = new import_ts_sdk2.AptosConfig({
101
+ network: aptosNetwork,
102
+ fullnode: fullnodeUrl
103
+ });
104
+ return new import_ts_sdk2.Aptos(config);
105
+ }
106
+ function encodeAptosPayload(transactionBytes, authenticatorBytes) {
107
+ const payload = {
108
+ transaction: Array.from(transactionBytes),
109
+ senderAuthenticator: Array.from(authenticatorBytes)
110
+ };
111
+ return Buffer.from(JSON.stringify(payload)).toString("base64");
112
+ }
113
+
114
+ // src/exact/client/scheme.ts
115
+ var ExactAptosScheme = class {
116
+ /**
117
+ * Creates a new ExactAptosScheme instance.
118
+ *
119
+ * @param signer - The Aptos account for signing transactions
120
+ * @param config - Optional configuration with custom RPC URL
121
+ */
122
+ constructor(signer, config) {
123
+ this.signer = signer;
124
+ this.config = config;
125
+ this.scheme = "exact";
126
+ }
127
+ /**
128
+ * Creates a payment payload for the Exact scheme.
129
+ *
130
+ * @param x402Version - The x402 protocol version
131
+ * @param paymentRequirements - The payment requirements
132
+ * @returns Promise resolving to a payment payload
133
+ */
134
+ async createPaymentPayload(x402Version, paymentRequirements) {
135
+ if (!this.signer.accountAddress) {
136
+ throw new Error("Aptos account address is required");
137
+ }
138
+ if (!paymentRequirements.asset) {
139
+ throw new Error("Asset is required");
140
+ }
141
+ if (!paymentRequirements.asset.match(APTOS_ADDRESS_REGEX)) {
142
+ throw new Error("Invalid asset address");
143
+ }
144
+ if (!paymentRequirements.payTo) {
145
+ throw new Error("Pay-to address is required");
146
+ }
147
+ if (!paymentRequirements.payTo.match(APTOS_ADDRESS_REGEX)) {
148
+ throw new Error("Invalid pay-to address");
149
+ }
150
+ if (!paymentRequirements.amount) {
151
+ throw new Error("Amount is required");
152
+ }
153
+ if (!paymentRequirements.amount.match(/^[0-9]+$/)) {
154
+ throw new Error("Amount must be a number");
155
+ }
156
+ const aptosNetwork = getAptosNetwork(paymentRequirements.network);
157
+ const rpcUrl = this.config?.rpcUrl || getAptosRpcUrl(aptosNetwork);
158
+ const aptosConfig = new import_ts_sdk3.AptosConfig({
159
+ network: aptosNetwork,
160
+ fullnode: rpcUrl
161
+ });
162
+ const aptos = new import_ts_sdk3.Aptos(aptosConfig);
163
+ const feePayer = paymentRequirements.extra?.feePayer;
164
+ const isSponsored = typeof feePayer === "string";
165
+ const builtTransaction = await aptos.transaction.build.simple({
166
+ sender: this.signer.accountAddress,
167
+ data: {
168
+ function: "0x1::primary_fungible_store::transfer",
169
+ typeArguments: ["0x1::fungible_asset::Metadata"],
170
+ functionArguments: [
171
+ paymentRequirements.asset,
172
+ paymentRequirements.payTo,
173
+ paymentRequirements.amount
174
+ ]
175
+ },
176
+ withFeePayer: isSponsored
177
+ });
178
+ const transaction = isSponsored ? new import_ts_sdk3.SimpleTransaction(builtTransaction.rawTransaction, import_ts_sdk3.AccountAddress.from(feePayer)) : builtTransaction;
179
+ const senderAuthenticator = this.signer.signTransactionWithAuthenticator(transaction);
180
+ const transactionBytes = transaction.bcsToBytes();
181
+ const authenticatorBytes = senderAuthenticator.bcsToBytes();
182
+ const base64Transaction = encodeAptosPayload(transactionBytes, authenticatorBytes);
183
+ const payload = {
184
+ transaction: base64Transaction
185
+ };
186
+ return {
187
+ x402Version,
188
+ payload
189
+ };
190
+ }
191
+ };
192
+
193
+ // src/signer.ts
194
+ var import_ts_sdk4 = require("@aptos-labs/ts-sdk");
195
+ async function createClientSigner(privateKey) {
196
+ const formattedKey = import_ts_sdk4.PrivateKey.formatPrivateKey(privateKey, import_ts_sdk4.PrivateKeyVariants.Ed25519);
197
+ const privateKeyBytes = new import_ts_sdk4.Ed25519PrivateKey(formattedKey);
198
+ return import_ts_sdk4.Account.fromPrivateKey({ privateKey: privateKeyBytes });
199
+ }
200
+ function toFacilitatorAptosSigner(account, rpcConfig) {
201
+ const getRpcUrl = (network) => {
202
+ if (rpcConfig) {
203
+ if ("defaultRpcUrl" in rpcConfig && rpcConfig.defaultRpcUrl) {
204
+ return rpcConfig.defaultRpcUrl;
205
+ }
206
+ if (network in rpcConfig) {
207
+ return rpcConfig[network];
208
+ }
209
+ }
210
+ return getAptosRpcUrl(getAptosNetwork(network));
211
+ };
212
+ const getAptos = (network) => {
213
+ const aptosNetwork = getAptosNetwork(network);
214
+ const rpcUrl = getRpcUrl(network);
215
+ return new import_ts_sdk4.Aptos(new import_ts_sdk4.AptosConfig({ network: aptosNetwork, fullnode: rpcUrl }));
216
+ };
217
+ return {
218
+ getAddresses: () => [account.accountAddress.toStringLong()],
219
+ signAndSubmitAsFeePayer: async (transaction, senderAuthenticator, network) => {
220
+ const aptos = getAptos(network);
221
+ transaction.feePayerAddress = account.accountAddress;
222
+ const feePayerAuthenticator = aptos.transaction.signAsFeePayer({
223
+ signer: account,
224
+ transaction
225
+ });
226
+ return aptos.transaction.submit.simple({
227
+ transaction,
228
+ senderAuthenticator,
229
+ feePayerAuthenticator
230
+ });
231
+ },
232
+ submitTransaction: async (transaction, senderAuthenticator, network) => {
233
+ const aptos = getAptos(network);
234
+ return aptos.transaction.submit.simple({ transaction, senderAuthenticator });
235
+ },
236
+ simulateTransaction: async (transaction, network) => {
237
+ const aptos = getAptos(network);
238
+ const results = await aptos.transaction.simulate.simple({ transaction });
239
+ if (results.length === 0 || !results[0].success) {
240
+ throw new Error(`Simulation failed: ${results[0]?.vm_status || "unknown error"}`);
241
+ }
242
+ },
243
+ waitForTransaction: async (txHash, network) => {
244
+ const aptos = getAptos(network);
245
+ await aptos.waitForTransaction({ transactionHash: txHash });
246
+ }
247
+ };
248
+ }
249
+ // Annotate the CommonJS export names for ESM import in node:
250
+ 0 && (module.exports = {
251
+ APTOS_ADDRESS_REGEX,
252
+ APTOS_MAINNET_CAIP2,
253
+ APTOS_TESTNET_CAIP2,
254
+ ExactAptosScheme,
255
+ MAX_GAS_AMOUNT,
256
+ TRANSFER_FUNCTION,
257
+ USDC_MAINNET_FA,
258
+ USDC_TESTNET_FA,
259
+ createAptosClient,
260
+ createClientSigner,
261
+ deserializeAptosPayment,
262
+ encodeAptosPayload,
263
+ getAptosChainId,
264
+ getAptosNetwork,
265
+ getAptosRpcUrl,
266
+ isEntryFunctionPayload,
267
+ toFacilitatorAptosSigner
268
+ });
269
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/index.ts","../../src/exact/client/scheme.ts","../../src/constants.ts","../../src/utils.ts","../../src/signer.ts"],"sourcesContent":["// Exact scheme exports\nexport * from \"./exact\";\n\n// Types\nexport * from \"./types\";\n\n// Constants\nexport * from \"./constants\";\n\n// Signer utilities\nexport * from \"./signer\";\n\n// Utils\nexport * from \"./utils\";\n","import { AccountAddress, Aptos, AptosConfig, SimpleTransaction } from \"@aptos-labs/ts-sdk\";\nimport type { PaymentPayload, PaymentRequirements, SchemeNetworkClient } from \"@x402/core/types\";\nimport { APTOS_ADDRESS_REGEX, getAptosNetwork, getAptosRpcUrl } from \"../../constants\";\nimport type { ClientAptosSigner, ClientAptosConfig } from \"../../signer\";\nimport type { ExactAptosPayload } from \"../../types\";\nimport { encodeAptosPayload } from \"../../utils\";\n\n/**\n * Aptos client implementation for the Exact payment scheme.\n */\nexport class ExactAptosScheme implements SchemeNetworkClient {\n readonly scheme = \"exact\";\n\n /**\n * Creates a new ExactAptosScheme instance.\n *\n * @param signer - The Aptos account for signing transactions\n * @param config - Optional configuration with custom RPC URL\n */\n constructor(\n private readonly signer: ClientAptosSigner,\n private readonly config?: ClientAptosConfig,\n ) {}\n\n /**\n * Creates a payment payload for the Exact scheme.\n *\n * @param x402Version - The x402 protocol version\n * @param paymentRequirements - The payment requirements\n * @returns Promise resolving to a payment payload\n */\n async createPaymentPayload(\n x402Version: number,\n paymentRequirements: PaymentRequirements,\n ): Promise<Pick<PaymentPayload, \"x402Version\" | \"payload\">> {\n if (!this.signer.accountAddress) {\n throw new Error(\"Aptos account address is required\");\n }\n if (!paymentRequirements.asset) {\n throw new Error(\"Asset is required\");\n }\n if (!paymentRequirements.asset.match(APTOS_ADDRESS_REGEX)) {\n throw new Error(\"Invalid asset address\");\n }\n if (!paymentRequirements.payTo) {\n throw new Error(\"Pay-to address is required\");\n }\n if (!paymentRequirements.payTo.match(APTOS_ADDRESS_REGEX)) {\n throw new Error(\"Invalid pay-to address\");\n }\n if (!paymentRequirements.amount) {\n throw new Error(\"Amount is required\");\n }\n if (!paymentRequirements.amount.match(/^[0-9]+$/)) {\n throw new Error(\"Amount must be a number\");\n }\n\n const aptosNetwork = getAptosNetwork(paymentRequirements.network);\n const rpcUrl = this.config?.rpcUrl || getAptosRpcUrl(aptosNetwork);\n const aptosConfig = new AptosConfig({\n network: aptosNetwork,\n fullnode: rpcUrl,\n });\n const aptos = new Aptos(aptosConfig);\n\n const feePayer = paymentRequirements.extra?.feePayer;\n const isSponsored = typeof feePayer === \"string\";\n\n const builtTransaction = await aptos.transaction.build.simple({\n sender: this.signer.accountAddress,\n data: {\n function: \"0x1::primary_fungible_store::transfer\",\n typeArguments: [\"0x1::fungible_asset::Metadata\"],\n functionArguments: [\n paymentRequirements.asset,\n paymentRequirements.payTo,\n paymentRequirements.amount,\n ],\n },\n withFeePayer: isSponsored,\n });\n\n // For sponsored transactions, set the actual fee payer address (SDK uses 0x0 placeholder)\n const transaction = isSponsored\n ? new SimpleTransaction(builtTransaction.rawTransaction, AccountAddress.from(feePayer))\n : builtTransaction;\n\n const senderAuthenticator = this.signer.signTransactionWithAuthenticator(transaction);\n const transactionBytes = transaction.bcsToBytes();\n const authenticatorBytes = senderAuthenticator.bcsToBytes();\n const base64Transaction = encodeAptosPayload(transactionBytes, authenticatorBytes);\n\n const payload: ExactAptosPayload = {\n transaction: base64Transaction,\n };\n\n return {\n x402Version,\n payload,\n };\n }\n}\n","import { Network, NetworkToNodeAPI } from \"@aptos-labs/ts-sdk\";\n\n/**\n * CAIP-2 network identifier for Aptos Mainnet\n */\nexport const APTOS_MAINNET_CAIP2 = \"aptos:1\";\n\n/**\n * CAIP-2 network identifier for Aptos Testnet\n */\nexport const APTOS_TESTNET_CAIP2 = \"aptos:2\";\n\n/**\n * Regex pattern for validating Aptos addresses\n * Matches 64 hex characters with 0x prefix\n */\nexport const APTOS_ADDRESS_REGEX = /^0x[a-fA-F0-9]{64}$/;\n\n/**\n * The primary fungible store transfer function\n */\nexport const TRANSFER_FUNCTION = \"0x1::primary_fungible_store::transfer\";\n\n/**\n * Maximum gas amount allowed for sponsored transactions to prevent gas draining attacks.\n * The Aptos SDK defaults to 200000 for simple transactions, so we allow some headroom.\n */\nexport const MAX_GAS_AMOUNT = 500000n;\n\n/**\n * Maps CAIP-2 network identifiers to Aptos chain IDs.\n *\n * @param network - The CAIP-2 network identifier (e.g., \"aptos:1\")\n * @returns The corresponding chain ID\n */\nexport function getAptosChainId(network: string): number {\n switch (network) {\n case APTOS_MAINNET_CAIP2:\n return 1;\n case APTOS_TESTNET_CAIP2:\n return 2;\n default:\n throw new Error(`Unsupported Aptos network: ${network}`);\n }\n}\n\n/**\n * Default USDC fungible asset metadata address on mainnet.\n */\nexport const USDC_MAINNET_FA = \"0xbae207659db88bea0cbead6da0ed00aac12edcdda169e591cd41c94180b46f3b\";\n\n/**\n * Default USDC fungible asset metadata address on testnet.\n */\nexport const USDC_TESTNET_FA = \"0x69091fbab5f7d635ee7ac5098cf0c1efbe31d68fec0f2cd565e8d168daf52832\";\n\n/**\n * Maps CAIP-2 network identifiers to Aptos SDK Network enum.\n *\n * @param network - The CAIP-2 network identifier (e.g., \"aptos:1\")\n * @returns The corresponding Aptos SDK Network enum value\n */\nexport function getAptosNetwork(network: string): Network {\n switch (network) {\n case APTOS_MAINNET_CAIP2:\n return Network.MAINNET;\n case APTOS_TESTNET_CAIP2:\n return Network.TESTNET;\n default:\n throw new Error(`Unsupported Aptos network: ${network}`);\n }\n}\n\n/**\n * Gets the default RPC URL for the given Aptos network.\n *\n * @param network - The Aptos SDK Network enum value\n * @returns The default RPC URL for the network\n */\nexport function getAptosRpcUrl(network: Network): string {\n return NetworkToNodeAPI[network];\n}\n","import {\n Deserializer,\n SimpleTransaction,\n AccountAuthenticator,\n TransactionPayloadEntryFunction,\n TransactionPayload,\n EntryFunction,\n Aptos,\n AptosConfig,\n} from \"@aptos-labs/ts-sdk\";\nimport type { DecodedAptosPayload } from \"./types\";\nimport { getAptosNetwork, getAptosRpcUrl } from \"./constants\";\n\n/**\n * Deserialize an Aptos transaction and authenticator from the payment payload.\n *\n * @param transactionBase64 - The base64 encoded transaction payload\n * @returns The deserialized transaction and authenticator\n */\nexport function deserializeAptosPayment(transactionBase64: string): {\n transaction: SimpleTransaction;\n senderAuthenticator: AccountAuthenticator;\n entryFunction?: EntryFunction;\n} {\n // Decode the base64 payload\n const decoded = Buffer.from(transactionBase64, \"base64\").toString(\"utf8\");\n const parsed: DecodedAptosPayload = JSON.parse(decoded);\n\n // Deserialize the transaction bytes\n const transactionBytes = Uint8Array.from(parsed.transaction);\n const transaction = SimpleTransaction.deserialize(new Deserializer(transactionBytes));\n\n // Deserialize the authenticator bytes\n const authBytes = Uint8Array.from(parsed.senderAuthenticator);\n const senderAuthenticator = AccountAuthenticator.deserialize(new Deserializer(authBytes));\n\n // Only Entry Function transactions are supported\n if (!isEntryFunctionPayload(transaction.rawTransaction.payload)) {\n return { transaction, senderAuthenticator };\n }\n\n const entryFunction = transaction.rawTransaction.payload.entryFunction;\n\n return { transaction, senderAuthenticator, entryFunction };\n}\n\n/**\n * Checks if it's an entry function payload.\n *\n * @param payload - The payload to check\n * @returns True if it's an entry function payload\n */\nexport function isEntryFunctionPayload(\n payload: TransactionPayload,\n): payload is TransactionPayloadEntryFunction {\n return \"entryFunction\" in payload;\n}\n\n/**\n * Create an Aptos SDK client for the given network\n *\n * @param network - CAIP-2 network identifier (e.g., \"aptos:1\")\n * @param rpcUrl - Optional custom RPC URL\n * @returns Aptos SDK client\n */\nexport function createAptosClient(network: string, rpcUrl?: string): Aptos {\n const aptosNetwork = getAptosNetwork(network);\n const fullnodeUrl = rpcUrl || getAptosRpcUrl(aptosNetwork);\n\n const config = new AptosConfig({\n network: aptosNetwork,\n fullnode: fullnodeUrl,\n });\n\n return new Aptos(config);\n}\n\n/**\n * Encode an Aptos payment payload to base64\n *\n * @param transactionBytes - The serialized transaction bytes\n * @param authenticatorBytes - The serialized authenticator bytes\n * @returns Base64 encoded payload\n */\nexport function encodeAptosPayload(\n transactionBytes: Uint8Array,\n authenticatorBytes: Uint8Array,\n): string {\n const payload: DecodedAptosPayload = {\n transaction: Array.from(transactionBytes),\n senderAuthenticator: Array.from(authenticatorBytes),\n };\n return Buffer.from(JSON.stringify(payload)).toString(\"base64\");\n}\n","import {\n Account,\n Ed25519PrivateKey,\n Aptos,\n AptosConfig,\n SimpleTransaction,\n AccountAuthenticator,\n PrivateKey,\n PrivateKeyVariants,\n type PendingTransactionResponse,\n} from \"@aptos-labs/ts-sdk\";\nimport { getAptosNetwork, getAptosRpcUrl } from \"./constants\";\n\n/**\n * Client-side signer for creating and signing Aptos transactions\n */\nexport type ClientAptosSigner = Account;\n\n/**\n * Configuration for client operations\n */\nexport type ClientAptosConfig = {\n /**\n * Optional custom RPC URL for the client to use\n */\n rpcUrl?: string;\n};\n\n/**\n * Minimal facilitator signer interface for Aptos operations\n */\nexport type FacilitatorAptosSigner = {\n /**\n * Get all addresses this facilitator can use for signing\n */\n getAddresses(): readonly string[];\n\n /**\n * Sign a transaction as the fee payer and submit it\n */\n signAndSubmitAsFeePayer(\n transaction: SimpleTransaction,\n senderAuthenticator: AccountAuthenticator,\n network: string,\n ): Promise<PendingTransactionResponse>;\n\n /**\n * Submit a fully-signed transaction (non-sponsored)\n */\n submitTransaction(\n transaction: SimpleTransaction,\n senderAuthenticator: AccountAuthenticator,\n network: string,\n ): Promise<PendingTransactionResponse>;\n\n /**\n * Simulate a transaction to verify it would succeed\n */\n simulateTransaction(transaction: SimpleTransaction, network: string): Promise<void>;\n\n /**\n * Wait for transaction confirmation\n */\n waitForTransaction(txHash: string, network: string): Promise<void>;\n};\n\n/**\n * Creates a client signer from a private key\n *\n * @param privateKey - The private key as a hex string or AIP-80 format\n * @returns An Aptos Account instance\n */\nexport async function createClientSigner(privateKey: string): Promise<ClientAptosSigner> {\n const formattedKey = PrivateKey.formatPrivateKey(privateKey, PrivateKeyVariants.Ed25519);\n const privateKeyBytes = new Ed25519PrivateKey(formattedKey);\n return Account.fromPrivateKey({ privateKey: privateKeyBytes });\n}\n\n/**\n * Create a facilitator signer from an Aptos Account\n *\n * @param account - The Aptos Account that will act as fee payer\n * @param rpcConfig - Optional RPC configuration\n * @returns FacilitatorAptosSigner instance\n */\nexport function toFacilitatorAptosSigner(\n account: Account,\n rpcConfig?: { defaultRpcUrl?: string } | Record<string, string>,\n): FacilitatorAptosSigner {\n const getRpcUrl = (network: string): string => {\n if (rpcConfig) {\n if (\"defaultRpcUrl\" in rpcConfig && rpcConfig.defaultRpcUrl) {\n return rpcConfig.defaultRpcUrl;\n }\n if (network in rpcConfig) {\n return (rpcConfig as Record<string, string>)[network];\n }\n }\n return getAptosRpcUrl(getAptosNetwork(network));\n };\n\n const getAptos = (network: string): Aptos => {\n const aptosNetwork = getAptosNetwork(network);\n const rpcUrl = getRpcUrl(network);\n return new Aptos(new AptosConfig({ network: aptosNetwork, fullnode: rpcUrl }));\n };\n\n return {\n getAddresses: () => [account.accountAddress.toStringLong()],\n\n signAndSubmitAsFeePayer: async (\n transaction: SimpleTransaction,\n senderAuthenticator: AccountAuthenticator,\n network: string,\n ) => {\n const aptos = getAptos(network);\n transaction.feePayerAddress = account.accountAddress;\n const feePayerAuthenticator = aptos.transaction.signAsFeePayer({\n signer: account,\n transaction,\n });\n return aptos.transaction.submit.simple({\n transaction,\n senderAuthenticator,\n feePayerAuthenticator,\n });\n },\n\n submitTransaction: async (\n transaction: SimpleTransaction,\n senderAuthenticator: AccountAuthenticator,\n network: string,\n ) => {\n const aptos = getAptos(network);\n return aptos.transaction.submit.simple({ transaction, senderAuthenticator });\n },\n\n simulateTransaction: async (transaction: SimpleTransaction, network: string) => {\n const aptos = getAptos(network);\n const results = await aptos.transaction.simulate.simple({ transaction });\n if (results.length === 0 || !results[0].success) {\n throw new Error(`Simulation failed: ${results[0]?.vm_status || \"unknown error\"}`);\n }\n },\n\n waitForTransaction: async (txHash: string, network: string) => {\n const aptos = getAptos(network);\n await aptos.waitForTransaction({ transactionHash: txHash });\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,iBAAsE;;;ACAtE,oBAA0C;AAKnC,IAAM,sBAAsB;AAK5B,IAAM,sBAAsB;AAM5B,IAA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5D;AAAA,EACF;AACF;","names":["import_ts_sdk","import_ts_sdk","import_ts_sdk"]}
@@ -0,0 +1,59 @@
1
+ import { Account, SimpleTransaction, AccountAuthenticator, PendingTransactionResponse } from '@aptos-labs/ts-sdk';
2
+
3
+ /**
4
+ * Client-side signer for creating and signing Aptos transactions
5
+ */
6
+ type ClientAptosSigner = Account;
7
+ /**
8
+ * Configuration for client operations
9
+ */
10
+ type ClientAptosConfig = {
11
+ /**
12
+ * Optional custom RPC URL for the client to use
13
+ */
14
+ rpcUrl?: string;
15
+ };
16
+ /**
17
+ * Minimal facilitator signer interface for Aptos operations
18
+ */
19
+ type FacilitatorAptosSigner = {
20
+ /**
21
+ * Get all addresses this facilitator can use for signing
22
+ */
23
+ getAddresses(): readonly string[];
24
+ /**
25
+ * Sign a transaction as the fee payer and submit it
26
+ */
27
+ signAndSubmitAsFeePayer(transaction: SimpleTransaction, senderAuthenticator: AccountAuthenticator, network: string): Promise<PendingTransactionResponse>;
28
+ /**
29
+ * Submit a fully-signed transaction (non-sponsored)
30
+ */
31
+ submitTransaction(transaction: SimpleTransaction, senderAuthenticator: AccountAuthenticator, network: string): Promise<PendingTransactionResponse>;
32
+ /**
33
+ * Simulate a transaction to verify it would succeed
34
+ */
35
+ simulateTransaction(transaction: SimpleTransaction, network: string): Promise<void>;
36
+ /**
37
+ * Wait for transaction confirmation
38
+ */
39
+ waitForTransaction(txHash: string, network: string): Promise<void>;
40
+ };
41
+ /**
42
+ * Creates a client signer from a private key
43
+ *
44
+ * @param privateKey - The private key as a hex string or AIP-80 format
45
+ * @returns An Aptos Account instance
46
+ */
47
+ declare function createClientSigner(privateKey: string): Promise<ClientAptosSigner>;
48
+ /**
49
+ * Create a facilitator signer from an Aptos Account
50
+ *
51
+ * @param account - The Aptos Account that will act as fee payer
52
+ * @param rpcConfig - Optional RPC configuration
53
+ * @returns FacilitatorAptosSigner instance
54
+ */
55
+ declare function toFacilitatorAptosSigner(account: Account, rpcConfig?: {
56
+ defaultRpcUrl?: string;
57
+ } | Record<string, string>): FacilitatorAptosSigner;
58
+
59
+ export { type ClientAptosConfig as C, type FacilitatorAptosSigner as F, type ClientAptosSigner as a, createClientSigner as c, toFacilitatorAptosSigner as t };
@@ -0,0 +1,59 @@
1
+ import { Account, SimpleTransaction, AccountAuthenticator, PendingTransactionResponse } from '@aptos-labs/ts-sdk';
2
+
3
+ /**
4
+ * Client-side signer for creating and signing Aptos transactions
5
+ */
6
+ type ClientAptosSigner = Account;
7
+ /**
8
+ * Configuration for client operations
9
+ */
10
+ type ClientAptosConfig = {
11
+ /**
12
+ * Optional custom RPC URL for the client to use
13
+ */
14
+ rpcUrl?: string;
15
+ };
16
+ /**
17
+ * Minimal facilitator signer interface for Aptos operations
18
+ */
19
+ type FacilitatorAptosSigner = {
20
+ /**
21
+ * Get all addresses this facilitator can use for signing
22
+ */
23
+ getAddresses(): readonly string[];
24
+ /**
25
+ * Sign a transaction as the fee payer and submit it
26
+ */
27
+ signAndSubmitAsFeePayer(transaction: SimpleTransaction, senderAuthenticator: AccountAuthenticator, network: string): Promise<PendingTransactionResponse>;
28
+ /**
29
+ * Submit a fully-signed transaction (non-sponsored)
30
+ */
31
+ submitTransaction(transaction: SimpleTransaction, senderAuthenticator: AccountAuthenticator, network: string): Promise<PendingTransactionResponse>;
32
+ /**
33
+ * Simulate a transaction to verify it would succeed
34
+ */
35
+ simulateTransaction(transaction: SimpleTransaction, network: string): Promise<void>;
36
+ /**
37
+ * Wait for transaction confirmation
38
+ */
39
+ waitForTransaction(txHash: string, network: string): Promise<void>;
40
+ };
41
+ /**
42
+ * Creates a client signer from a private key
43
+ *
44
+ * @param privateKey - The private key as a hex string or AIP-80 format
45
+ * @returns An Aptos Account instance
46
+ */
47
+ declare function createClientSigner(privateKey: string): Promise<ClientAptosSigner>;
48
+ /**
49
+ * Create a facilitator signer from an Aptos Account
50
+ *
51
+ * @param account - The Aptos Account that will act as fee payer
52
+ * @param rpcConfig - Optional RPC configuration
53
+ * @returns FacilitatorAptosSigner instance
54
+ */
55
+ declare function toFacilitatorAptosSigner(account: Account, rpcConfig?: {
56
+ defaultRpcUrl?: string;
57
+ } | Record<string, string>): FacilitatorAptosSigner;
58
+
59
+ export { type ClientAptosSigner as C, type FacilitatorAptosSigner as F, type ClientAptosConfig as a, createClientSigner as c, toFacilitatorAptosSigner as t };
@@ -0,0 +1,93 @@
1
+ import {
2
+ encodeAptosPayload
3
+ } from "./chunk-D4UVBSUH.mjs";
4
+ import {
5
+ APTOS_ADDRESS_REGEX,
6
+ getAptosNetwork,
7
+ getAptosRpcUrl
8
+ } from "./chunk-FG4ANPDN.mjs";
9
+
10
+ // src/exact/client/scheme.ts
11
+ import { AccountAddress, Aptos, AptosConfig, SimpleTransaction } from "@aptos-labs/ts-sdk";
12
+ var ExactAptosScheme = class {
13
+ /**
14
+ * Creates a new ExactAptosScheme instance.
15
+ *
16
+ * @param signer - The Aptos account for signing transactions
17
+ * @param config - Optional configuration with custom RPC URL
18
+ */
19
+ constructor(signer, config) {
20
+ this.signer = signer;
21
+ this.config = config;
22
+ this.scheme = "exact";
23
+ }
24
+ /**
25
+ * Creates a payment payload for the Exact scheme.
26
+ *
27
+ * @param x402Version - The x402 protocol version
28
+ * @param paymentRequirements - The payment requirements
29
+ * @returns Promise resolving to a payment payload
30
+ */
31
+ async createPaymentPayload(x402Version, paymentRequirements) {
32
+ if (!this.signer.accountAddress) {
33
+ throw new Error("Aptos account address is required");
34
+ }
35
+ if (!paymentRequirements.asset) {
36
+ throw new Error("Asset is required");
37
+ }
38
+ if (!paymentRequirements.asset.match(APTOS_ADDRESS_REGEX)) {
39
+ throw new Error("Invalid asset address");
40
+ }
41
+ if (!paymentRequirements.payTo) {
42
+ throw new Error("Pay-to address is required");
43
+ }
44
+ if (!paymentRequirements.payTo.match(APTOS_ADDRESS_REGEX)) {
45
+ throw new Error("Invalid pay-to address");
46
+ }
47
+ if (!paymentRequirements.amount) {
48
+ throw new Error("Amount is required");
49
+ }
50
+ if (!paymentRequirements.amount.match(/^[0-9]+$/)) {
51
+ throw new Error("Amount must be a number");
52
+ }
53
+ const aptosNetwork = getAptosNetwork(paymentRequirements.network);
54
+ const rpcUrl = this.config?.rpcUrl || getAptosRpcUrl(aptosNetwork);
55
+ const aptosConfig = new AptosConfig({
56
+ network: aptosNetwork,
57
+ fullnode: rpcUrl
58
+ });
59
+ const aptos = new Aptos(aptosConfig);
60
+ const feePayer = paymentRequirements.extra?.feePayer;
61
+ const isSponsored = typeof feePayer === "string";
62
+ const builtTransaction = await aptos.transaction.build.simple({
63
+ sender: this.signer.accountAddress,
64
+ data: {
65
+ function: "0x1::primary_fungible_store::transfer",
66
+ typeArguments: ["0x1::fungible_asset::Metadata"],
67
+ functionArguments: [
68
+ paymentRequirements.asset,
69
+ paymentRequirements.payTo,
70
+ paymentRequirements.amount
71
+ ]
72
+ },
73
+ withFeePayer: isSponsored
74
+ });
75
+ const transaction = isSponsored ? new SimpleTransaction(builtTransaction.rawTransaction, AccountAddress.from(feePayer)) : builtTransaction;
76
+ const senderAuthenticator = this.signer.signTransactionWithAuthenticator(transaction);
77
+ const transactionBytes = transaction.bcsToBytes();
78
+ const authenticatorBytes = senderAuthenticator.bcsToBytes();
79
+ const base64Transaction = encodeAptosPayload(transactionBytes, authenticatorBytes);
80
+ const payload = {
81
+ transaction: base64Transaction
82
+ };
83
+ return {
84
+ x402Version,
85
+ payload
86
+ };
87
+ }
88
+ };
89
+
90
+ export {
91
+ ExactAptosScheme
92
+ };
93
+ //# sourceMappingURL=chunk-6BMAMLXJ.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/exact/client/scheme.ts"],"sourcesContent":["import { AccountAddress, Aptos, AptosConfig, SimpleTransaction } from \"@aptos-labs/ts-sdk\";\nimport type { PaymentPayload, PaymentRequirements, SchemeNetworkClient } from \"@x402/core/types\";\nimport { APTOS_ADDRESS_REGEX, getAptosNetwork, getAptosRpcUrl } from \"../../constants\";\nimport type { ClientAptosSigner, ClientAptosConfig } from \"../../signer\";\nimport type { ExactAptosPayload } from \"../../types\";\nimport { encodeAptosPayload } from \"../../utils\";\n\n/**\n * Aptos client implementation for the Exact payment scheme.\n */\nexport class ExactAptosScheme implements SchemeNetworkClient {\n readonly scheme = \"exact\";\n\n /**\n * Creates a new ExactAptosScheme instance.\n *\n * @param signer - The Aptos account for signing transactions\n * @param config - Optional configuration with custom RPC URL\n */\n constructor(\n private readonly signer: ClientAptosSigner,\n private readonly config?: ClientAptosConfig,\n ) {}\n\n /**\n * Creates a payment payload for the Exact scheme.\n *\n * @param x402Version - The x402 protocol version\n * @param paymentRequirements - The payment requirements\n * @returns Promise resolving to a payment payload\n */\n async createPaymentPayload(\n x402Version: number,\n paymentRequirements: PaymentRequirements,\n ): Promise<Pick<PaymentPayload, \"x402Version\" | \"payload\">> {\n if (!this.signer.accountAddress) {\n throw new Error(\"Aptos account address is required\");\n }\n if (!paymentRequirements.asset) {\n throw new Error(\"Asset is required\");\n }\n if (!paymentRequirements.asset.match(APTOS_ADDRESS_REGEX)) {\n throw new Error(\"Invalid asset address\");\n }\n if (!paymentRequirements.payTo) {\n throw new Error(\"Pay-to address is required\");\n }\n if (!paymentRequirements.payTo.match(APTOS_ADDRESS_REGEX)) {\n throw new Error(\"Invalid pay-to address\");\n }\n if (!paymentRequirements.amount) {\n throw new Error(\"Amount is required\");\n }\n if (!paymentRequirements.amount.match(/^[0-9]+$/)) {\n throw new Error(\"Amount must be a number\");\n }\n\n const aptosNetwork = getAptosNetwork(paymentRequirements.network);\n const rpcUrl = this.config?.rpcUrl || getAptosRpcUrl(aptosNetwork);\n const aptosConfig = new AptosConfig({\n network: aptosNetwork,\n fullnode: rpcUrl,\n });\n const aptos = new Aptos(aptosConfig);\n\n const feePayer = paymentRequirements.extra?.feePayer;\n const isSponsored = typeof feePayer === \"string\";\n\n const builtTransaction = await aptos.transaction.build.simple({\n sender: this.signer.accountAddress,\n data: {\n function: \"0x1::primary_fungible_store::transfer\",\n typeArguments: [\"0x1::fungible_asset::Metadata\"],\n functionArguments: [\n paymentRequirements.asset,\n paymentRequirements.payTo,\n paymentRequirements.amount,\n ],\n },\n withFeePayer: isSponsored,\n });\n\n // For sponsored transactions, set the actual fee payer address (SDK uses 0x0 placeholder)\n const transaction = isSponsored\n ? new SimpleTransaction(builtTransaction.rawTransaction, AccountAddress.from(feePayer))\n : builtTransaction;\n\n const senderAuthenticator = this.signer.signTransactionWithAuthenticator(transaction);\n const transactionBytes = transaction.bcsToBytes();\n const authenticatorBytes = senderAuthenticator.bcsToBytes();\n const base64Transaction = encodeAptosPayload(transactionBytes, authenticatorBytes);\n\n const payload: ExactAptosPayload = {\n transaction: base64Transaction,\n };\n\n return {\n x402Version,\n payload,\n };\n }\n}\n"],"mappings":";;;;;;;;;;AAAA,SAAS,gBAAgB,OAAO,aAAa,yBAAyB;AAU/D,IAAM,mBAAN,MAAsD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAS3D,YACmB,QACA,QACjB;AAFiB;AACA;AAVnB,SAAS,SAAS;AAAA,EAWf;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASH,MAAM,qBACJ,aACA,qBAC0D;AAC1D,QAAI,CAAC,KAAK,OAAO,gBAAgB;AAC/B,YAAM,IAAI,MAAM,mCAAmC;AAAA,IACrD;AACA,QAAI,CAAC,oBAAoB,OAAO;AAC9B,YAAM,IAAI,MAAM,mBAAmB;AAAA,IACrC;AACA,QAAI,CAAC,oBAAoB,MAAM,MAAM,mBAAmB,GAAG;AACzD,YAAM,IAAI,MAAM,uBAAuB;AAAA,IACzC;AACA,QAAI,CAAC,oBAAoB,OAAO;AAC9B,YAAM,IAAI,MAAM,4BAA4B;AAAA,IAC9C;AACA,QAAI,CAAC,oBAAoB,MAAM,MAAM,mBAAmB,GAAG;AACzD,YAAM,IAAI,MAAM,wBAAwB;AAAA,IAC1C;AACA,QAAI,CAAC,oBAAoB,QAAQ;AAC/B,YAAM,IAAI,MAAM,oBAAoB;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