{"version":3,"sources":["../../src/shared.ts"],"sourcesContent":["import { Transaction, Address, Operation, xdr } from \"@stellar/stellar-sdk\";\nimport { Api, assembleTransaction } from \"@stellar/stellar-sdk/rpc\";\n\n/**\n * Handles the simulation result of a Stellar transaction.\n *\n * @param simulation - The simulation result to handle\n * @throws An error if the simulation result is of type \"RESTORE\" or \"ERROR\"\n */\nexport function handleSimulationResult(simulation?: Api.SimulateTransactionResponse) {\n  if (!simulation) {\n    throw new Error(\"Simulation result is undefined\");\n  }\n\n  if (Api.isSimulationRestore(simulation)) {\n    throw new Error(\n      `Stellar simulation result has type \"RESTORE\" with restorePreamble: ${simulation.restorePreamble}`,\n    );\n  }\n\n  if (Api.isSimulationError(simulation)) {\n    const msg = `Stellar simulation failed${simulation.error ? ` with error message: ${simulation.error}` : \"\"}`;\n\n    throw new Error(msg);\n  }\n}\n\n/**\n * Analysis result of transaction signers\n */\nexport type ContractSigners = {\n  /** Accounts that have already signed auth entries */\n  alreadySigned: string[];\n  /** Accounts that still need to sign auth entries */\n  pendingSignature: string[];\n};\n\n/**\n * Input parameters for gathering auth entry signature status\n */\nexport type GatherAuthEntrySignatureStatusInput = {\n  /** The transaction to analyze */\n  transaction: Transaction;\n  /** Optional simulation response to assemble with transaction before analysis */\n  simulationResponse?: Api.SimulateTransactionResponse;\n  /** Whether to simulate/assemble the transaction with simulation data (default: true if simulationResponse was not provided) */\n  simulate?: boolean;\n};\n\n/**\n * Gathers the signature status of auth entries in a Stellar transaction.\n *\n * This function inspects the auth entries in the transaction's InvokeHostFunction\n * operation and categorizes them based on their signature status.\n *\n * @param input - Input containing transaction and optional simulation data\n * @param input.transaction - The transaction to analyze\n * @param input.simulationResponse - Optional simulation response to assemble with transaction before analysis\n * @param input.simulate - Whether to simulate/assemble the transaction with simulation data (default: true if simulationResponse was not provided)\n * @returns ContractSigners with arrays of signed and pending signer addresses\n * @throws Error if transaction doesn't have exactly one InvokeHostFunction operation\n *\n * @example\n * ```ts\n * const status = gatherAuthEntrySignatureStatus({\n *   transaction: tx,\n *   simulationResponse: simResult\n * });\n * console.log('Already signed:', status.alreadySigned);\n * console.log('Pending:', status.pendingSignature);\n * ```\n */\nexport function gatherAuthEntrySignatureStatus({\n  transaction,\n  simulationResponse,\n  simulate,\n}: GatherAuthEntrySignatureStatusInput): ContractSigners {\n  // Determine if we should assemble with simulation\n  const shouldAssemble = simulate ?? simulationResponse !== undefined;\n  let assembledTx = transaction;\n\n  // Assemble transaction with simulation if requested\n  if (shouldAssemble && simulationResponse) {\n    const assembledTxBuilder = assembleTransaction(transaction, simulationResponse);\n    assembledTx = assembledTxBuilder.build();\n  }\n\n  // Validate transaction structure\n  if (assembledTx.operations.length !== 1) {\n    throw new Error(\n      `Expected transaction with exactly one operation, got ${assembledTx.operations.length}`,\n    );\n  }\n\n  const operation = assembledTx.operations[0];\n  if (operation.type !== \"invokeHostFunction\") {\n    throw new Error(`Expected InvokeHostFunction operation, got ${operation.type}`);\n  }\n\n  const invokeOp = operation as Operation.InvokeHostFunction;\n\n  const alreadySigned: string[] = [];\n  const pendingSignature: string[] = [];\n\n  for (const entry of invokeOp.auth ?? []) {\n    const credentialsType = entry.credentials().switch();\n\n    // Skip source account credentials - these use the transaction source\n    if (credentialsType === xdr.SorobanCredentialsType.sorobanCredentialsSourceAccount()) {\n      continue;\n    }\n\n    // Handle address-based credentials\n    if (credentialsType === xdr.SorobanCredentialsType.sorobanCredentialsAddress()) {\n      const addressCredentials = entry.credentials().address();\n      const address = Address.fromScAddress(addressCredentials.address()).toString();\n      const signature = addressCredentials.signature();\n\n      // Check if already signed (signature is not scvVoid)\n      const isSigned = signature.switch().name !== \"scvVoid\";\n\n      if (isSigned) {\n        alreadySigned.push(address);\n      } else {\n        pendingSignature.push(address);\n      }\n    }\n  }\n\n  return {\n    alreadySigned: [...new Set(alreadySigned)], // Remove duplicates\n    pendingSignature: [...new Set(pendingSignature)],\n  };\n}\n"],"mappings":";AAAA,SAAsB,SAAoB,WAAW;AACrD,SAAS,KA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