{"version":3,"sources":["../../src/exact/client/scheme.ts"],"sourcesContent":["import { AccountAddress, Aptos, AptosConfig, SimpleTransaction } from \"@aptos-labs/ts-sdk\";\nimport type {\n  PaymentPayload,\n  PaymentRequirements,\n  SchemeNetworkClient,\n} from \"@bankofai/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;AAc/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,IA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