@opendatalabs/vana-sdk 0.1.0-alpha.fd33fc9 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.cjs.map +1 -1
- package/dist/browser.d.ts +33 -1
- package/dist/browser.js.map +1 -1
- package/dist/chains/index.cjs.map +1 -1
- package/dist/chains/index.d.ts +30 -1
- package/dist/chains/index.js.map +1 -1
- package/dist/client/__tests__/enhancedResponse.test.d.ts +1 -0
- package/dist/client/enhancedResponse.cjs +164 -0
- package/dist/client/enhancedResponse.cjs.map +1 -0
- package/dist/client/enhancedResponse.d.ts +120 -0
- package/dist/client/enhancedResponse.js +138 -0
- package/dist/client/enhancedResponse.js.map +1 -0
- package/dist/config/chains.cjs.map +1 -1
- package/dist/config/chains.d.ts +99 -0
- package/dist/config/chains.js.map +1 -1
- package/dist/contracts/contractController.cjs.map +1 -1
- package/dist/contracts/contractController.d.ts +66 -10
- package/dist/contracts/contractController.js.map +1 -1
- package/dist/controllers/__tests__/data-consistency-integration.test.d.ts +7 -0
- package/dist/controllers/__tests__/operations.processQueue.test.d.ts +1 -0
- package/dist/controllers/base.cjs +33 -0
- package/dist/controllers/base.cjs.map +1 -1
- package/dist/controllers/base.d.ts +10 -0
- package/dist/controllers/base.js +33 -0
- package/dist/controllers/base.js.map +1 -1
- package/dist/controllers/data.cjs +417 -276
- package/dist/controllers/data.cjs.map +1 -1
- package/dist/controllers/data.d.ts +246 -193
- package/dist/controllers/data.js +430 -279
- package/dist/controllers/data.js.map +1 -1
- package/dist/controllers/operations.cjs +430 -0
- package/dist/controllers/operations.cjs.map +1 -0
- package/dist/controllers/operations.d.ts +229 -0
- package/dist/controllers/operations.js +406 -0
- package/dist/controllers/operations.js.map +1 -0
- package/dist/controllers/permissions.cjs +690 -209
- package/dist/controllers/permissions.cjs.map +1 -1
- package/dist/controllers/permissions.d.ts +196 -68
- package/dist/controllers/permissions.js +690 -209
- package/dist/controllers/permissions.js.map +1 -1
- package/dist/controllers/protocol.cjs.map +1 -1
- package/dist/controllers/protocol.d.ts +27 -28
- package/dist/controllers/protocol.js.map +1 -1
- package/dist/controllers/schemas.cjs +104 -25
- package/dist/controllers/schemas.cjs.map +1 -1
- package/dist/controllers/schemas.d.ts +88 -40
- package/dist/controllers/schemas.js +104 -25
- package/dist/controllers/schemas.js.map +1 -1
- package/dist/controllers/server.cjs +269 -58
- package/dist/controllers/server.cjs.map +1 -1
- package/dist/controllers/server.d.ts +157 -52
- package/dist/controllers/server.js +269 -58
- package/dist/controllers/server.js.map +1 -1
- package/dist/core/__tests__/health.test.d.ts +1 -0
- package/dist/core/__tests__/inMemoryNonceManager.test.d.ts +1 -0
- package/dist/core/__tests__/nonceManager.test.d.ts +1 -0
- package/dist/core/__tests__/pollingManager.test.d.ts +4 -0
- package/dist/core/apiClient.cjs +53 -3
- package/dist/core/apiClient.cjs.map +1 -1
- package/dist/core/apiClient.d.ts +132 -7
- package/dist/core/apiClient.js +53 -3
- package/dist/core/apiClient.js.map +1 -1
- package/dist/core/generics.cjs +30 -3
- package/dist/core/generics.cjs.map +1 -1
- package/dist/core/generics.d.ts +95 -6
- package/dist/core/generics.js +30 -3
- package/dist/core/generics.js.map +1 -1
- package/dist/core/health.cjs +289 -0
- package/dist/core/health.cjs.map +1 -0
- package/dist/core/health.d.ts +143 -0
- package/dist/core/health.js +265 -0
- package/dist/core/health.js.map +1 -0
- package/dist/core/inMemoryNonceManager.cjs +138 -0
- package/dist/core/inMemoryNonceManager.cjs.map +1 -0
- package/dist/core/inMemoryNonceManager.d.ts +69 -0
- package/dist/core/inMemoryNonceManager.js +114 -0
- package/dist/core/inMemoryNonceManager.js.map +1 -0
- package/dist/core/nonceManager.cjs +304 -0
- package/dist/core/nonceManager.cjs.map +1 -0
- package/dist/core/nonceManager.d.ts +116 -0
- package/dist/core/nonceManager.js +280 -0
- package/dist/core/nonceManager.js.map +1 -0
- package/dist/core/pollingManager.cjs +292 -0
- package/dist/core/pollingManager.cjs.map +1 -0
- package/dist/core/pollingManager.d.ts +120 -0
- package/dist/core/pollingManager.js +268 -0
- package/dist/core/pollingManager.js.map +1 -0
- package/dist/core.cjs +55 -1
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.ts +54 -3
- package/dist/core.js +55 -1
- package/dist/core.js.map +1 -1
- package/dist/crypto/ecies/base.cjs +16 -3
- package/dist/crypto/ecies/base.cjs.map +1 -1
- package/dist/crypto/ecies/base.js +16 -3
- package/dist/crypto/ecies/base.js.map +1 -1
- package/dist/errors.cjs +29 -0
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.ts +64 -0
- package/dist/errors.js +28 -0
- package/dist/errors.js.map +1 -1
- package/dist/generated/abi/ComputeInstructionRegistryImplementation.cjs.map +1 -1
- package/dist/generated/abi/ComputeInstructionRegistryImplementation.js.map +1 -1
- package/dist/generated/abi/DLPPerformanceImplementation.cjs +42 -0
- package/dist/generated/abi/DLPPerformanceImplementation.cjs.map +1 -1
- package/dist/generated/abi/DLPPerformanceImplementation.d.ts +32 -0
- package/dist/generated/abi/DLPPerformanceImplementation.js +42 -0
- package/dist/generated/abi/DLPPerformanceImplementation.js.map +1 -1
- package/dist/generated/abi/DLPRegistryImplementation.cjs +5 -5
- package/dist/generated/abi/DLPRegistryImplementation.cjs.map +1 -1
- package/dist/generated/abi/DLPRegistryImplementation.d.ts +4 -4
- package/dist/generated/abi/DLPRegistryImplementation.js +5 -5
- package/dist/generated/abi/DLPRegistryImplementation.js.map +1 -1
- package/dist/generated/abi/DLPRewardDeployerImplementation.cjs +166 -2
- package/dist/generated/abi/DLPRewardDeployerImplementation.cjs.map +1 -1
- package/dist/generated/abi/DLPRewardDeployerImplementation.d.ts +129 -2
- package/dist/generated/abi/DLPRewardDeployerImplementation.js +166 -2
- package/dist/generated/abi/DLPRewardDeployerImplementation.js.map +1 -1
- package/dist/generated/abi/DataPortabilityGranteesImplementation.cjs +167 -19
- package/dist/generated/abi/DataPortabilityGranteesImplementation.cjs.map +1 -1
- package/dist/generated/abi/DataPortabilityGranteesImplementation.d.ts +127 -14
- package/dist/generated/abi/DataPortabilityGranteesImplementation.js +167 -19
- package/dist/generated/abi/DataPortabilityGranteesImplementation.js.map +1 -1
- package/dist/generated/abi/DataPortabilityPermissionsImplementation.cjs +0 -19
- package/dist/generated/abi/DataPortabilityPermissionsImplementation.cjs.map +1 -1
- package/dist/generated/abi/DataPortabilityPermissionsImplementation.d.ts +0 -14
- package/dist/generated/abi/DataPortabilityPermissionsImplementation.js +0 -19
- package/dist/generated/abi/DataPortabilityPermissionsImplementation.js.map +1 -1
- package/dist/generated/abi/DataPortabilityServersImplementation.cjs +0 -19
- package/dist/generated/abi/DataPortabilityServersImplementation.cjs.map +1 -1
- package/dist/generated/abi/DataPortabilityServersImplementation.d.ts +0 -14
- package/dist/generated/abi/DataPortabilityServersImplementation.js +0 -19
- package/dist/generated/abi/DataPortabilityServersImplementation.js.map +1 -1
- package/dist/generated/abi/DataRegistryImplementation.cjs +0 -13
- package/dist/generated/abi/DataRegistryImplementation.cjs.map +1 -1
- package/dist/generated/abi/DataRegistryImplementation.d.ts +0 -10
- package/dist/generated/abi/DataRegistryImplementation.js +0 -13
- package/dist/generated/abi/DataRegistryImplementation.js.map +1 -1
- package/dist/generated/abi/SwapHelperImplementation.cjs +0 -43
- package/dist/generated/abi/SwapHelperImplementation.cjs.map +1 -1
- package/dist/generated/abi/SwapHelperImplementation.d.ts +0 -35
- package/dist/generated/abi/SwapHelperImplementation.js +0 -43
- package/dist/generated/abi/SwapHelperImplementation.js.map +1 -1
- package/dist/generated/abi/VanaEpochImplementation.cjs +195 -0
- package/dist/generated/abi/VanaEpochImplementation.cjs.map +1 -1
- package/dist/generated/abi/VanaEpochImplementation.d.ts +151 -0
- package/dist/generated/abi/VanaEpochImplementation.js +195 -0
- package/dist/generated/abi/VanaEpochImplementation.js.map +1 -1
- package/dist/generated/abi/VanaPoolEntityImplementation.cjs +22 -65
- package/dist/generated/abi/VanaPoolEntityImplementation.cjs.map +1 -1
- package/dist/generated/abi/VanaPoolEntityImplementation.d.ts +17 -51
- package/dist/generated/abi/VanaPoolEntityImplementation.js +22 -65
- package/dist/generated/abi/VanaPoolEntityImplementation.js.map +1 -1
- package/dist/generated/abi/VanaPoolStakingImplementation.cjs +113 -1
- package/dist/generated/abi/VanaPoolStakingImplementation.cjs.map +1 -1
- package/dist/generated/abi/VanaPoolStakingImplementation.d.ts +85 -1
- package/dist/generated/abi/VanaPoolStakingImplementation.js +113 -1
- package/dist/generated/abi/VanaPoolStakingImplementation.js.map +1 -1
- package/dist/generated/abi/index.d.ts +546 -146
- package/dist/generated/event-types.cjs.map +1 -1
- package/dist/generated/event-types.d.ts +14 -8
- package/dist/generated/eventRegistry.cjs +42 -18
- package/dist/generated/eventRegistry.cjs.map +1 -1
- package/dist/generated/eventRegistry.js +42 -18
- package/dist/generated/eventRegistry.js.map +1 -1
- package/dist/generated/server/server-exports.cjs +22 -0
- package/dist/generated/server/server-exports.cjs.map +1 -1
- package/dist/generated/server/server-exports.d.ts +27 -10
- package/dist/generated/server/server-exports.js +17 -0
- package/dist/generated/server/server-exports.js.map +1 -1
- package/dist/generated/server/server.cjs.map +1 -1
- package/dist/generated/server/server.d.ts +771 -402
- package/dist/generated/subgraph.cjs +797 -32
- package/dist/generated/subgraph.cjs.map +1 -1
- package/dist/generated/subgraph.d.ts +135 -0
- package/dist/generated/subgraph.js +792 -32
- package/dist/generated/subgraph.js.map +1 -1
- package/dist/index.browser.d.ts +2 -0
- package/dist/index.browser.js +10 -0
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.node.cjs +26 -0
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.d.ts +49 -5
- package/dist/index.node.js +25 -1
- package/dist/index.node.js.map +1 -1
- package/dist/lib/__tests__/redisAtomicStore.test.d.ts +1 -0
- package/dist/lib/redisAtomicStore.cjs +201 -0
- package/dist/lib/redisAtomicStore.cjs.map +1 -0
- package/dist/lib/redisAtomicStore.d.ts +120 -0
- package/dist/lib/redisAtomicStore.js +177 -0
- package/dist/lib/redisAtomicStore.js.map +1 -0
- package/dist/node.cjs.map +1 -1
- package/dist/node.d.ts +39 -1
- package/dist/node.js.map +1 -1
- package/dist/platform/browser.cjs +160 -2
- package/dist/platform/browser.cjs.map +1 -1
- package/dist/platform/browser.d.ts +232 -12
- package/dist/platform/browser.js +160 -2
- package/dist/platform/browser.js.map +1 -1
- package/dist/platform/interface.cjs.map +1 -1
- package/dist/platform/interface.d.ts +283 -90
- package/dist/platform/node.cjs +163 -2
- package/dist/platform/node.cjs.map +1 -1
- package/dist/platform/node.d.ts +69 -6
- package/dist/platform/node.js +163 -2
- package/dist/platform/node.js.map +1 -1
- package/dist/server/relayerHandler.cjs +315 -81
- package/dist/server/relayerHandler.cjs.map +1 -1
- package/dist/server/relayerHandler.d.ts +35 -2
- package/dist/server/relayerHandler.js +315 -81
- package/dist/server/relayerHandler.js.map +1 -1
- package/dist/storage/index.cjs +3 -0
- package/dist/storage/index.cjs.map +1 -1
- package/dist/storage/index.d.ts +1 -0
- package/dist/storage/index.js +2 -0
- package/dist/storage/index.js.map +1 -1
- package/dist/storage/manager.cjs +108 -25
- package/dist/storage/manager.cjs.map +1 -1
- package/dist/storage/manager.d.ts +119 -25
- package/dist/storage/manager.js +108 -25
- package/dist/storage/manager.js.map +1 -1
- package/dist/storage/providers/callback-storage.cjs +86 -15
- package/dist/storage/providers/callback-storage.cjs.map +1 -1
- package/dist/storage/providers/callback-storage.d.ts +109 -20
- package/dist/storage/providers/callback-storage.js +86 -15
- package/dist/storage/providers/callback-storage.js.map +1 -1
- package/dist/storage/providers/dropbox.cjs +237 -0
- package/dist/storage/providers/dropbox.cjs.map +1 -0
- package/dist/storage/providers/dropbox.d.ts +39 -0
- package/dist/storage/providers/dropbox.js +215 -0
- package/dist/storage/providers/dropbox.js.map +1 -0
- package/dist/storage/providers/dropbox.test.d.ts +1 -0
- package/dist/storage/providers/pinata.cjs.map +1 -1
- package/dist/storage/providers/pinata.d.ts +12 -14
- package/dist/storage/providers/pinata.js.map +1 -1
- package/dist/tests/data-upload-owner-validation.test.d.ts +1 -0
- package/dist/tests/permissions-transaction-options.test.d.ts +1 -0
- package/dist/types/atomicStore.cjs +31 -0
- package/dist/types/atomicStore.cjs.map +1 -0
- package/dist/types/atomicStore.d.ts +236 -0
- package/dist/types/atomicStore.js +7 -0
- package/dist/types/atomicStore.js.map +1 -0
- package/dist/types/blockchain.cjs.map +1 -1
- package/dist/types/blockchain.d.ts +39 -11
- package/dist/types/chains.cjs.map +1 -1
- package/dist/types/chains.d.ts +74 -7
- package/dist/types/chains.js.map +1 -1
- package/dist/types/config.cjs.map +1 -1
- package/dist/types/config.d.ts +38 -4
- package/dist/types/config.js.map +1 -1
- package/dist/types/contracts.cjs.map +1 -1
- package/dist/types/contracts.d.ts +71 -7
- package/dist/types/controller-context.cjs.map +1 -1
- package/dist/types/controller-context.d.ts +4 -1
- package/dist/types/data.cjs.map +1 -1
- package/dist/types/data.d.ts +11 -10
- package/dist/types/generics.cjs.map +1 -1
- package/dist/types/generics.d.ts +81 -10
- package/dist/types/index.cjs.map +1 -1
- package/dist/types/index.d.ts +31 -3
- package/dist/types/index.js.map +1 -1
- package/dist/types/operationStore.cjs +17 -0
- package/dist/types/operationStore.cjs.map +1 -0
- package/dist/types/operationStore.d.ts +171 -0
- package/dist/types/operationStore.js +1 -0
- package/dist/types/operationStore.js.map +1 -0
- package/dist/types/operations.cjs +3 -15
- package/dist/types/operations.cjs.map +1 -1
- package/dist/types/operations.d.ts +131 -39
- package/dist/types/operations.js +2 -13
- package/dist/types/operations.js.map +1 -1
- package/dist/types/options.cjs +17 -0
- package/dist/types/options.cjs.map +1 -0
- package/dist/types/options.d.ts +308 -0
- package/dist/types/options.js +1 -0
- package/dist/types/options.js.map +1 -0
- package/dist/types/permissions.cjs.map +1 -1
- package/dist/types/permissions.d.ts +19 -20
- package/dist/types/personal.cjs.map +1 -1
- package/dist/types/personal.d.ts +150 -14
- package/dist/types/relayer.cjs.map +1 -1
- package/dist/types/relayer.d.ts +145 -24
- package/dist/types/storage.cjs.map +1 -1
- package/dist/types/storage.d.ts +9 -21
- package/dist/types/storage.js.map +1 -1
- package/dist/types/utils.cjs.map +1 -1
- package/dist/types/utils.d.ts +0 -45
- package/dist/utils/__tests__/chainQuery.test.d.ts +1 -0
- package/dist/utils/__tests__/subgraphConsistency.test.d.ts +4 -0
- package/dist/utils/__tests__/subgraphPagination.test.d.ts +4 -0
- package/dist/utils/chainQuery.cjs +107 -0
- package/dist/utils/chainQuery.cjs.map +1 -0
- package/dist/utils/chainQuery.d.ts +31 -0
- package/dist/utils/chainQuery.js +82 -0
- package/dist/utils/chainQuery.js.map +1 -0
- package/dist/utils/grantFiles.cjs +4 -1
- package/dist/utils/grantFiles.cjs.map +1 -1
- package/dist/utils/grantFiles.d.ts +10 -20
- package/dist/utils/grantFiles.js +4 -1
- package/dist/utils/grantFiles.js.map +1 -1
- package/dist/utils/grantValidation.cjs.map +1 -1
- package/dist/utils/grantValidation.d.ts +95 -16
- package/dist/utils/grantValidation.js.map +1 -1
- package/dist/utils/grants.cjs.map +1 -1
- package/dist/utils/grants.d.ts +93 -12
- package/dist/utils/grants.js.map +1 -1
- package/dist/utils/ipfs.cjs +2 -4
- package/dist/utils/ipfs.cjs.map +1 -1
- package/dist/utils/ipfs.d.ts +1 -1
- package/dist/utils/ipfs.js +2 -4
- package/dist/utils/ipfs.js.map +1 -1
- package/dist/utils/lazy-import.cjs.map +1 -1
- package/dist/utils/lazy-import.d.ts +32 -7
- package/dist/utils/lazy-import.js.map +1 -1
- package/dist/utils/signatureCache.cjs +8 -2
- package/dist/utils/signatureCache.cjs.map +1 -1
- package/dist/utils/signatureCache.d.ts +49 -8
- package/dist/utils/signatureCache.js +8 -2
- package/dist/utils/signatureCache.js.map +1 -1
- package/dist/utils/subgraphConsistency.cjs +184 -0
- package/dist/utils/subgraphConsistency.cjs.map +1 -0
- package/dist/utils/subgraphConsistency.d.ts +65 -0
- package/dist/utils/subgraphConsistency.js +155 -0
- package/dist/utils/subgraphConsistency.js.map +1 -0
- package/dist/utils/subgraphMetaCache.cjs +101 -0
- package/dist/utils/subgraphMetaCache.cjs.map +1 -0
- package/dist/utils/subgraphMetaCache.d.ts +56 -0
- package/dist/utils/subgraphMetaCache.js +76 -0
- package/dist/utils/subgraphMetaCache.js.map +1 -0
- package/dist/utils/subgraphPagination.cjs +104 -0
- package/dist/utils/subgraphPagination.cjs.map +1 -0
- package/dist/utils/subgraphPagination.d.ts +78 -0
- package/dist/utils/subgraphPagination.js +78 -0
- package/dist/utils/subgraphPagination.js.map +1 -0
- package/dist/utils/transactionHelpers.cjs.map +1 -1
- package/dist/utils/transactionHelpers.d.ts +12 -12
- package/dist/utils/transactionHelpers.js.map +1 -1
- package/dist/utils/typedDataConverter.cjs.map +1 -1
- package/dist/utils/typedDataConverter.d.ts +39 -3
- package/dist/utils/typedDataConverter.js.map +1 -1
- package/dist/utils/urlResolver.cjs +7 -0
- package/dist/utils/urlResolver.cjs.map +1 -1
- package/dist/utils/urlResolver.d.ts +22 -4
- package/dist/utils/urlResolver.js +7 -0
- package/dist/utils/urlResolver.js.map +1 -1
- package/dist/utils/wallet.cjs.map +1 -1
- package/dist/utils/wallet.d.ts +78 -16
- package/dist/utils/wallet.js.map +1 -1
- package/package.json +3 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/grantFiles.ts"],"sourcesContent":["import { keccak256, toHex } from \"viem\";\nimport type { GrantFile, GrantPermissionParams } from \"../types/permissions\";\nimport { SerializationError, NetworkError } from \"../errors\";\n\ninterface GrantFileStorageResponse {\n success: boolean;\n error?: string;\n url?: string;\n}\n\n/**\n * Creates a grant file structure from permission parameters.\n *\n * @remarks\n * This function constructs the JSON structure that represents a permission grant\n * in the Vana protocol. The grant file contains all necessary information for\n * a grantee to perform operations on behalf of the grantor.\n *\n * @param params - The permission parameters to create the grant file from\n * @returns The constructed grant file object\n * @example\n * ```typescript\n * const grantFile = createGrantFile({\n * grantee: '0x742d35Cc6558Fd4D9e9E0E888F0462ef6919Bd36',\n * operation: 'llm_inference',\n * parameters: {\n * model: 'gpt-4',\n * maxTokens: 1000\n * },\n * expiresAt: Date.now() + 86400000 // 24 hours\n * });\n *\n * console.log(grantFile);\n * // {\n * // grantee: '0x742d...',\n * // operation: 'llm_inference',\n * // parameters: { model: 'gpt-4', maxTokens: 1000 },\n * // expires: 1234567890\n * // }\n * ```\n */\nexport function createGrantFile(params: GrantPermissionParams): GrantFile {\n const grantFile: GrantFile = {\n grantee: params.grantee,\n operation: params.operation,\n parameters: params.parameters,\n };\n\n // Add expiration if provided\n if (params.expiresAt) {\n grantFile.expires = params.expiresAt;\n }\n\n return grantFile;\n}\n\n/**\n * Stores a grant file in IPFS via the relayer service.\n *\n * @remarks\n * This function uploads the grant file to IPFS through the relayer's upload endpoint.\n * The returned URL can be stored on-chain as part of the permission grant, allowing\n * anyone to retrieve the detailed permission parameters later.\n *\n * @param grantFile - The grant file to store\n * @param relayerUrl - URL of the relayer service\n * @returns Promise resolving to the IPFS URL\n * @throws {NetworkError} When the upload fails or relayer is unavailable\n * @example\n * ```typescript\n * const grantFile = createGrantFile(params);\n *\n * try {\n * const ipfsUrl = await storeGrantFile(grantFile, 'https://relayer.vana.com');\n * console.log(`Grant file stored at: ${ipfsUrl}`);\n * // ipfsUrl: \"ipfs://QmHash123...\"\n * } catch (error) {\n * console.error('Failed to store grant file:', error);\n * }\n * ```\n */\nexport async function storeGrantFile(\n grantFile: GrantFile,\n relayerUrl: string,\n): Promise<string> {\n try {\n // Convert grant file to blob and use IPFS upload endpoint\n const grantFileBlob = new Blob([JSON.stringify(grantFile, null, 2)], {\n type: \"application/json\",\n });\n\n const formData = new FormData();\n formData.append(\"file\", grantFileBlob, \"grant-file.json\");\n\n const response = await fetch(`${relayerUrl}/api/ipfs/upload`, {\n method: \"POST\",\n body: formData,\n });\n\n if (!response.ok) {\n throw new NetworkError(\n `Failed to store grant file: ${response.statusText}`,\n new Error(`HTTP ${response.status}`),\n );\n }\n\n const responseData: unknown = await response.json();\n const data = responseData as GrantFileStorageResponse;\n\n if (!data.success) {\n throw new NetworkError(data.error ?? \"Failed to store grant file\");\n }\n\n if (!data.url) {\n throw new NetworkError(\"Upload succeeded but no URL was returned\");\n }\n return data.url; // The IPFS URL from the upload response\n } catch (error) {\n if (error instanceof NetworkError) {\n throw error;\n }\n throw new NetworkError(\n `Network error while storing grant file: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n error as Error,\n );\n }\n}\n\n/**\n * Retrieves detailed grant file data from IPFS or HTTP storage.\n *\n * @remarks\n * **This is Step 2 of the performant two-step permission API.**\n *\n * Use this method to resolve detailed permission data (operation, parameters, etc.)\n * for specific grants after first getting the fast on-chain data using\n * `getUserPermissionGrantsOnChain()`. This design eliminates N+1 query problems\n * by allowing selective lazy-loading of expensive off-chain data.\n *\n * **Performance**: Single network request per grant file (typically 100-500ms).\n * **Reliability**: Tries multiple IPFS gateways as fallbacks if primary URL fails.\n *\n * @param grantUrl - The grant file URL from OnChainPermissionGrant.grantUrl\n * @param _relayerUrl - URL of the relayer service (optional, unused)\n * @param downloadRelayer - Optional download relayer for proxying CORS-restricted downloads\n * @param downloadRelayer.proxyDownload - Function to proxy download requests through application server\n * @returns Promise resolving to the complete grant file with operation details\n * @throws {NetworkError} When all retrieval attempts fail\n * @throws {SerializationError} When grant file format is invalid\n * @example\n * ```typescript\n * // Step 1: Fast on-chain data (no N+1 queries)\n * const grants = await vana.permissions.getUserPermissionGrantsOnChain();\n *\n * // Step 2: Lazy-load details for specific grant when needed\n * const grantFile = await retrieveGrantFile(grants[0].grantUrl);\n *\n * console.log(`Operation: ${grantFile.operation}`);\n * console.log(`Grantee: ${grantFile.grantee}`);\n * console.log(`Parameters:`, grantFile.parameters);\n *\n * // Only fetch details for grants user actually wants to see\n * for (const grant of selectedGrants) {\n * const details = await retrieveGrantFile(grant.grantUrl);\n * displayGrantDetails(details);\n * }\n * ```\n */\nexport async function retrieveGrantFile(\n grantUrl: string,\n _relayerUrl?: string,\n downloadRelayer?: { proxyDownload: (url: string) => Promise<Blob> },\n): Promise<GrantFile> {\n try {\n // Check if the URL is a gateway URL instead of ipfs:// protocol\n if (grantUrl.startsWith(\"http\") && grantUrl.includes(\"/ipfs/\")) {\n console.warn(\n `⚠️ Grant URL uses HTTP gateway format instead of ipfs:// protocol. ` +\n `Found: ${grantUrl}. ` +\n `Consider using ipfs:// format for better protocol-agnostic storage.`,\n );\n }\n\n // Use the unified download utility\n const { universalFetch } = await import(\"./download\");\n const response = await universalFetch(grantUrl, downloadRelayer);\n\n if (!response.ok) {\n throw new NetworkError(\n `Failed to retrieve grant file: HTTP ${response.status}`,\n );\n }\n\n const text = await response.text();\n const grantFile = JSON.parse(text);\n\n if (!validateGrantFile(grantFile)) {\n throw new NetworkError(`Invalid grant file format from ${grantUrl}`);\n }\n\n return grantFile;\n } catch (error) {\n if (error instanceof NetworkError) {\n throw error;\n }\n throw new NetworkError(\n `Error retrieving grant file: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n error as Error,\n );\n }\n}\n\n/**\n * Generates a content hash for a grant file.\n * This can be used for integrity verification.\n *\n * @remarks\n * Creates a deterministic keccak256 hash of the grant file by first sorting\n * all object keys recursively to ensure consistent hashing regardless of\n * property order. This hash can be used to verify grant file integrity\n * or as a unique identifier.\n *\n * @param grantFile - The grant file to generate a hash for\n * @returns The keccak256 hash of the grant file as a hex string\n * @throws {SerializationError} When the grant file cannot be serialized\n * @example\n * ```typescript\n * const grantFile = {\n * grantee: '0x742d35Cc6558Fd4D9e9E0E888F0462ef6919Bd36',\n * operation: 'read',\n * parameters: { version: '1.0', mode: 'full' }\n * };\n *\n * const hash = getGrantFileHash(grantFile);\n * console.log(`Grant file hash: ${hash}`);\n * // \"0x1234567890abcdef...\"\n *\n * // Same grant file with different property order produces same hash\n * const grantFile2 = {\n * operation: 'read',\n * parameters: { mode: 'full', version: '1.0' },\n * grantee: '0x742d35Cc6558Fd4D9e9E0E888F0462ef6919Bd36'\n * };\n *\n * const hash2 = getGrantFileHash(grantFile2);\n * console.log(hash === hash2); // true\n * ```\n */\nexport function getGrantFileHash(grantFile: GrantFile): string {\n try {\n // Create a stable JSON representation\n const sortedFile: GrantFile = {\n grantee: grantFile.grantee,\n operation: grantFile.operation,\n parameters: sortObjectKeys(grantFile.parameters) as Record<\n string,\n unknown\n >,\n };\n\n // Add expires if present\n if (grantFile.expires !== undefined) {\n sortedFile.expires = grantFile.expires;\n }\n\n const jsonString = JSON.stringify(sortedFile);\n console.info(`Hash: ${keccak256(toHex(jsonString))}`);\n return keccak256(toHex(jsonString));\n } catch (error) {\n throw new SerializationError(\n `Failed to generate grant file hash: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n );\n }\n}\n\n/**\n * Recursively sorts object keys for stable serialization.\n *\n * @param obj - The object to sort keys recursively\n * @returns The object with all keys sorted recursively\n */\nfunction sortObjectKeys(obj: unknown): unknown {\n if (obj === null || typeof obj !== \"object\") {\n return obj;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => sortObjectKeys(item));\n }\n\n const sortedObj: Record<string, unknown> = {};\n Object.keys(obj as Record<string, unknown>)\n .sort()\n .forEach((key) => {\n sortedObj[key] = sortObjectKeys((obj as Record<string, unknown>)[key]);\n });\n\n return sortedObj;\n}\n\n/**\n * Validates that a grant file has the required structure.\n *\n * @remarks\n * Performs runtime validation to ensure data conforms to the GrantFile interface.\n * Checks for required fields (grantee, operation, parameters) and validates their\n * types and formats. This is a type guard function that enables TypeScript to\n * narrow the type when it returns true.\n *\n * @param data - The data to validate as a grant file\n * @returns True if the data is a valid grant file, false otherwise\n * @example\n * ```typescript\n * const unknownData = await fetch(url).then(r => r.json());\n *\n * if (validateGrantFile(unknownData)) {\n * // TypeScript now knows unknownData is a GrantFile\n * console.log(`Grant for operation: ${unknownData.operation}`);\n * console.log(`Grantee: ${unknownData.grantee}`);\n * } else {\n * throw new Error('Invalid grant file format');\n * }\n *\n * // Validation examples:\n * validateGrantFile({\n * grantee: '0x742d35Cc6558Fd4D9e9E0E888F0462ef6919Bd36',\n * operation: 'read',\n * parameters: {}\n * }); // true\n *\n * validateGrantFile({\n * grantee: 'invalid-address',\n * operation: 'read',\n * parameters: {}\n * }); // false (invalid address format)\n *\n * validateGrantFile({\n * operation: 'read',\n * parameters: {}\n * }); // false (missing grantee)\n * ```\n */\nexport function validateGrantFile(data: unknown): data is GrantFile {\n if (!data || typeof data !== \"object\") {\n return false;\n }\n\n const obj = data as Record<string, unknown>;\n\n // Validate required fields\n // Validate grantee address\n if (\n typeof obj.grantee !== \"string\" ||\n !obj.grantee.match(/^0x[a-fA-F0-9]{40}$/)\n ) {\n return false;\n }\n\n if (typeof obj.operation !== \"string\" || obj.operation.length === 0) {\n return false;\n }\n\n // Files are no longer stored in grant files - they're tracked in the contract\n\n if (!obj.parameters || typeof obj.parameters !== \"object\") {\n return false;\n }\n\n // Validate optional expires field\n if (obj.expires !== undefined) {\n if (\n typeof obj.expires !== \"number\" ||\n obj.expires < 0 ||\n !Number.isInteger(obj.expires)\n ) {\n return false;\n }\n }\n\n return true;\n}\n"],"mappings":"AAAA,SAAS,WAAW,aAAa;AAEjC,SAAS,oBAAoB,oBAAoB;AAuC1C,SAAS,gBAAgB,QAA0C;AACxE,QAAM,YAAuB;AAAA,IAC3B,SAAS,OAAO;AAAA,IAChB,WAAW,OAAO;AAAA,IAClB,YAAY,OAAO;AAAA,EACrB;AAGA,MAAI,OAAO,WAAW;AACpB,cAAU,UAAU,OAAO;AAAA,EAC7B;AAEA,SAAO;AACT;AA2BA,eAAsB,eACpB,WACA,YACiB;AACjB,MAAI;AAEF,UAAM,gBAAgB,IAAI,KAAK,CAAC,KAAK,UAAU,WAAW,MAAM,CAAC,CAAC,GAAG;AAAA,MACnE,MAAM;AAAA,IACR,CAAC;AAED,UAAM,WAAW,IAAI,SAAS;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MAAM,qBAAqB,GACxC;AACA,WAAO;AAAA,EACT;AAEA,MAAI,OAAO,IAAI,cAAc,YAAY,IAAI,UAAU,WAAW,GAAG;AACnE,WAAO;AAAA,EACT;AAIA,MAAI,CAAC,IAAI,cAAc,OAAO,IAAI,eAAe,UAAU;AACzD,WAAO;AAAA,EACT;AAGA,MAAI,IAAI,YAAY,QAAW;AAC7B,QACE,OAAO,IAAI,YAAY,YACvB,IAAI,UAAU,KACd,CAAC,OAAO,UAAU,IAAI,OAAO,GAC7B;AACA,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/utils/grantFiles.ts"],"sourcesContent":["import { keccak256, toHex } from \"viem\";\nimport type { GrantFile, GrantPermissionParams } from \"../types/permissions\";\nimport { SerializationError, NetworkError } from \"../errors\";\n\ninterface GrantFileStorageResponse {\n success: boolean;\n error?: string;\n url?: string;\n}\n\n/**\n * Creates grant file structure for permission storage.\n *\n * @remarks\n * Constructs JSON structure that represents a permission grant\n * in the Vana protocol. The grant file contains all necessary information\n * for a grantee to perform operations on behalf of the grantor.\n *\n * @param params - Permission parameters to create the grant file from\n * @returns Grant file object for IPFS storage\n *\n * @example\n * ```typescript\n * const grant = createGrantFile({\n * grantee: '0x742d35Cc...',\n * operation: 'llm_inference',\n * parameters: { model: 'gpt-4' },\n * expiresAt: Date.now() + 86400000 // 24 hours\n * });\n * ```\n */\nexport function createGrantFile(params: GrantPermissionParams): GrantFile {\n const grantFile: GrantFile = {\n grantee: params.grantee,\n operation: params.operation,\n parameters: { ...params.parameters },\n };\n\n // Add filters to parameters if provided\n if (params.filters) {\n grantFile.parameters.filters = params.filters;\n }\n\n // Add expiration if provided\n if (params.expiresAt) {\n grantFile.expires = params.expiresAt;\n }\n\n return grantFile;\n}\n\n/**\n * Stores a grant file in IPFS via the relayer service.\n *\n * @remarks\n * This function uploads the grant file to IPFS through the relayer's upload endpoint.\n * The returned URL can be stored on-chain as part of the permission grant, allowing\n * anyone to retrieve the detailed permission parameters later.\n *\n * @param grantFile - The grant file to store\n * @param relayerUrl - URL of the relayer service\n * @returns Promise resolving to the IPFS URL\n * @throws {NetworkError} When the upload fails or relayer is unavailable\n * @example\n * ```typescript\n * const grantFile = createGrantFile(params);\n *\n * try {\n * const ipfsUrl = await storeGrantFile(grantFile, 'https://relayer.vana.com');\n * console.log(`Grant file stored at: ${ipfsUrl}`);\n * // ipfsUrl: \"ipfs://QmHash123...\"\n * } catch (error) {\n * console.error('Failed to store grant file:', error);\n * }\n * ```\n */\nexport async function storeGrantFile(\n grantFile: GrantFile,\n relayerUrl: string,\n): Promise<string> {\n try {\n // Convert grant file to blob and use IPFS upload endpoint\n const grantFileBlob = new Blob([JSON.stringify(grantFile, null, 2)], {\n type: \"application/json\",\n });\n\n const formData = new FormData();\n formData.append(\"file\", grantFileBlob, \"grant-file.json\");\n\n const response = await fetch(`${relayerUrl}/api/ipfs/upload`, {\n method: \"POST\",\n body: formData,\n });\n\n if (!response.ok) {\n throw new NetworkError(\n `Failed to store grant file: ${response.statusText}`,\n new Error(`HTTP ${response.status}`),\n );\n }\n\n const responseData: unknown = await response.json();\n const data = responseData as GrantFileStorageResponse;\n\n if (!data.success) {\n throw new NetworkError(data.error ?? \"Failed to store grant file\");\n }\n\n if (!data.url) {\n throw new NetworkError(\"Upload succeeded but no URL was returned\");\n }\n return data.url; // The IPFS URL from the upload response\n } catch (error) {\n if (error instanceof NetworkError) {\n throw error;\n }\n throw new NetworkError(\n `Network error while storing grant file: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n error as Error,\n );\n }\n}\n\n/**\n * Retrieves detailed grant file data from IPFS or HTTP storage.\n *\n * @remarks\n * **This is Step 2 of the performant two-step permission API.**\n *\n * Use this method to resolve detailed permission data (operation, parameters, etc.)\n * for specific grants after first getting the fast on-chain data using\n * `getUserPermissionGrantsOnChain()`. This design eliminates N+1 query problems\n * by allowing selective lazy-loading of expensive off-chain data.\n *\n * **Performance**: Single network request per grant file (typically 100-500ms).\n * **Reliability**: Tries multiple IPFS gateways as fallbacks if primary URL fails.\n *\n * @param grantUrl - The grant file URL from OnChainPermissionGrant.grantUrl\n * @param _relayerUrl - URL of the relayer service (optional, unused)\n * @param downloadRelayer - Optional download relayer for proxying CORS-restricted downloads\n * @param downloadRelayer.proxyDownload - Function to proxy download requests through application server\n * @returns Promise resolving to the complete grant file with operation details\n * @throws {NetworkError} When all retrieval attempts fail\n * @throws {SerializationError} When grant file format is invalid\n * @example\n * ```typescript\n * // Step 1: Fast on-chain data (no N+1 queries)\n * const grants = await vana.permissions.getUserPermissionGrantsOnChain();\n *\n * // Step 2: Lazy-load details for specific grant when needed\n * const grantFile = await retrieveGrantFile(grants[0].grantUrl);\n *\n * console.log(`Operation: ${grantFile.operation}`);\n * console.log(`Grantee: ${grantFile.grantee}`);\n * console.log(`Parameters:`, grantFile.parameters);\n *\n * // Only fetch details for grants user actually wants to see\n * for (const grant of selectedGrants) {\n * const details = await retrieveGrantFile(grant.grantUrl);\n * displayGrantDetails(details);\n * }\n * ```\n */\nexport async function retrieveGrantFile(\n grantUrl: string,\n _relayerUrl?: string,\n downloadRelayer?: { proxyDownload: (url: string) => Promise<Blob> },\n): Promise<GrantFile> {\n try {\n // Check if the URL is a gateway URL instead of ipfs:// protocol\n if (grantUrl.startsWith(\"http\") && grantUrl.includes(\"/ipfs/\")) {\n console.warn(\n `⚠️ Grant URL uses HTTP gateway format instead of ipfs:// protocol. ` +\n `Found: ${grantUrl}. ` +\n `Consider using ipfs:// format for better protocol-agnostic storage.`,\n );\n }\n\n // Use the unified download utility\n const { universalFetch } = await import(\"./download\");\n const response = await universalFetch(grantUrl, downloadRelayer);\n\n if (!response.ok) {\n throw new NetworkError(\n `Failed to retrieve grant file: HTTP ${response.status}`,\n );\n }\n\n const text = await response.text();\n const grantFile = JSON.parse(text);\n\n if (!validateGrantFile(grantFile)) {\n throw new NetworkError(`Invalid grant file format from ${grantUrl}`);\n }\n\n return grantFile;\n } catch (error) {\n if (error instanceof NetworkError) {\n throw error;\n }\n throw new NetworkError(\n `Error retrieving grant file: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n error as Error,\n );\n }\n}\n\n/**\n * Generates a content hash for a grant file.\n * This can be used for integrity verification.\n *\n * @remarks\n * Creates a deterministic keccak256 hash of the grant file by first sorting\n * all object keys recursively to ensure consistent hashing regardless of\n * property order. This hash can be used to verify grant file integrity\n * or as a unique identifier.\n *\n * @param grantFile - The grant file to generate a hash for\n * @returns The keccak256 hash of the grant file as a hex string\n * @throws {SerializationError} When the grant file cannot be serialized\n * @example\n * ```typescript\n * const grantFile = {\n * grantee: '0x742d35Cc6558Fd4D9e9E0E888F0462ef6919Bd36',\n * operation: 'read',\n * parameters: { version: '1.0', mode: 'full' }\n * };\n *\n * const hash = getGrantFileHash(grantFile);\n * console.log(`Grant file hash: ${hash}`);\n * // \"0x1234567890abcdef...\"\n *\n * // Same grant file with different property order produces same hash\n * const grantFile2 = {\n * operation: 'read',\n * parameters: { mode: 'full', version: '1.0' },\n * grantee: '0x742d35Cc6558Fd4D9e9E0E888F0462ef6919Bd36'\n * };\n *\n * const hash2 = getGrantFileHash(grantFile2);\n * console.log(hash === hash2); // true\n * ```\n */\nexport function getGrantFileHash(grantFile: GrantFile): string {\n try {\n // Create a stable JSON representation\n const sortedFile: GrantFile = {\n grantee: grantFile.grantee,\n operation: grantFile.operation,\n parameters: sortObjectKeys(grantFile.parameters) as Record<\n string,\n unknown\n >,\n };\n\n // Add expires if present\n if (grantFile.expires !== undefined) {\n sortedFile.expires = grantFile.expires;\n }\n\n const jsonString = JSON.stringify(sortedFile);\n console.info(`Hash: ${keccak256(toHex(jsonString))}`);\n return keccak256(toHex(jsonString));\n } catch (error) {\n throw new SerializationError(\n `Failed to generate grant file hash: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n );\n }\n}\n\n/**\n * Recursively sorts object keys for stable serialization.\n *\n * @param obj - The object to sort keys recursively\n * @returns The object with all keys sorted recursively\n */\nfunction sortObjectKeys(obj: unknown): unknown {\n if (obj === null || typeof obj !== \"object\") {\n return obj;\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => sortObjectKeys(item));\n }\n\n const sortedObj: Record<string, unknown> = {};\n Object.keys(obj as Record<string, unknown>)\n .sort()\n .forEach((key) => {\n sortedObj[key] = sortObjectKeys((obj as Record<string, unknown>)[key]);\n });\n\n return sortedObj;\n}\n\n/**\n * Validates that a grant file has the required structure.\n *\n * @remarks\n * Performs runtime validation to ensure data conforms to the GrantFile interface.\n * Checks for required fields (grantee, operation, parameters) and validates their\n * types and formats. This is a type guard function that enables TypeScript to\n * narrow the type when it returns true.\n *\n * @param data - The data to validate as a grant file\n * @returns True if the data is a valid grant file, false otherwise\n * @example\n * ```typescript\n * const unknownData = await fetch(url).then(r => r.json());\n *\n * if (validateGrantFile(unknownData)) {\n * // TypeScript now knows unknownData is a GrantFile\n * console.log(`Grant for operation: ${unknownData.operation}`);\n * console.log(`Grantee: ${unknownData.grantee}`);\n * } else {\n * throw new Error('Invalid grant file format');\n * }\n *\n * // Validation examples:\n * validateGrantFile({\n * grantee: '0x742d35Cc6558Fd4D9e9E0E888F0462ef6919Bd36',\n * operation: 'read',\n * parameters: {}\n * }); // true\n *\n * validateGrantFile({\n * grantee: 'invalid-address',\n * operation: 'read',\n * parameters: {}\n * }); // false (invalid address format)\n *\n * validateGrantFile({\n * operation: 'read',\n * parameters: {}\n * }); // false (missing grantee)\n * ```\n */\nexport function validateGrantFile(data: unknown): data is GrantFile {\n if (!data || typeof data !== \"object\") {\n return false;\n }\n\n const obj = data as Record<string, unknown>;\n\n // Validate required fields\n // Validate grantee address\n if (\n typeof obj.grantee !== \"string\" ||\n !obj.grantee.match(/^0x[a-fA-F0-9]{40}$/)\n ) {\n return false;\n }\n\n if (typeof obj.operation !== \"string\" || obj.operation.length === 0) {\n return false;\n }\n\n // Files are no longer stored in grant files - they're tracked in the contract\n\n if (!obj.parameters || typeof obj.parameters !== \"object\") {\n return false;\n }\n\n // Validate optional expires field\n if (obj.expires !== undefined) {\n if (\n typeof obj.expires !== \"number\" ||\n obj.expires < 0 ||\n !Number.isInteger(obj.expires)\n ) {\n return false;\n }\n }\n\n return true;\n}\n"],"mappings":"AAAA,SAAS,WAAW,aAAa;AAEjC,SAAS,oBAAoB,oBAAoB;AA6B1C,SAAS,gBAAgB,QAA0C;AACxE,QAAM,YAAuB;AAAA,IAC3B,SAAS,OAAO;AAAA,IAChB,WAAW,OAAO;AAAA,IAClB,YAAY,EAAE,GAAG,OAAO,WAAW;AAAA,EACrC;AAGA,MAAI,OAAO,SAAS;AAClB,cAAU,WAAW,UAAU,OAAO;AAAA,EACxC;AAGA,MAAI,OAAO,WAAW;AACpB,cAAU,UAAU,OAAO;AAAA,EAC7B;AAEA,SAAO;AACT;AA2BA,eAAsB,eACpB,WACA,YACiB;AACjB,MAAI;AAEF,UAAM,gBAAgB,IAAI,KAAK,CAAC,KAAK,UAAU,WAAW,MAAM,CAAC,CAAC,GAAG;AAAA,MACnE,MAAM;AAAA,IACR,CAAC;AAED,UAAM,WAAW,IAAI,SAAS;AAC9B,aAAS,OAAO,QAAQ,eAAe,iBAAiB;AAExD,UAAM,WAAW,MAAM,MAAM,GAAG,UAAU,oBAAoB;AAAA,MAC5D,QAAQ;AAAA,MACR,MAAM;AAAA,IACR,CAAC;AAED,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,IAAI;AAAA,QACR,+BAA+B,SAAS,UAAU;AAAA,QAClD,IAAI,MAAM,QAAQ,SAAS,MAAM,EAAE;AAAA,MACrC;AAAA,IACF;AAEA,UAAM,eAAwB,MAAM,SAAS,KAAK;AAClD,UAAM,OAAO;AAEb,QAAI,CAAC,KAAK,SAAS;AACjB,YAAM,IAAI,aAAa,KAAK,SAAS,4BAA4B;AAAA,IACnE;AAEA,QAAI,CAAC,KAAK,KAAK;AACb,YAAM,IAAI,aAAa,0CAA0C;AAAA,IACnE;AACA,WAAO,KAAK;AAAA,EACd,S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O,UAAU,MAAM,UAAU,CAAC;AAAA,EACpC,SAAS,OAAO;AACd,UAAM,IAAI;AAAA,MACR,uCAAuC,iBAAiB,QAAQ,MAAM,UAAU,eAAe;AAAA,IACjG;AAAA,EACF;AACF;AAQA,SAAS,eAAe,KAAuB;AAC7C,MAAI,QAAQ,QAAQ,OAAO,QAAQ,UAAU;AAC3C,WAAO;AAAA,EACT;AAEA,MAAI,MAAM,QAAQ,GAAG,GAAG;AACtB,WAAO,IAAI,IAAI,CAAC,SAAS,eAAe,IAAI,CAAC;AAAA,EAC/C;AAEA,QAAM,YAAqC,CAAC;AAC5C,SAAO,KAAK,GAA8B,EACvC,KAAK,EACL,QAAQ,CAAC,QAAQ;AAChB,cAAU,GAAG,IAAI,eAAgB,IAAgC,GAAG,CAAC;AAAA,EACvE,CAAC;AAEH,SAAO;AACT;AA4CO,SAAS,kBAAkB,MAAkC;AAClE,MAAI,CAAC,QAAQ,OAAO,SAAS,UAAU;AACrC,WAAO;AAAA,EACT;AAEA,QAAM,MAAM;AAIZ,MACE,OAAO,IAAI,YAAY,YACvB,CAAC,IAAI,QAAQ,MAAM,qBAAqB,GACxC;AACA,WAAO;AAAA,EACT;AAEA,MAAI,OAAO,IAAI,cAAc,YAAY,IAAI,UAAU,WAAW,GAAG;AACnE,WAAO;AAAA,EACT;AAIA,MAAI,CAAC,IAAI,cAAc,OAAO,IAAI,eAAe,UAAU;AACzD,WAAO;AAAA,EACT;AAGA,MAAI,IAAI,YAAY,QAAW;AAC7B,QACE,OAAO,IAAI,YAAY,YACvB,IAAI,UAAU,KACd,CAAC,OAAO,UAAU,IAAI,OAAO,GAC7B;AACA,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/grantValidation.ts"],"sourcesContent":["import type { Address } from \"viem\";\nimport { getAddress } from \"viem\";\nimport Ajv, { type ValidateFunction } from \"ajv\";\nimport addFormats from \"ajv-formats\";\nimport type { GrantFile } from \"../types/permissions\";\nimport grantFileSchema from \"../schemas/grantFile.schema.json\";\n\n/**\n * Base error class for grant validation failures\n *\n * @category Permissions\n */\nexport class GrantValidationError extends Error {\n constructor(\n message: string,\n public details?: Record<string, unknown>,\n ) {\n super(message);\n this.name = \"GrantValidationError\";\n }\n}\n\n/**\n * Error thrown when a grant has expired\n *\n * @category Permissions\n */\nexport class GrantExpiredError extends GrantValidationError {\n constructor(\n message: string,\n public expires: number,\n public currentTime: number,\n ) {\n super(message, { expires, currentTime });\n this.name = \"GrantExpiredError\";\n }\n}\n\n/**\n * Error thrown when grantee doesn't match requesting address\n *\n * @category Permissions\n */\nexport class GranteeMismatchError extends GrantValidationError {\n constructor(\n message: string,\n public grantee: Address,\n public requestingAddress: Address,\n ) {\n super(message, { grantee, requestingAddress });\n this.name = \"GranteeMismatchError\";\n }\n}\n\n/**\n * Error thrown when operation is not allowed by grant\n *\n * @category Permissions\n */\nexport class OperationNotAllowedError extends GrantValidationError {\n constructor(\n message: string,\n public grantedOperation: string,\n public requestedOperation: string,\n ) {\n super(message, { grantedOperation, requestedOperation });\n this.name = \"OperationNotAllowedError\";\n }\n}\n\n/**\n * Error thrown when grant file structure is invalid\n *\n * @category Permissions\n */\nexport class GrantSchemaError extends GrantValidationError {\n constructor(\n message: string,\n public schemaErrors: unknown[],\n public invalidData: unknown,\n ) {\n super(message, { errors: schemaErrors, data: invalidData });\n this.name = \"GrantSchemaError\";\n }\n}\n\n/**\n * Ajv instance for grant file validation with draft 2020-12 support\n */\nconst ajv = new Ajv({\n strict: true,\n removeAdditional: false,\n useDefaults: false,\n coerceTypes: false,\n});\n\n// Add format validators (email, date, etc.)\naddFormats(ajv);\n\n/**\n * Compiled grant file schema validator\n */\nconst validateGrantFileSchema: ValidateFunction = ajv.compile(grantFileSchema);\n\n/**\n * Options for grant validation\n *\n * @category Permissions\n */\nexport interface GrantValidationOptions {\n /** Enable JSON schema validation (default: true) */\n schema?: boolean;\n /** Grantee address to validate access for */\n grantee?: Address;\n /** Operation to validate permission for */\n operation?: string;\n /** Override current time for expiry checking (Unix timestamp) */\n currentTime?: number;\n /** Return detailed results instead of throwing (default: false) */\n throwOnError?: boolean;\n}\n\n/**\n * Detailed validation result\n *\n * @category Permissions\n */\nexport interface GrantValidationResult {\n /** Whether validation passed */\n valid: boolean;\n /** Validation errors encountered */\n errors: Array<{\n type: \"schema\" | \"business\";\n field?: string;\n message: string;\n error?: Error;\n }>;\n /** The validated grant file (if validation passed) */\n grant?: GrantFile;\n}\n\n/**\n * Validates a grant file with comprehensive schema and business rule checking.\n *\n * This function provides flexible validation with TypeScript overloads:\n * - When `throwOnError` is false (or `{ throwOnError: false }`), returns a detailed validation result\n * - When `throwOnError` is true (default), throws specific errors or returns the validated grant\n *\n * @param data - The grant file data to validate (unknown type for safety)\n * @param options - Validation options including grantee, operation, files, etc.\n * @returns Either a GrantFile (when throwing) or GrantValidationResult (when not throwing)\n * @throws {GrantSchemaError} When the grant file structure is invalid\n * @throws {GrantExpiredError} When the grant has expired\n * @throws {GranteeMismatchError} When the grantee doesn't match the requesting address\n * @throws {OperationNotAllowedError} When the requested operation is not allowed\n * @example\n * ```typescript\n * // Throwing mode (default) - returns GrantFile or throws\n * const grant = validateGrant(data, {\n * grantee: '0x123...',\n * operation: 'llm_inference',\n * });\n *\n * // Non-throwing mode - returns validation result\n * const result = validateGrant(data, {\n * grantee: '0x123...',\n * throwOnError: false\n * });\n * if (result.valid) {\n * console.log('Grant is valid:', result.grant);\n * } else {\n * console.log('Validation errors:', result.errors);\n * }\n * ```\n */\n\nexport function validateGrant(\n data: unknown,\n options: GrantValidationOptions & { throwOnError: false },\n): GrantValidationResult;\n\nexport function validateGrant(\n data: unknown,\n options?:\n | Omit<GrantValidationOptions, \"throwOnError\">\n | (GrantValidationOptions & { throwOnError?: true }),\n): GrantFile;\n\n/**\n * Implementation function for grant validation with flexible return types\n *\n * @param data - The grant file data to validate\n * @param options - Validation configuration options\n * @returns Either a GrantFile or GrantValidationResult depending on throwOnError setting\n */\nexport function validateGrant(\n data: unknown,\n options: GrantValidationOptions = {},\n): GrantFile | GrantValidationResult {\n const {\n schema = true,\n grantee,\n operation,\n currentTime,\n throwOnError = true,\n } = options;\n\n const errors: GrantValidationResult[\"errors\"] = [];\n let grant: GrantFile | undefined;\n\n // 1. Schema Validation\n if (schema) {\n try {\n if (validateGrantFileSchema(data)) {\n grant = data as GrantFile;\n } else {\n throw new GrantValidationError(\"Invalid grant file schema\");\n }\n } catch (error) {\n if (error instanceof GrantValidationError) {\n const schemaError = new GrantSchemaError(\n error.message,\n Array.isArray(error.details?.errors) ? error.details.errors : [],\n data,\n );\n errors.push({\n type: \"schema\",\n message: error.message,\n error: schemaError,\n });\n } else {\n errors.push({\n type: \"schema\",\n message: `Schema validation failed: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n error: error instanceof Error ? error : new Error(\"Unknown error\"),\n });\n }\n }\n } else {\n // Minimal type assertion if schema validation is skipped\n grant = data as GrantFile;\n }\n\n // 2. Business Logic Validation (only if we have a valid grant)\n if (grant) {\n // Check grantee access\n if (grantee) {\n try {\n validateGranteeAccess(grant, grantee);\n } catch (error) {\n const field = extractFieldFromBusinessError(error);\n errors.push({\n type: \"business\",\n field,\n message:\n error instanceof Error\n ? error.message\n : \"Unknown business rule error\",\n error: error instanceof Error ? error : new Error(\"Unknown error\"),\n });\n }\n }\n\n // Check expiration\n try {\n validateGrantExpiry(grant, currentTime);\n } catch (error) {\n const field = extractFieldFromBusinessError(error);\n errors.push({\n type: \"business\",\n field,\n message:\n error instanceof Error\n ? error.message\n : \"Unknown business rule error\",\n error: error instanceof Error ? error : new Error(\"Unknown error\"),\n });\n }\n\n // Check operation access\n if (operation) {\n try {\n validateOperationAccess(grant, operation);\n } catch (error) {\n const field = extractFieldFromBusinessError(error);\n errors.push({\n type: \"business\",\n field,\n message:\n error instanceof Error\n ? error.message\n : \"Unknown business rule error\",\n error: error instanceof Error ? error : new Error(\"Unknown error\"),\n });\n }\n }\n }\n\n // 3. Return Results\n if (errors.length > 0) {\n if (throwOnError) {\n // Throw the most specific error we have\n const firstError = errors[0];\n if (firstError.error) {\n throw firstError.error;\n } else {\n const combinedMessage = errors.map((e) => e.message).join(\"; \");\n throw new GrantValidationError(\n `Grant validation failed: ${combinedMessage}`,\n { errors, data },\n );\n }\n }\n\n return { valid: false, errors, grant };\n }\n\n if (throwOnError) {\n return grant as GrantFile;\n } else {\n return { valid: true, errors: [], grant: grant as GrantFile };\n }\n}\n\n/**\n * Helper function to extract field name from business validation errors\n *\n * @param error - The validation error to extract field information from\n * @returns The field name associated with the error, or undefined if not applicable\n */\nfunction extractFieldFromBusinessError(error: unknown): string | undefined {\n if (error instanceof GrantExpiredError) return \"expires\";\n if (error instanceof GranteeMismatchError) return \"grantee\";\n if (error instanceof OperationNotAllowedError) return \"operation\";\n return undefined;\n}\n\n/**\n * Validates that a grant file allows access for a specific grantee\n *\n * @param grantFile - The grant file to validate access for\n * @param requestingAddress - The address requesting access to check against the grantee\n */\nexport function validateGranteeAccess(\n grantFile: GrantFile,\n requestingAddress: Address,\n): void {\n const normalizedGrantee = getAddress(grantFile.grantee);\n const normalizedRequesting = getAddress(requestingAddress);\n\n if (normalizedGrantee !== normalizedRequesting) {\n throw new GranteeMismatchError(\n \"Permission denied: requesting address does not match grantee\",\n grantFile.grantee,\n requestingAddress,\n );\n }\n}\n\n/**\n * Validates that a grant has not expired (if expiry is set)\n *\n * @param grantFile - The grant file to check expiration for\n * @param currentTime - Optional override for current time (Unix timestamp)\n */\nexport function validateGrantExpiry(\n grantFile: GrantFile,\n currentTime?: number,\n): void {\n if (grantFile.expires) {\n const now =\n currentTime !== undefined ? currentTime : Math.floor(Date.now() / 1000); // Current Unix timestamp\n\n if (now > grantFile.expires) {\n throw new GrantExpiredError(\n \"Permission denied: grant has expired\",\n grantFile.expires,\n now,\n );\n }\n }\n}\n\n/**\n * Validates that a grant allows a specific operation\n *\n * @param grantFile - The grant file to validate operation access for\n * @param requestedOperation - The operation being requested to validate against the grant\n */\nexport function validateOperationAccess(\n grantFile: GrantFile,\n requestedOperation: string,\n): void {\n if (grantFile.operation !== requestedOperation) {\n throw new OperationNotAllowedError(\n \"Permission denied: operation not allowed by grant\",\n grantFile.operation,\n requestedOperation,\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,kBAA2B;AAC3B,iBAA2C;AAC3C,yBAAuB;AAEvB,8BAA4B;AAOrB,MAAM,6BAA6B,MAAM;AAAA,EAC9C,YACE,SACO,SACP;AACA,UAAM,OAAO;AAFN;AAGP,SAAK,OAAO;AAAA,EACd;AACF;AAOO,MAAM,0BAA0B,qBAAqB;AAAA,EAC1D,YACE,SACO,SACA,aACP;AACA,UAAM,SAAS,EAAE,SAAS,YAAY,CAAC;AAHhC;AACA;AAGP,SAAK,OAAO;AAAA,EACd;AACF;AAOO,MAAM,6BAA6B,qBAAqB;AAAA,EAC7D,YACE,SACO,SACA,mBACP;AACA,UAAM,SAAS,EAAE,SAAS,kBAAkB,CAAC;AAHtC;AACA;AAGP,SAAK,OAAO;AAAA,EACd;AACF;AAOO,MAAM,iCAAiC,qBAAqB;AAAA,EACjE,YACE,SACO,kBACA,oBACP;AACA,UAAM,SAAS,EAAE,kBAAkB,mBAAmB,CAAC;AAHhD;AACA;AAGP,SAAK,OAAO;AAAA,EACd;AACF;AAOO,MAAM,yBAAyB,qBAAqB;AAAA,EACzD,YACE,SACO,cACA,aACP;AACA,UAAM,SAAS,EAAE,QAAQ,cAAc,MAAM,YAAY,CAAC;AAHnD;AACA;AAGP,SAAK,OAAO;AAAA,EACd;AACF;AAKA,MAAM,MAAM,IAAI,WAAAA,QAAI;AAAA,EAClB,QAAQ;AAAA,EACR,kBAAkB;AAAA,EAClB,aAAa;AAAA,EACb,aAAa;AACf,CAAC;AAAA,IA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S,sBACd,WACA,mBACM;AACN,QAAM,wBAAoB,wBAAW,UAAU,OAAO;AACtD,QAAM,2BAAuB,wBAAW,iBAAiB;AAEzD,MAAI,sBAAsB,sBAAsB;AAC9C,UAAM,IAAI;AAAA,MACR;AAAA,MACA,UAAU;AAAA,MACV;AAAA,IACF;AAAA,EACF;AACF;AAQO,SAAS,oBACd,WACA,aACM;AACN,MAAI,UAAU,SAAS;AACrB,UAAM,MACJ,gBAAgB,SAAY,cAAc,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI;AAExE,QAAI,MAAM,UAAU,SAAS;AAC3B,YAAM,IAAI;AAAA,QACR;AAAA,QACA,UAAU;AAAA,QACV;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAQO,SAAS,wBACd,WACA,oBACM;AACN,MAAI,UAAU,cAAc,oBAAoB;AAC9C,UAAM,IAAI;AAAA,MACR;AAAA,MACA,UAAU;AAAA,MACV;AAAA,IACF;AAAA,EACF;AACF;","names":["Ajv","addFormats","grantFileSchema"]}
|
|
1
|
+
{"version":3,"sources":["../../src/utils/grantValidation.ts"],"sourcesContent":["/**\n * Provides comprehensive validation for permission grant files.\n *\n * @remarks\n * This module implements multi-layer validation for grant files including\n * JSON schema validation, business rule checking, expiration verification,\n * and access control validation. It provides both throwing and non-throwing\n * modes for flexible error handling.\n *\n * @category Permissions\n * @module utils/grantValidation\n */\n\nimport type { Address } from \"viem\";\nimport { getAddress } from \"viem\";\nimport Ajv, { type ValidateFunction } from \"ajv\";\nimport addFormats from \"ajv-formats\";\nimport type { GrantFile } from \"../types/permissions\";\nimport grantFileSchema from \"../schemas/grantFile.schema.json\";\n\n/**\n * Indicates a general grant validation failure.\n *\n * @remarks\n * Base class for all grant validation errors. Provides structured\n * error details for debugging and recovery.\n *\n * @category Permissions\n */\nexport class GrantValidationError extends Error {\n constructor(\n message: string,\n public details?: Record<string, unknown>,\n ) {\n super(message);\n this.name = \"GrantValidationError\";\n }\n}\n\n/**\n * Indicates that a grant has expired and is no longer valid.\n *\n * @remarks\n * Thrown when attempting to use a grant past its expiration timestamp.\n * Includes both the expiration time and current time for debugging.\n *\n * @category Permissions\n */\nexport class GrantExpiredError extends GrantValidationError {\n constructor(\n message: string,\n public expires: number,\n public currentTime: number,\n ) {\n super(message, { expires, currentTime });\n this.name = \"GrantExpiredError\";\n }\n}\n\n/**\n * Indicates that the requesting address doesn't match the grant's grantee.\n *\n * @remarks\n * Thrown when a user attempts to use a grant that was issued to a\n * different address. This prevents unauthorized use of permissions.\n *\n * @category Permissions\n */\nexport class GranteeMismatchError extends GrantValidationError {\n constructor(\n message: string,\n public grantee: Address,\n public requestingAddress: Address,\n ) {\n super(message, { grantee, requestingAddress });\n this.name = \"GranteeMismatchError\";\n }\n}\n\n/**\n * Indicates that the requested operation is not allowed by the grant.\n *\n * @remarks\n * Thrown when attempting an operation that differs from what the\n * grant authorizes. Includes both granted and requested operations.\n *\n * @category Permissions\n */\nexport class OperationNotAllowedError extends GrantValidationError {\n constructor(\n message: string,\n public grantedOperation: string,\n public requestedOperation: string,\n ) {\n super(message, { grantedOperation, requestedOperation });\n this.name = \"OperationNotAllowedError\";\n }\n}\n\n/**\n * Indicates that the grant file structure violates the JSON schema.\n *\n * @remarks\n * Thrown when a grant file doesn't conform to the expected schema.\n * Includes detailed schema validation errors and the invalid data.\n *\n * @category Permissions\n */\nexport class GrantSchemaError extends GrantValidationError {\n constructor(\n message: string,\n public schemaErrors: unknown[],\n public invalidData: unknown,\n ) {\n super(message, { errors: schemaErrors, data: invalidData });\n this.name = \"GrantSchemaError\";\n }\n}\n\n/**\n * Ajv instance configured for strict grant file validation.\n *\n * @internal\n */\nconst ajv = new Ajv({\n strict: true,\n removeAdditional: false,\n useDefaults: false,\n coerceTypes: false,\n});\n\n// Add format validators (email, date, etc.)\naddFormats(ajv);\n\n/**\n * Pre-compiled grant file schema validator for performance.\n *\n * @internal\n */\nconst validateGrantFileSchema: ValidateFunction = ajv.compile(grantFileSchema);\n\n/**\n * Configures grant validation behavior and scope.\n *\n * @remarks\n * Controls which validations to perform and how to handle errors.\n * Allows selective validation of specific aspects of a grant.\n *\n * @category Permissions\n */\nexport interface GrantValidationOptions {\n /** Enable JSON schema validation (default: true) */\n schema?: boolean;\n /** Grantee address to validate access for */\n grantee?: Address;\n /** Operation to validate permission for */\n operation?: string;\n /** Override current time for expiry checking (Unix timestamp) */\n currentTime?: number;\n /** Return detailed results instead of throwing (default: false) */\n throwOnError?: boolean;\n}\n\n/**\n * Represents the detailed result of grant validation.\n *\n * @remarks\n * Provides comprehensive validation results including all errors\n * encountered during validation. Used in non-throwing mode.\n *\n * @category Permissions\n */\nexport interface GrantValidationResult {\n /** Whether validation passed */\n valid: boolean;\n /** Validation errors encountered */\n errors: Array<{\n type: \"schema\" | \"business\";\n field?: string;\n message: string;\n error?: Error;\n }>;\n /** The validated grant file (if validation passed) */\n grant?: GrantFile;\n}\n\n/**\n * Validates a grant file with comprehensive schema and business rule checking.\n *\n * This function provides flexible validation with TypeScript overloads:\n * - When `throwOnError` is false (or `{ throwOnError: false }`), returns a detailed validation result\n * - When `throwOnError` is true (default), throws specific errors or returns the validated grant\n *\n * @param data - The grant file data to validate (unknown type for safety)\n * @param options - Validation options including grantee, operation, files, etc.\n * @returns Either a GrantFile (when throwing) or GrantValidationResult (when not throwing)\n * @throws {GrantSchemaError} When the grant file structure is invalid\n * @throws {GrantExpiredError} When the grant has expired\n * @throws {GranteeMismatchError} When the grantee doesn't match the requesting address\n * @throws {OperationNotAllowedError} When the requested operation is not allowed\n * @example\n * ```typescript\n * // Throwing mode (default) - returns GrantFile or throws\n * const grant = validateGrant(data, {\n * grantee: '0x123...',\n * operation: 'llm_inference',\n * });\n *\n * // Non-throwing mode - returns validation result\n * const result = validateGrant(data, {\n * grantee: '0x123...',\n * throwOnError: false\n * });\n * if (result.valid) {\n * console.log('Grant is valid:', result.grant);\n * } else {\n * console.log('Validation errors:', result.errors);\n * }\n * ```\n */\n\nexport function validateGrant(\n data: unknown,\n options: GrantValidationOptions & { throwOnError: false },\n): GrantValidationResult;\n\nexport function validateGrant(\n data: unknown,\n options?:\n | Omit<GrantValidationOptions, \"throwOnError\">\n | (GrantValidationOptions & { throwOnError?: true }),\n): GrantFile;\n\n/** @internal */\nexport function validateGrant(\n data: unknown,\n options: GrantValidationOptions = {},\n): GrantFile | GrantValidationResult {\n const {\n schema = true,\n grantee,\n operation,\n currentTime,\n throwOnError = true,\n } = options;\n\n const errors: GrantValidationResult[\"errors\"] = [];\n let grant: GrantFile | undefined;\n\n // 1. Schema Validation\n if (schema) {\n try {\n if (validateGrantFileSchema(data)) {\n grant = data as GrantFile;\n } else {\n throw new GrantValidationError(\"Invalid grant file schema\");\n }\n } catch (error) {\n if (error instanceof GrantValidationError) {\n const schemaError = new GrantSchemaError(\n error.message,\n Array.isArray(error.details?.errors) ? error.details.errors : [],\n data,\n );\n errors.push({\n type: \"schema\",\n message: error.message,\n error: schemaError,\n });\n } else {\n errors.push({\n type: \"schema\",\n message: `Schema validation failed: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n error: error instanceof Error ? error : new Error(\"Unknown error\"),\n });\n }\n }\n } else {\n // Minimal type assertion if schema validation is skipped\n grant = data as GrantFile;\n }\n\n // 2. Business Logic Validation (only if we have a valid grant)\n if (grant) {\n // Check grantee access\n if (grantee) {\n try {\n validateGranteeAccess(grant, grantee);\n } catch (error) {\n const field = extractFieldFromBusinessError(error);\n errors.push({\n type: \"business\",\n field,\n message:\n error instanceof Error\n ? error.message\n : \"Unknown business rule error\",\n error: error instanceof Error ? error : new Error(\"Unknown error\"),\n });\n }\n }\n\n // Check expiration\n try {\n validateGrantExpiry(grant, currentTime);\n } catch (error) {\n const field = extractFieldFromBusinessError(error);\n errors.push({\n type: \"business\",\n field,\n message:\n error instanceof Error\n ? error.message\n : \"Unknown business rule error\",\n error: error instanceof Error ? error : new Error(\"Unknown error\"),\n });\n }\n\n // Check operation access\n if (operation) {\n try {\n validateOperationAccess(grant, operation);\n } catch (error) {\n const field = extractFieldFromBusinessError(error);\n errors.push({\n type: \"business\",\n field,\n message:\n error instanceof Error\n ? error.message\n : \"Unknown business rule error\",\n error: error instanceof Error ? error : new Error(\"Unknown error\"),\n });\n }\n }\n }\n\n // 3. Return Results\n if (errors.length > 0) {\n if (throwOnError) {\n // Throw the most specific error we have\n const firstError = errors[0];\n if (firstError.error) {\n throw firstError.error;\n } else {\n const combinedMessage = errors.map((e) => e.message).join(\"; \");\n throw new GrantValidationError(\n `Grant validation failed: ${combinedMessage}`,\n { errors, data },\n );\n }\n }\n\n return { valid: false, errors, grant };\n }\n\n if (throwOnError) {\n return grant as GrantFile;\n } else {\n return { valid: true, errors: [], grant: grant as GrantFile };\n }\n}\n\n/**\n * Extracts the field name from business validation errors for reporting.\n *\n * @param error - The validation error to analyze\n * @returns The field name associated with the error, or undefined\n *\n * @internal\n */\nfunction extractFieldFromBusinessError(error: unknown): string | undefined {\n if (error instanceof GrantExpiredError) return \"expires\";\n if (error instanceof GranteeMismatchError) return \"grantee\";\n if (error instanceof OperationNotAllowedError) return \"operation\";\n return undefined;\n}\n\n/**\n * Validates that a grant allows access for the requesting address.\n *\n * @param grantFile - The grant file to validate.\n * Obtain from permission operations or server responses.\n * @param requestingAddress - The address requesting access.\n * Typically the current wallet address.\n *\n * @throws {GranteeMismatchError} If addresses don't match.\n * Ensure using the correct wallet that received the grant.\n *\n * @example\n * ```typescript\n * validateGranteeAccess(grantFile, walletAddress);\n * // Throws if walletAddress doesn't match grantFile.grantee\n * ```\n *\n * @category Permissions\n */\nexport function validateGranteeAccess(\n grantFile: GrantFile,\n requestingAddress: Address,\n): void {\n const normalizedGrantee = getAddress(grantFile.grantee);\n const normalizedRequesting = getAddress(requestingAddress);\n\n if (normalizedGrantee !== normalizedRequesting) {\n throw new GranteeMismatchError(\n \"Permission denied: requesting address does not match grantee\",\n grantFile.grantee,\n requestingAddress,\n );\n }\n}\n\n/**\n * Validates that a grant has not expired.\n *\n * @param grantFile - The grant file to check.\n * Must include expiry timestamp if time-limited.\n * @param currentTime - Optional time override for testing.\n * Unix timestamp in seconds. Defaults to current time.\n *\n * @throws {GrantExpiredError} If grant has expired.\n * Request a new grant from the data owner.\n *\n * @example\n * ```typescript\n * validateGrantExpiry(grantFile);\n * // Throws if current time > grantFile.expires\n * ```\n *\n * @category Permissions\n */\nexport function validateGrantExpiry(\n grantFile: GrantFile,\n currentTime?: number,\n): void {\n if (grantFile.expires) {\n const now =\n currentTime !== undefined ? currentTime : Math.floor(Date.now() / 1000); // Current Unix timestamp\n\n if (now > grantFile.expires) {\n throw new GrantExpiredError(\n \"Permission denied: grant has expired\",\n grantFile.expires,\n now,\n );\n }\n }\n}\n\n/**\n * Validates that a grant authorizes the requested operation.\n *\n * @param grantFile - The grant file to check.\n * Contains the authorized operation.\n * @param requestedOperation - The operation to validate.\n * Must match the grant's operation exactly.\n *\n * @throws {OperationNotAllowedError} If operations don't match.\n * Request a grant for the specific operation needed.\n *\n * @example\n * ```typescript\n * validateOperationAccess(grantFile, 'llm_inference');\n * // Throws if grantFile.operation !== 'llm_inference'\n * ```\n *\n * @category Permissions\n */\nexport function validateOperationAccess(\n grantFile: GrantFile,\n requestedOperation: string,\n): void {\n if (grantFile.operation !== requestedOperation) {\n throw new OperationNotAllowedError(\n \"Permission denied: operation not allowed by grant\",\n grantFile.operation,\n requestedOperation,\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAcA,kBAA2B;AAC3B,iBAA2C;AAC3C,yBAAuB;AAEvB,8BAA4B;AAWrB,MAAM,6BAA6B,MAAM;AAAA,EAC9C,YACE,SACO,SACP;AACA,UAAM,OAAO;AAFN;AAGP,SAAK,OAAO;AAAA,EACd;AACF;AAWO,MAAM,0BAA0B,qBAAqB;AAAA,EAC1D,YACE,SACO,SACA,aACP;AACA,UAAM,SAAS,EAAE,SAAS,YA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AAA,IACF;AAAA,EACF;AACF;","names":["Ajv","addFormats","grantFileSchema"]}
|
|
@@ -1,7 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides comprehensive validation for permission grant files.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* This module implements multi-layer validation for grant files including
|
|
6
|
+
* JSON schema validation, business rule checking, expiration verification,
|
|
7
|
+
* and access control validation. It provides both throwing and non-throwing
|
|
8
|
+
* modes for flexible error handling.
|
|
9
|
+
*
|
|
10
|
+
* @category Permissions
|
|
11
|
+
* @module utils/grantValidation
|
|
12
|
+
*/
|
|
1
13
|
import type { Address } from "viem";
|
|
2
14
|
import type { GrantFile } from "../types/permissions";
|
|
3
15
|
/**
|
|
4
|
-
*
|
|
16
|
+
* Indicates a general grant validation failure.
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* Base class for all grant validation errors. Provides structured
|
|
20
|
+
* error details for debugging and recovery.
|
|
5
21
|
*
|
|
6
22
|
* @category Permissions
|
|
7
23
|
*/
|
|
@@ -10,7 +26,11 @@ export declare class GrantValidationError extends Error {
|
|
|
10
26
|
constructor(message: string, details?: Record<string, unknown> | undefined);
|
|
11
27
|
}
|
|
12
28
|
/**
|
|
13
|
-
*
|
|
29
|
+
* Indicates that a grant has expired and is no longer valid.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* Thrown when attempting to use a grant past its expiration timestamp.
|
|
33
|
+
* Includes both the expiration time and current time for debugging.
|
|
14
34
|
*
|
|
15
35
|
* @category Permissions
|
|
16
36
|
*/
|
|
@@ -20,7 +40,11 @@ export declare class GrantExpiredError extends GrantValidationError {
|
|
|
20
40
|
constructor(message: string, expires: number, currentTime: number);
|
|
21
41
|
}
|
|
22
42
|
/**
|
|
23
|
-
*
|
|
43
|
+
* Indicates that the requesting address doesn't match the grant's grantee.
|
|
44
|
+
*
|
|
45
|
+
* @remarks
|
|
46
|
+
* Thrown when a user attempts to use a grant that was issued to a
|
|
47
|
+
* different address. This prevents unauthorized use of permissions.
|
|
24
48
|
*
|
|
25
49
|
* @category Permissions
|
|
26
50
|
*/
|
|
@@ -30,7 +54,11 @@ export declare class GranteeMismatchError extends GrantValidationError {
|
|
|
30
54
|
constructor(message: string, grantee: Address, requestingAddress: Address);
|
|
31
55
|
}
|
|
32
56
|
/**
|
|
33
|
-
*
|
|
57
|
+
* Indicates that the requested operation is not allowed by the grant.
|
|
58
|
+
*
|
|
59
|
+
* @remarks
|
|
60
|
+
* Thrown when attempting an operation that differs from what the
|
|
61
|
+
* grant authorizes. Includes both granted and requested operations.
|
|
34
62
|
*
|
|
35
63
|
* @category Permissions
|
|
36
64
|
*/
|
|
@@ -40,7 +68,11 @@ export declare class OperationNotAllowedError extends GrantValidationError {
|
|
|
40
68
|
constructor(message: string, grantedOperation: string, requestedOperation: string);
|
|
41
69
|
}
|
|
42
70
|
/**
|
|
43
|
-
*
|
|
71
|
+
* Indicates that the grant file structure violates the JSON schema.
|
|
72
|
+
*
|
|
73
|
+
* @remarks
|
|
74
|
+
* Thrown when a grant file doesn't conform to the expected schema.
|
|
75
|
+
* Includes detailed schema validation errors and the invalid data.
|
|
44
76
|
*
|
|
45
77
|
* @category Permissions
|
|
46
78
|
*/
|
|
@@ -50,7 +82,11 @@ export declare class GrantSchemaError extends GrantValidationError {
|
|
|
50
82
|
constructor(message: string, schemaErrors: unknown[], invalidData: unknown);
|
|
51
83
|
}
|
|
52
84
|
/**
|
|
53
|
-
*
|
|
85
|
+
* Configures grant validation behavior and scope.
|
|
86
|
+
*
|
|
87
|
+
* @remarks
|
|
88
|
+
* Controls which validations to perform and how to handle errors.
|
|
89
|
+
* Allows selective validation of specific aspects of a grant.
|
|
54
90
|
*
|
|
55
91
|
* @category Permissions
|
|
56
92
|
*/
|
|
@@ -67,7 +103,11 @@ export interface GrantValidationOptions {
|
|
|
67
103
|
throwOnError?: boolean;
|
|
68
104
|
}
|
|
69
105
|
/**
|
|
70
|
-
*
|
|
106
|
+
* Represents the detailed result of grant validation.
|
|
107
|
+
*
|
|
108
|
+
* @remarks
|
|
109
|
+
* Provides comprehensive validation results including all errors
|
|
110
|
+
* encountered during validation. Used in non-throwing mode.
|
|
71
111
|
*
|
|
72
112
|
* @category Permissions
|
|
73
113
|
*/
|
|
@@ -125,23 +165,62 @@ export declare function validateGrant(data: unknown, options?: Omit<GrantValidat
|
|
|
125
165
|
throwOnError?: true;
|
|
126
166
|
})): GrantFile;
|
|
127
167
|
/**
|
|
128
|
-
* Validates that a grant
|
|
168
|
+
* Validates that a grant allows access for the requesting address.
|
|
169
|
+
*
|
|
170
|
+
* @param grantFile - The grant file to validate.
|
|
171
|
+
* Obtain from permission operations or server responses.
|
|
172
|
+
* @param requestingAddress - The address requesting access.
|
|
173
|
+
* Typically the current wallet address.
|
|
129
174
|
*
|
|
130
|
-
* @
|
|
131
|
-
*
|
|
175
|
+
* @throws {GranteeMismatchError} If addresses don't match.
|
|
176
|
+
* Ensure using the correct wallet that received the grant.
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```typescript
|
|
180
|
+
* validateGranteeAccess(grantFile, walletAddress);
|
|
181
|
+
* // Throws if walletAddress doesn't match grantFile.grantee
|
|
182
|
+
* ```
|
|
183
|
+
*
|
|
184
|
+
* @category Permissions
|
|
132
185
|
*/
|
|
133
186
|
export declare function validateGranteeAccess(grantFile: GrantFile, requestingAddress: Address): void;
|
|
134
187
|
/**
|
|
135
|
-
* Validates that a grant has not expired
|
|
188
|
+
* Validates that a grant has not expired.
|
|
189
|
+
*
|
|
190
|
+
* @param grantFile - The grant file to check.
|
|
191
|
+
* Must include expiry timestamp if time-limited.
|
|
192
|
+
* @param currentTime - Optional time override for testing.
|
|
193
|
+
* Unix timestamp in seconds. Defaults to current time.
|
|
194
|
+
*
|
|
195
|
+
* @throws {GrantExpiredError} If grant has expired.
|
|
196
|
+
* Request a new grant from the data owner.
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```typescript
|
|
200
|
+
* validateGrantExpiry(grantFile);
|
|
201
|
+
* // Throws if current time > grantFile.expires
|
|
202
|
+
* ```
|
|
136
203
|
*
|
|
137
|
-
* @
|
|
138
|
-
* @param currentTime - Optional override for current time (Unix timestamp)
|
|
204
|
+
* @category Permissions
|
|
139
205
|
*/
|
|
140
206
|
export declare function validateGrantExpiry(grantFile: GrantFile, currentTime?: number): void;
|
|
141
207
|
/**
|
|
142
|
-
* Validates that a grant
|
|
208
|
+
* Validates that a grant authorizes the requested operation.
|
|
209
|
+
*
|
|
210
|
+
* @param grantFile - The grant file to check.
|
|
211
|
+
* Contains the authorized operation.
|
|
212
|
+
* @param requestedOperation - The operation to validate.
|
|
213
|
+
* Must match the grant's operation exactly.
|
|
214
|
+
*
|
|
215
|
+
* @throws {OperationNotAllowedError} If operations don't match.
|
|
216
|
+
* Request a grant for the specific operation needed.
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```typescript
|
|
220
|
+
* validateOperationAccess(grantFile, 'llm_inference');
|
|
221
|
+
* // Throws if grantFile.operation !== 'llm_inference'
|
|
222
|
+
* ```
|
|
143
223
|
*
|
|
144
|
-
* @
|
|
145
|
-
* @param requestedOperation - The operation being requested to validate against the grant
|
|
224
|
+
* @category Permissions
|
|
146
225
|
*/
|
|
147
226
|
export declare function validateOperationAccess(grantFile: GrantFile, requestedOperation: string): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/grantValidation.ts"],"sourcesContent":["import type { Address } from \"viem\";\nimport { getAddress } from \"viem\";\nimport Ajv, { type ValidateFunction } from \"ajv\";\nimport addFormats from \"ajv-formats\";\nimport type { GrantFile } from \"../types/permissions\";\nimport grantFileSchema from \"../schemas/grantFile.schema.json\";\n\n/**\n * Base error class for grant validation failures\n *\n * @category Permissions\n */\nexport class GrantValidationError extends Error {\n constructor(\n message: string,\n public details?: Record<string, unknown>,\n ) {\n super(message);\n this.name = \"GrantValidationError\";\n }\n}\n\n/**\n * Error thrown when a grant has expired\n *\n * @category Permissions\n */\nexport class GrantExpiredError extends GrantValidationError {\n constructor(\n message: string,\n public expires: number,\n public currentTime: number,\n ) {\n super(message, { expires, currentTime });\n this.name = \"GrantExpiredError\";\n }\n}\n\n/**\n * Error thrown when grantee doesn't match requesting address\n *\n * @category Permissions\n */\nexport class GranteeMismatchError extends GrantValidationError {\n constructor(\n message: string,\n public grantee: Address,\n public requestingAddress: Address,\n ) {\n super(message, { grantee, requestingAddress });\n this.name = \"GranteeMismatchError\";\n }\n}\n\n/**\n * Error thrown when operation is not allowed by grant\n *\n * @category Permissions\n */\nexport class OperationNotAllowedError extends GrantValidationError {\n constructor(\n message: string,\n public grantedOperation: string,\n public requestedOperation: string,\n ) {\n super(message, { grantedOperation, requestedOperation });\n this.name = \"OperationNotAllowedError\";\n }\n}\n\n/**\n * Error thrown when grant file structure is invalid\n *\n * @category Permissions\n */\nexport class GrantSchemaError extends GrantValidationError {\n constructor(\n message: string,\n public schemaErrors: unknown[],\n public invalidData: unknown,\n ) {\n super(message, { errors: schemaErrors, data: invalidData });\n this.name = \"GrantSchemaError\";\n }\n}\n\n/**\n * Ajv instance for grant file validation with draft 2020-12 support\n */\nconst ajv = new Ajv({\n strict: true,\n removeAdditional: false,\n useDefaults: false,\n coerceTypes: false,\n});\n\n// Add format validators (email, date, etc.)\naddFormats(ajv);\n\n/**\n * Compiled grant file schema validator\n */\nconst validateGrantFileSchema: ValidateFunction = ajv.compile(grantFileSchema);\n\n/**\n * Options for grant validation\n *\n * @category Permissions\n */\nexport interface GrantValidationOptions {\n /** Enable JSON schema validation (default: true) */\n schema?: boolean;\n /** Grantee address to validate access for */\n grantee?: Address;\n /** Operation to validate permission for */\n operation?: string;\n /** Override current time for expiry checking (Unix timestamp) */\n currentTime?: number;\n /** Return detailed results instead of throwing (default: false) */\n throwOnError?: boolean;\n}\n\n/**\n * Detailed validation result\n *\n * @category Permissions\n */\nexport interface GrantValidationResult {\n /** Whether validation passed */\n valid: boolean;\n /** Validation errors encountered */\n errors: Array<{\n type: \"schema\" | \"business\";\n field?: string;\n message: string;\n error?: Error;\n }>;\n /** The validated grant file (if validation passed) */\n grant?: GrantFile;\n}\n\n/**\n * Validates a grant file with comprehensive schema and business rule checking.\n *\n * This function provides flexible validation with TypeScript overloads:\n * - When `throwOnError` is false (or `{ throwOnError: false }`), returns a detailed validation result\n * - When `throwOnError` is true (default), throws specific errors or returns the validated grant\n *\n * @param data - The grant file data to validate (unknown type for safety)\n * @param options - Validation options including grantee, operation, files, etc.\n * @returns Either a GrantFile (when throwing) or GrantValidationResult (when not throwing)\n * @throws {GrantSchemaError} When the grant file structure is invalid\n * @throws {GrantExpiredError} When the grant has expired\n * @throws {GranteeMismatchError} When the grantee doesn't match the requesting address\n * @throws {OperationNotAllowedError} When the requested operation is not allowed\n * @example\n * ```typescript\n * // Throwing mode (default) - returns GrantFile or throws\n * const grant = validateGrant(data, {\n * grantee: '0x123...',\n * operation: 'llm_inference',\n * });\n *\n * // Non-throwing mode - returns validation result\n * const result = validateGrant(data, {\n * grantee: '0x123...',\n * throwOnError: false\n * });\n * if (result.valid) {\n * console.log('Grant is valid:', result.grant);\n * } else {\n * console.log('Validation errors:', result.errors);\n * }\n * ```\n */\n\nexport function validateGrant(\n data: unknown,\n options: GrantValidationOptions & { throwOnError: false },\n): GrantValidationResult;\n\nexport function validateGrant(\n data: unknown,\n options?:\n | Omit<GrantValidationOptions, \"throwOnError\">\n | (GrantValidationOptions & { throwOnError?: true }),\n): GrantFile;\n\n/**\n * Implementation function for grant validation with flexible return types\n *\n * @param data - The grant file data to validate\n * @param options - Validation configuration options\n * @returns Either a GrantFile or GrantValidationResult depending on throwOnError setting\n */\nexport function validateGrant(\n data: unknown,\n options: GrantValidationOptions = {},\n): GrantFile | GrantValidationResult {\n const {\n schema = true,\n grantee,\n operation,\n currentTime,\n throwOnError = true,\n } = options;\n\n const errors: GrantValidationResult[\"errors\"] = [];\n let grant: GrantFile | undefined;\n\n // 1. Schema Validation\n if (schema) {\n try {\n if (validateGrantFileSchema(data)) {\n grant = data as GrantFile;\n } else {\n throw new GrantValidationError(\"Invalid grant file schema\");\n }\n } catch (error) {\n if (error instanceof GrantValidationError) {\n const schemaError = new GrantSchemaError(\n error.message,\n Array.isArray(error.details?.errors) ? error.details.errors : [],\n data,\n );\n errors.push({\n type: \"schema\",\n message: error.message,\n error: schemaError,\n });\n } else {\n errors.push({\n type: \"schema\",\n message: `Schema validation failed: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n error: error instanceof Error ? error : new Error(\"Unknown error\"),\n });\n }\n }\n } else {\n // Minimal type assertion if schema validation is skipped\n grant = data as GrantFile;\n }\n\n // 2. Business Logic Validation (only if we have a valid grant)\n if (grant) {\n // Check grantee access\n if (grantee) {\n try {\n validateGranteeAccess(grant, grantee);\n } catch (error) {\n const field = extractFieldFromBusinessError(error);\n errors.push({\n type: \"business\",\n field,\n message:\n error instanceof Error\n ? error.message\n : \"Unknown business rule error\",\n error: error instanceof Error ? error : new Error(\"Unknown error\"),\n });\n }\n }\n\n // Check expiration\n try {\n validateGrantExpiry(grant, currentTime);\n } catch (error) {\n const field = extractFieldFromBusinessError(error);\n errors.push({\n type: \"business\",\n field,\n message:\n error instanceof Error\n ? error.message\n : \"Unknown business rule error\",\n error: error instanceof Error ? error : new Error(\"Unknown error\"),\n });\n }\n\n // Check operation access\n if (operation) {\n try {\n validateOperationAccess(grant, operation);\n } catch (error) {\n const field = extractFieldFromBusinessError(error);\n errors.push({\n type: \"business\",\n field,\n message:\n error instanceof Error\n ? error.message\n : \"Unknown business rule error\",\n error: error instanceof Error ? error : new Error(\"Unknown error\"),\n });\n }\n }\n }\n\n // 3. Return Results\n if (errors.length > 0) {\n if (throwOnError) {\n // Throw the most specific error we have\n const firstError = errors[0];\n if (firstError.error) {\n throw firstError.error;\n } else {\n const combinedMessage = errors.map((e) => e.message).join(\"; \");\n throw new GrantValidationError(\n `Grant validation failed: ${combinedMessage}`,\n { errors, data },\n );\n }\n }\n\n return { valid: false, errors, grant };\n }\n\n if (throwOnError) {\n return grant as GrantFile;\n } else {\n return { valid: true, errors: [], grant: grant as GrantFile };\n }\n}\n\n/**\n * Helper function to extract field name from business validation errors\n *\n * @param error - The validation error to extract field information from\n * @returns The field name associated with the error, or undefined if not applicable\n */\nfunction extractFieldFromBusinessError(error: unknown): string | undefined {\n if (error instanceof GrantExpiredError) return \"expires\";\n if (error instanceof GranteeMismatchError) return \"grantee\";\n if (error instanceof OperationNotAllowedError) return \"operation\";\n return undefined;\n}\n\n/**\n * Validates that a grant file allows access for a specific grantee\n *\n * @param grantFile - The grant file to validate access for\n * @param requestingAddress - The address requesting access to check against the grantee\n */\nexport function validateGranteeAccess(\n grantFile: GrantFile,\n requestingAddress: Address,\n): void {\n const normalizedGrantee = getAddress(grantFile.grantee);\n const normalizedRequesting = getAddress(requestingAddress);\n\n if (normalizedGrantee !== normalizedRequesting) {\n throw new GranteeMismatchError(\n \"Permission denied: requesting address does not match grantee\",\n grantFile.grantee,\n requestingAddress,\n );\n }\n}\n\n/**\n * Validates that a grant has not expired (if expiry is set)\n *\n * @param grantFile - The grant file to check expiration for\n * @param currentTime - Optional override for current time (Unix timestamp)\n */\nexport function validateGrantExpiry(\n grantFile: GrantFile,\n currentTime?: number,\n): void {\n if (grantFile.expires) {\n const now =\n currentTime !== undefined ? currentTime : Math.floor(Date.now() / 1000); // Current Unix timestamp\n\n if (now > grantFile.expires) {\n throw new GrantExpiredError(\n \"Permission denied: grant has expired\",\n grantFile.expires,\n now,\n );\n }\n }\n}\n\n/**\n * Validates that a grant allows a specific operation\n *\n * @param grantFile - The grant file to validate operation access for\n * @param requestedOperation - The operation being requested to validate against the grant\n */\nexport function validateOperationAccess(\n grantFile: GrantFile,\n requestedOperation: string,\n): void {\n if (grantFile.operation !== requestedOperation) {\n throw new OperationNotAllowedError(\n \"Permission denied: operation not allowed by grant\",\n grantFile.operation,\n requestedOperation,\n );\n }\n}\n"],"mappings":"AAC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AAI;AAExE,QAAI,MAAM,UAAU,SAAS;AAC3B,YAAM,IAAI;AAAA,QACR;AAAA,QACA,UAAU;AAAA,QACV;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAQO,SAAS,wBACd,WACA,oBACM;AACN,MAAI,UAAU,cAAc,oBAAoB;AAC9C,UAAM,IAAI;AAAA,MACR;AAAA,MACA,UAAU;AAAA,MACV;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/utils/grantValidation.ts"],"sourcesContent":["/**\n * Provides comprehensive validation for permission grant files.\n *\n * @remarks\n * This module implements multi-layer validation for grant files including\n * JSON schema validation, business rule checking, expiration verification,\n * and access control validation. It provides both throwing and non-throwing\n * modes for flexible error handling.\n *\n * @category Permissions\n * @module utils/grantValidation\n */\n\nimport type { Address } from \"viem\";\nimport { getAddress } from \"viem\";\nimport Ajv, { type ValidateFunction } from \"ajv\";\nimport addFormats from \"ajv-formats\";\nimport type { GrantFile } from \"../types/permissions\";\nimport grantFileSchema from \"../schemas/grantFile.schema.json\";\n\n/**\n * Indicates a general grant validation failure.\n *\n * @remarks\n * Base class for all grant validation errors. Provides structured\n * error details for debugging and recovery.\n *\n * @category Permissions\n */\nexport class GrantValidationError extends Error {\n constructor(\n message: string,\n public details?: Record<string, unknown>,\n ) {\n super(message);\n this.name = \"GrantValidationError\";\n }\n}\n\n/**\n * Indicates that a grant has expired and is no longer valid.\n *\n * @remarks\n * Thrown when attempting to use a grant past its expiration timestamp.\n * Includes both the expiration time and current time for debugging.\n *\n * @category Permissions\n */\nexport class GrantExpiredError extends GrantValidationError {\n constructor(\n message: string,\n public expires: number,\n public currentTime: number,\n ) {\n super(message, { expires, currentTime });\n this.name = \"GrantExpiredError\";\n }\n}\n\n/**\n * Indicates that the requesting address doesn't match the grant's grantee.\n *\n * @remarks\n * Thrown when a user attempts to use a grant that was issued to a\n * different address. This prevents unauthorized use of permissions.\n *\n * @category Permissions\n */\nexport class GranteeMismatchError extends GrantValidationError {\n constructor(\n message: string,\n public grantee: Address,\n public requestingAddress: Address,\n ) {\n super(message, { grantee, requestingAddress });\n this.name = \"GranteeMismatchError\";\n }\n}\n\n/**\n * Indicates that the requested operation is not allowed by the grant.\n *\n * @remarks\n * Thrown when attempting an operation that differs from what the\n * grant authorizes. Includes both granted and requested operations.\n *\n * @category Permissions\n */\nexport class OperationNotAllowedError extends GrantValidationError {\n constructor(\n message: string,\n public grantedOperation: string,\n public requestedOperation: string,\n ) {\n super(message, { grantedOperation, requestedOperation });\n this.name = \"OperationNotAllowedError\";\n }\n}\n\n/**\n * Indicates that the grant file structure violates the JSON schema.\n *\n * @remarks\n * Thrown when a grant file doesn't conform to the expected schema.\n * Includes detailed schema validation errors and the invalid data.\n *\n * @category Permissions\n */\nexport class GrantSchemaError extends GrantValidationError {\n constructor(\n message: string,\n public schemaErrors: unknown[],\n public invalidData: unknown,\n ) {\n super(message, { errors: schemaErrors, data: invalidData });\n this.name = \"GrantSchemaError\";\n }\n}\n\n/**\n * Ajv instance configured for strict grant file validation.\n *\n * @internal\n */\nconst ajv = new Ajv({\n strict: true,\n removeAdditional: false,\n useDefaults: false,\n coerceTypes: false,\n});\n\n// Add format validators (email, date, etc.)\naddFormats(ajv);\n\n/**\n * Pre-compiled grant file schema validator for performance.\n *\n * @internal\n */\nconst validateGrantFileSchema: ValidateFunction = ajv.compile(grantFileSchema);\n\n/**\n * Configures grant validation behavior and scope.\n *\n * @remarks\n * Controls which validations to perform and how to handle errors.\n * Allows selective validation of specific aspects of a grant.\n *\n * @category Permissions\n */\nexport interface GrantValidationOptions {\n /** Enable JSON schema validation (default: true) */\n schema?: boolean;\n /** Grantee address to validate access for */\n grantee?: Address;\n /** Operation to validate permission for */\n operation?: string;\n /** Override current time for expiry checking (Unix timestamp) */\n currentTime?: number;\n /** Return detailed results instead of throwing (default: false) */\n throwOnError?: boolean;\n}\n\n/**\n * Represents the detailed result of grant validation.\n *\n * @remarks\n * Provides comprehensive validation results including all errors\n * encountered during validation. Used in non-throwing mode.\n *\n * @category Permissions\n */\nexport interface GrantValidationResult {\n /** Whether validation passed */\n valid: boolean;\n /** Validation errors encountered */\n errors: Array<{\n type: \"schema\" | \"business\";\n field?: string;\n message: string;\n error?: Error;\n }>;\n /** The validated grant file (if validation passed) */\n grant?: GrantFile;\n}\n\n/**\n * Validates a grant file with comprehensive schema and business rule checking.\n *\n * This function provides flexible validation with TypeScript overloads:\n * - When `throwOnError` is false (or `{ throwOnError: false }`), returns a detailed validation result\n * - When `throwOnError` is true (default), throws specific errors or returns the validated grant\n *\n * @param data - The grant file data to validate (unknown type for safety)\n * @param options - Validation options including grantee, operation, files, etc.\n * @returns Either a GrantFile (when throwing) or GrantValidationResult (when not throwing)\n * @throws {GrantSchemaError} When the grant file structure is invalid\n * @throws {GrantExpiredError} When the grant has expired\n * @throws {GranteeMismatchError} When the grantee doesn't match the requesting address\n * @throws {OperationNotAllowedError} When the requested operation is not allowed\n * @example\n * ```typescript\n * // Throwing mode (default) - returns GrantFile or throws\n * const grant = validateGrant(data, {\n * grantee: '0x123...',\n * operation: 'llm_inference',\n * });\n *\n * // Non-throwing mode - returns validation result\n * const result = validateGrant(data, {\n * grantee: '0x123...',\n * throwOnError: false\n * });\n * if (result.valid) {\n * console.log('Grant is valid:', result.grant);\n * } else {\n * console.log('Validation errors:', result.errors);\n * }\n * ```\n */\n\nexport function validateGrant(\n data: unknown,\n options: GrantValidationOptions & { throwOnError: false },\n): GrantValidationResult;\n\nexport function validateGrant(\n data: unknown,\n options?:\n | Omit<GrantValidationOptions, \"throwOnError\">\n | (GrantValidationOptions & { throwOnError?: true }),\n): GrantFile;\n\n/** @internal */\nexport function validateGrant(\n data: unknown,\n options: GrantValidationOptions = {},\n): GrantFile | GrantValidationResult {\n const {\n schema = true,\n grantee,\n operation,\n currentTime,\n throwOnError = true,\n } = options;\n\n const errors: GrantValidationResult[\"errors\"] = [];\n let grant: GrantFile | undefined;\n\n // 1. Schema Validation\n if (schema) {\n try {\n if (validateGrantFileSchema(data)) {\n grant = data as GrantFile;\n } else {\n throw new GrantValidationError(\"Invalid grant file schema\");\n }\n } catch (error) {\n if (error instanceof GrantValidationError) {\n const schemaError = new GrantSchemaError(\n error.message,\n Array.isArray(error.details?.errors) ? error.details.errors : [],\n data,\n );\n errors.push({\n type: \"schema\",\n message: error.message,\n error: schemaError,\n });\n } else {\n errors.push({\n type: \"schema\",\n message: `Schema validation failed: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n error: error instanceof Error ? error : new Error(\"Unknown error\"),\n });\n }\n }\n } else {\n // Minimal type assertion if schema validation is skipped\n grant = data as GrantFile;\n }\n\n // 2. Business Logic Validation (only if we have a valid grant)\n if (grant) {\n // Check grantee access\n if (grantee) {\n try {\n validateGranteeAccess(grant, grantee);\n } catch (error) {\n const field = extractFieldFromBusinessError(error);\n errors.push({\n type: \"business\",\n field,\n message:\n error instanceof Error\n ? error.message\n : \"Unknown business rule error\",\n error: error instanceof Error ? error : new Error(\"Unknown error\"),\n });\n }\n }\n\n // Check expiration\n try {\n validateGrantExpiry(grant, currentTime);\n } catch (error) {\n const field = extractFieldFromBusinessError(error);\n errors.push({\n type: \"business\",\n field,\n message:\n error instanceof Error\n ? error.message\n : \"Unknown business rule error\",\n error: error instanceof Error ? error : new Error(\"Unknown error\"),\n });\n }\n\n // Check operation access\n if (operation) {\n try {\n validateOperationAccess(grant, operation);\n } catch (error) {\n const field = extractFieldFromBusinessError(error);\n errors.push({\n type: \"business\",\n field,\n message:\n error instanceof Error\n ? error.message\n : \"Unknown business rule error\",\n error: error instanceof Error ? error : new Error(\"Unknown error\"),\n });\n }\n }\n }\n\n // 3. Return Results\n if (errors.length > 0) {\n if (throwOnError) {\n // Throw the most specific error we have\n const firstError = errors[0];\n if (firstError.error) {\n throw firstError.error;\n } else {\n const combinedMessage = errors.map((e) => e.message).join(\"; \");\n throw new GrantValidationError(\n `Grant validation failed: ${combinedMessage}`,\n { errors, data },\n );\n }\n }\n\n return { valid: false, errors, grant };\n }\n\n if (throwOnError) {\n return grant as GrantFile;\n } else {\n return { valid: true, errors: [], grant: grant as GrantFile };\n }\n}\n\n/**\n * Extracts the field name from business validation errors for reporting.\n *\n * @param error - The validation error to analyze\n * @returns The field name associated with the error, or undefined\n *\n * @internal\n */\nfunction extractFieldFromBusinessError(error: unknown): string | undefined {\n if (error instanceof GrantExpiredError) return \"expires\";\n if (error instanceof GranteeMismatchError) return \"grantee\";\n if (error instanceof OperationNotAllowedError) return \"operation\";\n return undefined;\n}\n\n/**\n * Validates that a grant allows access for the requesting address.\n *\n * @param grantFile - The grant file to validate.\n * Obtain from permission operations or server responses.\n * @param requestingAddress - The address requesting access.\n * Typically the current wallet address.\n *\n * @throws {GranteeMismatchError} If addresses don't match.\n * Ensure using the correct wallet that received the grant.\n *\n * @example\n * ```typescript\n * validateGranteeAccess(grantFile, walletAddress);\n * // Throws if walletAddress doesn't match grantFile.grantee\n * ```\n *\n * @category Permissions\n */\nexport function validateGranteeAccess(\n grantFile: GrantFile,\n requestingAddress: Address,\n): void {\n const normalizedGrantee = getAddress(grantFile.grantee);\n const normalizedRequesting = getAddress(requestingAddress);\n\n if (normalizedGrantee !== normalizedRequesting) {\n throw new GranteeMismatchError(\n \"Permission denied: requesting address does not match grantee\",\n grantFile.grantee,\n requestingAddress,\n );\n }\n}\n\n/**\n * Validates that a grant has not expired.\n *\n * @param grantFile - The grant file to check.\n * Must include expiry timestamp if time-limited.\n * @param currentTime - Optional time override for testing.\n * Unix timestamp in seconds. Defaults to current time.\n *\n * @throws {GrantExpiredError} If grant has expired.\n * Request a new grant from the data owner.\n *\n * @example\n * ```typescript\n * validateGrantExpiry(grantFile);\n * // Throws if current time > grantFile.expires\n * ```\n *\n * @category Permissions\n */\nexport function validateGrantExpiry(\n grantFile: GrantFile,\n currentTime?: number,\n): void {\n if (grantFile.expires) {\n const now =\n currentTime !== undefined ? currentTime : Math.floor(Date.now() / 1000); // Current Unix timestamp\n\n if (now > grantFile.expires) {\n throw new GrantExpiredError(\n \"Permission denied: grant has expired\",\n grantFile.expires,\n now,\n );\n }\n }\n}\n\n/**\n * Validates that a grant authorizes the requested operation.\n *\n * @param grantFile - The grant file to check.\n * Contains the authorized operation.\n * @param requestedOperation - The operation to validate.\n * Must match the grant's operation exactly.\n *\n * @throws {OperationNotAllowedError} If operations don't match.\n * Request a grant for the specific operation needed.\n *\n * @example\n * ```typescript\n * validateOperationAccess(grantFile, 'llm_inference');\n * // Throws if grantFile.operation !== 'llm_inference'\n * ```\n *\n * @category Permissions\n */\nexport function validateOperationAccess(\n grantFile: GrantFile,\n requestedOperation: string,\n): void {\n if (grantFile.operation !== requestedOperation) {\n throw new OperationNotAllowedError(\n \"Permission denied: operation not allowed by grant\",\n grantFile.operation,\n requestedOperation,\n );\n }\n}\n"],"mappings":"AAcA,SAAS,kBAAkB;AAC3B,OAAO,SAAoC;AAC3C,OAAO,gBAAgB;AAEvB,OAAO,qBAAqB;AAWrB,MAAM,6BAA6B,MAAM;AAAA,EAC9C,YACE,SACO,SACP;AACA,UAAM,OAAO;AAFN;AAGP,SAAK,OAAO;AAAA,EACd;AACF;AAWO,MAAM,0BAA0B,qBAAqB;AAAA,EAC1D,YACE,SACO,SACA,aACP;AACA,UAAM,SAAS,EAAE,SAAS,YAAY,CAAC;AAHhC;AACA;AAGP,SAAK,OAAO;AAAA,EACd;AACF;AAWO,MAAM,6BAA6B,qBAAqB;AAAA,EAC7D,YACE,SACO,SACA,mBACP;AACA,UAAM,SAAS,EAAE,SAAS,kBAAkB,CAAC;AAHtC;AACA;AAGP,SAAK,OAAO;AAAA,EACd;AACF;AAWO,MAAM,iCAAiC,qBAAqB;AAAA,EACjE,YACE,SACO,kBACA,oBACP;AACA,UAAM,SAAS,EAAE,kBAAkB,mBAAmB,CAAC;AAHhD;AACA;AAGP,SAAK,OAAO;AAAA,EACd;AACF;AAWO,MAAM,yBAAyB,qBAAqB;AAAA,EACzD,YACE,SACO,cACA,aACP;AACA,UAAM,SAAS,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A0B;AAAA,EAC9D;AACF;AAUA,SAAS,8BAA8B,OAAoC;AACzE,MAAI,iBAAiB,kBAAmB,QAAO;AAC/C,MAAI,iBAAiB,qBAAsB,QAAO;AAClD,MAAI,iBAAiB,yBAA0B,QAAO;AACtD,SAAO;AACT;AAqBO,SAAS,sBACd,WACA,mBACM;AACN,QAAM,oBAAoB,WAAW,UAAU,OAAO;AACtD,QAAM,uBAAuB,WAAW,iBAAiB;AAEzD,MAAI,sBAAsB,sBAAsB;AAC9C,UAAM,IAAI;AAAA,MACR;AAAA,MACA,UAAU;AAAA,MACV;AAAA,IACF;AAAA,EACF;AACF;AAqBO,SAAS,oBACd,WACA,aACM;AACN,MAAI,UAAU,SAAS;AACrB,UAAM,MACJ,gBAAgB,SAAY,cAAc,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI;AAExE,QAAI,MAAM,UAAU,SAAS;AAC3B,YAAM,IAAI;AAAA,QACR;AAAA,QACA,UAAU;AAAA,QACV;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAqBO,SAAS,wBACd,WACA,oBACM;AACN,MAAI,UAAU,cAAc,oBAAoB;AAC9C,UAAM,IAAI;AAAA,MACR;AAAA,MACA,UAAU;AAAA,MACV;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/grants.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../../src/utils/grants.ts"],"sourcesContent":["/**\n * Provides high-level grant management utilities for the Vana permission system.\n *\n * @remarks\n * This module simplifies grant file creation, validation, storage, and retrieval.\n * Grants are the core mechanism for permission management in Vana, allowing users\n * to delegate specific operations to applications and services.\n *\n * @category Permissions\n * @module grants\n */\n\nimport type { Address } from \"viem\";\nimport type { GrantFile, GrantPermissionParams } from \"../types/permissions\";\nimport {\n createGrantFile,\n storeGrantFile,\n retrieveGrantFile,\n} from \"./grantFiles\";\nimport { validateGrant, GrantValidationError } from \"./grantValidation\";\n\n/**\n * Creates and validates a grant file from permission parameters.\n *\n * @remarks\n * Combines grant creation with immediate validation to ensure only valid\n * grants are created. Validates schema compliance, grantee address, and\n * operation parameters before returning the grant file.\n *\n * @param params - The permission parameters to create and validate the grant from.\n * Obtain from user input or application configuration.\n * @returns The validated grant file object ready for storage\n *\n * @throws {GrantValidationError} When grant parameters are invalid.\n * Check error message for specific validation failures.\n *\n * @example\n * ```typescript\n * const grant = createValidatedGrant({\n * grantee: '0x742d35Cc6634C0532925a3b844Bc9e7595f0b0Bb',\n * operation: 'llm_inference',\n * parameters: { model: 'gpt-4', maxTokens: 1000 },\n * expiresAt: Date.now() + 86400000 // 24 hours\n * });\n * ```\n *\n * @category Permissions\n */\nexport function createValidatedGrant(params: GrantPermissionParams): GrantFile {\n const grantFile = createGrantFile(params);\n\n // Validate the created grant file\n try {\n validateGrant(grantFile, {\n schema: true,\n grantee: params.grantee,\n operation: params.operation,\n });\n } catch (error) {\n throw new GrantValidationError(\n `Created grant file failed validation: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n { grantFile, params },\n );\n }\n\n return grantFile;\n}\n\n/**\n * Creates a grant file and stores it in IPFS.\n *\n * @remarks\n * Combines grant creation, validation, and IPFS storage in a single operation.\n * The grant is stored immutably on IPFS and can be referenced by its URL in\n * on-chain permission records.\n *\n * @param params - The permission parameters to create the grant from.\n * Obtain from user input or application configuration.\n * @param relayerUrl - The URL of the relayer service for IPFS storage.\n * Obtain from SDK configuration or environment.\n * @returns Promise resolving to an object containing the grant file and its IPFS URL\n *\n * @throws {GrantValidationError} When grant parameters are invalid.\n * Check error message for specific validation failures.\n * @throws {Error} When IPFS storage fails.\n * Retry with exponential backoff or check relayer status.\n *\n * @example\n * ```typescript\n * const { grantFile, grantUrl } = await createAndStoreGrant(\n * {\n * grantee: applicationAddress,\n * operation: 'data_processing',\n * parameters: { dataTypes: ['medical', 'financial'] }\n * },\n * 'https://relayer.vana.org'\n * );\n *\n * console.log('Grant stored at:', grantUrl);\n * ```\n *\n * @category Permissions\n */\nexport async function createAndStoreGrant(\n params: GrantPermissionParams,\n relayerUrl: string,\n): Promise<{ grantFile: GrantFile; grantUrl: string }> {\n const grantFile = createValidatedGrant(params);\n const grantUrl = await storeGrantFile(grantFile, relayerUrl);\n\n return { grantFile, grantUrl };\n}\n\n/**\n * Retrieves and validates a grant file from IPFS.\n *\n * @remarks\n * Fetches a grant file from IPFS and performs basic validation to ensure\n * the retrieved data is a valid grant structure. Use this when you need to\n * verify or process existing grants.\n *\n * @param grantUrl - The IPFS URL of the grant file to retrieve.\n * Obtain from on-chain permission records or grant events.\n * @param relayerUrl - Optional URL of the relayer service.\n * If not provided, uses default IPFS gateways.\n * @returns Promise resolving to the validated grant file\n *\n * @throws {Error} When grant retrieval fails.\n * Check network connectivity or IPFS gateway availability.\n * @throws {Error} When grant file is malformed.\n * Verify the grant URL points to a valid grant file.\n *\n * @example\n * ```typescript\n * const grant = await retrieveAndValidateGrant(\n * 'ipfs://QmXxx...'\n * );\n *\n * console.log('Grant for:', grant.grantee);\n * console.log('Operation:', grant.operation);\n * ```\n *\n * @category Permissions\n */\nexport async function retrieveAndValidateGrant(\n grantUrl: string,\n relayerUrl?: string,\n): Promise<GrantFile> {\n const grantFile = await retrieveGrantFile(grantUrl, relayerUrl);\n\n // Additional validation can be added here if needed\n return grantFile;\n}\n\n/**\n * Checks if a grant allows access for a specific request\n *\n * @param grantUrl - The IPFS URL of the grant file to check\n * @param requestingAddress - The address making the access request\n * @param operation - The operation being requested\n * @param _fileIds - Array of file IDs being accessed (currently unused but part of interface)\n * @param relayerUrl - Optional URL of the relayer service\n * @returns Promise resolving to access result with allowed status, reason, and grant file\n */\nexport async function checkGrantAccess(\n grantUrl: string,\n requestingAddress: Address,\n operation: string,\n _fileIds: number[],\n relayerUrl?: string,\n): Promise<{ allowed: boolean; reason?: string; grantFile?: GrantFile }> {\n try {\n const grantFile = await retrieveAndValidateGrant(grantUrl, relayerUrl);\n\n // Validate the grant for the request\n validateGrant(grantFile, {\n schema: true,\n grantee: requestingAddress,\n operation,\n });\n\n return { allowed: true, grantFile };\n } catch (error) {\n if (error instanceof GrantValidationError) {\n return {\n allowed: false,\n reason: error.message,\n };\n }\n\n return {\n allowed: false,\n reason: `Grant access check failed: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n };\n }\n}\n\n/**\n * Utility to check if a grant has expired\n *\n * @param grantFile - The grant file to check for expiration\n * @returns True if the grant has expired, false otherwise\n */\nexport function isGrantExpired(grantFile: GrantFile): boolean {\n if (!grantFile.expires) {\n return false; // No expiration set\n }\n\n const now = Math.floor(Date.now() / 1000);\n return now > grantFile.expires;\n}\n\n/**\n * Utility to get the time remaining before grant expires (in seconds)\n *\n * @param grantFile - The grant file to check time remaining for\n * @returns Number of seconds remaining, or null if no expiration is set\n */\nexport function getGrantTimeRemaining(grantFile: GrantFile): number | null {\n if (!grantFile.expires) {\n return null; // No expiration set\n }\n\n const now = Math.floor(Date.now() / 1000);\n const remaining = grantFile.expires - now;\n return Math.max(0, remaining);\n}\n\n/**\n * Creates a human-readable summary of a grant\n *\n * @param grantFile - The grant file to create a summary for\n * @returns A human-readable string describing the grant\n */\nexport function summarizeGrant(grantFile: GrantFile): string {\n const expiration = grantFile.expires\n ? new Date(grantFile.expires * 1000).toISOString()\n : \"No expiration\";\n\n return `Grant for ${grantFile.grantee} to perform \"${grantFile.operation}\" (expires: ${expiration})`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAcA,wBAIO;AACP,6BAAoD;AA6B7C,SAAS,qBAAqB,QAA0C;AAC7E,QAAM,gBAAY,mCAAgB,MAAM;AAGxC,MAAI;AACF,8CAAc,WAAW;AAAA,MACvB,QAAQ;AAAA,MACR,SAAS,OAAO;AAAA,MAChB,WAAW,OAAO;AAAA,IACpB,CAAC;AAAA,EACH,SAAS,OAAO;AACd,UAAM,IAAI;AAAA,MACR,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AAa,UAAU,UACzB,IAAI,KAAK,UAAU,UAAU,GAAI,EAAE,YAAY,IAC/C;AAEJ,SAAO,aAAa,UAAU,OAAO,gBAAgB,UAAU,SAAS,eAAe,UAAU;AACnG;","names":[]}
|
package/dist/utils/grants.d.ts
CHANGED
|
@@ -1,32 +1,113 @@
|
|
|
1
|
-
import type { Address } from "viem";
|
|
2
|
-
import type { GrantFile, GrantPermissionParams } from "../types/permissions";
|
|
3
1
|
/**
|
|
4
|
-
*
|
|
2
|
+
* Provides high-level grant management utilities for the Vana permission system.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* This module simplifies grant file creation, validation, storage, and retrieval.
|
|
6
|
+
* Grants are the core mechanism for permission management in Vana, allowing users
|
|
7
|
+
* to delegate specific operations to applications and services.
|
|
8
|
+
*
|
|
9
|
+
* @category Permissions
|
|
10
|
+
* @module grants
|
|
5
11
|
*/
|
|
12
|
+
import type { Address } from "viem";
|
|
13
|
+
import type { GrantFile, GrantPermissionParams } from "../types/permissions";
|
|
6
14
|
/**
|
|
7
|
-
* Creates and validates a grant file from permission parameters
|
|
15
|
+
* Creates and validates a grant file from permission parameters.
|
|
8
16
|
*
|
|
9
|
-
* @
|
|
10
|
-
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* Combines grant creation with immediate validation to ensure only valid
|
|
19
|
+
* grants are created. Validates schema compliance, grantee address, and
|
|
20
|
+
* operation parameters before returning the grant file.
|
|
21
|
+
*
|
|
22
|
+
* @param params - The permission parameters to create and validate the grant from.
|
|
23
|
+
* Obtain from user input or application configuration.
|
|
24
|
+
* @returns The validated grant file object ready for storage
|
|
25
|
+
*
|
|
26
|
+
* @throws {GrantValidationError} When grant parameters are invalid.
|
|
27
|
+
* Check error message for specific validation failures.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const grant = createValidatedGrant({
|
|
32
|
+
* grantee: '0x742d35Cc6634C0532925a3b844Bc9e7595f0b0Bb',
|
|
33
|
+
* operation: 'llm_inference',
|
|
34
|
+
* parameters: { model: 'gpt-4', maxTokens: 1000 },
|
|
35
|
+
* expiresAt: Date.now() + 86400000 // 24 hours
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @category Permissions
|
|
11
40
|
*/
|
|
12
41
|
export declare function createValidatedGrant(params: GrantPermissionParams): GrantFile;
|
|
13
42
|
/**
|
|
14
|
-
* Creates a grant file and stores it in IPFS
|
|
43
|
+
* Creates a grant file and stores it in IPFS.
|
|
44
|
+
*
|
|
45
|
+
* @remarks
|
|
46
|
+
* Combines grant creation, validation, and IPFS storage in a single operation.
|
|
47
|
+
* The grant is stored immutably on IPFS and can be referenced by its URL in
|
|
48
|
+
* on-chain permission records.
|
|
15
49
|
*
|
|
16
|
-
* @param params - The permission parameters to create the grant from
|
|
17
|
-
*
|
|
50
|
+
* @param params - The permission parameters to create the grant from.
|
|
51
|
+
* Obtain from user input or application configuration.
|
|
52
|
+
* @param relayerUrl - The URL of the relayer service for IPFS storage.
|
|
53
|
+
* Obtain from SDK configuration or environment.
|
|
18
54
|
* @returns Promise resolving to an object containing the grant file and its IPFS URL
|
|
55
|
+
*
|
|
56
|
+
* @throws {GrantValidationError} When grant parameters are invalid.
|
|
57
|
+
* Check error message for specific validation failures.
|
|
58
|
+
* @throws {Error} When IPFS storage fails.
|
|
59
|
+
* Retry with exponential backoff or check relayer status.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* const { grantFile, grantUrl } = await createAndStoreGrant(
|
|
64
|
+
* {
|
|
65
|
+
* grantee: applicationAddress,
|
|
66
|
+
* operation: 'data_processing',
|
|
67
|
+
* parameters: { dataTypes: ['medical', 'financial'] }
|
|
68
|
+
* },
|
|
69
|
+
* 'https://relayer.vana.org'
|
|
70
|
+
* );
|
|
71
|
+
*
|
|
72
|
+
* console.log('Grant stored at:', grantUrl);
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @category Permissions
|
|
19
76
|
*/
|
|
20
77
|
export declare function createAndStoreGrant(params: GrantPermissionParams, relayerUrl: string): Promise<{
|
|
21
78
|
grantFile: GrantFile;
|
|
22
79
|
grantUrl: string;
|
|
23
80
|
}>;
|
|
24
81
|
/**
|
|
25
|
-
* Retrieves and validates a grant file from IPFS
|
|
82
|
+
* Retrieves and validates a grant file from IPFS.
|
|
26
83
|
*
|
|
27
|
-
* @
|
|
28
|
-
*
|
|
84
|
+
* @remarks
|
|
85
|
+
* Fetches a grant file from IPFS and performs basic validation to ensure
|
|
86
|
+
* the retrieved data is a valid grant structure. Use this when you need to
|
|
87
|
+
* verify or process existing grants.
|
|
88
|
+
*
|
|
89
|
+
* @param grantUrl - The IPFS URL of the grant file to retrieve.
|
|
90
|
+
* Obtain from on-chain permission records or grant events.
|
|
91
|
+
* @param relayerUrl - Optional URL of the relayer service.
|
|
92
|
+
* If not provided, uses default IPFS gateways.
|
|
29
93
|
* @returns Promise resolving to the validated grant file
|
|
94
|
+
*
|
|
95
|
+
* @throws {Error} When grant retrieval fails.
|
|
96
|
+
* Check network connectivity or IPFS gateway availability.
|
|
97
|
+
* @throws {Error} When grant file is malformed.
|
|
98
|
+
* Verify the grant URL points to a valid grant file.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```typescript
|
|
102
|
+
* const grant = await retrieveAndValidateGrant(
|
|
103
|
+
* 'ipfs://QmXxx...'
|
|
104
|
+
* );
|
|
105
|
+
*
|
|
106
|
+
* console.log('Grant for:', grant.grantee);
|
|
107
|
+
* console.log('Operation:', grant.operation);
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* @category Permissions
|
|
30
111
|
*/
|
|
31
112
|
export declare function retrieveAndValidateGrant(grantUrl: string, relayerUrl?: string): Promise<GrantFile>;
|
|
32
113
|
/**
|
package/dist/utils/grants.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/grants.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../../src/utils/grants.ts"],"sourcesContent":["/**\n * Provides high-level grant management utilities for the Vana permission system.\n *\n * @remarks\n * This module simplifies grant file creation, validation, storage, and retrieval.\n * Grants are the core mechanism for permission management in Vana, allowing users\n * to delegate specific operations to applications and services.\n *\n * @category Permissions\n * @module grants\n */\n\nimport type { Address } from \"viem\";\nimport type { GrantFile, GrantPermissionParams } from \"../types/permissions\";\nimport {\n createGrantFile,\n storeGrantFile,\n retrieveGrantFile,\n} from \"./grantFiles\";\nimport { validateGrant, GrantValidationError } from \"./grantValidation\";\n\n/**\n * Creates and validates a grant file from permission parameters.\n *\n * @remarks\n * Combines grant creation with immediate validation to ensure only valid\n * grants are created. Validates schema compliance, grantee address, and\n * operation parameters before returning the grant file.\n *\n * @param params - The permission parameters to create and validate the grant from.\n * Obtain from user input or application configuration.\n * @returns The validated grant file object ready for storage\n *\n * @throws {GrantValidationError} When grant parameters are invalid.\n * Check error message for specific validation failures.\n *\n * @example\n * ```typescript\n * const grant = createValidatedGrant({\n * grantee: '0x742d35Cc6634C0532925a3b844Bc9e7595f0b0Bb',\n * operation: 'llm_inference',\n * parameters: { model: 'gpt-4', maxTokens: 1000 },\n * expiresAt: Date.now() + 86400000 // 24 hours\n * });\n * ```\n *\n * @category Permissions\n */\nexport function createValidatedGrant(params: GrantPermissionParams): GrantFile {\n const grantFile = createGrantFile(params);\n\n // Validate the created grant file\n try {\n validateGrant(grantFile, {\n schema: true,\n grantee: params.grantee,\n operation: params.operation,\n });\n } catch (error) {\n throw new GrantValidationError(\n `Created grant file failed validation: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n { grantFile, params },\n );\n }\n\n return grantFile;\n}\n\n/**\n * Creates a grant file and stores it in IPFS.\n *\n * @remarks\n * Combines grant creation, validation, and IPFS storage in a single operation.\n * The grant is stored immutably on IPFS and can be referenced by its URL in\n * on-chain permission records.\n *\n * @param params - The permission parameters to create the grant from.\n * Obtain from user input or application configuration.\n * @param relayerUrl - The URL of the relayer service for IPFS storage.\n * Obtain from SDK configuration or environment.\n * @returns Promise resolving to an object containing the grant file and its IPFS URL\n *\n * @throws {GrantValidationError} When grant parameters are invalid.\n * Check error message for specific validation failures.\n * @throws {Error} When IPFS storage fails.\n * Retry with exponential backoff or check relayer status.\n *\n * @example\n * ```typescript\n * const { grantFile, grantUrl } = await createAndStoreGrant(\n * {\n * grantee: applicationAddress,\n * operation: 'data_processing',\n * parameters: { dataTypes: ['medical', 'financial'] }\n * },\n * 'https://relayer.vana.org'\n * );\n *\n * console.log('Grant stored at:', grantUrl);\n * ```\n *\n * @category Permissions\n */\nexport async function createAndStoreGrant(\n params: GrantPermissionParams,\n relayerUrl: string,\n): Promise<{ grantFile: GrantFile; grantUrl: string }> {\n const grantFile = createValidatedGrant(params);\n const grantUrl = await storeGrantFile(grantFile, relayerUrl);\n\n return { grantFile, grantUrl };\n}\n\n/**\n * Retrieves and validates a grant file from IPFS.\n *\n * @remarks\n * Fetches a grant file from IPFS and performs basic validation to ensure\n * the retrieved data is a valid grant structure. Use this when you need to\n * verify or process existing grants.\n *\n * @param grantUrl - The IPFS URL of the grant file to retrieve.\n * Obtain from on-chain permission records or grant events.\n * @param relayerUrl - Optional URL of the relayer service.\n * If not provided, uses default IPFS gateways.\n * @returns Promise resolving to the validated grant file\n *\n * @throws {Error} When grant retrieval fails.\n * Check network connectivity or IPFS gateway availability.\n * @throws {Error} When grant file is malformed.\n * Verify the grant URL points to a valid grant file.\n *\n * @example\n * ```typescript\n * const grant = await retrieveAndValidateGrant(\n * 'ipfs://QmXxx...'\n * );\n *\n * console.log('Grant for:', grant.grantee);\n * console.log('Operation:', grant.operation);\n * ```\n *\n * @category Permissions\n */\nexport async function retrieveAndValidateGrant(\n grantUrl: string,\n relayerUrl?: string,\n): Promise<GrantFile> {\n const grantFile = await retrieveGrantFile(grantUrl, relayerUrl);\n\n // Additional validation can be added here if needed\n return grantFile;\n}\n\n/**\n * Checks if a grant allows access for a specific request\n *\n * @param grantUrl - The IPFS URL of the grant file to check\n * @param requestingAddress - The address making the access request\n * @param operation - The operation being requested\n * @param _fileIds - Array of file IDs being accessed (currently unused but part of interface)\n * @param relayerUrl - Optional URL of the relayer service\n * @returns Promise resolving to access result with allowed status, reason, and grant file\n */\nexport async function checkGrantAccess(\n grantUrl: string,\n requestingAddress: Address,\n operation: string,\n _fileIds: number[],\n relayerUrl?: string,\n): Promise<{ allowed: boolean; reason?: string; grantFile?: GrantFile }> {\n try {\n const grantFile = await retrieveAndValidateGrant(grantUrl, relayerUrl);\n\n // Validate the grant for the request\n validateGrant(grantFile, {\n schema: true,\n grantee: requestingAddress,\n operation,\n });\n\n return { allowed: true, grantFile };\n } catch (error) {\n if (error instanceof GrantValidationError) {\n return {\n allowed: false,\n reason: error.message,\n };\n }\n\n return {\n allowed: false,\n reason: `Grant access check failed: ${error instanceof Error ? error.message : \"Unknown error\"}`,\n };\n }\n}\n\n/**\n * Utility to check if a grant has expired\n *\n * @param grantFile - The grant file to check for expiration\n * @returns True if the grant has expired, false otherwise\n */\nexport function isGrantExpired(grantFile: GrantFile): boolean {\n if (!grantFile.expires) {\n return false; // No expiration set\n }\n\n const now = Math.floor(Date.now() / 1000);\n return now > grantFile.expires;\n}\n\n/**\n * Utility to get the time remaining before grant expires (in seconds)\n *\n * @param grantFile - The grant file to check time remaining for\n * @returns Number of seconds remaining, or null if no expiration is set\n */\nexport function getGrantTimeRemaining(grantFile: GrantFile): number | null {\n if (!grantFile.expires) {\n return null; // No expiration set\n }\n\n const now = Math.floor(Date.now() / 1000);\n const remaining = grantFile.expires - now;\n return Math.max(0, remaining);\n}\n\n/**\n * Creates a human-readable summary of a grant\n *\n * @param grantFile - The grant file to create a summary for\n * @returns A human-readable string describing the grant\n */\nexport function summarizeGrant(grantFile: GrantFile): string {\n const expiration = grantFile.expires\n ? new Date(grantFile.expires * 1000).toISOString()\n : \"No expiration\";\n\n return `Grant for ${grantFile.grantee} to perform \"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