{"version":3,"file":"index.cjs","names":["#publicClient","BaseSigner","#walletClient","#ethereum","#unsubscribeProvider","#subscribeToProvider","WalletNotConnectedError","#requireAccount","eip1193Subscribe","buildZamaConfig","confidentialBalanceOfContract","underlyingContract","supportsInterfaceContract","confidentialTransferContract","unwrapContract","unwrapFromBalanceContract","finalizeUnwrapContract","setOperatorContract","wrapContract","getTokenPairsContract","getTokenPairsLengthContract","getTokenPairsSliceContract","getTokenPairContract","getConfidentialTokenAddressContract","getTokenAddressContract","isConfidentialTokenValidContract"],"sources":["../../../src/viem/viem-provider.ts","../../../src/viem/viem-signer.ts","../../../src/viem/config.ts","../../../src/viem/contracts.ts"],"sourcesContent":["import type {\n  Abi,\n  ContractFunctionArgs,\n  ContractFunctionName,\n  ContractFunctionReturnType,\n  Hex,\n  PublicClient,\n} from \"viem\";\nimport type { GenericProvider, ReadContractConfig, TransactionReceipt } from \"../types\";\n\n/** Configuration for {@link ViemProvider}. */\nexport interface ViemProviderConfig {\n  /** A viem `PublicClient` backing all read operations. */\n  publicClient: PublicClient;\n}\n\n/**\n * Read-only {@link GenericProvider} backed by a viem `PublicClient`.\n *\n * Use this for integrations that only need public chain reads before the user has Pair with a\n * {@link ViemSigner} when wallet authority is required; the two can share a transport or point at\n * independent RPCs.\n *\n * @example\n * ```ts\n * const publicClient = createPublicClient({ chain: sepolia, transport: http(ALCHEMY_URL) });\n * const provider = new ViemProvider({ publicClient });\n * ```\n */\nexport class ViemProvider implements GenericProvider {\n  readonly #publicClient: PublicClient;\n\n  constructor(config: ViemProviderConfig) {\n    this.#publicClient = config.publicClient;\n  }\n\n  async getChainId(): Promise<number> {\n    return this.#publicClient.getChainId();\n  }\n\n  async readContract<\n    const TAbi extends Abi | readonly unknown[],\n    TFunctionName extends ContractFunctionName<TAbi, \"pure\" | \"view\">,\n    const TArgs extends ContractFunctionArgs<TAbi, \"pure\" | \"view\", TFunctionName>,\n  >(\n    config: ReadContractConfig<TAbi, TFunctionName, TArgs>,\n  ): Promise<ContractFunctionReturnType<TAbi, \"pure\" | \"view\", TFunctionName, TArgs>> {\n    return this.#publicClient.readContract(config);\n  }\n\n  async waitForTransactionReceipt(hash: Hex): Promise<TransactionReceipt> {\n    return this.#publicClient.waitForTransactionReceipt({ hash });\n  }\n\n  async getBlockTimestamp(): Promise<bigint> {\n    const block = await this.#publicClient.getBlock();\n    return block.timestamp;\n  }\n}\n","import type {\n  Account,\n  Abi,\n  ContractFunctionArgs,\n  ContractFunctionName,\n  EIP1193Provider,\n  WalletClient,\n  Hex,\n} from \"viem\";\nimport { getAddress } from \"viem\";\nimport type { writeContract } from \"viem/actions\";\nimport { WalletNotConnectedError } from \"../errors\";\nimport type { EIP712TypedData } from \"../relayer/relayer-sdk.types\";\nimport { BaseSigner } from \"../signer/base-signer\";\nimport { eip1193Subscribe } from \"../signer/eip1193-subscribe\";\nimport type { WalletAccount, WriteContractConfig } from \"../types\";\n\n/**\n * Configuration for {@link ViemSigner}.\n *\n * The optional `ethereum` field is needed for `subscribe()` (EIP-1193\n * `accountsChanged` / `disconnect` events). It cannot be auto-extracted from\n * `walletClient` because viem's `custom(ethereum)` transport captures the\n * provider in a closure and does **not** expose `on` / `removeListener` on\n * `walletClient.transport`.\n *\n * If you omit `ethereum`, `subscribe()` returns a no-op. For automatic\n * wallet lifecycle handling, consider using `WagmiSigner` instead.\n */\nexport interface ViemSignerConfig {\n  /** Wallet client for signing and write operations. */\n  walletClient: WalletClient;\n  ethereum?: EIP1193Provider;\n}\n\nfunction walletAccountFromWalletClient(walletClient: WalletClient): WalletAccount | undefined {\n  if (!walletClient.account || !walletClient.chain) {\n    return undefined;\n  }\n  const address = getAddress(walletClient.account.address);\n  return { address, chainId: walletClient.chain.id };\n}\n\nexport class ViemSigner extends BaseSigner {\n  readonly #walletClient: WalletClient;\n  readonly #ethereum?: EIP1193Provider;\n  readonly #unsubscribeProvider: () => void;\n\n  constructor(config: ViemSignerConfig) {\n    super(walletAccountFromWalletClient(config.walletClient));\n    this.#walletClient = config.walletClient;\n    this.#ethereum = config.ethereum;\n    this.#unsubscribeProvider = this.#subscribeToProvider();\n  }\n\n  #requireAccount(operation: string): { walletClient: WalletClient; account: Account } {\n    if (!this.#walletClient.account) {\n      throw new WalletNotConnectedError(operation);\n    }\n    return { walletClient: this.#walletClient, account: this.#walletClient.account };\n  }\n\n  async signTypedData(typedData: EIP712TypedData): Promise<Hex> {\n    const { walletClient, account } = this.#requireAccount(\"signTypedData\");\n    const { EIP712Domain: _, ...sigTypes } = typedData.types;\n    return walletClient.signTypedData({\n      account,\n      primaryType: typedData.primaryType,\n      types: sigTypes,\n      domain: typedData.domain,\n      message: {\n        ...typedData.message,\n        startTimestamp: BigInt(typedData.message.startTimestamp),\n        durationDays: BigInt(typedData.message.durationDays),\n      },\n      // Cast: EIP712TypedData is a union; viem cannot correlate primaryType/types/message across union members, so the inferred `message` collapses to `never`.\n    } as Parameters<typeof walletClient.signTypedData>[0]);\n  }\n\n  async writeContract<\n    const TAbi extends Abi | readonly unknown[],\n    TFunctionName extends ContractFunctionName<TAbi, \"nonpayable\" | \"payable\">,\n    const TArgs extends ContractFunctionArgs<TAbi, \"nonpayable\" | \"payable\", TFunctionName>,\n  >(config: WriteContractConfig<TAbi, TFunctionName, TArgs>): Promise<Hex> {\n    const { walletClient, account } = this.#requireAccount(\"writeContract\");\n    return walletClient.writeContract({\n      chain: walletClient.chain,\n      account,\n      ...config,\n    } as Parameters<typeof writeContract>[1]);\n  }\n\n  #subscribeToProvider(): () => void {\n    if (!this.#ethereum) {\n      return () => {};\n    }\n    return eip1193Subscribe({\n      provider: this.#ethereum,\n      getInitialWalletAccount: () => walletAccountFromWalletClient(this.#walletClient),\n      onWalletAccountChange: ({ next }) => {\n        this.walletAccount.setSnapshot(next);\n      },\n    });\n  }\n\n  protected override onDispose(): void {\n    this.#unsubscribeProvider();\n  }\n}\n","import type { FheChain } from \"../chains\";\nimport type { ZamaConfig } from \"../config/types\";\nimport { buildZamaConfig } from \"../config/build\";\nimport { ViemProvider } from \"./viem-provider\";\nimport { ViemSigner } from \"./viem-signer\";\nimport type { ZamaConfigViem } from \"./types\";\n\n/** Create a {@link ZamaConfig} from viem clients. */\nexport function createConfig<const TChains extends readonly [FheChain, ...FheChain[]]>(\n  params: ZamaConfigViem<TChains>,\n): ZamaConfig {\n  const signer = new ViemSigner({\n    walletClient: params.walletClient,\n    ethereum: params.ethereum,\n  });\n  const provider = new ViemProvider({ publicClient: params.publicClient });\n  return buildZamaConfig(signer, provider, params);\n}\n","import type { PublicClient, WalletClient, Address, Hex } from \"viem\";\nimport type { EncryptedValue } from \"../relayer/relayer-sdk.types\";\nimport {\n  confidentialBalanceOfContract,\n  confidentialTransferContract,\n  finalizeUnwrapContract,\n  setOperatorContract,\n  supportsInterfaceContract,\n  underlyingContract,\n  unwrapContract,\n  unwrapFromBalanceContract,\n  wrapContract,\n  getTokenPairsContract,\n  getTokenPairsLengthContract,\n  getTokenPairsSliceContract,\n  getTokenPairContract,\n  getConfidentialTokenAddressContract,\n  getTokenAddressContract,\n  isConfidentialTokenValidContract,\n} from \"../contracts\";\n\n// ── Helpers ────────────────────────────────────────────────\n\nfunction requireAccount(client: WalletClient) {\n  if (!client.account) {\n    throw new TypeError(\"WalletClient has no account\");\n  }\n  return client.account;\n}\n\n// ── Read helpers ────────────────────────────────────────────\n\nexport function readConfidentialBalanceOfContract(\n  client: PublicClient,\n  tokenAddress: Address,\n  userAddress: Address,\n) {\n  return client.readContract(confidentialBalanceOfContract(tokenAddress, userAddress));\n}\n\nexport function readUnderlyingTokenContract(client: PublicClient, wrapperAddress: Address) {\n  return client.readContract(underlyingContract(wrapperAddress));\n}\n\nexport function readSupportsInterfaceContract(\n  client: PublicClient,\n  tokenAddress: Address,\n  interfaceId: Address,\n) {\n  return client.readContract(supportsInterfaceContract(tokenAddress, interfaceId));\n}\n\n// ── Write helpers ───────────────────────────────────────────\n\nexport function writeConfidentialTransferContract(\n  client: WalletClient,\n  tokenAddress: Address,\n  to: Address,\n  encryptedAmount: EncryptedValue,\n  inputProof: Hex,\n) {\n  return client.writeContract({\n    chain: client.chain,\n    account: requireAccount(client),\n    ...confidentialTransferContract(tokenAddress, to, encryptedAmount, inputProof),\n  });\n}\n\nexport function writeUnwrapContract(\n  client: WalletClient,\n  encryptedErc20: Address,\n  from: Address,\n  to: Address,\n  encryptedAmount: EncryptedValue,\n  inputProof: Hex,\n) {\n  return client.writeContract({\n    chain: client.chain,\n    account: requireAccount(client),\n    ...unwrapContract(encryptedErc20, from, to, encryptedAmount, inputProof),\n  });\n}\n\nexport function writeUnwrapFromBalanceContract(\n  client: WalletClient,\n  encryptedErc20: Address,\n  from: Address,\n  to: Address,\n  encryptedBalance: EncryptedValue,\n) {\n  return client.writeContract({\n    chain: client.chain,\n    account: requireAccount(client),\n    ...unwrapFromBalanceContract(encryptedErc20, from, to, encryptedBalance),\n  });\n}\n\nexport function writeFinalizeUnwrapContract(\n  client: WalletClient,\n  wrapper: Address,\n  unwrapRequestId: EncryptedValue,\n  unwrapAmountCleartext: bigint,\n  decryptionProof: Hex,\n) {\n  return client.writeContract({\n    chain: client.chain,\n    account: requireAccount(client),\n    ...finalizeUnwrapContract(wrapper, unwrapRequestId, unwrapAmountCleartext, decryptionProof),\n  });\n}\n\nexport function writeSetOperatorContract(\n  client: WalletClient,\n  tokenAddress: Address,\n  operator: Address,\n  until?: number,\n) {\n  return client.writeContract({\n    chain: client.chain,\n    account: requireAccount(client),\n    ...setOperatorContract(tokenAddress, operator, until),\n  });\n}\n\nexport function writeWrapContract(\n  client: WalletClient,\n  wrapperAddress: Address,\n  to: Address,\n  amount: bigint,\n) {\n  return client.writeContract({\n    chain: client.chain,\n    account: requireAccount(client),\n    ...wrapContract(wrapperAddress, to, amount),\n  });\n}\n\n// ── Registry read helpers ──────────────────────────────────\n\nexport function readTokenPairsContract(client: PublicClient, registry: Address) {\n  return client.readContract(getTokenPairsContract(registry));\n}\n\nexport function readTokenPairsLengthContract(client: PublicClient, registry: Address) {\n  return client.readContract(getTokenPairsLengthContract(registry));\n}\n\nexport function readTokenPairsSliceContract(\n  client: PublicClient,\n  registry: Address,\n  fromIndex: bigint,\n  toIndex: bigint,\n) {\n  return client.readContract(getTokenPairsSliceContract(registry, fromIndex, toIndex));\n}\n\nexport function readTokenPairContract(client: PublicClient, registry: Address, index: bigint) {\n  return client.readContract(getTokenPairContract(registry, index));\n}\n\nexport function readConfidentialTokenAddressContract(\n  client: PublicClient,\n  registry: Address,\n  tokenAddress: Address,\n) {\n  return client.readContract(getConfidentialTokenAddressContract(registry, tokenAddress));\n}\n\nexport function readTokenAddressContract(\n  client: PublicClient,\n  registry: Address,\n  confidentialTokenAddress: Address,\n) {\n  return client.readContract(getTokenAddressContract(registry, confidentialTokenAddress));\n}\n\nexport function readIsConfidentialTokenValidContract(\n  client: PublicClient,\n  registry: Address,\n  confidentialTokenAddress: Address,\n) {\n  return client.readContract(isConfidentialTokenValidContract(registry, confidentialTokenAddress));\n}\n"],"mappings":"2MA6BA,IAAa,EAAb,KAAqD,CACnD,GAEA,YAAY,EAA4B,CACtC,KAAKA,GAAgB,EAAO,YAC9B,CAEA,MAAM,YAA8B,CAClC,OAAO,KAAKA,GAAc,WAAW,CACvC,CAEA,MAAM,aAKJ,EACkF,CAClF,OAAO,KAAKA,GAAc,aAAa,CAAM,CAC/C,CAEA,MAAM,0BAA0B,EAAwC,CACtE,OAAO,KAAKA,GAAc,0BAA0B,CAAE,MAAK,CAAC,CAC9D,CAEA,MAAM,mBAAqC,CAEzC,OAAO,MADa,KAAKA,GAAc,SAAS,EAAA,CACnC,SACf,CACF,ECvBA,SAAS,EAA8B,EAAuD,CACxF,MAAC,EAAa,SAAW,CAAC,EAAa,OAI3C,MAAO,CAAE,SAAA,EAAA,EAAA,WAAA,CADkB,EAAa,QAAQ,OACjC,EAAG,QAAS,EAAa,MAAM,EAAG,CACnD,CAEA,IAAa,EAAb,cAAgCC,EAAAA,CAAW,CACzC,GACA,GACA,GAEA,YAAY,EAA0B,CACpC,MAA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