{"version":3,"sources":["../../src/protocol/write-signer.ts"],"sourcesContent":["/**\n * Builder key abstraction shared by the Write API and lineage reads.\n *\n * @remarks\n * Everything the Personal Server asks a builder to sign is an EIP-191\n * `personal_sign` over a Web3Signed payload, so one `signMessage` callback is\n * the whole contract. {@link resolveWriteSigner} accepts the shapes a builder\n * already has: a viem `LocalAccount` (backend, `privateKeyToAccount`), a viem\n * `WalletClient` (browser wallet), or a bare `{ signMessage }` object.\n *\n * @category Protocol\n */\n\nimport type { Account, Address, Hex } from \"viem\";\nimport type { Web3SignedSignFn } from \"../auth/web3-signed-builder\";\nimport { WriteRequestError } from \"../errors\";\n\n/** The signer the Write API drives: an EIP-191 signature over a string. */\nexport interface WriteSigner {\n  /** The builder address, when known. Used for messages only. */\n  address?: Address;\n  /** EIP-191 (`personal_sign`) over the Web3Signed payload string. */\n  signMessage: Web3SignedSignFn;\n}\n\n/** The subset of a viem `LocalAccount` the Write API uses. */\nexport interface ViemWriteAccount {\n  address: Address;\n  type: \"local\";\n  signMessage(args: { message: string }): Promise<Hex>;\n}\n\n/** The subset of a viem `WalletClient` the Write API uses. */\nexport interface ViemWriteWalletClient {\n  type?: string;\n  transport?: unknown;\n  account?: Account | undefined;\n  signMessage(args: {\n    account: Account | Address;\n    message: string;\n  }): Promise<Hex>;\n}\n\n/** Any signer {@link resolveWriteSigner} understands. */\nexport type WriteSignerSource =\n  | WriteSigner\n  | ViemWriteAccount\n  | ViemWriteWalletClient;\n\nexport interface ResolveWriteSignerOptions {\n  /**\n   * Account to sign with when the viem wallet client has no hoisted account\n   * (browser wallets). Ignored for other signer shapes.\n   */\n  account?: Account | Address;\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n  return value !== null && typeof value === \"object\";\n}\n\nfunction isViemWriteAccount(source: unknown): source is ViemWriteAccount {\n  return (\n    isRecord(source) &&\n    source.type === \"local\" &&\n    typeof source.address === \"string\" &&\n    typeof source.signMessage === \"function\"\n  );\n}\n\n/**\n * A viem client: `createWalletClient` stamps `type: \"walletClient\"`, and every\n * viem client carries a `transport`. Either marker is enough; a bare\n * `{ signMessage }` signer has neither, and a local account has\n * `type: \"local\"`.\n */\nfunction isViemWriteWalletClient(\n  source: unknown,\n): source is ViemWriteWalletClient {\n  return (\n    isRecord(source) &&\n    typeof source.signMessage === \"function\" &&\n    source.type !== \"local\" &&\n    (source.type === \"walletClient\" || \"transport\" in source)\n  );\n}\n\nfunction accountAddress(account: Account | Address): Address {\n  return typeof account === \"string\" ? account : account.address;\n}\n\n/**\n * Normalise a builder key into a {@link WriteSigner}.\n *\n * @param source - A viem `LocalAccount`, a viem `WalletClient`, or a\n *   `{ signMessage }` object (returned as-is).\n * @param options - `account` for a wallet client without a hoisted account.\n * @returns A signer whose `signMessage` produces EIP-191 signatures.\n * @throws {WriteRequestError} When a wallet client has no account to sign\n *   with, or the source exposes no `signMessage` function.\n */\nexport function resolveWriteSigner(\n  source: WriteSignerSource,\n  options: ResolveWriteSignerOptions = {},\n): WriteSigner {\n  if (isViemWriteWalletClient(source)) {\n    const account = options.account ?? source.account;\n    if (account === undefined) {\n      throw new WriteRequestError(\n        \"Viem wallet client requires an account option or account property\",\n      );\n    }\n    return {\n      address: accountAddress(account),\n      signMessage: (message) => source.signMessage({ account, message }),\n    };\n  }\n  if (isViemWriteAccount(source)) {\n    return {\n      address: source.address,\n      signMessage: (message) => source.signMessage({ message }),\n    };\n  }\n  if (!isRecord(source) || typeof source.signMessage !== \"function\") {\n    throw new WriteRequestError(\n      \"signer must be a viem LocalAccount, a viem WalletClient, or a { signMessage } object\",\n    );\n  }\n  return source as WriteSigner;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAeA,oBAAkC;AA0ClC,SAAS,SAAS,OAAkD;AAClE,SAAO,UAAU,QAAQ,OAAO,UAAU;AAC5C;AAEA,SAAS,mBAAmB,QAA6C;AACvE,SACE,SAAS,MAAM,KACf,OAAO,SAAS,WAChB,OAAO,OAAO,YAAY,YAC1B,OAAO,OAAO,gBAAgB;AAElC;AAQA,SAAS,wBACP,QACiC;AACjC,SACE,SAAS,MAAM,KACf,OAAO,OAAO,gBAAgB,cAC9B,OAAO,SAAS,YACf,OAAO,SAAS,kBAAkB,eAAe;AAEtD;AAEA,SAAS,eAAe,SAAqC;AAC3D,SAAO,OAAO,YAAY,WAAW,UAAU,QAAQ;AACzD;AAYO,SAAS,mBACd,QACA,UAAqC,CAAC,GACzB;AACb,MAAI,wBAAwB,MAAM,GAAG;AACnC,UAAM,UAAU,QAAQ,WAAW,OAAO;AAC1C,QAAI,YAAY,QAAW;AACzB,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AACA,WAAO;AAAA,MACL,SAAS,eAAe,OAAO;AAAA,MAC/B,aAAa,CAAC,YAAY,OAAO,YAAY,EAAE,SAAS,QAAQ,CAAC;AAAA,IACnE;AAAA,EACF;AACA,MAAI,mBAAmB,MAAM,GAAG;AAC9B,WAAO;AAAA,MACL,SAAS,OAAO;AAAA,MAChB,aAAa,CAAC,YAAY,OAAO,YAAY,EAAE,QAAQ,CAAC;AAAA,IAC1D;AAAA,EACF;AACA,MAAI,CAAC,SAAS,MAAM,KAAK,OAAO,OAAO,gBAAgB,YAAY;AACjE,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,SAAO;AACT;","names":[]}