{"version":3,"sources":["../../src/protocol/personal-server-registration.ts"],"sourcesContent":["/**\n * Personal Server registration typed-data and signing helpers.\n *\n * These helpers are protocol-owned and runtime-neutral. Apps can sign with\n * viem local accounts, wallet clients, Account products, or any equivalent\n * signer by adapting to {@link PersonalServerRegistrationSigner}.\n *\n * @category Protocol\n */\n\nimport {\n  isAddress,\n  type Account,\n  type Address,\n  type Hex,\n  type TypedDataDomain,\n  type TypedDataDefinition,\n} from \"viem\";\nimport {\n  SERVER_REGISTRATION_TYPES,\n  serverRegistrationDomain,\n  type DataPortabilityGatewayConfig,\n  type ServerRegistrationMessage,\n} from \"./eip712\";\n\nexport const PERSONAL_SERVER_REGISTRATION_DEFAULT_CHAIN_ID = 1480;\nexport const PERSONAL_SERVER_REGISTRATION_DEFAULT_VERIFYING_CONTRACT =\n  \"0x1483B1F634DBA75AeaE60da7f01A679aabd5ee2c\" as const;\n\nexport type PersonalServerRegistrationTypedData = TypedDataDefinition<\n  typeof SERVER_REGISTRATION_TYPES,\n  \"ServerRegistration\"\n> & {\n  message: ServerRegistrationMessage;\n};\n\nexport interface PersonalServerRegistrationSigner {\n  address: Address;\n  signTypedData(\n    typedData: PersonalServerRegistrationTypedData,\n  ): Promise<Hex> | Hex;\n}\n\nexport interface ViemPersonalServerRegistrationWalletClient {\n  account?: Account | Address | null;\n  signTypedData(\n    typedData: PersonalServerRegistrationTypedData & {\n      account?: Account | Address;\n    },\n  ): Promise<Hex>;\n}\n\nexport type ViemPersonalServerRegistrationSignerSource =\n  | PersonalServerRegistrationSigner\n  | ViemPersonalServerRegistrationWalletClient;\n\nexport interface BuildPersonalServerRegistrationTypedDataInput {\n  ownerAddress: Address;\n  serverAddress: Address;\n  serverPublicKey: string;\n  serverUrl: string;\n  config?: DataPortabilityGatewayConfig;\n  chainId?: number;\n  verifyingContract?: Address;\n}\n\nexport interface BuildPersonalServerRegistrationSignatureInput {\n  signer: PersonalServerRegistrationSigner;\n  serverAddress: Address;\n  serverPublicKey: string;\n  serverUrl: string;\n  config?: DataPortabilityGatewayConfig;\n  chainId?: number;\n  verifyingContract?: Address;\n}\n\nexport interface PersonalServerRegistrationSignature {\n  signature: Hex;\n  signerAddress: Address;\n  typedData: PersonalServerRegistrationTypedData;\n}\n\nexport interface PersonalServerRegistrationDomainInput {\n  config?: DataPortabilityGatewayConfig;\n  chainId?: number;\n  verifyingContract?: Address;\n}\n\nfunction assertAddress(value: Address, name: string): void {\n  if (!isAddress(value)) {\n    throw new Error(`${name} must be a valid EVM address`);\n  }\n}\n\nfunction getAccountAddress(\n  account: Account | Address | null | undefined,\n): Address | undefined {\n  if (!account) {\n    return undefined;\n  }\n\n  return typeof account === \"string\" ? account : account.address;\n}\n\nfunction isPersonalServerRegistrationSigner(\n  source: ViemPersonalServerRegistrationSignerSource,\n): source is PersonalServerRegistrationSigner {\n  return \"address\" in source && typeof source.signTypedData === \"function\";\n}\n\nexport function createViemPersonalServerRegistrationSigner(\n  source: ViemPersonalServerRegistrationSignerSource,\n  options: { account?: Account | Address } = {},\n): PersonalServerRegistrationSigner {\n  if (isPersonalServerRegistrationSigner(source)) {\n    return source;\n  }\n\n  const accountAddress =\n    getAccountAddress(options.account) ?? getAccountAddress(source.account);\n\n  if (accountAddress) {\n    return {\n      address: accountAddress,\n      signTypedData: (typedData) =>\n        source.signTypedData({\n          ...typedData,\n          account: options.account ?? source.account ?? accountAddress,\n        }),\n    };\n  }\n\n  throw new Error(\n    \"Viem wallet client requires an account option or account property\",\n  );\n}\n\nexport function personalServerRegistrationDomain(\n  input: PersonalServerRegistrationDomainInput = {},\n): TypedDataDomain {\n  if (input.config) {\n    return serverRegistrationDomain(input.config);\n  }\n\n  const verifyingContract =\n    input.verifyingContract ??\n    PERSONAL_SERVER_REGISTRATION_DEFAULT_VERIFYING_CONTRACT;\n  assertAddress(verifyingContract, \"verifyingContract\");\n\n  return {\n    name: \"Vana Data Portability\",\n    version: \"1\",\n    chainId: input.chainId ?? PERSONAL_SERVER_REGISTRATION_DEFAULT_CHAIN_ID,\n    verifyingContract,\n  };\n}\n\nexport function buildPersonalServerRegistrationTypedData(\n  input: BuildPersonalServerRegistrationTypedDataInput,\n): PersonalServerRegistrationTypedData {\n  assertAddress(input.ownerAddress, \"ownerAddress\");\n  assertAddress(input.serverAddress, \"serverAddress\");\n\n  return {\n    domain: personalServerRegistrationDomain(input),\n    types: SERVER_REGISTRATION_TYPES,\n    primaryType: \"ServerRegistration\",\n    message: {\n      ownerAddress: input.ownerAddress,\n      serverAddress: input.serverAddress,\n      publicKey: input.serverPublicKey,\n      serverUrl: input.serverUrl,\n    },\n  };\n}\n\nexport async function buildPersonalServerRegistrationSignature(\n  input: BuildPersonalServerRegistrationSignatureInput,\n): Promise<PersonalServerRegistrationSignature> {\n  const typedData = buildPersonalServerRegistrationTypedData({\n    ownerAddress: input.signer.address,\n    serverAddress: input.serverAddress,\n    serverPublicKey: input.serverPublicKey,\n    serverUrl: input.serverUrl,\n    config: input.config,\n    chainId: input.chainId,\n    verifyingContract: input.verifyingContract,\n  });\n  const signature = await input.signer.signTypedData(typedData);\n\n  return {\n    signature,\n    signerAddress: input.signer.address,\n    typedData,\n  };\n}\n\nexport const registerPersonalServerSignature =\n  buildPersonalServerRegistrationSignature;\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAUA,kBAOO;AACP,oBAKO;AAEA,MAAM,gDAAgD;AACtD,MAAM,0DACX;AA6DF,SAAS,cAAc,OAAgB,MAAoB;AACzD,MAAI,KAAC,uBAAU,KAAK,GAAG;AACrB,UAAM,IAAI,MAAM,GAAG,IAAI,8BAA8B;AAAA,EACvD;AACF;AAEA,SAAS,kBACP,SACqB;AACrB,MAAI,CAAC,SAAS;AACZ,WAAO;AAAA,EACT;AAEA,SAAO,OAAO,YAAY,WAAW,UAAU,QAAQ;AACzD;AAEA,SAAS,mCACP,QAC4C;AAC5C,SAAO,aAAa,UAAU,OAAO,O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