{"version":3,"sources":["../../src/protocol/eip712.ts"],"sourcesContent":["/**\n * EIP-712 domain and type builders for Data Portability protocol writes.\n *\n * These helpers are shared primitives only. Personal Server runtimes own when\n * to sign and submit these payloads.\n *\n * @category Protocol\n */\n\nimport type { TypedDataDomain } from \"viem\";\n\nconst DOMAIN_NAME = \"Vana Data Portability\";\nconst DOMAIN_VERSION = \"1\";\n\nexport interface DataPortabilityContracts {\n  dataRegistry: string;\n  dataPortabilityPermissions: string;\n  dataPortabilityServer: string;\n  dataPortabilityGrantees: string;\n  // DataPortabilityEscrow — verifies GENERIC_PAYMENT_TYPES signatures backing\n  // /v1/escrow/pay (the data-access payment path).\n  dataPortabilityEscrow: string;\n  // FeeRegistry — per-operation `{amount, asset, payee, enabled}` records\n  // keyed on `keccak256(name)`. The gateway re-resolves fees against this\n  // contract on every /v1/escrow/pay so the SDK has to read the same\n  // source of truth to size payment amounts.\n  feeRegistry: string;\n}\n\n// Native VANA asset sentinel used by /v1/escrow/pay's `asset` field — pay any\n// other ERC-20 by passing its contract address instead.\nexport const NATIVE_VANA_ASSET =\n  \"0x0000000000000000000000000000000000000000\" as const;\n\nexport interface DataPortabilityGatewayConfig {\n  chainId: number;\n  contracts: DataPortabilityContracts;\n}\n\nfunction buildDomain(\n  chainId: number,\n  verifyingContract: `0x${string}`,\n): TypedDataDomain {\n  return {\n    name: DOMAIN_NAME,\n    version: DOMAIN_VERSION,\n    chainId,\n    verifyingContract,\n  };\n}\n\n// Domain for the DataRegistryV2 contract — verifies the AddData and\n// RecordDataAccess EIP-712 signatures. The v2 contract dropped the file\n// concept entirely in favor of versioned data points keyed on (owner,\n// scope); the primaryType distinguishes the two flows.\nexport function dataRegistryDomain(\n  config: DataPortabilityGatewayConfig,\n): TypedDataDomain {\n  return buildDomain(\n    config.chainId,\n    config.contracts.dataRegistry as `0x${string}`,\n  );\n}\n\nexport function grantRegistrationDomain(\n  config: DataPortabilityGatewayConfig,\n): TypedDataDomain {\n  return buildDomain(\n    config.chainId,\n    config.contracts.dataPortabilityPermissions as `0x${string}`,\n  );\n}\n\nexport function grantRevocationDomain(\n  config: DataPortabilityGatewayConfig,\n): TypedDataDomain {\n  return buildDomain(\n    config.chainId,\n    config.contracts.dataPortabilityPermissions as `0x${string}`,\n  );\n}\n\nexport function serverRegistrationDomain(\n  config: DataPortabilityGatewayConfig,\n): TypedDataDomain {\n  return buildDomain(\n    config.chainId,\n    config.contracts.dataPortabilityServer as `0x${string}`,\n  );\n}\n\nexport function builderRegistrationDomain(\n  config: DataPortabilityGatewayConfig,\n): TypedDataDomain {\n  return buildDomain(\n    config.chainId,\n    config.contracts.dataPortabilityGrantees as `0x${string}`,\n  );\n}\n\n// Domain for the generic-payment EIP-712 signature consumed by\n// /v1/escrow/pay. The verifyingContract is the escrow itself (not the per-op\n// contract), so a single signature debits the payer's escrow balance for any\n// supported op — `grant` today; file/builder/schema in the future.\nexport function escrowPaymentDomain(\n  config: DataPortabilityGatewayConfig,\n): TypedDataDomain {\n  return buildDomain(\n    config.chainId,\n    config.contracts.dataPortabilityEscrow as `0x${string}`,\n  );\n}\n\n/**\n * Domain for a gateway-authorized escrow withdrawal.\n *\n * Withdrawals use the escrow contract as their verifying contract, just like\n * generic payments. The distinct primary type prevents a payment signature\n * from authorizing a withdrawal.\n */\nexport function withdrawAuthorizationDomain(\n  config: DataPortabilityGatewayConfig,\n): TypedDataDomain {\n  return escrowPaymentDomain(config);\n}\n\n// grantVersion is a monotonic uint256 nonce per (grantor, grantee) pair. The\n// gateway rejects any registration whose version is <= the stored value,\n// which is the replay-attack defence now that re-registering the same pair\n// is a permitted override. expiresAt is unix seconds; 0 = no expiry.\nexport const GRANT_REGISTRATION_TYPES = {\n  GrantRegistration: [\n    { name: \"grantorAddress\", type: \"address\" },\n    { name: \"granteeId\", type: \"bytes32\" },\n    { name: \"scopes\", type: \"string[]\" },\n    { name: \"grantVersion\", type: \"uint256\" },\n    { name: \"expiresAt\", type: \"uint256\" },\n  ],\n} as const;\n\n// Revocation shares the (grantor, grantee) monotonic nonce with registration —\n// both events advance the same grantVersion counter so an old revocation sig\n// can't be replayed across a revoke → re-register cycle.\nexport const GRANT_REVOCATION_TYPES = {\n  GrantRevocation: [\n    { name: \"grantorAddress\", type: \"address\" },\n    { name: \"grantId\", type: \"bytes32\" },\n    { name: \"grantVersion\", type: \"uint256\" },\n  ],\n} as const;\n\nexport const SERVER_REGISTRATION_TYPES = {\n  ServerRegistration: [\n    { name: \"ownerAddress\", type: \"address\" },\n    { name: \"serverAddress\", type: \"address\" },\n    { name: \"publicKey\", type: \"string\" },\n    { name: \"serverUrl\", type: \"string\" },\n  ],\n} as const;\n\nexport const BUILDER_REGISTRATION_TYPES = {\n  BuilderRegistration: [\n    { name: \"ownerAddress\", type: \"address\" },\n    { name: \"granteeAddress\", type: \"address\" },\n    { name: \"publicKey\", type: \"string\" },\n    { name: \"appUrl\", type: \"string\" },\n  ],\n} as const;\n\n// Generic payment for the escrow flow. The (opType, opId) pair routes the\n// debit to the right op-row; paymentNonce is per-payer monotonic so the same\n// signed message can't be replayed after a revoke + re-register cycle.\n//\n// Legacy grant payments bind opId to grantId; standalone data_access payments\n// bind opId to the accompanying accessRecord.recordId.\nexport const GENERIC_PAYMENT_TYPES = {\n  GenericPayment: [\n    { name: \"payerAddress\", type: \"address\" },\n    { name: \"opType\", type: \"string\" },\n    { name: \"opId\", type: \"bytes32\" },\n    { name: \"asset\", type: \"address\" },\n    { name: \"amount\", type: \"uint256\" },\n    { name: \"paymentNonce\", type: \"uint256\" },\n  ],\n} as const;\n\n/**\n * Authorization consumed by `POST /v1/escrow/withdraw`.\n *\n * The current escrow contract always pays `account` itself. Do not add an\n * unsigned recipient field: a future recipient capability must be introduced\n * as a new, signed protocol type after the contract and gateway support it.\n */\nexport const WITHDRAW_AUTHORIZATION_TYPES = {\n  WithdrawAuthorization: [\n    { name: \"account\", type: \"address\" },\n    { name: \"asset\", type: \"address\" },\n    { name: \"amount\", type: \"uint256\" },\n    { name: \"withdrawNonce\", type: \"uint256\" },\n    { name: \"deadline\", type: \"uint256\" },\n  ],\n} as const;\n\n// AddData is signed by the data point's owner — registers (scope, dataHash,\n// metadataHash) on DataRegistryV2. expectedVersion is the version the caller\n// believes is current; the contract rejects with a CAS error if it isn't.\n// Used at POST /v1/data.\nexport const ADD_DATA_TYPES = {\n  AddData: [\n    { name: \"ownerAddress\", type: \"address\" },\n    { name: \"scope\", type: \"string\" },\n    { name: \"dataHash\", type: \"bytes32\" },\n    { name: \"metadataHash\", type: \"bytes32\" },\n    { name: \"expectedVersion\", type: \"uint256\" },\n  ],\n} as const;\n\n// RecordDataAccess is signed by a *trusted personal server* of `ownerAddress`\n// — attests that (scope, version) was served to `accessor`. recordId is a\n// per-event bytes32 the contract pins in `_usedRecordIds` to prevent replay.\n// Used as the optional `accessRecord` on POST /v1/escrow/pay.\nexport const RECORD_DATA_ACCESS_TYPES = {\n  RecordDataAccess: [\n    { name: \"ownerAddress\", type: \"address\" },\n    { name: \"scope\", type: \"string\" },\n    { name: \"version\", type: \"uint256\" },\n    { name: \"accessor\", type: \"address\" },\n    { name: \"recordId\", type: \"bytes32\" },\n  ],\n} as const;\n\nexport interface GrantRegistrationMessage {\n  grantorAddress: `0x${string}`;\n  granteeId: `0x${string}`;\n  scopes: string[];\n  grantVersion: bigint;\n  expiresAt: bigint;\n}\n\nexport interface GrantRevocationMessage {\n  grantorAddress: `0x${string}`;\n  grantId: `0x${string}`;\n  grantVersion: bigint;\n}\n\nexport interface ServerRegistrationMessage {\n  ownerAddress: `0x${string}`;\n  serverAddress: `0x${string}`;\n  publicKey: string;\n  serverUrl: string;\n}\n\nexport interface BuilderRegistrationMessage {\n  ownerAddress: `0x${string}`;\n  granteeAddress: `0x${string}`;\n  publicKey: string;\n  appUrl: string;\n}\n\nexport interface GenericPaymentMessage {\n  payerAddress: `0x${string}`;\n  // 'grant' today — extensible to 'file' | 'builder' | 'schema' as those\n  // op-types become payable. Sent verbatim over the wire and into the\n  // typed-data string field, so callers must match the gateway's spelling.\n  opType: string;\n  opId: `0x${string}`;\n  // NATIVE_VANA_ASSET for native VANA; an ERC-20 contract address otherwise.\n  asset: `0x${string}`;\n  amount: bigint;\n  paymentNonce: bigint;\n}\n\n/** EIP-712 message authorizing a withdrawal of an account's escrow balance. */\nexport interface WithdrawAuthorizationMessage {\n  /** The escrow account to debit and the withdrawal recipient. */\n  account: `0x${string}`;\n  /** Native VANA sentinel or the ERC-20 asset contract. */\n  asset: `0x${string}`;\n  /** Base-unit amount. */\n  amount: bigint;\n  /** Caller-managed, strictly increasing nonce for newly accepted intents. */\n  withdrawNonce: bigint;\n  /** Unix seconds. Bounds first acceptance; exact accepted retries remain valid. */\n  deadline: bigint;\n}\n\n/** Builds the typed data a wallet must sign before calling the withdraw API. */\nexport function buildWithdrawAuthorizationTypedData(\n  config: DataPortabilityGatewayConfig,\n  message: WithdrawAuthorizationMessage,\n) {\n  return {\n    domain: withdrawAuthorizationDomain(config),\n    types: WITHDRAW_AUTHORIZATION_TYPES,\n    primaryType: \"WithdrawAuthorization\" as const,\n    message,\n  };\n}\n\nexport interface AddDataMessage {\n  ownerAddress: `0x${string}`;\n  scope: string;\n  dataHash: `0x${string}`;\n  metadataHash: `0x${string}`;\n  expectedVersion: bigint;\n}\n\nexport interface RecordDataAccessMessage {\n  ownerAddress: `0x${string}`;\n  scope: string;\n  version: bigint;\n  accessor: `0x${string}`;\n  recordId: `0x${string}`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWA,MAAM,cAAc;AACpB,MAAM,iBAAiB;AAmBhB,MAAM,oBACX;AAOF,SAAS,YACP,SACA,mBACiB;AACjB,SAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,IACT;AAAA,IACA;AAAA,EACF;AACF;AAMO,SAAS,mBACd,Q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