{"version":3,"sources":["../../src/exact/client/scheme.ts"],"sourcesContent":["import { nativeToScVal, contract } from \"@stellar/stellar-sdk\";\nimport { handleSimulationResult } from \"../../shared\";\nimport {\n  getEstimatedLedgerCloseTimeSeconds,\n  getNetworkPassphrase,\n  getRpcClient,\n  getRpcUrl,\n  isStellarNetwork,\n  RpcConfig,\n  validateStellarAssetAddress,\n  validateStellarDestinationAddress,\n} from \"../../utils\";\nimport type { ClientStellarSigner } from \"../../signer\";\nimport type { PaymentPayload, PaymentRequirements, SchemeNetworkClient } from \"@x402/core/types\";\n\n/**\n * Stellar client implementation for the Exact payment scheme.\n */\nexport class ExactStellarScheme implements SchemeNetworkClient {\n  readonly scheme = \"exact\";\n\n  /**\n   * Creates a new ExactStellarScheme instance.\n   *\n   * @param signer - The Stellar signer for client operations\n   * @param rpcConfig - Optional configuration with custom RPC URL\n   * @returns ExactStellarScheme instance\n   */\n  constructor(\n    private readonly signer: ClientStellarSigner,\n    private readonly rpcConfig?: RpcConfig,\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    try {\n      this.validateCreateAndSignPaymentInput(paymentRequirements);\n    } catch (error) {\n      throw new Error(`Invalid input parameters for creating Stellar payment, cause: ${error}`);\n    }\n\n    const sourcePublicKey = this.signer.address;\n    const { network, payTo, asset, amount, extra, maxTimeoutSeconds } = paymentRequirements;\n    const networkPassphrase = getNetworkPassphrase(network);\n    const rpcUrl = getRpcUrl(network, this.rpcConfig);\n\n    if (!extra.areFeesSponsored) {\n      throw new Error(`Exact scheme requires areFeesSponsored to be true`);\n    }\n\n    // Fetch current ledger and calculate maxLedger\n    const rpcServer = getRpcClient(network, this.rpcConfig);\n    const latestLedger = await rpcServer.getLatestLedger();\n    const currentLedger = latestLedger.sequence;\n    const estimatedLedgerSeconds = await getEstimatedLedgerCloseTimeSeconds(network);\n    const maxLedger = currentLedger + Math.ceil(maxTimeoutSeconds / estimatedLedgerSeconds);\n\n    const tx = await contract.AssembledTransaction.build({\n      contractId: asset,\n      method: \"transfer\",\n      args: [\n        // SEP-41 spec: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0041.md#interface\n        nativeToScVal(sourcePublicKey, { type: \"address\" }), // from\n        nativeToScVal(payTo, { type: \"address\" }), // to\n        nativeToScVal(amount, { type: \"i128\" }), // amount\n      ],\n      networkPassphrase,\n      rpcUrl,\n      parseResultXdr: result => result,\n    });\n    handleSimulationResult(tx.simulation);\n\n    let missingSigners = tx.needsNonInvokerSigningBy();\n    if (!missingSigners.includes(sourcePublicKey) || missingSigners.length > 1) {\n      throw new Error(\n        `Expected to sign with [${sourcePublicKey}], but got [${missingSigners.join(\", \")}]`,\n      );\n    }\n    await tx.signAuthEntries({\n      address: sourcePublicKey,\n      signAuthEntry: this.signer.signAuthEntry,\n      expiration: maxLedger,\n    });\n\n    await tx.simulate();\n    handleSimulationResult(tx.simulation);\n\n    missingSigners = tx.needsNonInvokerSigningBy();\n    if (missingSigners.length > 0) {\n      throw new Error(`unexpected signer(s) required: [${missingSigners.join(\", \")}]`);\n    }\n\n    return {\n      x402Version,\n      payload: {\n        transaction: tx.built!.toXDR(),\n      },\n    };\n  }\n\n  /**\n   * Validates the input parameters for the createAndSignPayment function.\n   *\n   * @param paymentRequirements - Payment requirements\n   * @throws Error if validation fails\n   */\n  private validateCreateAndSignPaymentInput(paymentRequirements: PaymentRequirements): void {\n    const { scheme, network, payTo, asset, amount } = paymentRequirements;\n    if (typeof amount !== \"string\" || !Number.isInteger(Number(amount)) || Number(amount) <= 0) {\n      throw new Error(`Invalid amount: ${amount}. Amount must be a positive integer.`);\n    }\n\n    if (scheme !== \"exact\") {\n      throw new Error(`Unsupported scheme: ${scheme}`);\n    }\n\n    if (!isStellarNetwork(network)) {\n      throw new Error(`Unsupported Stellar network: ${network}`);\n    }\n\n    if (!validateStellarDestinationAddress(payTo)) {\n      throw new Error(`Invalid Stellar destination address: ${payTo}`);\n    }\n\n    if (!validateStellarAssetAddress(asset)) {\n      throw new Error(`Invalid Stellar asset address: ${asset}`);\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;AAAA,SAAS,eAAe,gBAAgB;AAkBjC,IAAM,qBAAN,MAAwD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAU7D,YACmB,QACA,WACjB;AAFiB;AACA;AAXnB,SAAS,SAAS;AAAA,EAYf;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASH,MAAM,qBACJ,aACA,qBAC0D;AAC1D,QAAI;AACF,WAAK,kCAAkC,mBAAmB;AAAA,IAC5D,SAAS,OAAO;AACd,YAAM,IAAI,MAAM,iEAAiE,KAAK,EAAE;AAAA,IAC1F;AAEA,UAAM,kBAAkB,KAAK,OAAO;AACpC,UAAM,EAAE,SAAS,OAAO,OAAO,QAAQ,OAAO,kBAAkB,IAAI;AACpE,UAAM,oBAAoB,qBAAqB,OAAO;AACtD,UAAM,SAAS,UAAU,SAAS,KAAK,SAAS;AAEhD,QAAI,CAAC,MAAM,kBAAkB;AAC3B,YAAM,IAAI,MAAM,mDAAmD;AAAA,IACrE;AAGA,UAAM,YAAY,aAAa,SAAS,KAAK,SAAS;AACtD,UAAM,eAAe,MAAM,UAAU,gBAAgB;AACrD,UAAM,gBAAgB,aAAa;AACnC,UAAM,yBAAyB,MAAM,mCAAmC,OAAO;AAC/E,UAAM,YAAY,gBAAgB,KAAK,KAAK,oBAAoB,sBAAsB;AAEtF,UAAM,KAAK,MAAM,SAAS,qBAAqB,MAAM;AAAA,MACnD,YAAY;AAAA,MACZ,QAAQ;AAAA,MACR,MAAM;AAAA;AAAA,QAEJ,cAAc,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAAA;AA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