{"version":3,"file":"barrel.mjs","names":[],"sources":["../src/signer.ts","../src/signersFactory.ts","../src/barrel.ts"],"sourcesContent":["import { ccc } from \"@ckb-ccc/core\";\nimport { cccA } from \"@ckb-ccc/core/advanced\";\n\nimport { Provider } from \"./advancedBarrel.js\";\n\n/**\n * Class representing a CKB signer that extends Signer from @ckb-ccc/core.\n * @public\n */\nexport class ReiSigner extends ccc.Signer {\n  /**\n   * Creates an instance of Signer.\n   * @param client - The client instance.\n   * @param provider - The provider instance.\n   */\n  constructor(\n    client: ccc.Client,\n    public readonly provider: Provider,\n  ) {\n    super(client);\n  }\n\n  /**\n   * Register a listener to be called when this signer is replaced.\n   *\n   * @returns A function for unregister\n   */\n\n  onReplaced(listener: () => void): () => void {\n    const stop: (() => void)[] = [];\n    const replacer = async () => {\n      listener();\n      stop[0]?.();\n    };\n    stop.push(() => {\n      this.provider.off(\"accountsChanged\", replacer);\n      this.provider.off(\"chainChanged\", replacer);\n    });\n    this.provider.on(\"accountsChanged\", replacer);\n    this.provider.on(\"chainChanged\", replacer);\n\n    return stop[0];\n  }\n\n  /**\n   * Gets the signer type.\n   * @returns The type of the signer.\n   */\n  get type(): ccc.SignerType {\n    return ccc.SignerType.CKB;\n  }\n\n  /**\n   * Gets the sign type.\n   * @returns The sign type.\n   */\n  get signType(): ccc.SignerSignType {\n    return ccc.SignerSignType.CkbSecp256k1;\n  }\n\n  /**\n   * Connects to the provider by requesting authentication.\n   * @returns A promise that resolves when the connection is established.\n   */\n  async connect(): Promise<void> {\n    const prefixClient = this.client.addressPrefix;\n    const netChange = await this._getNetworkChanged();\n    if (netChange) {\n      const data = prefixClient === \"ckb\" ? \"mainnet\" : \"testnet\";\n      await this.provider.request({ method: \"ckb_switchNetwork\", data });\n    }\n  }\n\n  /**\n   * Checks if the signer is connected.\n   * @returns A promise that resolves to true if connected, false otherwise.\n   */\n  async isConnected(): Promise<boolean> {\n    const connected = await this.provider.isConnected();\n    if (!connected || (await this._getNetworkChanged())) {\n      return false;\n    }\n    return connected;\n  }\n\n  async _getNetworkChanged(): Promise<boolean> {\n    const prefixClient = this.client.addressPrefix;\n\n    const address = await this.getInternalAddress();\n    const { prefix } = cccA.addressPayloadFromString(address);\n    return prefixClient !== prefix;\n  }\n\n  /**\n   * Gets the internal address.\n   * @returns A promise that resolves to the internal address.\n   */\n  async getInternalAddress(): Promise<string> {\n    return this.provider.request({ method: \"ckb_requestAccounts\" });\n  }\n\n  /**\n   * Gets the address object.\n   * @returns A promise that resolves to the address object.\n   */\n  async getAddressObj(): Promise<ccc.Address> {\n    return ccc.Address.fromString(await this.getInternalAddress(), this.client);\n  }\n\n  /**\n   * Gets the address objects.\n   * @returns A promise that resolves to an array of address objects.\n   */\n  async getAddressObjs(): Promise<ccc.Address[]> {\n    return [await this.getAddressObj()];\n  }\n\n  /**\n   * Gets the identity of the signer.\n   * @returns A promise that resolves to the identity.\n   */\n  async getIdentity(): Promise<string> {\n    return this.provider.request({\n      method: \"ckb_getPublicKey\",\n    });\n  }\n\n  /**\n   * Signs a raw message with the personal account.\n   * @param message - The message to sign.\n   * @returns A promise that resolves to the signed message.\n   */\n  async signMessageRaw(message: string): Promise<ccc.Hex> {\n    return this.provider.request({\n      method: \"ckb_signMessage\",\n      data: { message },\n    });\n  }\n\n  /**\n   * prepare a transaction before signing.\n   *\n   * @param txLike - The transaction to prepare, represented as a TransactionLike object.\n   * @returns A promise that resolves to the prepared Transaction object.\n   */\n\n  async prepareTransaction(\n    txLike: ccc.TransactionLike,\n  ): Promise<ccc.Transaction> {\n    const tx = ccc.Transaction.from(txLike);\n    await tx.addCellDepsOfKnownScripts(\n      this.client,\n      ccc.KnownScript.Secp256k1Blake160,\n    );\n    return ccc.reduceAsync(\n      await this.getAddressObjs(),\n      (tx: ccc.Transaction, { script }) =>\n        tx.prepareSighashAllWitness(script, 65, this.client),\n      tx,\n    );\n  }\n\n  /**\n   * Signs a transaction without preparing information for it.\n   *\n   * @param txLike - The transaction to sign, represented as a TransactionLike object.\n   * @returns A promise that resolves to the signed Transaction object.\n   */\n\n  async signOnlyTransaction(\n    txLike: ccc.TransactionLike,\n  ): Promise<ccc.Transaction> {\n    const txFormat = cccA.JsonRpcTransformers.transactionFrom(txLike);\n\n    return this.provider.request({\n      method: \"ckb_signTransaction\",\n      data: { txSkeleton: txFormat },\n    });\n  }\n}\n","import { ccc } from \"@ckb-ccc/core\";\nimport { Provider } from \"./advancedBarrel.js\";\nimport { ReiSigner } from \"./signer.js\";\n\n/**\n * Retrieves the Rei signer if available.\n * @param {ccc.Client} client - The client instance.\n * @returns {Signer | undefined} The Signer instance if the Rei provider is available, otherwise undefined.\n\n */\nexport function getReiSigners(client: ccc.Client): ccc.SignerInfo[] {\n  const windowRef = window as { rei?: { ckb: Provider } };\n\n  if (typeof windowRef?.rei?.ckb === \"undefined\") {\n    return [];\n  }\n  return [\n    {\n      signer: new ReiSigner(client, windowRef.rei.ckb),\n      name: \"CKB\",\n    },\n  ];\n}\n","export * from \"./signer.js\";\nexport * from \"./signersFactory.js\";\n"],"mappings":"sIASA,IAAa,EAAb,cAA+B,EAAI,MAAO,CAMxC,YACE,EACA,EACA,CACA,MAAM,CAAM,EAFI,KAAA,SAAA,CAGlB,CAQA,WAAW,EAAkC,CAC3C,IAAM,EAAuB,CAAC,EACxB,EAAW,SAAY,CAC3B,EAAS,EACT,EAAK,EAAE,GAAG,CACZ,EAQA,OAPA,EAAK,SAAW,CACd,KAAK,SAAS,IAAI,kBAAmB,CAAQ,EAC7C,KAAK,SAAS,IAAI,eAAgB,CAAQ,CAC5C,CAAC,EACD,KAAK,SAAS,GAAG,kBAAmB,CAAQ,EAC5C,KAAK,SAAS,GAAG,eAAgB,CAAQ,EAElC,EAAK,EACd,CAMA,IAAI,MAAuB,CACzB,OAAO,EAAI,WAAW,GACxB,CAMA,IAAI,UAA+B,CACjC,OAAO,EAAI,eAAe,YAC5B,CAMA,MAAM,SAAyB,CAC7B,IAAM,EAAe,KAAK,OAAO,cAEjC,GAAI,MADoB,KA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