{"version":3,"file":"barrel.mjs","names":[],"sources":["../src/signer.ts","../src/signersFactory.ts","../src/barrel.ts"],"sourcesContent":["import { ccc } from \"@ckb-ccc/core\";\nimport {\n  Connection,\n  ConnectionsRepo,\n  ConnectionsRepoLocalStorage,\n} from \"./connectionsStorage/index.js\";\nimport { Provider } from \"./nip07.advanced.js\";\n\n/**\n * Error thrown when the wallet signs with a different account than the restored\n * NIP-07 connection.\n * @public\n */\nexport class NostrAccountChangedError extends Error {\n  constructor() {\n    super(\"The active Nostr account has changed. Please reconnect the wallet.\");\n    this.name = \"NostrAccountChangedError\";\n  }\n}\n\n/**\n * @public\n */\nexport class Signer extends ccc.SignerNostr {\n  private connection?: Connection;\n\n  constructor(\n    client: ccc.Client,\n    public readonly provider: Provider,\n    private readonly connectionsRepo: ConnectionsRepo = new ConnectionsRepoLocalStorage(),\n  ) {\n    super(client);\n  }\n\n  private async assertConnection(): Promise<Connection> {\n    if (!(await this.isConnected()) || !this.connection) {\n      throw new Error(\"Not connected\");\n    }\n\n    return this.connection;\n  }\n\n  private async saveConnection(): Promise<void> {\n    await this.connectionsRepo.set(this.connection);\n  }\n\n  private async restoreConnection(): Promise<void> {\n    this.connection = await this.connectionsRepo.get();\n  }\n\n  async getNostrPublicKey(): Promise<ccc.Hex> {\n    return (await this.assertConnection()).publicKey;\n  }\n\n  async signNostrEvent(\n    event: ccc.NostrEvent,\n  ): Promise<Required<ccc.NostrEvent>> {\n    const expectedPublicKey = await this.getNostrPublicKey();\n    const signedEvent = await this.provider.signEvent({\n      ...event,\n      pubkey: expectedPublicKey.slice(2),\n    });\n\n    try {\n      if (ccc.hexFrom(signedEvent.pubkey) !== expectedPublicKey) {\n        throw new Error(\"Public key mismatch\");\n      }\n    } catch {\n      this.connection = undefined;\n      await this.saveConnection();\n      throw new NostrAccountChangedError();\n    }\n\n    return signedEvent;\n  }\n\n  async connect(): Promise<void> {\n    try {\n      this.connection = {\n        publicKey: ccc.hexFrom(await this.provider.getPublicKey()),\n      };\n      await this.saveConnection();\n    } catch (error) {\n      this.connection = undefined;\n      await this.saveConnection();\n      throw error;\n    }\n  }\n\n  async disconnect(): Promise<void> {\n    await super.disconnect();\n\n    this.connection = undefined;\n    await this.saveConnection();\n  }\n\n  async isConnected(): Promise<boolean> {\n    if (this.connection) {\n      return true;\n    }\n\n    await this.restoreConnection();\n    return this.connection !== undefined;\n  }\n}\n","import { ccc } from \"@ckb-ccc/core\";\nimport { Provider } from \"./nip07.advanced.js\";\nimport { Signer } from \"./signer.js\";\n\n/**\n * @public\n */\nexport function getNip07Signer(client: ccc.Client): Signer | undefined {\n  const windowRef = window as { nostr?: Provider };\n\n  if (typeof windowRef.nostr === \"undefined\") {\n    return;\n  }\n\n  return new Signer(client, windowRef.nostr);\n}\n","export * from \"./signer.js\";\nexport * from \"./signersFactory.js\";\n"],"mappings":"yIAaA,IAAa,EAAb,cAA8C,KAAM,CAClD,aAAc,CACZ,MAAM,oEAAoE,EAC1E,KAAK,KAAO,0BACd,CACF,EAKa,EAAb,cAA4B,EAAI,WAAY,CAG1C,YACE,EACA,EACA,EAAoD,IAAI,EACxD,CACA,MAAM,CAAM,EAHI,KAAA,SAAA,EACC,KAAA,gBAAA,CAGnB,CAEA,MAAc,kBAAwC,CACpD,GAAI,CAAE,MAAM,KAAK,YAAY,GAAM,CAAC,KAAK,WACvC,MAAU,MAAM,eAAe,EAGjC,OAAO,KAAK,UACd,CAEA,MAAc,gBAAgC,CAC5C,MAAM,KAAK,gBAAgB,IAAI,KAAK,UAAU,CAChD,CAEA,MAAc,mBAAmC,CAC/C,KAAK,WAAa,MAAM,KAAK,gBAAgB,IAAI,CACnD,CAEA,MAAM,mBAAsC,CAC1C,OAAQ,MAAM,KAAK,iBAAiB,EAAA,CAAG,SACzC,CAEA,MAAM,eACJ,EACmC,CACnC,IAAM,EAAoB,MAAM,KAAK,kBAAkB,EACjD,EAAc,MAAM,KAAK,SAAS,UAAU,CAChD,GAAG,EACH,OAAQ,EAAkB,MAAM,CAA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