{"version":3,"file":"client.mjs","names":["#client","#network","#host","#path","#claimApi","#contract"],"sources":["../src/client.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { ClientWithCoreApi, SuiClientRegistration } from '@mysten/sui/client';\nimport type { Keypair } from '@mysten/sui/cryptography';\nimport { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';\nimport type { Transaction } from '@mysten/sui/transactions';\nimport { fromBase64 } from '@mysten/sui/utils';\n\nimport { ZkSendLink } from './links/claim.js';\nimport { ZkSendLinkBuilder } from './links/builder.js';\nimport type { ZkBagContractOptions } from './links/zk-bag.js';\nimport { getContractIds } from './links/zk-bag.js';\n\nexport type ZkSendCompatibleClient = ClientWithCoreApi;\n\nexport interface ZkSendOptions<Name extends string = 'zksend'> {\n\tname?: Name;\n\thost?: string;\n\tpath?: string;\n\tclaimApi?: string;\n\tcontract?: ZkBagContractOptions;\n}\n\nexport interface LinkBuilderOptions {\n\tsender: string;\n\tkeypair?: Keypair;\n}\n\nexport type LoadLinkOptions =\n\t| {\n\t\t\taddress: string;\n\t\t\tkeypair?: never;\n\t  }\n\t| {\n\t\t\tkeypair: Keypair;\n\t\t\taddress?: never;\n\t  };\n\nfunction getDefaultContractIds(clientNetwork: string): ZkBagContractOptions {\n\tif (clientNetwork === 'mainnet' || clientNetwork === 'testnet') {\n\t\treturn getContractIds(clientNetwork);\n\t}\n\tthrow new Error(\n\t\t`zkSend only has built-in contract IDs for mainnet and testnet. For network \"${clientNetwork}\", you must provide a custom contract option.`,\n\t);\n}\n\nexport class ZkSendClient {\n\t#client: ZkSendCompatibleClient;\n\t#network: string;\n\t#host: string;\n\t#path: string;\n\t#claimApi: string;\n\t#contract: ZkBagContractOptions;\n\n\tconstructor(client: ZkSendCompatibleClient, options: Omit<ZkSendOptions, 'name'> = {}) {\n\t\tthis.#client = client;\n\t\tthis.#network = client.network;\n\t\tthis.#host = options.host ?? 'https://my.slush.app';\n\t\tthis.#path = options.path ?? '/claim';\n\t\tthis.#claimApi = options.claimApi ?? 'https://api.slush.app/api';\n\t\tthis.#contract = options.contract ?? getDefaultContractIds(client.network);\n\t}\n\n\tlinkBuilder(options: LinkBuilderOptions): ZkSendLinkBuilder {\n\t\treturn new ZkSendLinkBuilder({\n\t\t\tclient: this.#client,\n\t\t\tsender: options.sender,\n\t\t\tkeypair: options.keypair,\n\t\t\tnetwork: this.#network,\n\t\t\thost: this.#host,\n\t\t\tpath: this.#path,\n\t\t\tcontract: this.#contract,\n\t\t});\n\t}\n\n\tasync createLinks(options: {\n\t\tlinks: ZkSendLinkBuilder[];\n\t\ttransaction?: Transaction;\n\t}): Promise<Transaction> {\n\t\treturn ZkSendLinkBuilder.createLinks({\n\t\t\tlinks: options.links,\n\t\t\ttransaction: options.transaction,\n\t\t\tclient: this.#client,\n\t\t\tnetwork: this.#network,\n\t\t\tcontract: this.#contract,\n\t\t});\n\t}\n\n\t/**\n\t * Get a link without loading its assets (synchronous)\n\t */\n\tgetLink(options: LoadLinkOptions): ZkSendLink {\n\t\treturn new ZkSendLink({\n\t\t\tclient: this.#client,\n\t\t\tnetwork: this.#network,\n\t\t\thost: this.#host,\n\t\t\tpath: this.#path,\n\t\t\tclaimApi: this.#claimApi,\n\t\t\tcontract: this.#contract,\n\t\t\t...options,\n\t\t});\n\t}\n\n\t/**\n\t * Load a link and its assets\n\t */\n\tasync loadLink(options: LoadLinkOptions): Promise<ZkSendLink> {\n\t\tconst link = this.getLink(options);\n\t\tawait link.loadAssets();\n\t\treturn link;\n\t}\n\n\t/**\n\t * Get a link from a URL without loading assets (synchronous)\n\t */\n\tgetLinkFromUrl(url: string): ZkSendLink {\n\t\tconst parsed = new URL(url);\n\t\tif (!parsed.hash.startsWith('#$')) {\n\t\t\tthrow new Error('Invalid link URL');\n\t\t}\n\n\t\tconst parsedNetwork = parsed.searchParams.get('network') === 'testnet' ? 'testnet' : 'mainnet';\n\n\t\tif (parsedNetwork !== this.#network) {\n\t\t\tthrow new Error(\n\t\t\t\t`Link network \"${parsedNetwork}\" does not match client network \"${this.#network}\"`,\n\t\t\t);\n\t\t}\n\n\t\tconst keypair = Ed25519Keypair.fromSecretKey(fromBase64(parsed.hash.slice(2)));\n\n\t\treturn new ZkSendLink({\n\t\t\tclient: this.#client,\n\t\t\tkeypair,\n\t\t\tnetwork: this.#network,\n\t\t\thost: `${parsed.protocol}//${parsed.host}`,\n\t\t\tpath: parsed.pathname,\n\t\t\tclaimApi: this.#claimApi,\n\t\t\tcontract: this.#contract,\n\t\t});\n\t}\n\n\t/**\n\t * Load a link from a URL and its assets\n\t */\n\tasync loadLinkFromUrl(url: string): Promise<ZkSendLink> {\n\t\tconst link = this.getLinkFromUrl(url);\n\t\tawait link.loadAssets();\n\t\treturn link;\n\t}\n}\n\nexport function zksend<Name extends string = 'zksend'>({\n\tname = 'zksend' as Name,\n\t...options\n}: ZkSendOptions<Name> = {}): SuiClientRegistration<ZkSendCompatibleClient, Name, ZkSendClient> {\n\treturn {\n\t\tname,\n\t\tregister: (client) => {\n\t\t\treturn new ZkSendClient(client, options);\n\t\t},\n\t};\n}\n"],"mappings":";;;;;;;AAuCA,SAAS,sBAAsB,eAA6C;AAC3E,KAAI,kBAAkB,aAAa,kBAAkB,UACpD,QAAO,eAAe,cAAc;AAErC,OAAM,IAAI,MACT,+EAA+E,cAAc,+CAC7F;;AAGF,IAAa,eAAb,MAA0B;CACzB;CACA;CACA;CACA;CACA;CACA;CAEA,YAAY,QAAgC,UAAuC,EAAE,EAAE;AACtF,QAAKA,SAAU;AACf,QAAKC,UAAW,OAAO;AACvB,QAAKC,OAAQ,QAAQ,QAAQ;AAC7B,QAAKC,OAAQ,QAAQ,QAAQ;AAC7B,QAAKC,WAAY,QAAQ,YAAY;AACrC,QAAKC,WAAY,QAAQ,YAAY,sBAAsB,OAAO,QAAQ;;CAG3E,YAAY,SAAgD;AAC3D,SAAO,IAAI,kBAAkB;GAC5B,QAAQ,MAAKL;GACb,QAAQ,QAAQ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