@opendatalabs/vana-sdk 2.3.0 → 3.0.1-canary.b068ac6
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/README.md +76 -92
- package/dist/auth/errors.cjs +54 -0
- package/dist/auth/errors.cjs.map +1 -0
- package/dist/auth/errors.d.ts +26 -0
- package/dist/auth/errors.js +28 -0
- package/dist/auth/errors.js.map +1 -0
- package/dist/auth/pkce.cjs +100 -0
- package/dist/auth/pkce.cjs.map +1 -0
- package/dist/auth/pkce.d.ts +55 -0
- package/dist/auth/pkce.js +71 -0
- package/dist/auth/pkce.js.map +1 -0
- package/dist/auth/token-store.cjs +59 -0
- package/dist/auth/token-store.cjs.map +1 -0
- package/dist/auth/token-store.d.ts +61 -0
- package/dist/auth/token-store.js +35 -0
- package/dist/auth/token-store.js.map +1 -0
- package/dist/auth/web3-signed-builder.cjs +70 -0
- package/dist/auth/web3-signed-builder.cjs.map +1 -0
- package/dist/auth/web3-signed-builder.d.ts +47 -0
- package/dist/auth/web3-signed-builder.js +45 -0
- package/dist/auth/web3-signed-builder.js.map +1 -0
- package/dist/auth/web3-signed.cjs +150 -0
- package/dist/auth/web3-signed.cjs.map +1 -0
- package/dist/auth/web3-signed.d.ts +59 -0
- package/dist/auth/web3-signed.js +129 -0
- package/dist/auth/web3-signed.js.map +1 -0
- package/dist/chains/definitions.cjs +2 -6
- package/dist/chains/definitions.cjs.map +1 -1
- package/dist/chains/definitions.d.ts +1 -7
- package/dist/chains/definitions.js +2 -6
- package/dist/chains/definitions.js.map +1 -1
- package/dist/config/chains.d.ts +18 -0
- package/dist/config/contracts.config.cjs +7 -95
- package/dist/config/contracts.config.cjs.map +1 -1
- package/dist/config/contracts.config.d.ts +0 -54
- package/dist/config/contracts.config.js +6 -93
- package/dist/config/contracts.config.js.map +1 -1
- package/dist/config/default-services.cjs +0 -10
- package/dist/config/default-services.cjs.map +1 -1
- package/dist/config/default-services.d.ts +1 -20
- package/dist/config/default-services.js +0 -9
- package/dist/config/default-services.js.map +1 -1
- package/dist/crypto/ecies/interface.cjs +2 -0
- package/dist/crypto/ecies/interface.cjs.map +1 -1
- package/dist/crypto/ecies/interface.js +2 -0
- package/dist/crypto/ecies/interface.js.map +1 -1
- package/dist/crypto/ecies/node.cjs +6 -6
- package/dist/crypto/ecies/node.cjs.map +1 -1
- package/dist/crypto/ecies/node.js +1 -1
- package/dist/crypto/ecies/node.js.map +1 -1
- package/dist/crypto/envelope/openpgp.cjs +59 -0
- package/dist/crypto/envelope/openpgp.cjs.map +1 -0
- package/dist/crypto/envelope/openpgp.d.ts +28 -0
- package/dist/crypto/envelope/openpgp.js +24 -0
- package/dist/crypto/envelope/openpgp.js.map +1 -0
- package/dist/crypto/keys/derive.cjs +65 -0
- package/dist/crypto/keys/derive.cjs.map +1 -0
- package/dist/crypto/keys/derive.d.ts +45 -0
- package/dist/crypto/keys/derive.js +38 -0
- package/dist/crypto/keys/derive.js.map +1 -0
- package/dist/errors.cjs +10 -0
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.js +10 -0
- package/dist/errors.js.map +1 -1
- package/dist/generated/abi/index.cjs +2 -37
- package/dist/generated/abi/index.cjs.map +1 -1
- package/dist/generated/abi/index.d.ts +2683 -9296
- package/dist/generated/abi/index.js +2 -29
- package/dist/generated/abi/index.js.map +1 -1
- package/dist/generated/addresses.cjs +5 -107
- package/dist/generated/addresses.cjs.map +1 -1
- package/dist/generated/addresses.d.ts +5 -99
- package/dist/generated/addresses.js +5 -105
- package/dist/generated/addresses.js.map +1 -1
- package/dist/index.browser.d.ts +24 -140
- package/dist/index.browser.js +32258 -114
- package/dist/index.browser.js.map +7 -1
- package/dist/index.node.cjs +32980 -160
- package/dist/index.node.cjs.map +7 -1
- package/dist/index.node.d.ts +23 -210
- package/dist/index.node.js +32884 -133
- package/dist/index.node.js.map +7 -1
- package/dist/platform/node.cjs +2 -2
- package/dist/platform/node.cjs.map +1 -1
- package/dist/platform/node.js +1 -1
- package/dist/platform/node.js.map +1 -1
- package/dist/protocol/data-file.cjs +56 -0
- package/dist/protocol/data-file.cjs.map +1 -0
- package/dist/protocol/data-file.d.ts +20 -0
- package/dist/protocol/data-file.js +30 -0
- package/dist/protocol/data-file.js.map +1 -0
- package/dist/protocol/eip712.cjs +123 -0
- package/dist/protocol/eip712.cjs.map +1 -0
- package/dist/protocol/eip712.d.ts +117 -0
- package/dist/protocol/eip712.js +90 -0
- package/dist/protocol/eip712.js.map +1 -0
- package/dist/protocol/gateway.cjs +226 -0
- package/dist/protocol/gateway.cjs.map +1 -0
- package/dist/protocol/gateway.d.ts +120 -0
- package/dist/protocol/gateway.js +202 -0
- package/dist/protocol/gateway.js.map +1 -0
- package/dist/protocol/grants.cjs +146 -0
- package/dist/protocol/grants.cjs.map +1 -0
- package/dist/protocol/grants.d.ts +31 -0
- package/dist/protocol/grants.js +123 -0
- package/dist/protocol/grants.js.map +1 -0
- package/dist/protocol/scopes.cjs +78 -0
- package/dist/protocol/scopes.cjs.map +1 -0
- package/dist/protocol/scopes.d.ts +13 -0
- package/dist/protocol/scopes.js +50 -0
- package/dist/protocol/scopes.js.map +1 -0
- package/dist/{types/atomicStore.cjs → storage/default.cjs} +9 -8
- package/dist/storage/default.cjs.map +1 -0
- package/dist/storage/default.d.ts +4 -0
- package/dist/storage/default.js +8 -0
- package/dist/storage/default.js.map +1 -0
- package/dist/storage/index.cjs +11 -2
- package/dist/storage/index.cjs.map +1 -1
- package/dist/storage/index.d.ts +9 -0
- package/dist/storage/index.js +7 -1
- package/dist/storage/index.js.map +1 -1
- package/dist/storage/providers/callback-storage.cjs +1 -0
- package/dist/storage/providers/callback-storage.cjs.map +1 -1
- package/dist/storage/providers/callback-storage.js +1 -0
- package/dist/storage/providers/callback-storage.js.map +1 -1
- package/dist/storage/providers/dropbox.cjs +1 -0
- package/dist/storage/providers/dropbox.cjs.map +1 -1
- package/dist/storage/providers/dropbox.js +1 -0
- package/dist/storage/providers/dropbox.js.map +1 -1
- package/dist/storage/providers/google-drive.cjs +1 -0
- package/dist/storage/providers/google-drive.cjs.map +1 -1
- package/dist/storage/providers/google-drive.js +1 -0
- package/dist/storage/providers/google-drive.js.map +1 -1
- package/dist/storage/providers/ipfs.cjs +1 -0
- package/dist/storage/providers/ipfs.cjs.map +1 -1
- package/dist/storage/providers/ipfs.js +1 -0
- package/dist/storage/providers/ipfs.js.map +1 -1
- package/dist/storage/providers/pinata.cjs +1 -0
- package/dist/storage/providers/pinata.cjs.map +1 -1
- package/dist/storage/providers/pinata.js +1 -0
- package/dist/storage/providers/pinata.js.map +1 -1
- package/dist/storage/providers/r2.cjs +376 -0
- package/dist/storage/providers/r2.cjs.map +1 -0
- package/dist/storage/providers/r2.d.ts +91 -0
- package/dist/storage/providers/r2.js +354 -0
- package/dist/storage/providers/r2.js.map +1 -0
- package/dist/storage/providers/vana-storage.cjs +251 -0
- package/dist/storage/providers/vana-storage.cjs.map +1 -0
- package/dist/storage/providers/vana-storage.d.ts +100 -0
- package/dist/storage/providers/vana-storage.js +231 -0
- package/dist/storage/providers/vana-storage.js.map +1 -0
- package/dist/types/config.cjs +0 -34
- package/dist/types/config.cjs.map +1 -1
- package/dist/types/config.d.ts +1 -607
- package/dist/types/config.js +0 -22
- package/dist/types/config.js.map +1 -1
- package/dist/types/contracts.cjs.map +1 -1
- package/dist/types/contracts.d.ts +1 -1
- package/dist/types/index.cjs +2 -33
- package/dist/types/index.cjs.map +1 -1
- package/dist/types/index.d.ts +2 -33
- package/dist/types/index.js +1 -35
- package/dist/types/index.js.map +1 -1
- package/dist/types/ps-errors.cjs +91 -0
- package/dist/types/ps-errors.cjs.map +1 -0
- package/dist/types/ps-errors.d.ts +26 -0
- package/dist/types/ps-errors.js +66 -0
- package/dist/types/ps-errors.js.map +1 -0
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.ts +0 -29
- package/dist/types.js.map +1 -1
- package/package.json +8 -25
- package/dist/client/enhancedResponse.cjs +0 -164
- package/dist/client/enhancedResponse.cjs.map +0 -1
- package/dist/client/enhancedResponse.d.ts +0 -120
- package/dist/client/enhancedResponse.js +0 -138
- package/dist/client/enhancedResponse.js.map +0 -1
- package/dist/controllers/__tests__/data-consistency-integration.test.d.ts +0 -7
- package/dist/controllers/base.cjs +0 -116
- package/dist/controllers/base.cjs.map +0 -1
- package/dist/controllers/base.d.ts +0 -94
- package/dist/controllers/base.js +0 -92
- package/dist/controllers/base.js.map +0 -1
- package/dist/controllers/data.cjs +0 -2633
- package/dist/controllers/data.cjs.map +0 -1
- package/dist/controllers/data.d.ts +0 -1067
- package/dist/controllers/data.js +0 -2626
- package/dist/controllers/data.js.map +0 -1
- package/dist/controllers/operations.cjs +0 -430
- package/dist/controllers/operations.cjs.map +0 -1
- package/dist/controllers/operations.d.ts +0 -229
- package/dist/controllers/operations.js +0 -406
- package/dist/controllers/operations.js.map +0 -1
- package/dist/controllers/permissions.cjs +0 -4368
- package/dist/controllers/permissions.cjs.map +0 -1
- package/dist/controllers/permissions.d.ts +0 -1411
- package/dist/controllers/permissions.js +0 -4344
- package/dist/controllers/permissions.js.map +0 -1
- package/dist/controllers/protocol.cjs +0 -183
- package/dist/controllers/protocol.cjs.map +0 -1
- package/dist/controllers/protocol.d.ts +0 -138
- package/dist/controllers/protocol.js +0 -163
- package/dist/controllers/protocol.js.map +0 -1
- package/dist/controllers/schemas.cjs +0 -678
- package/dist/controllers/schemas.cjs.map +0 -1
- package/dist/controllers/schemas.d.ts +0 -293
- package/dist/controllers/schemas.js +0 -654
- package/dist/controllers/schemas.js.map +0 -1
- package/dist/controllers/server.cjs +0 -643
- package/dist/controllers/server.cjs.map +0 -1
- package/dist/controllers/server.d.ts +0 -322
- package/dist/controllers/server.js +0 -624
- package/dist/controllers/server.js.map +0 -1
- package/dist/controllers/staking.cjs +0 -626
- package/dist/controllers/staking.cjs.map +0 -1
- package/dist/controllers/staking.d.ts +0 -457
- package/dist/controllers/staking.js +0 -602
- package/dist/controllers/staking.js.map +0 -1
- package/dist/core/__tests__/pollingManager.test.d.ts +0 -4
- package/dist/core/apiClient.cjs +0 -378
- package/dist/core/apiClient.cjs.map +0 -1
- package/dist/core/apiClient.d.ts +0 -286
- package/dist/core/apiClient.js +0 -359
- package/dist/core/apiClient.js.map +0 -1
- package/dist/core/generics.cjs +0 -417
- package/dist/core/generics.cjs.map +0 -1
- package/dist/core/generics.d.ts +0 -205
- package/dist/core/generics.js +0 -386
- package/dist/core/generics.js.map +0 -1
- package/dist/core/health.cjs +0 -289
- package/dist/core/health.cjs.map +0 -1
- package/dist/core/health.d.ts +0 -143
- package/dist/core/health.js +0 -265
- package/dist/core/health.js.map +0 -1
- package/dist/core/inMemoryNonceManager.cjs +0 -138
- package/dist/core/inMemoryNonceManager.cjs.map +0 -1
- package/dist/core/inMemoryNonceManager.d.ts +0 -69
- package/dist/core/inMemoryNonceManager.js +0 -114
- package/dist/core/inMemoryNonceManager.js.map +0 -1
- package/dist/core/nonceManager.cjs +0 -304
- package/dist/core/nonceManager.cjs.map +0 -1
- package/dist/core/nonceManager.d.ts +0 -116
- package/dist/core/nonceManager.js +0 -280
- package/dist/core/nonceManager.js.map +0 -1
- package/dist/core/pollingManager.cjs +0 -292
- package/dist/core/pollingManager.cjs.map +0 -1
- package/dist/core/pollingManager.d.ts +0 -120
- package/dist/core/pollingManager.js +0 -268
- package/dist/core/pollingManager.js.map +0 -1
- package/dist/core.cjs +0 -781
- package/dist/core.cjs.map +0 -1
- package/dist/core.d.ts +0 -496
- package/dist/core.js +0 -756
- package/dist/core.js.map +0 -1
- package/dist/diagnostics.cjs +0 -37
- package/dist/diagnostics.cjs.map +0 -1
- package/dist/diagnostics.d.ts +0 -24
- package/dist/diagnostics.js +0 -13
- package/dist/diagnostics.js.map +0 -1
- package/dist/generated/abi/DLPPerformanceImplementation.cjs +0 -1202
- package/dist/generated/abi/DLPPerformanceImplementation.cjs.map +0 -1
- package/dist/generated/abi/DLPPerformanceImplementation.d.ts +0 -914
- package/dist/generated/abi/DLPPerformanceImplementation.js +0 -1178
- package/dist/generated/abi/DLPPerformanceImplementation.js.map +0 -1
- package/dist/generated/abi/DLPRewardDeployerImplementation.cjs +0 -1112
- package/dist/generated/abi/DLPRewardDeployerImplementation.cjs.map +0 -1
- package/dist/generated/abi/DLPRewardDeployerImplementation.d.ts +0 -840
- package/dist/generated/abi/DLPRewardDeployerImplementation.js +0 -1088
- package/dist/generated/abi/DLPRewardDeployerImplementation.js.map +0 -1
- package/dist/generated/abi/DLPRewardDeployerTreasuryImplementation.cjs +0 -612
- package/dist/generated/abi/DLPRewardDeployerTreasuryImplementation.cjs.map +0 -1
- package/dist/generated/abi/DLPRewardDeployerTreasuryImplementation.d.ts +0 -451
- package/dist/generated/abi/DLPRewardDeployerTreasuryImplementation.js +0 -588
- package/dist/generated/abi/DLPRewardDeployerTreasuryImplementation.js.map +0 -1
- package/dist/generated/abi/DLPRewardSwapImplementation.cjs +0 -939
- package/dist/generated/abi/DLPRewardSwapImplementation.cjs.map +0 -1
- package/dist/generated/abi/DLPRewardSwapImplementation.d.ts +0 -705
- package/dist/generated/abi/DLPRewardSwapImplementation.js +0 -915
- package/dist/generated/abi/DLPRewardSwapImplementation.js.map +0 -1
- package/dist/generated/abi/DLPRootImplementation.cjs +0 -1644
- package/dist/generated/abi/DLPRootImplementation.cjs.map +0 -1
- package/dist/generated/abi/DLPRootImplementation.d.ts +0 -1246
- package/dist/generated/abi/DLPRootImplementation.js +0 -1620
- package/dist/generated/abi/DLPRootImplementation.js.map +0 -1
- package/dist/generated/abi/DataLiquidityPoolImplementation.cjs +0 -985
- package/dist/generated/abi/DataLiquidityPoolImplementation.cjs.map +0 -1
- package/dist/generated/abi/DataLiquidityPoolImplementation.d.ts +0 -735
- package/dist/generated/abi/DataLiquidityPoolImplementation.js +0 -961
- package/dist/generated/abi/DataLiquidityPoolImplementation.js.map +0 -1
- package/dist/generated/abi/SwapHelperImplementation.cjs +0 -976
- package/dist/generated/abi/SwapHelperImplementation.cjs.map +0 -1
- package/dist/generated/abi/SwapHelperImplementation.d.ts +0 -728
- package/dist/generated/abi/SwapHelperImplementation.js +0 -952
- package/dist/generated/abi/SwapHelperImplementation.js.map +0 -1
- package/dist/generated/abi/TeePoolImplementation.cjs +0 -1313
- package/dist/generated/abi/TeePoolImplementation.cjs.map +0 -1
- package/dist/generated/abi/TeePoolImplementation.d.ts +0 -992
- package/dist/generated/abi/TeePoolImplementation.js +0 -1289
- package/dist/generated/abi/TeePoolImplementation.js.map +0 -1
- package/dist/generated/event-types.cjs +0 -17
- package/dist/generated/event-types.cjs.map +0 -1
- package/dist/generated/event-types.d.ts +0 -816
- package/dist/generated/event-types.js +0 -1
- package/dist/generated/event-types.js.map +0 -1
- package/dist/generated/eventRegistry.cjs +0 -4512
- package/dist/generated/eventRegistry.cjs.map +0 -1
- package/dist/generated/eventRegistry.d.ts +0 -14
- package/dist/generated/eventRegistry.js +0 -4487
- package/dist/generated/eventRegistry.js.map +0 -1
- package/dist/generated/server/server-exports.cjs +0 -45
- package/dist/generated/server/server-exports.cjs.map +0 -1
- package/dist/generated/server/server-exports.d.ts +0 -36
- package/dist/generated/server/server-exports.js +0 -19
- package/dist/generated/server/server-exports.js.map +0 -1
- package/dist/generated/server/server.cjs +0 -17
- package/dist/generated/server/server.cjs.map +0 -1
- package/dist/generated/server/server.d.ts +0 -907
- package/dist/generated/server/server.js +0 -1
- package/dist/generated/server/server.js.map +0 -1
- package/dist/generated/subgraph.cjs +0 -1440
- package/dist/generated/subgraph.cjs.map +0 -1
- package/dist/generated/subgraph.d.ts +0 -6113
- package/dist/generated/subgraph.js +0 -1404
- package/dist/generated/subgraph.js.map +0 -1
- package/dist/lib/__tests__/redisAtomicStore.test.d.ts +0 -1
- package/dist/lib/redisAtomicStore.cjs +0 -201
- package/dist/lib/redisAtomicStore.cjs.map +0 -1
- package/dist/lib/redisAtomicStore.d.ts +0 -120
- package/dist/lib/redisAtomicStore.js +0 -177
- package/dist/lib/redisAtomicStore.js.map +0 -1
- package/dist/server/relayerHandler.cjs +0 -452
- package/dist/server/relayerHandler.cjs.map +0 -1
- package/dist/server/relayerHandler.d.ts +0 -69
- package/dist/server/relayerHandler.js +0 -428
- package/dist/server/relayerHandler.js.map +0 -1
- package/dist/tests/abi.test.d.ts +0 -1
- package/dist/tests/chains-definitions.test.d.ts +0 -1
- package/dist/tests/core-encryption.test.d.ts +0 -1
- package/dist/tests/core-extended.test.d.ts +0 -1
- package/dist/tests/core-generics-coverage.test.d.ts +0 -1
- package/dist/tests/coverage-boost.test.d.ts +0 -1
- package/dist/tests/crypto-cross-platform-compatibility.test.d.ts +0 -1
- package/dist/tests/data-addfile-permissions-schema.test.d.ts +0 -1
- package/dist/tests/data-additional-methods.test.d.ts +0 -1
- package/dist/tests/data-controller-edge-cases.test.d.ts +0 -1
- package/dist/tests/data-ipfs-gateways.test.d.ts +0 -1
- package/dist/tests/data-relayer.test.d.ts +0 -1
- package/dist/tests/data-schema-validation.test.d.ts +0 -1
- package/dist/tests/data-simple-methods.test.d.ts +0 -1
- package/dist/tests/data-upload-owner-validation.test.d.ts +0 -1
- package/dist/tests/data.test.d.ts +0 -1
- package/dist/tests/demo-integration.test.d.ts +0 -1
- package/dist/tests/demo-trusted-server-integration.test.d.ts +0 -1
- package/dist/tests/download-relayer.test.d.ts +0 -1
- package/dist/tests/dual-mode-permissions.test.d.ts +0 -1
- package/dist/tests/dual-mode-trusted-servers.test.d.ts +0 -1
- package/dist/tests/encryption-correct-implementation.test.d.ts +0 -1
- package/dist/tests/encryption-coverage.test.d.ts +0 -1
- package/dist/tests/encryption-edge-cases.test.d.ts +0 -1
- package/dist/tests/encryption-utils-updated.test.d.ts +0 -1
- package/dist/tests/errors-coverage.test.d.ts +0 -1
- package/dist/tests/factories/mockFactory.d.ts +0 -316
- package/dist/tests/fakes/FakeStorageManager.d.ts +0 -200
- package/dist/tests/fakes/FakeStorageManager.test.d.ts +0 -1
- package/dist/tests/fakes/FakeWaitForTransactionEvents.d.ts +0 -170
- package/dist/tests/fakes/FakeWaitForTransactionEvents.test.d.ts +0 -1
- package/dist/tests/fakes/fake-pgp-port.d.ts +0 -13
- package/dist/tests/grantValidation-edge-cases.test.d.ts +0 -1
- package/dist/tests/grantValidation-unreachable-branch.test.d.ts +0 -1
- package/dist/tests/helper-methods.test.d.ts +0 -1
- package/dist/tests/helpers/typedMocks.d.ts +0 -64
- package/dist/tests/index-browser.test.d.ts +0 -1
- package/dist/tests/index-node.test.d.ts +0 -1
- package/dist/tests/index.test.d.ts +0 -1
- package/dist/tests/mocks/platformAdapter.d.ts +0 -12
- package/dist/tests/new-permissions-methods.test.d.ts +0 -1
- package/dist/tests/no-buffer-browser.test.d.ts +0 -1
- package/dist/tests/permissions-grantee.test.d.ts +0 -1
- package/dist/tests/permissions-revoke-relayer.test.d.ts +0 -1
- package/dist/tests/permissions-schema-validation.test.d.ts +0 -1
- package/dist/tests/permissions-server-files.test.d.ts +0 -1
- package/dist/tests/permissions-transaction-options.test.d.ts +0 -1
- package/dist/tests/permissions-trust-servers.test.d.ts +0 -1
- package/dist/tests/permissions.test.d.ts +0 -1
- package/dist/tests/personal.test.d.ts +0 -1
- package/dist/tests/platform-browser.test.d.ts +0 -1
- package/dist/tests/platform-crypto-expanded.test.d.ts +0 -1
- package/dist/tests/platform-crypto.test.d.ts +0 -1
- package/dist/tests/platform-index.test.d.ts +0 -1
- package/dist/tests/platform-node.test.d.ts +0 -1
- package/dist/tests/platform-shared-utils.test.d.ts +0 -1
- package/dist/tests/platform-updated.test.d.ts +0 -1
- package/dist/tests/protocol-additional-methods.test.d.ts +0 -1
- package/dist/tests/protocol.test.d.ts +0 -1
- package/dist/tests/read-only-mode.test.d.ts +0 -1
- package/dist/tests/relayer-integration.test.d.ts +0 -1
- package/dist/tests/relayer-unified.test.d.ts +0 -1
- package/dist/tests/schemas.test.d.ts +0 -1
- package/dist/tests/server-relayer-handler.test.d.ts +0 -1
- package/dist/tests/signatureFormatter.test.d.ts +0 -1
- package/dist/tests/staking.test.d.ts +0 -1
- package/dist/tests/trusted-server-queries.test.d.ts +0 -1
- package/dist/tests/typedDataConverter.test.d.ts +0 -1
- package/dist/tests/types-contracts.test.d.ts +0 -1
- package/dist/tests/types-data.test.d.ts +0 -1
- package/dist/tests/types-external-apis.test.d.ts +0 -1
- package/dist/tests/types-generics.test.d.ts +0 -1
- package/dist/tests/types-permissions.test.d.ts +0 -1
- package/dist/tests/types-upload-params.test.d.ts +0 -1
- package/dist/tests/types.test.d.ts +0 -1
- package/dist/tests/utils-formatters.test.d.ts +0 -1
- package/dist/tests/utils-grantFiles-edge-cases.test.d.ts +0 -1
- package/dist/tests/utils-grantFiles-validation.test.d.ts +0 -1
- package/dist/tests/utils-grantFiles.test.d.ts +0 -1
- package/dist/tests/utils-grantValidation-consolidated.test.d.ts +0 -1
- package/dist/tests/utils-grants.test.d.ts +0 -1
- package/dist/tests/utils-ipfs-additional.test.d.ts +0 -1
- package/dist/tests/utils-ipfs.test.d.ts +0 -4
- package/dist/tests/utils-schemaValidation.test.d.ts +0 -1
- package/dist/tests/vana.test.d.ts +0 -1
- package/dist/tests/wallet-crypto-compatibility.test.d.ts +0 -1
- package/dist/types/atomicStore.cjs.map +0 -1
- package/dist/types/atomicStore.d.ts +0 -236
- package/dist/types/atomicStore.js +0 -7
- package/dist/types/atomicStore.js.map +0 -1
- package/dist/types/blockchain.cjs +0 -17
- package/dist/types/blockchain.cjs.map +0 -1
- package/dist/types/blockchain.d.ts +0 -85
- package/dist/types/blockchain.js +0 -1
- package/dist/types/blockchain.js.map +0 -1
- package/dist/types/controller-context.cjs +0 -17
- package/dist/types/controller-context.cjs.map +0 -1
- package/dist/types/controller-context.d.ts +0 -68
- package/dist/types/controller-context.js +0 -1
- package/dist/types/controller-context.js.map +0 -1
- package/dist/types/data.cjs +0 -17
- package/dist/types/data.cjs.map +0 -1
- package/dist/types/data.d.ts +0 -763
- package/dist/types/data.js +0 -1
- package/dist/types/data.js.map +0 -1
- package/dist/types/external-apis.cjs +0 -61
- package/dist/types/external-apis.cjs.map +0 -1
- package/dist/types/external-apis.d.ts +0 -184
- package/dist/types/external-apis.js +0 -34
- package/dist/types/external-apis.js.map +0 -1
- package/dist/types/generics.cjs +0 -17
- package/dist/types/generics.cjs.map +0 -1
- package/dist/types/generics.d.ts +0 -518
- package/dist/types/generics.js +0 -1
- package/dist/types/generics.js.map +0 -1
- package/dist/types/operationStore.cjs +0 -17
- package/dist/types/operationStore.cjs.map +0 -1
- package/dist/types/operationStore.d.ts +0 -171
- package/dist/types/operationStore.js +0 -1
- package/dist/types/operationStore.js.map +0 -1
- package/dist/types/operations.cjs +0 -53
- package/dist/types/operations.cjs.map +0 -1
- package/dist/types/operations.d.ts +0 -204
- package/dist/types/operations.js +0 -26
- package/dist/types/operations.js.map +0 -1
- package/dist/types/options.cjs +0 -17
- package/dist/types/options.cjs.map +0 -1
- package/dist/types/options.d.ts +0 -308
- package/dist/types/options.js +0 -1
- package/dist/types/options.js.map +0 -1
- package/dist/types/permissions.cjs +0 -17
- package/dist/types/permissions.cjs.map +0 -1
- package/dist/types/permissions.d.ts +0 -955
- package/dist/types/permissions.js +0 -1
- package/dist/types/permissions.js.map +0 -1
- package/dist/types/personal.cjs +0 -17
- package/dist/types/personal.cjs.map +0 -1
- package/dist/types/personal.d.ts +0 -174
- package/dist/types/personal.js +0 -1
- package/dist/types/personal.js.map +0 -1
- package/dist/types/relayer.cjs +0 -17
- package/dist/types/relayer.cjs.map +0 -1
- package/dist/types/relayer.d.ts +0 -552
- package/dist/types/relayer.js +0 -1
- package/dist/types/relayer.js.map +0 -1
- package/dist/types/transactionResults.cjs +0 -17
- package/dist/types/transactionResults.cjs.map +0 -1
- package/dist/types/transactionResults.d.ts +0 -193
- package/dist/types/transactionResults.js +0 -1
- package/dist/types/transactionResults.js.map +0 -1
- package/dist/types/utils.cjs +0 -17
- package/dist/types/utils.cjs.map +0 -1
- package/dist/types/utils.d.ts +0 -771
- package/dist/types/utils.js +0 -1
- package/dist/types/utils.js.map +0 -1
- package/dist/utils/__tests__/chainQuery.test.d.ts +0 -1
- package/dist/utils/__tests__/parseTransaction.test.d.ts +0 -1
- package/dist/utils/__tests__/pojo-serialization.test.d.ts +0 -1
- package/dist/utils/__tests__/signatureCache.test.d.ts +0 -1
- package/dist/utils/__tests__/subgraphConsistency.test.d.ts +0 -4
- package/dist/utils/__tests__/subgraphPagination.test.d.ts +0 -4
- package/dist/utils/__tests__/transaction-edge-cases.test.d.ts +0 -1
- package/dist/utils/__tests__/transactionHelpers.test.d.ts +0 -1
- package/dist/utils/__tests__/urlResolver.test.d.ts +0 -4
- package/dist/utils/blockchain/registry.cjs +0 -81
- package/dist/utils/blockchain/registry.cjs.map +0 -1
- package/dist/utils/blockchain/registry.d.ts +0 -32
- package/dist/utils/blockchain/registry.js +0 -56
- package/dist/utils/blockchain/registry.js.map +0 -1
- package/dist/utils/blockchain/registry.test.d.ts +0 -1
- package/dist/utils/chainQuery.cjs +0 -107
- package/dist/utils/chainQuery.cjs.map +0 -1
- package/dist/utils/chainQuery.d.ts +0 -31
- package/dist/utils/chainQuery.js +0 -82
- package/dist/utils/chainQuery.js.map +0 -1
- package/dist/utils/download.cjs +0 -69
- package/dist/utils/download.cjs.map +0 -1
- package/dist/utils/download.d.ts +0 -40
- package/dist/utils/download.js +0 -45
- package/dist/utils/download.js.map +0 -1
- package/dist/utils/encryption.cjs +0 -176
- package/dist/utils/encryption.cjs.map +0 -1
- package/dist/utils/encryption.d.ts +0 -271
- package/dist/utils/encryption.js +0 -142
- package/dist/utils/encryption.js.map +0 -1
- package/dist/utils/formatters.cjs +0 -55
- package/dist/utils/formatters.cjs.map +0 -1
- package/dist/utils/formatters.d.ts +0 -118
- package/dist/utils/formatters.js +0 -28
- package/dist/utils/formatters.js.map +0 -1
- package/dist/utils/grantFiles.cjs +0 -181
- package/dist/utils/grantFiles.cjs.map +0 -1
- package/dist/utils/grantFiles.d.ts +0 -172
- package/dist/utils/grantFiles.js +0 -143
- package/dist/utils/grantFiles.js.map +0 -1
- package/dist/utils/grantValidation.cjs +0 -243
- package/dist/utils/grantValidation.cjs.map +0 -1
- package/dist/utils/grantValidation.d.ts +0 -226
- package/dist/utils/grantValidation.js +0 -201
- package/dist/utils/grantValidation.js.map +0 -1
- package/dist/utils/grants.cjs +0 -108
- package/dist/utils/grants.cjs.map +0 -1
- package/dist/utils/grants.d.ts +0 -148
- package/dist/utils/grants.js +0 -82
- package/dist/utils/grants.js.map +0 -1
- package/dist/utils/ipfs.cjs +0 -128
- package/dist/utils/ipfs.cjs.map +0 -1
- package/dist/utils/ipfs.d.ts +0 -88
- package/dist/utils/ipfs.js +0 -97
- package/dist/utils/ipfs.js.map +0 -1
- package/dist/utils/multicall.cjs +0 -233
- package/dist/utils/multicall.cjs.map +0 -1
- package/dist/utils/multicall.d.ts +0 -126
- package/dist/utils/multicall.js +0 -208
- package/dist/utils/multicall.js.map +0 -1
- package/dist/utils/parseTransactionPojo.cjs +0 -87
- package/dist/utils/parseTransactionPojo.cjs.map +0 -1
- package/dist/utils/parseTransactionPojo.d.ts +0 -31
- package/dist/utils/parseTransactionPojo.js +0 -63
- package/dist/utils/parseTransactionPojo.js.map +0 -1
- package/dist/utils/schemaValidation.cjs +0 -258
- package/dist/utils/schemaValidation.cjs.map +0 -1
- package/dist/utils/schemaValidation.d.ts +0 -168
- package/dist/utils/schemaValidation.js +0 -219
- package/dist/utils/schemaValidation.js.map +0 -1
- package/dist/utils/signatureCache.cjs +0 -192
- package/dist/utils/signatureCache.cjs.map +0 -1
- package/dist/utils/signatureCache.d.ts +0 -172
- package/dist/utils/signatureCache.js +0 -167
- package/dist/utils/signatureCache.js.map +0 -1
- package/dist/utils/signatureFormatter.cjs +0 -42
- package/dist/utils/signatureFormatter.cjs.map +0 -1
- package/dist/utils/signatureFormatter.d.ts +0 -36
- package/dist/utils/signatureFormatter.js +0 -18
- package/dist/utils/signatureFormatter.js.map +0 -1
- package/dist/utils/subgraphConsistency.cjs +0 -184
- package/dist/utils/subgraphConsistency.cjs.map +0 -1
- package/dist/utils/subgraphConsistency.d.ts +0 -65
- package/dist/utils/subgraphConsistency.js +0 -155
- package/dist/utils/subgraphConsistency.js.map +0 -1
- package/dist/utils/subgraphMetaCache.cjs +0 -101
- package/dist/utils/subgraphMetaCache.cjs.map +0 -1
- package/dist/utils/subgraphMetaCache.d.ts +0 -56
- package/dist/utils/subgraphMetaCache.js +0 -76
- package/dist/utils/subgraphMetaCache.js.map +0 -1
- package/dist/utils/subgraphPagination.cjs +0 -104
- package/dist/utils/subgraphPagination.cjs.map +0 -1
- package/dist/utils/subgraphPagination.d.ts +0 -78
- package/dist/utils/subgraphPagination.js +0 -78
- package/dist/utils/subgraphPagination.js.map +0 -1
- package/dist/utils/tests/multicall.test.d.ts +0 -1
- package/dist/utils/transactionHelpers.cjs +0 -54
- package/dist/utils/transactionHelpers.cjs.map +0 -1
- package/dist/utils/transactionHelpers.d.ts +0 -80
- package/dist/utils/transactionHelpers.js +0 -29
- package/dist/utils/transactionHelpers.js.map +0 -1
- package/dist/utils/typeGuards.cjs +0 -109
- package/dist/utils/typeGuards.cjs.map +0 -1
- package/dist/utils/typeGuards.d.ts +0 -138
- package/dist/utils/typeGuards.js +0 -74
- package/dist/utils/typeGuards.js.map +0 -1
- package/dist/utils/typedDataConverter.cjs +0 -43
- package/dist/utils/typedDataConverter.cjs.map +0 -1
- package/dist/utils/typedDataConverter.d.ts +0 -46
- package/dist/utils/typedDataConverter.js +0 -19
- package/dist/utils/typedDataConverter.js.map +0 -1
- package/dist/utils/urlResolver.cjs +0 -62
- package/dist/utils/urlResolver.cjs.map +0 -1
- package/dist/utils/urlResolver.d.ts +0 -56
- package/dist/utils/urlResolver.js +0 -37
- package/dist/utils/urlResolver.js.map +0 -1
- package/dist/utils/wallet.cjs +0 -63
- package/dist/utils/wallet.cjs.map +0 -1
- package/dist/utils/wallet.d.ts +0 -94
- package/dist/utils/wallet.js +0 -37
- package/dist/utils/wallet.js.map +0 -1
- package/dist/utils/withEvents.cjs +0 -44
- package/dist/utils/withEvents.cjs.map +0 -1
- package/dist/utils/withEvents.d.ts +0 -56
- package/dist/utils/withEvents.js +0 -18
- package/dist/utils/withEvents.js.map +0 -1
- /package/dist/{__tests__/waitForTransactionEvents.test.d.ts → auth/pkce.test.d.ts} +0 -0
- /package/dist/{client/__tests__/enhancedResponse.test.d.ts → auth/token-store.test.d.ts} +0 -0
- /package/dist/{controllers/__tests__/operations.processQueue.test.d.ts → auth/web3-signed.test.d.ts} +0 -0
- /package/dist/{controllers/__tests__/schemas-edge-cases.test.d.ts → crypto/envelope/openpgp.test.d.ts} +0 -0
- /package/dist/{controllers/data-error-handling.test.d.ts → crypto/keys/derive.test.d.ts} +0 -0
- /package/dist/{tests/errors.test.d.ts → errors.test.d.ts} +0 -0
- /package/dist/{controllers/server-additional.test.d.ts → protocol/data-file.test.d.ts} +0 -0
- /package/dist/{core/__tests__/health.test.d.ts → protocol/eip712.test.d.ts} +0 -0
- /package/dist/{core/__tests__/inMemoryNonceManager.test.d.ts → protocol/gateway.test.d.ts} +0 -0
- /package/dist/{core/__tests__/nonceManager.test.d.ts → protocol/grants.test.d.ts} +0 -0
- /package/dist/{core/core.test.d.ts → protocol/scopes.test.d.ts} +0 -0
- /package/dist/{core/tests/apiClient.test.d.ts → storage/tests/defaultStorage.test.d.ts} +0 -0
- /package/dist/{core/tests/client.test.d.ts → storage/tests/r2Storage.test.d.ts} +0 -0
- /package/dist/{core/tests/generics.test.d.ts → storage/tests/vanaStorage.test.d.ts} +0 -0
- /package/dist/{diagnostics.test.d.ts → types/ps-errors.test.d.ts} +0 -0
package/dist/controllers/data.js
DELETED
|
@@ -1,2626 +0,0 @@
|
|
|
1
|
-
import { getContract } from "viem";
|
|
2
|
-
import { PollingManager } from "../core/pollingManager";
|
|
3
|
-
import { BaseController } from "./base";
|
|
4
|
-
import { getContractAddress } from "../generated/addresses";
|
|
5
|
-
import { getAbi } from "../generated/abi";
|
|
6
|
-
import { InvalidConfigurationError } from "../errors";
|
|
7
|
-
import {
|
|
8
|
-
GetUserFilesPaginatedDocument,
|
|
9
|
-
GetFileProofsDocument,
|
|
10
|
-
GetDlpDocument,
|
|
11
|
-
GetUserPermissionsPaginatedDocument,
|
|
12
|
-
GetUserTrustedServersPaginatedDocument
|
|
13
|
-
} from "../generated/subgraph";
|
|
14
|
-
import { print } from "graphql";
|
|
15
|
-
import {
|
|
16
|
-
checkSubgraphConsistency,
|
|
17
|
-
fetchSubgraphMeta
|
|
18
|
-
} from "../utils/subgraphConsistency";
|
|
19
|
-
import {
|
|
20
|
-
executePaginatedQuery,
|
|
21
|
-
mapOrderByToEnum,
|
|
22
|
-
mapOrderDirection
|
|
23
|
-
} from "../utils/subgraphPagination";
|
|
24
|
-
import {
|
|
25
|
-
getUserFilesFromChain,
|
|
26
|
-
determineDataSource
|
|
27
|
-
} from "../utils/chainQuery";
|
|
28
|
-
import {
|
|
29
|
-
generateEncryptionKey,
|
|
30
|
-
decryptBlobWithSignedKey,
|
|
31
|
-
DEFAULT_ENCRYPTION_SEED,
|
|
32
|
-
encryptBlobWithSignedKey,
|
|
33
|
-
encryptWithWalletPublicKey,
|
|
34
|
-
decryptWithWalletPrivateKey
|
|
35
|
-
} from "../utils/encryption";
|
|
36
|
-
import {
|
|
37
|
-
validateDataSchemaAgainstMetaSchema,
|
|
38
|
-
validateDataAgainstSchema,
|
|
39
|
-
fetchAndValidateSchema
|
|
40
|
-
} from "../utils/schemaValidation";
|
|
41
|
-
import { gasAwareMulticall } from "../utils/multicall";
|
|
42
|
-
class DataController extends BaseController {
|
|
43
|
-
constructor(context) {
|
|
44
|
-
super(context);
|
|
45
|
-
}
|
|
46
|
-
async upload(params) {
|
|
47
|
-
this.assertWallet();
|
|
48
|
-
const {
|
|
49
|
-
content,
|
|
50
|
-
filename,
|
|
51
|
-
schemaId,
|
|
52
|
-
permissions = [],
|
|
53
|
-
encrypt = true,
|
|
54
|
-
providerName,
|
|
55
|
-
owner,
|
|
56
|
-
schemaValidation = "strict"
|
|
57
|
-
} = params;
|
|
58
|
-
if (encrypt && owner && owner.toLowerCase() !== this.context.userAddress.toLowerCase()) {
|
|
59
|
-
throw new InvalidConfigurationError(
|
|
60
|
-
"The 'owner' parameter cannot be different from the connected wallet's address when encryption is enabled. This would create an un-decryptable file."
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
try {
|
|
64
|
-
let isValid = true;
|
|
65
|
-
let validationErrors = [];
|
|
66
|
-
if (schemaId !== void 0 && schemaValidation !== "skip") {
|
|
67
|
-
try {
|
|
68
|
-
const { SchemaController } = await import("./schemas");
|
|
69
|
-
const schemaController = new SchemaController(this.context);
|
|
70
|
-
const schema = await schemaController.get(schemaId);
|
|
71
|
-
let parsedContent;
|
|
72
|
-
if (typeof content === "string") {
|
|
73
|
-
try {
|
|
74
|
-
parsedContent = JSON.parse(content);
|
|
75
|
-
} catch {
|
|
76
|
-
parsedContent = content;
|
|
77
|
-
}
|
|
78
|
-
} else if (content instanceof Blob) {
|
|
79
|
-
const text = await content.text();
|
|
80
|
-
try {
|
|
81
|
-
parsedContent = JSON.parse(text);
|
|
82
|
-
} catch {
|
|
83
|
-
parsedContent = text;
|
|
84
|
-
}
|
|
85
|
-
} else {
|
|
86
|
-
parsedContent = content;
|
|
87
|
-
}
|
|
88
|
-
validateDataAgainstSchema(parsedContent, schema);
|
|
89
|
-
} catch (error) {
|
|
90
|
-
if (schemaValidation === "strict") {
|
|
91
|
-
throw error;
|
|
92
|
-
} else if (schemaValidation === "warn") {
|
|
93
|
-
console.warn(
|
|
94
|
-
'[Vana SDK] Schema validation failed, but continuing due to validation mode "warn"'
|
|
95
|
-
);
|
|
96
|
-
if (error instanceof Error) {
|
|
97
|
-
console.warn(" Validation error:", error.message);
|
|
98
|
-
if (typeof error === "object" && "errors" in error && Array.isArray(error.errors)) {
|
|
99
|
-
console.warn(" Detailed errors:", error.errors);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
isValid = false;
|
|
103
|
-
validationErrors = error instanceof Error ? [error.message] : ["Schema validation failed"];
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
const uploadResult = await this.uploadToStorage(
|
|
108
|
-
content,
|
|
109
|
-
filename,
|
|
110
|
-
encrypt,
|
|
111
|
-
providerName
|
|
112
|
-
);
|
|
113
|
-
const userAddress = owner ?? this.context.userAddress;
|
|
114
|
-
let encryptedPermissions = [];
|
|
115
|
-
if (permissions.length > 0 && encrypt) {
|
|
116
|
-
this.assertWallet();
|
|
117
|
-
const userEncryptionKey = await generateEncryptionKey(
|
|
118
|
-
this.context.walletClient,
|
|
119
|
-
this.context.platform,
|
|
120
|
-
DEFAULT_ENCRYPTION_SEED
|
|
121
|
-
);
|
|
122
|
-
encryptedPermissions = await Promise.all(
|
|
123
|
-
permissions.map(async (permission) => {
|
|
124
|
-
const encryptedKey = await encryptWithWalletPublicKey(
|
|
125
|
-
userEncryptionKey,
|
|
126
|
-
permission.publicKey,
|
|
127
|
-
this.context.platform
|
|
128
|
-
);
|
|
129
|
-
return {
|
|
130
|
-
account: permission.account,
|
|
131
|
-
key: encryptedKey
|
|
132
|
-
};
|
|
133
|
-
})
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
let result;
|
|
137
|
-
if (this.context.relayer) {
|
|
138
|
-
const request = {
|
|
139
|
-
type: "direct",
|
|
140
|
-
operation: "submitFileAdditionComplete",
|
|
141
|
-
params: {
|
|
142
|
-
url: uploadResult.url,
|
|
143
|
-
userAddress,
|
|
144
|
-
permissions: encryptedPermissions,
|
|
145
|
-
schemaId: schemaId ?? 0,
|
|
146
|
-
ownerAddress: owner
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
const response = await this.context.relayer(request);
|
|
150
|
-
if (response.type === "error") {
|
|
151
|
-
throw new Error(response.error);
|
|
152
|
-
}
|
|
153
|
-
if (response.type === "pending") {
|
|
154
|
-
result = await this.pollRelayerForConfirmation(
|
|
155
|
-
response.operationId,
|
|
156
|
-
void 0
|
|
157
|
-
// TODO: Add TransactionOptions to upload method signature
|
|
158
|
-
);
|
|
159
|
-
} else if (response.type === "direct" && typeof response.result === "object" && response.result !== null && "fileId" in response.result) {
|
|
160
|
-
result = response.result;
|
|
161
|
-
} else {
|
|
162
|
-
throw new Error("Invalid response from relayer");
|
|
163
|
-
}
|
|
164
|
-
} else {
|
|
165
|
-
const txResult = await this.addFileWithEncryptedPermissionsAndSchema(
|
|
166
|
-
uploadResult.url,
|
|
167
|
-
userAddress,
|
|
168
|
-
encryptedPermissions,
|
|
169
|
-
schemaId ?? 0
|
|
170
|
-
);
|
|
171
|
-
if (!this.context.waitForTransactionEvents) {
|
|
172
|
-
throw new Error(
|
|
173
|
-
"Cannot upload without relay: waitForTransactionEvents not configured"
|
|
174
|
-
);
|
|
175
|
-
}
|
|
176
|
-
const eventResult = await this.context.waitForTransactionEvents(txResult);
|
|
177
|
-
const fileAddedEvent = eventResult.expectedEvents.FileAdded;
|
|
178
|
-
if (!fileAddedEvent) {
|
|
179
|
-
throw new Error("FileAdded event not found in transaction");
|
|
180
|
-
}
|
|
181
|
-
result = {
|
|
182
|
-
fileId: Number(fileAddedEvent.fileId),
|
|
183
|
-
transactionHash: txResult.hash
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
return {
|
|
187
|
-
fileId: result.fileId,
|
|
188
|
-
url: uploadResult.url,
|
|
189
|
-
transactionHash: result.transactionHash,
|
|
190
|
-
size: uploadResult.size,
|
|
191
|
-
isValid,
|
|
192
|
-
validationErrors: validationErrors.length > 0 ? validationErrors : void 0
|
|
193
|
-
};
|
|
194
|
-
} catch (error) {
|
|
195
|
-
throw new Error(
|
|
196
|
-
`Upload failed: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
197
|
-
);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
/**
|
|
201
|
-
* Encrypts data using wallet-derived encryption.
|
|
202
|
-
*
|
|
203
|
-
* @remarks
|
|
204
|
-
* This method provides secure, wallet-based encryption for data before uploading
|
|
205
|
-
* to the Vana network. It's the counterpart to decryptFile for preparing data
|
|
206
|
-
* for secure storage.
|
|
207
|
-
*
|
|
208
|
-
* The method automatically:
|
|
209
|
-
* - Generates an encryption key from the user's wallet signature
|
|
210
|
-
* - Converts the input data to a Blob if necessary
|
|
211
|
-
* - Encrypts the data using the generated key
|
|
212
|
-
* - Returns both the encrypted data and the encryption key
|
|
213
|
-
*
|
|
214
|
-
* The encryption key returned can be stored and later used for decryption,
|
|
215
|
-
* or shared with others to grant them decryption access.
|
|
216
|
-
*
|
|
217
|
-
* @param data - The data to encrypt (Blob, string, or object)
|
|
218
|
-
* @param options - Optional encryption configuration
|
|
219
|
-
* @returns Promise resolving to encrypted data and the encryption key used
|
|
220
|
-
* @throws {Error} When wallet is not connected or encryption fails
|
|
221
|
-
* @example
|
|
222
|
-
* ```typescript
|
|
223
|
-
* // Encrypt a string
|
|
224
|
-
* const { encryptedData, encryptionKey } = await vana.data.encryptFile(
|
|
225
|
-
* "My secret data"
|
|
226
|
-
* );
|
|
227
|
-
*
|
|
228
|
-
* // Encrypt JSON with custom MIME type
|
|
229
|
-
* const { encryptedData, encryptionKey } = await vana.data.encryptFile(
|
|
230
|
-
* { name: "Alice", age: 30 },
|
|
231
|
-
* { mimeType: "application/json" }
|
|
232
|
-
* );
|
|
233
|
-
*
|
|
234
|
-
* // With custom encryption seed
|
|
235
|
-
* const { encryptedData, encryptionKey } = await vana.data.encryptFile(
|
|
236
|
-
* "Secret message",
|
|
237
|
-
* { seed: "My custom encryption seed" }
|
|
238
|
-
* );
|
|
239
|
-
*
|
|
240
|
-
* // Upload the encrypted data
|
|
241
|
-
* const result = await vana.data.uploadToStorage(encryptedData);
|
|
242
|
-
* ```
|
|
243
|
-
*/
|
|
244
|
-
async encryptFile(data, options) {
|
|
245
|
-
this.assertWallet();
|
|
246
|
-
try {
|
|
247
|
-
const encryptionKey = await generateEncryptionKey(
|
|
248
|
-
this.context.walletClient,
|
|
249
|
-
this.context.platform,
|
|
250
|
-
options?.seed ?? DEFAULT_ENCRYPTION_SEED
|
|
251
|
-
);
|
|
252
|
-
let blob;
|
|
253
|
-
if (data instanceof Blob) {
|
|
254
|
-
blob = data;
|
|
255
|
-
} else if (typeof data === "string") {
|
|
256
|
-
blob = new Blob([data], { type: options?.mimeType ?? "text/plain" });
|
|
257
|
-
} else {
|
|
258
|
-
blob = new Blob([JSON.stringify(data)], {
|
|
259
|
-
type: options?.mimeType ?? "application/json"
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
const encryptedData = await encryptBlobWithSignedKey(
|
|
263
|
-
blob,
|
|
264
|
-
encryptionKey,
|
|
265
|
-
this.context.platform
|
|
266
|
-
);
|
|
267
|
-
return {
|
|
268
|
-
encryptedData,
|
|
269
|
-
encryptionKey
|
|
270
|
-
};
|
|
271
|
-
} catch (error) {
|
|
272
|
-
throw new Error(
|
|
273
|
-
`Failed to encrypt file: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
274
|
-
);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* Decrypts a file using wallet-derived decryption key.
|
|
279
|
-
*
|
|
280
|
-
* @remarks
|
|
281
|
-
* Counterpart to `upload()` for decrypting user files. Automatically
|
|
282
|
-
* generates decryption key from wallet, fetches encrypted content,
|
|
283
|
-
* and decrypts. Supports IPFS (with gateway fallback) and HTTP URLs.
|
|
284
|
-
*
|
|
285
|
-
* @param file - UserFile object from `getUserFiles()`
|
|
286
|
-
* @param options - Decryption options
|
|
287
|
-
* @param options.seed - Custom encryption seed.
|
|
288
|
-
* Defaults to standard Vana seed.
|
|
289
|
-
*
|
|
290
|
-
* @returns Decrypted content as Blob
|
|
291
|
-
*
|
|
292
|
-
* @throws {Error} No wallet connected.
|
|
293
|
-
* Connect wallet before decrypting.
|
|
294
|
-
* @throws {Error} Network error accessing file.
|
|
295
|
-
* Check CORS settings or server availability.
|
|
296
|
-
* @throws {Error} File not found (404).
|
|
297
|
-
* File no longer available at stored URL.
|
|
298
|
-
* @throws {Error} Access denied (403).
|
|
299
|
-
* No permission to access file.
|
|
300
|
-
* @throws {Error} Invalid file format.
|
|
301
|
-
* File not encrypted with Vana protocol.
|
|
302
|
-
* @throws {Error} Wrong encryption key.
|
|
303
|
-
* Verify seed matches upload or use default.
|
|
304
|
-
*
|
|
305
|
-
* @example
|
|
306
|
-
* ```typescript
|
|
307
|
-
* // Basic file decryption
|
|
308
|
-
* const files = await vana.data.getUserFiles({ owner: userAddress });
|
|
309
|
-
* const decryptedBlob = await vana.data.decryptFile(files[0]);
|
|
310
|
-
*
|
|
311
|
-
* // Convert to text
|
|
312
|
-
* const text = await decryptedBlob.text();
|
|
313
|
-
* console.log('Decrypted content:', text);
|
|
314
|
-
*
|
|
315
|
-
* // Convert to JSON
|
|
316
|
-
* const json = JSON.parse(await decryptedBlob.text());
|
|
317
|
-
* console.log('Decrypted data:', json);
|
|
318
|
-
*
|
|
319
|
-
* // With custom encryption seed
|
|
320
|
-
* const decryptedBlob = await vana.data.decryptFile(
|
|
321
|
-
* files[0],
|
|
322
|
-
* "My custom encryption seed"
|
|
323
|
-
* );
|
|
324
|
-
*
|
|
325
|
-
* // Save to file (in Node.js)
|
|
326
|
-
* const buffer = await decryptedBlob.arrayBuffer();
|
|
327
|
-
* fs.writeFileSync('decrypted-file.txt', Buffer.from(buffer));
|
|
328
|
-
* ```
|
|
329
|
-
*/
|
|
330
|
-
async decryptFile(file, options) {
|
|
331
|
-
this.assertWallet();
|
|
332
|
-
try {
|
|
333
|
-
this.assertWallet();
|
|
334
|
-
const encryptionKey = await generateEncryptionKey(
|
|
335
|
-
this.context.walletClient,
|
|
336
|
-
this.context.platform,
|
|
337
|
-
options?.seed ?? DEFAULT_ENCRYPTION_SEED
|
|
338
|
-
);
|
|
339
|
-
let encryptedBlob;
|
|
340
|
-
try {
|
|
341
|
-
if (file.url.startsWith("ipfs://")) {
|
|
342
|
-
encryptedBlob = await this.fetchFromIPFS(file.url);
|
|
343
|
-
} else {
|
|
344
|
-
encryptedBlob = await this.fetch(file.url);
|
|
345
|
-
}
|
|
346
|
-
} catch (fetchError) {
|
|
347
|
-
const errorMessage = fetchError instanceof Error ? fetchError.message : "Unknown error";
|
|
348
|
-
if (errorMessage.includes("Failed to fetch IPFS content") && errorMessage.includes("from all gateways")) {
|
|
349
|
-
throw new Error(
|
|
350
|
-
"Network error: Cannot access the file URL. The file may be stored on a server that's not accessible or has CORS restrictions."
|
|
351
|
-
);
|
|
352
|
-
} else if (errorMessage.includes("Empty response")) {
|
|
353
|
-
throw new Error("File is empty or could not be retrieved");
|
|
354
|
-
} else if (errorMessage.includes("Network error:") || errorMessage.includes("Failed to fetch")) {
|
|
355
|
-
throw new Error(
|
|
356
|
-
"Network error: Cannot access the file URL. The file may be stored on a server that's not accessible or has CORS restrictions."
|
|
357
|
-
);
|
|
358
|
-
} else if (errorMessage.includes("HTTP error!")) {
|
|
359
|
-
const statusMatch = errorMessage.match(/status: (\d+)/);
|
|
360
|
-
const status = statusMatch ? statusMatch[1] : "unknown";
|
|
361
|
-
if (status === "500") {
|
|
362
|
-
throw new Error(
|
|
363
|
-
"Network error: Cannot access the file URL. The file may be stored on a server that's not accessible or has CORS restrictions."
|
|
364
|
-
);
|
|
365
|
-
} else if (status === "403") {
|
|
366
|
-
throw new Error(
|
|
367
|
-
"Access denied. You may not have permission to access this file"
|
|
368
|
-
);
|
|
369
|
-
} else if (status === "404") {
|
|
370
|
-
throw new Error(
|
|
371
|
-
"File not found: The encrypted file is no longer available at the stored URL."
|
|
372
|
-
);
|
|
373
|
-
} else {
|
|
374
|
-
throw new Error(
|
|
375
|
-
"Network error: Cannot access the file URL. The file may be stored on a server that's not accessible or has CORS restrictions."
|
|
376
|
-
);
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
throw fetchError;
|
|
380
|
-
}
|
|
381
|
-
if (encryptedBlob.size === 0) {
|
|
382
|
-
throw new Error("File is empty or could not be retrieved");
|
|
383
|
-
}
|
|
384
|
-
let decryptedBlob;
|
|
385
|
-
try {
|
|
386
|
-
decryptedBlob = await decryptBlobWithSignedKey(
|
|
387
|
-
encryptedBlob,
|
|
388
|
-
encryptionKey,
|
|
389
|
-
this.context.platform
|
|
390
|
-
);
|
|
391
|
-
} catch (decryptError) {
|
|
392
|
-
const errorMessage = decryptError instanceof Error ? decryptError.message : "Unknown error";
|
|
393
|
-
if (errorMessage.includes("not a valid OpenPGP message")) {
|
|
394
|
-
throw new Error(
|
|
395
|
-
"Invalid file format: This file doesn't appear to be encrypted with the Vana protocol"
|
|
396
|
-
);
|
|
397
|
-
} else if (errorMessage.includes("Session key decryption failed")) {
|
|
398
|
-
throw new Error("Wrong encryption key");
|
|
399
|
-
} else if (errorMessage.includes("Error decrypting message")) {
|
|
400
|
-
throw new Error("Wrong encryption key");
|
|
401
|
-
} else if (errorMessage.includes("File not found")) {
|
|
402
|
-
throw new Error(
|
|
403
|
-
"File not found: The encrypted file is no longer available"
|
|
404
|
-
);
|
|
405
|
-
} else {
|
|
406
|
-
throw decryptError;
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
return decryptedBlob;
|
|
410
|
-
} catch (error) {
|
|
411
|
-
if (error instanceof Error && (error.message.includes("Network error:") || error.message.includes("Invalid file format:") || error.message.includes("Wrong encryption key") || error.message.includes("Access denied") || error.message.includes("File not found:") || error.message.includes("File is empty"))) {
|
|
412
|
-
throw error;
|
|
413
|
-
}
|
|
414
|
-
throw new Error(
|
|
415
|
-
`Failed to decrypt file: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
416
|
-
);
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
/**
|
|
420
|
-
* Retrieves all files owned by a specific user address.
|
|
421
|
-
*
|
|
422
|
-
* @remarks
|
|
423
|
-
* Queries the Vana subgraph for files owned by the specified address.
|
|
424
|
-
* Automatically deduplicates by file ID, keeping the latest version
|
|
425
|
-
* when duplicates exist from re-indexing or chain reorganizations.
|
|
426
|
-
* Enriches results with DLP proof data when available.
|
|
427
|
-
*
|
|
428
|
-
* @param params - Query configuration
|
|
429
|
-
* @param params.owner - Wallet address of the file owner
|
|
430
|
-
* @param params.subgraphUrl - Subgraph endpoint override.
|
|
431
|
-
* Defaults to context configuration.
|
|
432
|
-
*
|
|
433
|
-
* @returns Array of UserFile objects sorted by timestamp (newest first)
|
|
434
|
-
*
|
|
435
|
-
* @throws {Error} Subgraph URL not configured.
|
|
436
|
-
* Provide `subgraphUrl` parameter or configure in Vana constructor.
|
|
437
|
-
* @throws {Error} Subgraph request failed.
|
|
438
|
-
* Check network connectivity and subgraph availability.
|
|
439
|
-
* @throws {Error} Subgraph returned errors.
|
|
440
|
-
* Review query parameters and subgraph logs.
|
|
441
|
-
*
|
|
442
|
-
* @example
|
|
443
|
-
* ```typescript
|
|
444
|
-
* const files = await vana.data.getUserFiles({
|
|
445
|
-
* owner: "0x742d35Cc6558Fd4D9e9E0E888F0462ef6919Bd36"
|
|
446
|
-
* });
|
|
447
|
-
*
|
|
448
|
-
* files.forEach(file => {
|
|
449
|
-
* console.log(`File ${file.id}: ${file.url}`);
|
|
450
|
-
* console.log(` Schema: ${file.schemaId}`);
|
|
451
|
-
* console.log(` DLPs: ${file.dlpIds?.join(", ") || "none"}`);
|
|
452
|
-
* });
|
|
453
|
-
* ```
|
|
454
|
-
*/
|
|
455
|
-
async getUserFiles(params, options) {
|
|
456
|
-
const { owner, subgraphUrl } = params;
|
|
457
|
-
let dataSource = options?.source === "chain" ? "chain" : "subgraph";
|
|
458
|
-
if (options?.source === "auto" || !options?.source && options?.minBlock) {
|
|
459
|
-
const endpoint2 = subgraphUrl ?? this.context.subgraphUrl;
|
|
460
|
-
const currentBlock = await this.context.publicClient.getBlockNumber();
|
|
461
|
-
let subgraphBlock;
|
|
462
|
-
if (endpoint2) {
|
|
463
|
-
try {
|
|
464
|
-
const meta = await fetchSubgraphMeta(endpoint2);
|
|
465
|
-
subgraphBlock = meta.blockNumber;
|
|
466
|
-
} catch {
|
|
467
|
-
subgraphBlock = void 0;
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
dataSource = determineDataSource(
|
|
471
|
-
options?.source,
|
|
472
|
-
options?.minBlock,
|
|
473
|
-
currentBlock,
|
|
474
|
-
subgraphBlock
|
|
475
|
-
);
|
|
476
|
-
}
|
|
477
|
-
if (dataSource === "chain") {
|
|
478
|
-
const publicClient = this.context.publicClient;
|
|
479
|
-
const chainId = await publicClient.getChainId();
|
|
480
|
-
const contractAddress = getContractAddress(chainId, "DataRegistry");
|
|
481
|
-
const files = await getUserFilesFromChain(
|
|
482
|
-
publicClient,
|
|
483
|
-
contractAddress,
|
|
484
|
-
owner,
|
|
485
|
-
options?.minBlock ? BigInt(options.minBlock) : void 0
|
|
486
|
-
);
|
|
487
|
-
const limit = options?.fetchAll ? files.length : options?.limit ?? 100;
|
|
488
|
-
const offset = options?.offset ?? 0;
|
|
489
|
-
return files.slice(offset, offset + limit);
|
|
490
|
-
}
|
|
491
|
-
const endpoint = subgraphUrl ?? this.context.subgraphUrl;
|
|
492
|
-
if (!endpoint) {
|
|
493
|
-
throw new Error(
|
|
494
|
-
"subgraphUrl is required. Please provide a valid subgraph endpoint or configure it in Vana constructor."
|
|
495
|
-
);
|
|
496
|
-
}
|
|
497
|
-
if (options?.minBlock || options?.waitForSync) {
|
|
498
|
-
await checkSubgraphConsistency(endpoint, options);
|
|
499
|
-
}
|
|
500
|
-
try {
|
|
501
|
-
const orderByMap = {
|
|
502
|
-
id: "id",
|
|
503
|
-
addedAtBlock: "addedAtBlock",
|
|
504
|
-
addedAtTimestamp: "addedAtTimestamp",
|
|
505
|
-
url: "url",
|
|
506
|
-
schemaId: "schemaId"
|
|
507
|
-
};
|
|
508
|
-
const allFiles = await executePaginatedQuery({
|
|
509
|
-
endpoint,
|
|
510
|
-
document: GetUserFilesPaginatedDocument,
|
|
511
|
-
baseVariables: {
|
|
512
|
-
userId: owner.toLowerCase(),
|
|
513
|
-
// Subgraph requires lowercase addresses
|
|
514
|
-
orderBy: mapOrderByToEnum(
|
|
515
|
-
options?.orderBy,
|
|
516
|
-
orderByMap,
|
|
517
|
-
"addedAtBlock"
|
|
518
|
-
),
|
|
519
|
-
orderDirection: mapOrderDirection(
|
|
520
|
-
options?.orderDirection,
|
|
521
|
-
"asc",
|
|
522
|
-
"desc"
|
|
523
|
-
)
|
|
524
|
-
},
|
|
525
|
-
options,
|
|
526
|
-
extractItems: (data) => data?.user?.files,
|
|
527
|
-
transformItem: (file) => ({
|
|
528
|
-
id: parseInt(file.id),
|
|
529
|
-
url: file.url,
|
|
530
|
-
ownerAddress: file.owner.id,
|
|
531
|
-
addedAtBlock: BigInt(file.addedAtBlock),
|
|
532
|
-
schemaId: parseInt(file.schemaId),
|
|
533
|
-
addedAtTimestamp: BigInt(file.addedAtTimestamp),
|
|
534
|
-
transactionHash: file.transactionHash
|
|
535
|
-
})
|
|
536
|
-
});
|
|
537
|
-
if (allFiles.length > 0) {
|
|
538
|
-
try {
|
|
539
|
-
const fileIds = allFiles.map((f) => f.id);
|
|
540
|
-
let proofMap;
|
|
541
|
-
try {
|
|
542
|
-
proofMap = await this._fetchProofsFromSubgraph(fileIds, endpoint);
|
|
543
|
-
} catch (subgraphError) {
|
|
544
|
-
console.debug(
|
|
545
|
-
"Failed to fetch proofs from subgraph, trying chain:",
|
|
546
|
-
subgraphError
|
|
547
|
-
);
|
|
548
|
-
proofMap = await this._fetchProofsFromChain(fileIds);
|
|
549
|
-
}
|
|
550
|
-
for (const file of allFiles) {
|
|
551
|
-
const dlpIds = proofMap.get(file.id);
|
|
552
|
-
if (dlpIds && dlpIds.length > 0) {
|
|
553
|
-
file.dlpIds = dlpIds;
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
} catch (error) {
|
|
557
|
-
console.warn("Failed to fetch proof data for files:", error);
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
return allFiles;
|
|
561
|
-
} catch (error) {
|
|
562
|
-
console.error("Failed to fetch user files from subgraph:", error);
|
|
563
|
-
throw new Error(
|
|
564
|
-
`Failed to fetch user files from subgraph: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
565
|
-
);
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
/**
|
|
569
|
-
* Fetches proof data for multiple files from the subgraph.
|
|
570
|
-
*
|
|
571
|
-
* @private
|
|
572
|
-
* @param fileIds - Array of file IDs to fetch proofs for
|
|
573
|
-
* @param subgraphUrl - The subgraph endpoint URL
|
|
574
|
-
* @returns Map of file IDs to their associated DLP IDs
|
|
575
|
-
*/
|
|
576
|
-
async _fetchProofsFromSubgraph(fileIds, subgraphUrl) {
|
|
577
|
-
const response = await fetch(subgraphUrl, {
|
|
578
|
-
method: "POST",
|
|
579
|
-
headers: {
|
|
580
|
-
"Content-Type": "application/json"
|
|
581
|
-
},
|
|
582
|
-
body: JSON.stringify({
|
|
583
|
-
query: print(GetFileProofsDocument),
|
|
584
|
-
variables: {
|
|
585
|
-
fileIds: fileIds.map((id) => id.toString())
|
|
586
|
-
}
|
|
587
|
-
})
|
|
588
|
-
});
|
|
589
|
-
if (!response.ok) {
|
|
590
|
-
throw new Error(
|
|
591
|
-
`Subgraph request failed: ${response.status} ${response.statusText}`
|
|
592
|
-
);
|
|
593
|
-
}
|
|
594
|
-
const result = await response.json();
|
|
595
|
-
if (result.errors) {
|
|
596
|
-
throw new Error(
|
|
597
|
-
`Subgraph errors: ${result.errors.map((e) => e.message).join(", ")}`
|
|
598
|
-
);
|
|
599
|
-
}
|
|
600
|
-
const proofMap = /* @__PURE__ */ new Map();
|
|
601
|
-
if (result.data?.dataRegistryProofs) {
|
|
602
|
-
for (const proof of result.data.dataRegistryProofs) {
|
|
603
|
-
if (proof.dlp?.id) {
|
|
604
|
-
const fileId = parseInt(proof.fileId);
|
|
605
|
-
const dlpId = parseInt(proof.dlp.id);
|
|
606
|
-
let dlpIds = proofMap.get(fileId);
|
|
607
|
-
if (!dlpIds) {
|
|
608
|
-
dlpIds = [];
|
|
609
|
-
proofMap.set(fileId, dlpIds);
|
|
610
|
-
}
|
|
611
|
-
if (!dlpIds.includes(dlpId)) {
|
|
612
|
-
dlpIds.push(dlpId);
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
return proofMap;
|
|
618
|
-
}
|
|
619
|
-
/**
|
|
620
|
-
* Fetches proof data for multiple files from the blockchain.
|
|
621
|
-
* Falls back to this when subgraph is unavailable.
|
|
622
|
-
*
|
|
623
|
-
* @private
|
|
624
|
-
* @param fileIds - Array of file IDs to fetch proofs for
|
|
625
|
-
* @returns Map of file IDs to their associated DLP IDs
|
|
626
|
-
*/
|
|
627
|
-
async _fetchProofsFromChain(fileIds) {
|
|
628
|
-
const chainId = this.context.publicClient.chain?.id;
|
|
629
|
-
if (!chainId) {
|
|
630
|
-
throw new Error("Chain ID not available");
|
|
631
|
-
}
|
|
632
|
-
const dataRegistryAddress = getContractAddress(chainId, "DataRegistry");
|
|
633
|
-
const dataRegistryAbi = getAbi("DataRegistry");
|
|
634
|
-
const proofMap = /* @__PURE__ */ new Map();
|
|
635
|
-
for (const fileId of fileIds) {
|
|
636
|
-
const dlpIds = [];
|
|
637
|
-
let proofIndex = 0;
|
|
638
|
-
let hasMoreProofs = true;
|
|
639
|
-
while (hasMoreProofs) {
|
|
640
|
-
try {
|
|
641
|
-
const proof = await this.context.publicClient.readContract({
|
|
642
|
-
address: dataRegistryAddress,
|
|
643
|
-
abi: dataRegistryAbi,
|
|
644
|
-
functionName: "fileProofs",
|
|
645
|
-
args: [BigInt(fileId), BigInt(proofIndex)]
|
|
646
|
-
});
|
|
647
|
-
if (proof?.data?.dlpId) {
|
|
648
|
-
const dlpId = Number(proof.data.dlpId);
|
|
649
|
-
if (!dlpIds.includes(dlpId)) {
|
|
650
|
-
dlpIds.push(dlpId);
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
proofIndex++;
|
|
654
|
-
} catch {
|
|
655
|
-
hasMoreProofs = false;
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
if (dlpIds.length > 0) {
|
|
659
|
-
proofMap.set(fileId, dlpIds);
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
return proofMap;
|
|
663
|
-
}
|
|
664
|
-
/**
|
|
665
|
-
* Retrieves information about a specific Data Liquidity Pool (DLP).
|
|
666
|
-
*
|
|
667
|
-
* @remarks
|
|
668
|
-
* DLPs are entities that process and verify data files in the Vana network.
|
|
669
|
-
* This method fetches DLP metadata including name, status, and performance rating.
|
|
670
|
-
* Uses subgraph first for efficiency, falls back to chain if unavailable.
|
|
671
|
-
*
|
|
672
|
-
* @param dlpId - The unique identifier of the DLP
|
|
673
|
-
* @param options - Optional parameters
|
|
674
|
-
* @param options.subgraphUrl - Custom subgraph URL to override default
|
|
675
|
-
* @returns Promise resolving to DLP information
|
|
676
|
-
* @throws {Error} When DLP cannot be found - "DLP not found: {dlpId}"
|
|
677
|
-
* @throws {Error} When query fails - "Failed to fetch DLP: {error}"
|
|
678
|
-
* @example
|
|
679
|
-
* ```typescript
|
|
680
|
-
* const dlp = await vana.data.getDLP(26);
|
|
681
|
-
* console.log(`DLP ${dlp.name}: ${dlp.status}`);
|
|
682
|
-
* ```
|
|
683
|
-
*/
|
|
684
|
-
async getDLP(dlpId, options = {}) {
|
|
685
|
-
const subgraphUrl = options.subgraphUrl ?? this.context.subgraphUrl;
|
|
686
|
-
if (subgraphUrl && (options.minBlock || options.waitForSync)) {
|
|
687
|
-
await checkSubgraphConsistency(subgraphUrl, options);
|
|
688
|
-
}
|
|
689
|
-
if (subgraphUrl) {
|
|
690
|
-
try {
|
|
691
|
-
const response = await fetch(subgraphUrl, {
|
|
692
|
-
method: "POST",
|
|
693
|
-
headers: {
|
|
694
|
-
"Content-Type": "application/json"
|
|
695
|
-
},
|
|
696
|
-
body: JSON.stringify({
|
|
697
|
-
query: print(GetDlpDocument),
|
|
698
|
-
variables: {
|
|
699
|
-
id: dlpId.toString()
|
|
700
|
-
}
|
|
701
|
-
})
|
|
702
|
-
});
|
|
703
|
-
if (!response.ok) {
|
|
704
|
-
throw new Error(
|
|
705
|
-
`Subgraph request failed: ${response.status} ${response.statusText}`
|
|
706
|
-
);
|
|
707
|
-
}
|
|
708
|
-
const result = await response.json();
|
|
709
|
-
if (result.errors) {
|
|
710
|
-
throw new Error(
|
|
711
|
-
`Subgraph errors: ${result.errors.map((e) => e.message).join(", ")}`
|
|
712
|
-
);
|
|
713
|
-
}
|
|
714
|
-
if (!result.data?.dlp) {
|
|
715
|
-
throw new Error(`DLP not found: ${dlpId}`);
|
|
716
|
-
}
|
|
717
|
-
return {
|
|
718
|
-
id: parseInt(result.data.dlp.id),
|
|
719
|
-
name: result.data.dlp.name ?? "",
|
|
720
|
-
metadata: result.data.dlp.metadata ?? void 0,
|
|
721
|
-
status: result.data.dlp.status ? parseInt(result.data.dlp.status) : void 0,
|
|
722
|
-
address: result.data.dlp.address ? result.data.dlp.address : void 0,
|
|
723
|
-
owner: result.data.dlp.owner ? result.data.dlp.owner : void 0
|
|
724
|
-
};
|
|
725
|
-
} catch (error) {
|
|
726
|
-
console.debug("Subgraph query failed, falling back to chain:", error);
|
|
727
|
-
}
|
|
728
|
-
}
|
|
729
|
-
try {
|
|
730
|
-
const chainId = this.context.publicClient.chain?.id;
|
|
731
|
-
if (!chainId) {
|
|
732
|
-
throw new Error("Chain ID not available");
|
|
733
|
-
}
|
|
734
|
-
const dlpRegistryAddress = getContractAddress(chainId, "DLPRegistry");
|
|
735
|
-
const dlpRegistryAbi = getAbi("DLPRegistry");
|
|
736
|
-
const dlpData = await this.context.publicClient.readContract({
|
|
737
|
-
address: dlpRegistryAddress,
|
|
738
|
-
abi: dlpRegistryAbi,
|
|
739
|
-
functionName: "dlps",
|
|
740
|
-
args: [BigInt(dlpId)]
|
|
741
|
-
});
|
|
742
|
-
if (!dlpData?.name) {
|
|
743
|
-
throw new Error(`DLP not found: ${dlpId}`);
|
|
744
|
-
}
|
|
745
|
-
return {
|
|
746
|
-
id: dlpId,
|
|
747
|
-
name: dlpData.name,
|
|
748
|
-
metadata: dlpData.metadata,
|
|
749
|
-
status: dlpData.status,
|
|
750
|
-
address: dlpData.dlpAddress,
|
|
751
|
-
owner: dlpData.ownerAddress
|
|
752
|
-
};
|
|
753
|
-
} catch (error) {
|
|
754
|
-
throw new Error(
|
|
755
|
-
`Failed to fetch DLP: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
756
|
-
);
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
/**
|
|
760
|
-
* Lists all Data Liquidity Pools (DLPs) with optional pagination.
|
|
761
|
-
*
|
|
762
|
-
* @remarks
|
|
763
|
-
* Fetches a paginated list of all DLPs registered in the network.
|
|
764
|
-
* Uses subgraph for efficient querying with fallback to chain multicall.
|
|
765
|
-
*
|
|
766
|
-
* @param options - Optional parameters for pagination and filtering
|
|
767
|
-
* @param options.limit - Maximum number of DLPs to return (default: 100)
|
|
768
|
-
* @param options.offset - Number of DLPs to skip (default: 0)
|
|
769
|
-
* @param options.subgraphUrl - Custom subgraph URL to override default
|
|
770
|
-
* @returns Promise resolving to array of DLP information
|
|
771
|
-
* @throws {Error} When query fails - "Failed to list DLPs: {error}"
|
|
772
|
-
* @example
|
|
773
|
-
* ```typescript
|
|
774
|
-
* // Get first 10 DLPs
|
|
775
|
-
* const dlps = await vana.data.listDLPs({ limit: 10 });
|
|
776
|
-
* dlps.forEach(dlp => console.log(`${dlp.id}: ${dlp.name}`));
|
|
777
|
-
*
|
|
778
|
-
* // Get next page
|
|
779
|
-
* const nextPage = await vana.data.listDLPs({ limit: 10, offset: 10 });
|
|
780
|
-
* ```
|
|
781
|
-
*/
|
|
782
|
-
async listDLPs(options = {}) {
|
|
783
|
-
const { limit = 100, offset = 0 } = options;
|
|
784
|
-
const subgraphUrl = options.subgraphUrl ?? this.context.subgraphUrl;
|
|
785
|
-
if (subgraphUrl) {
|
|
786
|
-
try {
|
|
787
|
-
const query = `
|
|
788
|
-
query ListDLPs($first: Int!, $skip: Int!) {
|
|
789
|
-
dlps(first: $first, skip: $skip, orderBy: id) {
|
|
790
|
-
id
|
|
791
|
-
name
|
|
792
|
-
metadata
|
|
793
|
-
status
|
|
794
|
-
address
|
|
795
|
-
owner
|
|
796
|
-
}
|
|
797
|
-
}
|
|
798
|
-
`;
|
|
799
|
-
const response = await fetch(subgraphUrl, {
|
|
800
|
-
method: "POST",
|
|
801
|
-
headers: {
|
|
802
|
-
"Content-Type": "application/json"
|
|
803
|
-
},
|
|
804
|
-
body: JSON.stringify({
|
|
805
|
-
query,
|
|
806
|
-
variables: {
|
|
807
|
-
first: limit,
|
|
808
|
-
skip: offset
|
|
809
|
-
}
|
|
810
|
-
})
|
|
811
|
-
});
|
|
812
|
-
if (!response.ok) {
|
|
813
|
-
throw new Error(
|
|
814
|
-
`Subgraph request failed: ${response.status} ${response.statusText}`
|
|
815
|
-
);
|
|
816
|
-
}
|
|
817
|
-
const result = await response.json();
|
|
818
|
-
if (result.errors) {
|
|
819
|
-
throw new Error(
|
|
820
|
-
`Subgraph errors: ${result.errors.map((e) => e.message).join(", ")}`
|
|
821
|
-
);
|
|
822
|
-
}
|
|
823
|
-
const dlps = result.data?.dlps ?? [];
|
|
824
|
-
return dlps.map((dlp) => ({
|
|
825
|
-
id: parseInt(dlp.id),
|
|
826
|
-
name: dlp.name ?? "",
|
|
827
|
-
metadata: dlp.metadata,
|
|
828
|
-
status: dlp.status ? parseInt(dlp.status) : void 0,
|
|
829
|
-
address: dlp.address ? dlp.address : void 0,
|
|
830
|
-
owner: dlp.owner ? dlp.owner : void 0
|
|
831
|
-
}));
|
|
832
|
-
} catch (error) {
|
|
833
|
-
console.debug("Subgraph query failed, falling back to chain:", error);
|
|
834
|
-
}
|
|
835
|
-
}
|
|
836
|
-
try {
|
|
837
|
-
const chainId = this.context.publicClient.chain?.id;
|
|
838
|
-
if (!chainId) {
|
|
839
|
-
throw new Error("Chain ID not available");
|
|
840
|
-
}
|
|
841
|
-
const dlpRegistryAddress = getContractAddress(chainId, "DLPRegistry");
|
|
842
|
-
const dlpRegistryAbi = getAbi("DLPRegistry");
|
|
843
|
-
const dlpCount = await this.context.publicClient.readContract({
|
|
844
|
-
address: dlpRegistryAddress,
|
|
845
|
-
abi: dlpRegistryAbi,
|
|
846
|
-
functionName: "dlpsCount",
|
|
847
|
-
args: []
|
|
848
|
-
});
|
|
849
|
-
const totalCount = Number(dlpCount);
|
|
850
|
-
const start = offset;
|
|
851
|
-
const end = Math.min(start + limit, totalCount);
|
|
852
|
-
if (end <= start) {
|
|
853
|
-
return [];
|
|
854
|
-
}
|
|
855
|
-
const calls = [];
|
|
856
|
-
for (let i = start + 1; i <= end; i++) {
|
|
857
|
-
calls.push({
|
|
858
|
-
address: dlpRegistryAddress,
|
|
859
|
-
abi: dlpRegistryAbi,
|
|
860
|
-
functionName: "dlps",
|
|
861
|
-
args: [BigInt(i)]
|
|
862
|
-
});
|
|
863
|
-
}
|
|
864
|
-
const results = await gasAwareMulticall(this.context.publicClient, {
|
|
865
|
-
contracts: calls,
|
|
866
|
-
allowFailure: true,
|
|
867
|
-
batchSize: 50
|
|
868
|
-
});
|
|
869
|
-
const dlps = [];
|
|
870
|
-
for (let i = 0; i < results.length; i++) {
|
|
871
|
-
const result = results[i];
|
|
872
|
-
if (result.status === "success" && result.result) {
|
|
873
|
-
const dlpData = result.result;
|
|
874
|
-
if (dlpData.name) {
|
|
875
|
-
dlps.push({
|
|
876
|
-
id: start + i + 1,
|
|
877
|
-
name: dlpData.name,
|
|
878
|
-
metadata: dlpData.metadata,
|
|
879
|
-
status: dlpData.status,
|
|
880
|
-
address: dlpData.dlpAddress,
|
|
881
|
-
owner: dlpData.ownerAddress
|
|
882
|
-
});
|
|
883
|
-
}
|
|
884
|
-
}
|
|
885
|
-
}
|
|
886
|
-
return dlps;
|
|
887
|
-
} catch (error) {
|
|
888
|
-
throw new Error(
|
|
889
|
-
`Failed to list DLPs: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
890
|
-
);
|
|
891
|
-
}
|
|
892
|
-
}
|
|
893
|
-
/**
|
|
894
|
-
* Retrieves a list of permissions granted by a user.
|
|
895
|
-
*
|
|
896
|
-
* This method supports automatic fallback between subgraph and RPC modes:
|
|
897
|
-
* - If subgraph URL is available, tries subgraph query first
|
|
898
|
-
* - Falls back to direct contract queries via RPC if subgraph fails
|
|
899
|
-
* - RPC mode uses gasAwareMulticall for efficient batch queries
|
|
900
|
-
*
|
|
901
|
-
* @param params - Object containing the user address and optional subgraph URL
|
|
902
|
-
* @param params.user - The wallet address of the user to query permissions for
|
|
903
|
-
* @param params.subgraphUrl - Optional subgraph URL to override the default
|
|
904
|
-
* @returns Promise resolving to an array of permission objects
|
|
905
|
-
* @throws Error if both subgraph and RPC queries fail
|
|
906
|
-
*/
|
|
907
|
-
async getUserPermissions(params, options) {
|
|
908
|
-
const { user, subgraphUrl } = params;
|
|
909
|
-
const endpoint = subgraphUrl ?? this.context.subgraphUrl;
|
|
910
|
-
if (endpoint && (options?.minBlock || options?.waitForSync)) {
|
|
911
|
-
await checkSubgraphConsistency(endpoint, options);
|
|
912
|
-
}
|
|
913
|
-
if (endpoint) {
|
|
914
|
-
try {
|
|
915
|
-
const permissions = await this._getUserPermissionsViaSubgraph(
|
|
916
|
-
{
|
|
917
|
-
user,
|
|
918
|
-
subgraphUrl: endpoint
|
|
919
|
-
},
|
|
920
|
-
options
|
|
921
|
-
);
|
|
922
|
-
return permissions;
|
|
923
|
-
} catch (error) {
|
|
924
|
-
console.warn("Subgraph query failed, falling back to RPC:", error);
|
|
925
|
-
}
|
|
926
|
-
}
|
|
927
|
-
const allPermissions = await this._getUserPermissionsViaRpc({ user });
|
|
928
|
-
if (options && !options.fetchAll) {
|
|
929
|
-
const limit = options.limit ?? 100;
|
|
930
|
-
const offset = options.offset ?? 0;
|
|
931
|
-
return allPermissions.slice(offset, offset + limit);
|
|
932
|
-
}
|
|
933
|
-
return allPermissions;
|
|
934
|
-
}
|
|
935
|
-
/**
|
|
936
|
-
* Internal method: Query user permissions via subgraph
|
|
937
|
-
*
|
|
938
|
-
* @param params - Query parameters object
|
|
939
|
-
* @param params.user - The user address to query permissions for
|
|
940
|
-
* @param params.subgraphUrl - The subgraph URL endpoint to query
|
|
941
|
-
* @returns Promise resolving to an array of permission objects
|
|
942
|
-
*/
|
|
943
|
-
async _getUserPermissionsViaSubgraph(params, options) {
|
|
944
|
-
const { user, subgraphUrl } = params;
|
|
945
|
-
try {
|
|
946
|
-
const orderByMap = {
|
|
947
|
-
id: "id",
|
|
948
|
-
addedAtBlock: "addedAtBlock",
|
|
949
|
-
addedAtTimestamp: "addedAtTimestamp",
|
|
950
|
-
grant: "grant",
|
|
951
|
-
nonce: "nonce",
|
|
952
|
-
startBlock: "startBlock",
|
|
953
|
-
endBlock: "endBlock"
|
|
954
|
-
};
|
|
955
|
-
const permissions = await executePaginatedQuery({
|
|
956
|
-
endpoint: subgraphUrl,
|
|
957
|
-
document: GetUserPermissionsPaginatedDocument,
|
|
958
|
-
baseVariables: {
|
|
959
|
-
userId: user.toLowerCase(),
|
|
960
|
-
orderBy: mapOrderByToEnum(
|
|
961
|
-
options?.orderBy,
|
|
962
|
-
orderByMap,
|
|
963
|
-
"addedAtTimestamp"
|
|
964
|
-
),
|
|
965
|
-
orderDirection: mapOrderDirection(
|
|
966
|
-
options?.orderDirection,
|
|
967
|
-
"desc",
|
|
968
|
-
"asc"
|
|
969
|
-
)
|
|
970
|
-
},
|
|
971
|
-
options,
|
|
972
|
-
extractItems: (data) => data?.user?.permissions,
|
|
973
|
-
transformItem: (permission) => ({
|
|
974
|
-
id: permission.id,
|
|
975
|
-
grant: permission.grant,
|
|
976
|
-
nonce: BigInt(permission.nonce),
|
|
977
|
-
signature: permission.signature,
|
|
978
|
-
addedAtBlock: BigInt(permission.addedAtBlock),
|
|
979
|
-
addedAtTimestamp: BigInt(permission.addedAtTimestamp),
|
|
980
|
-
transactionHash: permission.transactionHash,
|
|
981
|
-
user
|
|
982
|
-
})
|
|
983
|
-
});
|
|
984
|
-
return permissions;
|
|
985
|
-
} catch (error) {
|
|
986
|
-
console.error("Failed to query user permissions from subgraph:", error);
|
|
987
|
-
throw error;
|
|
988
|
-
}
|
|
989
|
-
}
|
|
990
|
-
/**
|
|
991
|
-
* Internal method: Query user permissions via direct RPC
|
|
992
|
-
*
|
|
993
|
-
* @param params - Query parameters object
|
|
994
|
-
* @param params.user - The user address to query permissions for
|
|
995
|
-
* @returns Promise resolving to an array of permission objects
|
|
996
|
-
*/
|
|
997
|
-
async _getUserPermissionsViaRpc(params) {
|
|
998
|
-
const { user } = params;
|
|
999
|
-
try {
|
|
1000
|
-
const chainId = this.context.publicClient.chain?.id;
|
|
1001
|
-
if (!chainId) {
|
|
1002
|
-
throw new Error("Chain ID not available");
|
|
1003
|
-
}
|
|
1004
|
-
const permissionsAddress = getContractAddress(
|
|
1005
|
-
chainId,
|
|
1006
|
-
"DataPortabilityPermissions"
|
|
1007
|
-
);
|
|
1008
|
-
const permissionsAbi = getAbi("DataPortabilityPermissions");
|
|
1009
|
-
const totalCount = await this.context.publicClient.readContract({
|
|
1010
|
-
address: permissionsAddress,
|
|
1011
|
-
abi: permissionsAbi,
|
|
1012
|
-
functionName: "userPermissionIdsLength",
|
|
1013
|
-
args: [user]
|
|
1014
|
-
});
|
|
1015
|
-
const total = Number(totalCount);
|
|
1016
|
-
if (total === 0) {
|
|
1017
|
-
return [];
|
|
1018
|
-
}
|
|
1019
|
-
const permissionIdCalls = [];
|
|
1020
|
-
for (let i = 0; i < total; i++) {
|
|
1021
|
-
permissionIdCalls.push({
|
|
1022
|
-
address: permissionsAddress,
|
|
1023
|
-
abi: permissionsAbi,
|
|
1024
|
-
functionName: "userPermissionIdsAt",
|
|
1025
|
-
args: [user, BigInt(i)]
|
|
1026
|
-
});
|
|
1027
|
-
}
|
|
1028
|
-
const permissionIdResults = await gasAwareMulticall(this.context.publicClient, {
|
|
1029
|
-
contracts: permissionIdCalls
|
|
1030
|
-
});
|
|
1031
|
-
const permissionIds = permissionIdResults.map((result) => result).filter((id) => id && id > 0n);
|
|
1032
|
-
const permissionInfoCalls = permissionIds.map(
|
|
1033
|
-
(permissionId) => ({
|
|
1034
|
-
address: permissionsAddress,
|
|
1035
|
-
abi: permissionsAbi,
|
|
1036
|
-
functionName: "permissions",
|
|
1037
|
-
args: [permissionId]
|
|
1038
|
-
})
|
|
1039
|
-
);
|
|
1040
|
-
const permissionInfoResults = await gasAwareMulticall(this.context.publicClient, {
|
|
1041
|
-
contracts: permissionInfoCalls,
|
|
1042
|
-
allowFailure: true
|
|
1043
|
-
});
|
|
1044
|
-
const permissions = permissionInfoResults.map((result, index) => {
|
|
1045
|
-
const permissionId = permissionIds[index];
|
|
1046
|
-
if (result.status === "success" && result.result) {
|
|
1047
|
-
const permissionInfo = result.result;
|
|
1048
|
-
return {
|
|
1049
|
-
id: permissionId.toString(),
|
|
1050
|
-
grant: permissionInfo.grant,
|
|
1051
|
-
nonce: permissionInfo.nonce,
|
|
1052
|
-
signature: "",
|
|
1053
|
-
// Not available from RPC, will be empty
|
|
1054
|
-
addedAtBlock: permissionInfo.startBlock,
|
|
1055
|
-
addedAtTimestamp: BigInt(0),
|
|
1056
|
-
// Not available from RPC
|
|
1057
|
-
transactionHash: "0x0000000000000000000000000000000000000000",
|
|
1058
|
-
// Not available from RPC
|
|
1059
|
-
user
|
|
1060
|
-
};
|
|
1061
|
-
} else {
|
|
1062
|
-
return {
|
|
1063
|
-
id: permissionId.toString(),
|
|
1064
|
-
grant: "",
|
|
1065
|
-
nonce: BigInt(0),
|
|
1066
|
-
signature: "",
|
|
1067
|
-
addedAtBlock: BigInt(0),
|
|
1068
|
-
addedAtTimestamp: BigInt(0),
|
|
1069
|
-
transactionHash: "0x0000000000000000000000000000000000000000",
|
|
1070
|
-
user
|
|
1071
|
-
};
|
|
1072
|
-
}
|
|
1073
|
-
}).filter((permission) => permission.grant !== "");
|
|
1074
|
-
return permissions;
|
|
1075
|
-
} catch (error) {
|
|
1076
|
-
throw new Error(
|
|
1077
|
-
`RPC query failed: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
1078
|
-
);
|
|
1079
|
-
}
|
|
1080
|
-
}
|
|
1081
|
-
/**
|
|
1082
|
-
* Retrieves a list of trusted servers for a user.
|
|
1083
|
-
*
|
|
1084
|
-
* This method supports automatic fallback between subgraph and RPC modes:
|
|
1085
|
-
* - If subgraph URL is available, tries subgraph query first for fast results
|
|
1086
|
-
* - Falls back to direct contract queries via RPC if subgraph fails
|
|
1087
|
-
* - RPC mode uses gasAwareMulticall for efficient batch queries
|
|
1088
|
-
*
|
|
1089
|
-
* @param params - Query parameters including user address and optional pagination
|
|
1090
|
-
* @param params.user - The wallet address of the user to query trusted servers for
|
|
1091
|
-
* @param params.subgraphUrl - Optional subgraph URL to override the default
|
|
1092
|
-
* @param params.limit - Maximum number of results to return (default: 50)
|
|
1093
|
-
* @param params.offset - Number of results to skip for pagination (default: 0)
|
|
1094
|
-
* @returns Promise resolving to an array of trusted server objects
|
|
1095
|
-
* @throws Error if both subgraph and RPC queries fail
|
|
1096
|
-
* @example
|
|
1097
|
-
* ```typescript
|
|
1098
|
-
* // Basic usage with automatic fallback
|
|
1099
|
-
* const servers = await vana.data.getUserTrustedServers({
|
|
1100
|
-
* user: '0x...'
|
|
1101
|
-
* });
|
|
1102
|
-
*
|
|
1103
|
-
* // With pagination
|
|
1104
|
-
* const servers = await vana.data.getUserTrustedServers({
|
|
1105
|
-
* user: '0x...',
|
|
1106
|
-
* limit: 10,
|
|
1107
|
-
* offset: 20
|
|
1108
|
-
* });
|
|
1109
|
-
*
|
|
1110
|
-
* // With custom subgraph URL
|
|
1111
|
-
* const servers = await vana.data.getUserTrustedServers({
|
|
1112
|
-
* user: '0x...',
|
|
1113
|
-
* subgraphUrl: 'https://custom-subgraph.com/graphql'
|
|
1114
|
-
* });
|
|
1115
|
-
* ```
|
|
1116
|
-
*/
|
|
1117
|
-
async getUserTrustedServers(params, options) {
|
|
1118
|
-
const { user } = params;
|
|
1119
|
-
const subgraphUrl = params.subgraphUrl ?? this.context.subgraphUrl;
|
|
1120
|
-
if (subgraphUrl && (options?.minBlock || options?.waitForSync)) {
|
|
1121
|
-
await checkSubgraphConsistency(subgraphUrl, options);
|
|
1122
|
-
}
|
|
1123
|
-
if (subgraphUrl) {
|
|
1124
|
-
try {
|
|
1125
|
-
const servers = await this._getUserTrustedServersViaSubgraph(
|
|
1126
|
-
{
|
|
1127
|
-
user,
|
|
1128
|
-
subgraphUrl
|
|
1129
|
-
},
|
|
1130
|
-
options
|
|
1131
|
-
);
|
|
1132
|
-
return servers;
|
|
1133
|
-
} catch (error) {
|
|
1134
|
-
console.warn("Subgraph query failed, falling back to RPC:", error);
|
|
1135
|
-
}
|
|
1136
|
-
}
|
|
1137
|
-
const limit = options?.fetchAll ? Number.MAX_SAFE_INTEGER : options?.limit ?? 100;
|
|
1138
|
-
const offset = options?.offset ?? 0;
|
|
1139
|
-
const rpcResult = await this._getUserTrustedServersViaRpc({
|
|
1140
|
-
user,
|
|
1141
|
-
limit,
|
|
1142
|
-
offset
|
|
1143
|
-
});
|
|
1144
|
-
return rpcResult.servers;
|
|
1145
|
-
}
|
|
1146
|
-
/**
|
|
1147
|
-
* Internal method: Query trusted servers via subgraph
|
|
1148
|
-
*
|
|
1149
|
-
* @param params - Query parameters object
|
|
1150
|
-
* @param params.user - The user address to query trusted servers for
|
|
1151
|
-
* @param params.subgraphUrl - The subgraph URL endpoint to query
|
|
1152
|
-
* @returns Promise resolving to an array of trusted server objects
|
|
1153
|
-
*/
|
|
1154
|
-
async _getUserTrustedServersViaSubgraph(params, options) {
|
|
1155
|
-
const { user, subgraphUrl } = params;
|
|
1156
|
-
const graphqlEndpoint = subgraphUrl;
|
|
1157
|
-
if (!graphqlEndpoint) {
|
|
1158
|
-
throw new Error(
|
|
1159
|
-
"subgraphUrl is required for subgraph mode. Please provide a valid subgraph endpoint or configure it in Vana constructor."
|
|
1160
|
-
);
|
|
1161
|
-
}
|
|
1162
|
-
try {
|
|
1163
|
-
const orderByMap = {
|
|
1164
|
-
id: "id",
|
|
1165
|
-
trustedAt: "trustedAt",
|
|
1166
|
-
trustedAtBlock: "trustedAtBlock",
|
|
1167
|
-
server: "server",
|
|
1168
|
-
user: "user"
|
|
1169
|
-
};
|
|
1170
|
-
const serverTrusts = await executePaginatedQuery({
|
|
1171
|
-
endpoint: graphqlEndpoint,
|
|
1172
|
-
document: GetUserTrustedServersPaginatedDocument,
|
|
1173
|
-
baseVariables: {
|
|
1174
|
-
userId: user.toLowerCase(),
|
|
1175
|
-
// Subgraph requires lowercase addresses
|
|
1176
|
-
orderBy: mapOrderByToEnum(
|
|
1177
|
-
options?.orderBy,
|
|
1178
|
-
orderByMap,
|
|
1179
|
-
"trustedAtBlock"
|
|
1180
|
-
),
|
|
1181
|
-
orderDirection: mapOrderDirection(
|
|
1182
|
-
options?.orderDirection,
|
|
1183
|
-
"desc",
|
|
1184
|
-
"asc"
|
|
1185
|
-
)
|
|
1186
|
-
},
|
|
1187
|
-
options,
|
|
1188
|
-
extractItems: (data) => {
|
|
1189
|
-
const trusts = data?.user?.serverTrusts ?? [];
|
|
1190
|
-
return trusts.filter((trust) => !trust.untrustedAtBlock);
|
|
1191
|
-
},
|
|
1192
|
-
transformItem: (trust) => ({
|
|
1193
|
-
id: trust.server.id,
|
|
1194
|
-
serverAddress: trust.server.serverAddress,
|
|
1195
|
-
serverUrl: trust.server.url,
|
|
1196
|
-
trustedAt: BigInt(trust.trustedAt),
|
|
1197
|
-
user,
|
|
1198
|
-
name: ""
|
|
1199
|
-
// Not available in new schema, will be empty
|
|
1200
|
-
})
|
|
1201
|
-
});
|
|
1202
|
-
return serverTrusts;
|
|
1203
|
-
} catch (error) {
|
|
1204
|
-
console.error("Failed to query trusted servers from subgraph:", error);
|
|
1205
|
-
throw error;
|
|
1206
|
-
}
|
|
1207
|
-
}
|
|
1208
|
-
/**
|
|
1209
|
-
* Internal method: Query trusted servers via direct RPC
|
|
1210
|
-
*
|
|
1211
|
-
* @param params - Query parameters object
|
|
1212
|
-
* @param params.user - The user address to query trusted servers for
|
|
1213
|
-
* @param params.limit - Maximum number of results to return
|
|
1214
|
-
* @param params.offset - Number of results to skip for pagination
|
|
1215
|
-
* @returns Promise resolving to pagination result with servers, total count, and hasMore flag
|
|
1216
|
-
*/
|
|
1217
|
-
async _getUserTrustedServersViaRpc(params) {
|
|
1218
|
-
const { user, limit, offset } = params;
|
|
1219
|
-
try {
|
|
1220
|
-
const chainId = this.context.publicClient.chain?.id;
|
|
1221
|
-
if (!chainId) {
|
|
1222
|
-
throw new Error("Chain ID not available");
|
|
1223
|
-
}
|
|
1224
|
-
const DataPortabilityServersAddress = getContractAddress(
|
|
1225
|
-
chainId,
|
|
1226
|
-
"DataPortabilityServers"
|
|
1227
|
-
);
|
|
1228
|
-
const DataPortabilityServersAbi = getAbi("DataPortabilityServers");
|
|
1229
|
-
const totalCount = await this.context.publicClient.readContract({
|
|
1230
|
-
address: DataPortabilityServersAddress,
|
|
1231
|
-
abi: DataPortabilityServersAbi,
|
|
1232
|
-
functionName: "userServerIdsLength",
|
|
1233
|
-
args: [user]
|
|
1234
|
-
});
|
|
1235
|
-
const total = Number(totalCount);
|
|
1236
|
-
if (total === 0 || offset >= total) {
|
|
1237
|
-
return {
|
|
1238
|
-
servers: [],
|
|
1239
|
-
total,
|
|
1240
|
-
hasMore: false
|
|
1241
|
-
};
|
|
1242
|
-
}
|
|
1243
|
-
const endIndex = Math.min(offset + limit, total);
|
|
1244
|
-
const serverIdCalls = [];
|
|
1245
|
-
for (let i = offset; i < endIndex; i++) {
|
|
1246
|
-
serverIdCalls.push({
|
|
1247
|
-
address: DataPortabilityServersAddress,
|
|
1248
|
-
abi: DataPortabilityServersAbi,
|
|
1249
|
-
functionName: "userServerIdsAt",
|
|
1250
|
-
args: [user, BigInt(i)]
|
|
1251
|
-
});
|
|
1252
|
-
}
|
|
1253
|
-
const serverIdResults = await gasAwareMulticall(this.context.publicClient, {
|
|
1254
|
-
contracts: serverIdCalls
|
|
1255
|
-
});
|
|
1256
|
-
const serverIds = serverIdResults.map((result) => result).filter((id) => id && id > 0n);
|
|
1257
|
-
const serverInfoCalls = serverIds.map(
|
|
1258
|
-
(serverId) => ({
|
|
1259
|
-
address: DataPortabilityServersAddress,
|
|
1260
|
-
abi: DataPortabilityServersAbi,
|
|
1261
|
-
functionName: "servers",
|
|
1262
|
-
args: [serverId]
|
|
1263
|
-
})
|
|
1264
|
-
);
|
|
1265
|
-
const serverInfoResults = await gasAwareMulticall(this.context.publicClient, {
|
|
1266
|
-
contracts: serverInfoCalls,
|
|
1267
|
-
allowFailure: true
|
|
1268
|
-
});
|
|
1269
|
-
const servers = serverInfoResults.map((result, index) => {
|
|
1270
|
-
const serverId = serverIds[index];
|
|
1271
|
-
if (result.status === "success" && result.result) {
|
|
1272
|
-
const serverInfo = result.result;
|
|
1273
|
-
return {
|
|
1274
|
-
id: `${user.toLowerCase()}-${serverId.toString()}`,
|
|
1275
|
-
serverAddress: serverInfo.serverAddress,
|
|
1276
|
-
serverUrl: serverInfo.url,
|
|
1277
|
-
trustedAt: BigInt(Date.now()),
|
|
1278
|
-
user,
|
|
1279
|
-
trustIndex: offset + index
|
|
1280
|
-
};
|
|
1281
|
-
} else {
|
|
1282
|
-
return {
|
|
1283
|
-
id: `${user.toLowerCase()}-${serverId.toString()}`,
|
|
1284
|
-
serverAddress: "0x0000000000000000000000000000000000000000",
|
|
1285
|
-
serverUrl: "",
|
|
1286
|
-
trustedAt: BigInt(Date.now()),
|
|
1287
|
-
user,
|
|
1288
|
-
trustIndex: offset + index
|
|
1289
|
-
};
|
|
1290
|
-
}
|
|
1291
|
-
});
|
|
1292
|
-
return {
|
|
1293
|
-
servers,
|
|
1294
|
-
total,
|
|
1295
|
-
hasMore: offset + limit < total
|
|
1296
|
-
};
|
|
1297
|
-
} catch (error) {
|
|
1298
|
-
throw new Error(
|
|
1299
|
-
`RPC query failed: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
1300
|
-
);
|
|
1301
|
-
}
|
|
1302
|
-
}
|
|
1303
|
-
/**
|
|
1304
|
-
* Retrieves total file count from Data Registry.
|
|
1305
|
-
*
|
|
1306
|
-
* @remarks
|
|
1307
|
-
* Queries blockchain for complete file count across all users.
|
|
1308
|
-
* Useful for pagination and network statistics.
|
|
1309
|
-
*
|
|
1310
|
-
* @returns Total number of registered files
|
|
1311
|
-
*
|
|
1312
|
-
* @throws {Error} Chain ID not available.
|
|
1313
|
-
* Ensure network connection.
|
|
1314
|
-
* @throws {Error} Contract read failed.
|
|
1315
|
-
* Check RPC availability.
|
|
1316
|
-
*
|
|
1317
|
-
* @example
|
|
1318
|
-
* ```typescript
|
|
1319
|
-
* const total = await vana.data.getTotalFilesCount();
|
|
1320
|
-
* console.log(`Total files: ${total}`);
|
|
1321
|
-
*
|
|
1322
|
-
* // Calculate pagination
|
|
1323
|
-
* const pages = Math.ceil(total / 20);
|
|
1324
|
-
* ```
|
|
1325
|
-
*/
|
|
1326
|
-
async getTotalFilesCount() {
|
|
1327
|
-
try {
|
|
1328
|
-
const chainId = this.context.publicClient.chain?.id;
|
|
1329
|
-
if (!chainId) {
|
|
1330
|
-
throw new Error("Chain ID not available");
|
|
1331
|
-
}
|
|
1332
|
-
const dataRegistryAddress = getContractAddress(chainId, "DataRegistry");
|
|
1333
|
-
const dataRegistryAbi = getAbi("DataRegistry");
|
|
1334
|
-
const dataRegistry = getContract({
|
|
1335
|
-
address: dataRegistryAddress,
|
|
1336
|
-
abi: dataRegistryAbi,
|
|
1337
|
-
client: this.context.publicClient
|
|
1338
|
-
});
|
|
1339
|
-
const count = await dataRegistry.read.filesCount();
|
|
1340
|
-
return Number(count);
|
|
1341
|
-
} catch (error) {
|
|
1342
|
-
if (error instanceof Error && error.message === "Chain ID not available") {
|
|
1343
|
-
throw error;
|
|
1344
|
-
}
|
|
1345
|
-
console.error("Failed to fetch total files count:", error);
|
|
1346
|
-
return 0;
|
|
1347
|
-
}
|
|
1348
|
-
}
|
|
1349
|
-
/**
|
|
1350
|
-
* Retrieves file metadata by ID from the blockchain.
|
|
1351
|
-
*
|
|
1352
|
-
* @remarks
|
|
1353
|
-
* Queries DataRegistry contract directly for file details.
|
|
1354
|
-
* Works for any file ID regardless of ownership, enabling
|
|
1355
|
-
* cross-user file discovery and verification.
|
|
1356
|
-
*
|
|
1357
|
-
* @param fileId - Numeric file ID to retrieve
|
|
1358
|
-
*
|
|
1359
|
-
* @returns UserFile object with metadata
|
|
1360
|
-
*
|
|
1361
|
-
* @throws {Error} Chain ID not available.
|
|
1362
|
-
* Ensure proper network connection.
|
|
1363
|
-
* @throws {Error} File not found.
|
|
1364
|
-
* Verify file ID exists on-chain.
|
|
1365
|
-
* @throws {Error} Contract call failed.
|
|
1366
|
-
* Check network and RPC availability.
|
|
1367
|
-
*
|
|
1368
|
-
* @example
|
|
1369
|
-
* ```typescript
|
|
1370
|
-
* const file = await vana.data.getFileById(123);
|
|
1371
|
-
* console.log(`File ${file.id}:`);
|
|
1372
|
-
* console.log(` URL: ${file.url}`);
|
|
1373
|
-
* console.log(` Owner: ${file.ownerAddress}`);
|
|
1374
|
-
* console.log(` Block: ${file.addedAtBlock}`);
|
|
1375
|
-
* ```
|
|
1376
|
-
*/
|
|
1377
|
-
async getFileById(fileId) {
|
|
1378
|
-
try {
|
|
1379
|
-
const chainId = this.context.publicClient.chain?.id;
|
|
1380
|
-
if (!chainId) {
|
|
1381
|
-
throw new Error("Chain ID not available");
|
|
1382
|
-
}
|
|
1383
|
-
const dataRegistryAddress = getContractAddress(chainId, "DataRegistry");
|
|
1384
|
-
const dataRegistryAbi = getAbi("DataRegistry");
|
|
1385
|
-
const dataRegistry = getContract({
|
|
1386
|
-
address: dataRegistryAddress,
|
|
1387
|
-
abi: dataRegistryAbi,
|
|
1388
|
-
client: this.context.publicClient
|
|
1389
|
-
});
|
|
1390
|
-
const fileDetails = await dataRegistry.read.files([BigInt(fileId)]);
|
|
1391
|
-
if (!fileDetails) {
|
|
1392
|
-
throw new Error("File not found");
|
|
1393
|
-
}
|
|
1394
|
-
if (Array.isArray(fileDetails)) {
|
|
1395
|
-
const [id, url, ownerAddress, addedAtBlock] = fileDetails;
|
|
1396
|
-
if (id === BigInt(0)) {
|
|
1397
|
-
throw new Error("File not found");
|
|
1398
|
-
}
|
|
1399
|
-
return {
|
|
1400
|
-
id: Number(id),
|
|
1401
|
-
url,
|
|
1402
|
-
ownerAddress,
|
|
1403
|
-
addedAtBlock: BigInt(addedAtBlock)
|
|
1404
|
-
};
|
|
1405
|
-
} else {
|
|
1406
|
-
if (!fileDetails.id || fileDetails.id === BigInt(0)) {
|
|
1407
|
-
throw new Error("File not found");
|
|
1408
|
-
}
|
|
1409
|
-
return {
|
|
1410
|
-
id: Number(fileDetails.id),
|
|
1411
|
-
ownerAddress: fileDetails.ownerAddress,
|
|
1412
|
-
url: fileDetails.url,
|
|
1413
|
-
addedAtBlock: BigInt(fileDetails.addedAtBlock)
|
|
1414
|
-
};
|
|
1415
|
-
}
|
|
1416
|
-
} catch (error) {
|
|
1417
|
-
console.error("Failed to fetch file by ID:", error);
|
|
1418
|
-
throw new Error(
|
|
1419
|
-
`Failed to fetch file ${fileId}: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
1420
|
-
);
|
|
1421
|
-
}
|
|
1422
|
-
}
|
|
1423
|
-
/**
|
|
1424
|
-
* Registers a file URL directly on the blockchain with a schema ID.
|
|
1425
|
-
*
|
|
1426
|
-
* @remarks
|
|
1427
|
-
* This method registers an existing file URL on the DataRegistry contract
|
|
1428
|
-
* with a schema ID, without uploading any data. Useful when you have already
|
|
1429
|
-
* uploaded content to storage and just need to register it on-chain.
|
|
1430
|
-
*
|
|
1431
|
-
* @param url - The URL of the file to register (IPFS or HTTP/HTTPS)
|
|
1432
|
-
* @param schemaId - The schema ID to associate with the file
|
|
1433
|
-
* @returns Promise resolving to the file ID and transaction hash
|
|
1434
|
-
* @throws {Error} When chain ID is not available - "Chain ID not available"
|
|
1435
|
-
* @throws {Error} When wallet address is unavailable - "No addresses available"
|
|
1436
|
-
* @throws {Error} When transaction fails - "Failed to register file with schema"
|
|
1437
|
-
* @example
|
|
1438
|
-
* ```typescript
|
|
1439
|
-
* const { fileId, transactionHash } = await vana.data.registerFileWithSchema(
|
|
1440
|
-
* "ipfs://QmXxx...",
|
|
1441
|
-
* 1
|
|
1442
|
-
* );
|
|
1443
|
-
* console.log(`File ${fileId} registered with schema in tx ${transactionHash}`);
|
|
1444
|
-
* ```
|
|
1445
|
-
*/
|
|
1446
|
-
async registerFileWithSchema(url, schemaId, options) {
|
|
1447
|
-
this.assertWallet();
|
|
1448
|
-
try {
|
|
1449
|
-
const chainId = this.context.publicClient.chain?.id;
|
|
1450
|
-
if (!chainId) {
|
|
1451
|
-
throw new Error("Chain ID not available");
|
|
1452
|
-
}
|
|
1453
|
-
this.assertWallet();
|
|
1454
|
-
const dataRegistryAddress = getContractAddress(chainId, "DataRegistry");
|
|
1455
|
-
const dataRegistryAbi = getAbi("DataRegistry");
|
|
1456
|
-
const account = this.context.walletClient.account ?? this.context.userAddress;
|
|
1457
|
-
const from = typeof account === "string" ? account : account.address;
|
|
1458
|
-
const hash = await this.context.walletClient.writeContract({
|
|
1459
|
-
address: dataRegistryAddress,
|
|
1460
|
-
abi: dataRegistryAbi,
|
|
1461
|
-
functionName: "addFileWithSchema",
|
|
1462
|
-
args: [url, BigInt(schemaId)],
|
|
1463
|
-
account,
|
|
1464
|
-
chain: this.context.walletClient.chain ?? null,
|
|
1465
|
-
...this.spreadTransactionOptions(options)
|
|
1466
|
-
});
|
|
1467
|
-
const { tx } = await import("../utils/transactionHelpers");
|
|
1468
|
-
return tx({
|
|
1469
|
-
hash,
|
|
1470
|
-
from,
|
|
1471
|
-
contract: "DataRegistry",
|
|
1472
|
-
fn: "addFileWithSchema"
|
|
1473
|
-
});
|
|
1474
|
-
} catch (error) {
|
|
1475
|
-
console.error("Failed to register file with schema:", error);
|
|
1476
|
-
throw new Error(
|
|
1477
|
-
`Registration failed: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
1478
|
-
);
|
|
1479
|
-
}
|
|
1480
|
-
}
|
|
1481
|
-
/**
|
|
1482
|
-
* Gets the user's address from the wallet client.
|
|
1483
|
-
*
|
|
1484
|
-
* @returns Promise resolving to the user's wallet address
|
|
1485
|
-
* @throws {Error} When no addresses are available in wallet client
|
|
1486
|
-
*/
|
|
1487
|
-
/**
|
|
1488
|
-
* Adds a file with permissions to the DataRegistry contract.
|
|
1489
|
-
*
|
|
1490
|
-
* @param url - The file URL to register
|
|
1491
|
-
* @param ownerAddress - The address of the file owner
|
|
1492
|
-
* @param permissions - Array of permissions to set for the file
|
|
1493
|
-
* @returns Promise resolving to file ID and transaction hash
|
|
1494
|
-
* @throws {Error} When chain ID is not available
|
|
1495
|
-
* @throws {ContractError} When contract execution fails
|
|
1496
|
-
* @throws {Error} When transaction receipt is not available
|
|
1497
|
-
* @throws {Error} When FileAdded event cannot be parsed
|
|
1498
|
-
*
|
|
1499
|
-
* This method handles the core logic of registering a file
|
|
1500
|
-
* with specific permissions on the DataRegistry contract. It can be used
|
|
1501
|
-
* by both direct transactions and relayer services.
|
|
1502
|
-
*/
|
|
1503
|
-
async addFileWithPermissions(url, ownerAddress, permissions = [], options) {
|
|
1504
|
-
this.assertWallet();
|
|
1505
|
-
try {
|
|
1506
|
-
const chainId = this.context.publicClient.chain?.id;
|
|
1507
|
-
if (!chainId) {
|
|
1508
|
-
throw new Error("Chain ID not available");
|
|
1509
|
-
}
|
|
1510
|
-
this.assertWallet();
|
|
1511
|
-
const dataRegistryAddress = getContractAddress(chainId, "DataRegistry");
|
|
1512
|
-
const dataRegistryAbi = getAbi("DataRegistry");
|
|
1513
|
-
const account = this.context.walletClient.account ?? ownerAddress;
|
|
1514
|
-
const from = typeof account === "string" ? account : account.address;
|
|
1515
|
-
const hash = await this.context.walletClient.writeContract({
|
|
1516
|
-
address: dataRegistryAddress,
|
|
1517
|
-
abi: dataRegistryAbi,
|
|
1518
|
-
functionName: "addFileWithPermissions",
|
|
1519
|
-
args: [url, ownerAddress, permissions],
|
|
1520
|
-
account,
|
|
1521
|
-
chain: this.context.walletClient.chain ?? null,
|
|
1522
|
-
...this.spreadTransactionOptions(options)
|
|
1523
|
-
});
|
|
1524
|
-
const { tx } = await import("../utils/transactionHelpers");
|
|
1525
|
-
return tx({
|
|
1526
|
-
hash,
|
|
1527
|
-
from,
|
|
1528
|
-
contract: "DataRegistry",
|
|
1529
|
-
fn: "addFileWithPermissions"
|
|
1530
|
-
});
|
|
1531
|
-
} catch (error) {
|
|
1532
|
-
console.error("Failed to add file with permissions:", error);
|
|
1533
|
-
throw new Error(
|
|
1534
|
-
`Failed to add file with permissions: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
1535
|
-
);
|
|
1536
|
-
}
|
|
1537
|
-
}
|
|
1538
|
-
/**
|
|
1539
|
-
* Adds a file to the registry with permissions and schema.
|
|
1540
|
-
* This combines the functionality of addFileWithPermissions and schema validation.
|
|
1541
|
-
*
|
|
1542
|
-
* @remarks
|
|
1543
|
-
* This method automatically encrypts permissions when a publicKey is provided.
|
|
1544
|
-
* It generates the user's encryption key and encrypts it with each recipient's
|
|
1545
|
-
* public key before registering on the blockchain.
|
|
1546
|
-
*
|
|
1547
|
-
* @param url - The URL of the file to register
|
|
1548
|
-
* @param ownerAddress - The address of the file owner
|
|
1549
|
-
* @param permissions - Array of permissions to grant, each with account and publicKey properties
|
|
1550
|
-
* @param schemaId - The schema ID to associate with the file (0 for no schema)
|
|
1551
|
-
* @returns Promise resolving to TransactionResult with fileId and transactionHash
|
|
1552
|
-
* @throws {Error} "Chain ID not available" - When wallet chain is not configured
|
|
1553
|
-
* @throws {Error} "Failed to generate encryption key" - When encryption key generation fails
|
|
1554
|
-
* @throws {Error} "Permission for {account} must include 'publicKey'" - When publicKey is missing
|
|
1555
|
-
* @throws {Error} "Failed to add file with permissions and schema: {error}" - When transaction fails
|
|
1556
|
-
* @example
|
|
1557
|
-
* ```typescript
|
|
1558
|
-
* // Get server's public key
|
|
1559
|
-
* const serverIdentity = await vana.server.getIdentity({
|
|
1560
|
-
* userAddress: "0x..."
|
|
1561
|
-
* });
|
|
1562
|
-
*
|
|
1563
|
-
* // Add file with permissions and schema
|
|
1564
|
-
* const result = await vana.data.addFileWithPermissionsAndSchema(
|
|
1565
|
-
* "ipfs://QmXxx...",
|
|
1566
|
-
* ownerAddress,
|
|
1567
|
-
* [{
|
|
1568
|
-
* account: serverIdentity.address,
|
|
1569
|
-
* publicKey: serverIdentity.publicKey
|
|
1570
|
-
* }],
|
|
1571
|
-
* schemaId
|
|
1572
|
-
* );
|
|
1573
|
-
*
|
|
1574
|
-
* console.log(`File ${result.fileId} registered in tx ${result.hash}`);
|
|
1575
|
-
* ```
|
|
1576
|
-
*/
|
|
1577
|
-
async addFileWithPermissionsAndSchema(url, ownerAddress, permissions = [], schemaId = 0, options) {
|
|
1578
|
-
this.assertWallet();
|
|
1579
|
-
try {
|
|
1580
|
-
let encryptedPermissions = [];
|
|
1581
|
-
if (permissions.length > 0) {
|
|
1582
|
-
this.assertWallet();
|
|
1583
|
-
const userEncryptionKey = await generateEncryptionKey(
|
|
1584
|
-
this.context.walletClient,
|
|
1585
|
-
this.context.platform,
|
|
1586
|
-
DEFAULT_ENCRYPTION_SEED
|
|
1587
|
-
);
|
|
1588
|
-
encryptedPermissions = await Promise.all(
|
|
1589
|
-
permissions.map(async (permission) => {
|
|
1590
|
-
if (!permission.publicKey) {
|
|
1591
|
-
throw new Error(
|
|
1592
|
-
`Permission for ${permission.account} must include 'publicKey'`
|
|
1593
|
-
);
|
|
1594
|
-
}
|
|
1595
|
-
const encryptedKey = await encryptWithWalletPublicKey(
|
|
1596
|
-
userEncryptionKey,
|
|
1597
|
-
permission.publicKey,
|
|
1598
|
-
this.context.platform
|
|
1599
|
-
);
|
|
1600
|
-
return {
|
|
1601
|
-
account: permission.account,
|
|
1602
|
-
key: encryptedKey
|
|
1603
|
-
};
|
|
1604
|
-
})
|
|
1605
|
-
);
|
|
1606
|
-
}
|
|
1607
|
-
return await this.addFileWithEncryptedPermissionsAndSchema(
|
|
1608
|
-
url,
|
|
1609
|
-
ownerAddress,
|
|
1610
|
-
encryptedPermissions,
|
|
1611
|
-
schemaId,
|
|
1612
|
-
options
|
|
1613
|
-
);
|
|
1614
|
-
} catch (error) {
|
|
1615
|
-
console.error("Failed to add file with permissions and schema:", error);
|
|
1616
|
-
throw new Error(
|
|
1617
|
-
`Failed to add file with permissions and schema: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
1618
|
-
);
|
|
1619
|
-
}
|
|
1620
|
-
}
|
|
1621
|
-
/**
|
|
1622
|
-
* Adds a file with pre-encrypted permissions and schema to the DataRegistry.
|
|
1623
|
-
*
|
|
1624
|
-
* @remarks
|
|
1625
|
-
* This method is designed for relay services and advanced use cases where permissions
|
|
1626
|
-
* have already been encrypted client-side. Unlike `addFileWithPermissionsAndSchema()`,
|
|
1627
|
-
* this method expects permissions in the encrypted format with a 'key' field instead
|
|
1628
|
-
* of 'publicKey'.
|
|
1629
|
-
*
|
|
1630
|
-
* This is typically used by relay endpoints that receive pre-encrypted data from
|
|
1631
|
-
* the client SDK's `upload()` method, avoiding double encryption.
|
|
1632
|
-
*
|
|
1633
|
-
* @param url - The storage URL of the file (e.g., IPFS URL)
|
|
1634
|
-
* @param ownerAddress - The address that will own this file
|
|
1635
|
-
* @param permissions - Array of pre-encrypted permissions with 'account' and 'key' fields
|
|
1636
|
-
* @param schemaId - Optional schema ID for data validation (defaults to 0)
|
|
1637
|
-
* @returns Promise resolving to transaction result with hash and contract details
|
|
1638
|
-
* @throws {Error} When chain ID is not available
|
|
1639
|
-
* @throws {Error} When wallet is not connected
|
|
1640
|
-
* @throws {Error} When transaction fails
|
|
1641
|
-
* @example
|
|
1642
|
-
* ```typescript
|
|
1643
|
-
* // In a relay endpoint that receives pre-encrypted permissions
|
|
1644
|
-
* const result = await vana.data.addFileWithEncryptedPermissionsAndSchema(
|
|
1645
|
-
* "ipfs://QmXxx...",
|
|
1646
|
-
* ownerAddress,
|
|
1647
|
-
* [
|
|
1648
|
-
* {
|
|
1649
|
-
* account: "0xServerAddress...",
|
|
1650
|
-
* key: "encrypted_key_string" // Already encrypted by client
|
|
1651
|
-
* }
|
|
1652
|
-
* ],
|
|
1653
|
-
* schemaId
|
|
1654
|
-
* );
|
|
1655
|
-
*
|
|
1656
|
-
* console.log(`File registered in tx ${result.hash}`);
|
|
1657
|
-
* ```
|
|
1658
|
-
*/
|
|
1659
|
-
async addFileWithEncryptedPermissionsAndSchema(url, ownerAddress, permissions = [], schemaId = 0, options) {
|
|
1660
|
-
try {
|
|
1661
|
-
const chainId = this.context.publicClient.chain?.id;
|
|
1662
|
-
if (!chainId) {
|
|
1663
|
-
throw new Error("Chain ID not available");
|
|
1664
|
-
}
|
|
1665
|
-
this.assertWallet();
|
|
1666
|
-
const dataRegistryAddress = getContractAddress(chainId, "DataRegistry");
|
|
1667
|
-
const dataRegistryAbi = getAbi("DataRegistry");
|
|
1668
|
-
const account = this.context.walletClient.account ?? ownerAddress;
|
|
1669
|
-
const from = typeof account === "string" ? account : account.address;
|
|
1670
|
-
const hash = await this.context.walletClient.writeContract({
|
|
1671
|
-
address: dataRegistryAddress,
|
|
1672
|
-
abi: dataRegistryAbi,
|
|
1673
|
-
functionName: "addFileWithPermissionsAndSchema",
|
|
1674
|
-
args: [url, ownerAddress, permissions, BigInt(schemaId)],
|
|
1675
|
-
account,
|
|
1676
|
-
chain: this.context.walletClient.chain ?? null,
|
|
1677
|
-
...this.spreadTransactionOptions(options)
|
|
1678
|
-
});
|
|
1679
|
-
const { tx } = await import("../utils/transactionHelpers");
|
|
1680
|
-
return tx({
|
|
1681
|
-
hash,
|
|
1682
|
-
from,
|
|
1683
|
-
contract: "DataRegistry",
|
|
1684
|
-
fn: "addFileWithPermissionsAndSchema"
|
|
1685
|
-
});
|
|
1686
|
-
} catch (error) {
|
|
1687
|
-
console.error("Failed to add file with permissions and schema:", error);
|
|
1688
|
-
throw new Error(
|
|
1689
|
-
`Failed to add file with permissions and schema: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
1690
|
-
);
|
|
1691
|
-
}
|
|
1692
|
-
}
|
|
1693
|
-
/**
|
|
1694
|
-
* Registers a data refiner for processing templates.
|
|
1695
|
-
*
|
|
1696
|
-
* @remarks
|
|
1697
|
-
* Refiners define data transformation rules for DLPs.
|
|
1698
|
-
* Associates schema, instructions, and processing logic.
|
|
1699
|
-
*
|
|
1700
|
-
* @param params - Refiner configuration
|
|
1701
|
-
* @param params.dlpId - Data Liquidity Pool ID
|
|
1702
|
-
* @param params.name - Refiner display name
|
|
1703
|
-
* @param params.schemaId - Output schema ID.
|
|
1704
|
-
* Obtain via `vana.schemas.list()`.
|
|
1705
|
-
* @param params.refinementInstructionUrl - Processing instructions URL
|
|
1706
|
-
*
|
|
1707
|
-
* @returns Refiner ID and transaction hash
|
|
1708
|
-
*
|
|
1709
|
-
* @throws {Error} Chain ID not available.
|
|
1710
|
-
* Ensure network connection.
|
|
1711
|
-
* @throws {Error} Transaction failed.
|
|
1712
|
-
* Check wallet balance and network status.
|
|
1713
|
-
*
|
|
1714
|
-
* @example
|
|
1715
|
-
* ```typescript
|
|
1716
|
-
* const result = await vana.data.addRefiner({
|
|
1717
|
-
* dlpId: 1,
|
|
1718
|
-
* name: "Sentiment Analyzer",
|
|
1719
|
-
* schemaId: 42,
|
|
1720
|
-
* refinementInstructionUrl: "ipfs://QmXxx..."
|
|
1721
|
-
* });
|
|
1722
|
-
* console.log(`Refiner ${result.refinerId} created`);
|
|
1723
|
-
* ```
|
|
1724
|
-
*/
|
|
1725
|
-
async addRefiner(params, options) {
|
|
1726
|
-
this.assertWallet();
|
|
1727
|
-
try {
|
|
1728
|
-
const chainId = this.context.publicClient.chain?.id;
|
|
1729
|
-
if (!chainId) {
|
|
1730
|
-
throw new Error("Chain ID not available");
|
|
1731
|
-
}
|
|
1732
|
-
const dataRefinerRegistryAddress = getContractAddress(
|
|
1733
|
-
chainId,
|
|
1734
|
-
"DataRefinerRegistry"
|
|
1735
|
-
);
|
|
1736
|
-
const dataRefinerRegistryAbi = getAbi("DataRefinerRegistry");
|
|
1737
|
-
this.assertWallet();
|
|
1738
|
-
const account = this.context.walletClient.account ?? this.context.userAddress;
|
|
1739
|
-
const from = typeof account === "string" ? account : account.address;
|
|
1740
|
-
const hash = await this.context.walletClient.writeContract({
|
|
1741
|
-
address: dataRefinerRegistryAddress,
|
|
1742
|
-
abi: dataRefinerRegistryAbi,
|
|
1743
|
-
functionName: "addRefinerWithSchemaId",
|
|
1744
|
-
args: [
|
|
1745
|
-
BigInt(params.dlpId),
|
|
1746
|
-
params.name,
|
|
1747
|
-
BigInt(params.schemaId),
|
|
1748
|
-
params.refinementInstructionUrl
|
|
1749
|
-
],
|
|
1750
|
-
account,
|
|
1751
|
-
chain: this.context.walletClient.chain ?? null,
|
|
1752
|
-
...this.spreadTransactionOptions(options)
|
|
1753
|
-
});
|
|
1754
|
-
const { tx } = await import("../utils/transactionHelpers");
|
|
1755
|
-
const txResult = tx({
|
|
1756
|
-
hash,
|
|
1757
|
-
from,
|
|
1758
|
-
contract: "DataRefinerRegistry",
|
|
1759
|
-
fn: "addRefinerWithSchemaId"
|
|
1760
|
-
});
|
|
1761
|
-
if (!this.context.waitForTransactionEvents) {
|
|
1762
|
-
throw new Error("waitForTransactionEvents not configured");
|
|
1763
|
-
}
|
|
1764
|
-
const result = await this.context.waitForTransactionEvents(txResult);
|
|
1765
|
-
const event = result.expectedEvents.RefinerAdded;
|
|
1766
|
-
if (!event) {
|
|
1767
|
-
throw new Error("RefinerAdded event not found in transaction");
|
|
1768
|
-
}
|
|
1769
|
-
return {
|
|
1770
|
-
refinerId: Number(event.refinerId),
|
|
1771
|
-
transactionHash: hash
|
|
1772
|
-
};
|
|
1773
|
-
} catch (error) {
|
|
1774
|
-
console.error("Failed to add refiner:", error);
|
|
1775
|
-
throw new Error(
|
|
1776
|
-
`Failed to add refiner: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
1777
|
-
);
|
|
1778
|
-
}
|
|
1779
|
-
}
|
|
1780
|
-
/**
|
|
1781
|
-
* Retrieves refiner configuration by ID.
|
|
1782
|
-
*
|
|
1783
|
-
* @remarks
|
|
1784
|
-
* Queries DataRefinerRegistry for refiner details.
|
|
1785
|
-
* Returns DLP association, schema, and processing instructions.
|
|
1786
|
-
*
|
|
1787
|
-
* @param refinerId - Numeric refiner ID
|
|
1788
|
-
*
|
|
1789
|
-
* @returns Refiner configuration object
|
|
1790
|
-
*
|
|
1791
|
-
* @throws {Error} Chain ID not available.
|
|
1792
|
-
* Ensure network connection.
|
|
1793
|
-
* @throws {Error} Refiner not found.
|
|
1794
|
-
* Verify refiner ID exists.
|
|
1795
|
-
* @throws {Error} Contract read failed.
|
|
1796
|
-
* Check network and RPC status.
|
|
1797
|
-
*
|
|
1798
|
-
* @example
|
|
1799
|
-
* ```typescript
|
|
1800
|
-
* const refiner = await vana.data.getRefiner(1);
|
|
1801
|
-
* console.log(`Refiner: ${refiner.name}`);
|
|
1802
|
-
* console.log(`DLP: ${refiner.dlpId}`);
|
|
1803
|
-
* console.log(`Schema: ${refiner.schemaId}`);
|
|
1804
|
-
* ```
|
|
1805
|
-
*/
|
|
1806
|
-
async getRefiner(refinerId) {
|
|
1807
|
-
try {
|
|
1808
|
-
const chainId = this.context.publicClient.chain?.id;
|
|
1809
|
-
if (!chainId) {
|
|
1810
|
-
throw new Error("Chain ID not available");
|
|
1811
|
-
}
|
|
1812
|
-
const dataRefinerRegistryAddress = getContractAddress(
|
|
1813
|
-
chainId,
|
|
1814
|
-
"DataRefinerRegistry"
|
|
1815
|
-
);
|
|
1816
|
-
const dataRefinerRegistryAbi = getAbi("DataRefinerRegistry");
|
|
1817
|
-
const dataRefinerRegistry = getContract({
|
|
1818
|
-
address: dataRefinerRegistryAddress,
|
|
1819
|
-
abi: dataRefinerRegistryAbi,
|
|
1820
|
-
client: this.context.publicClient
|
|
1821
|
-
});
|
|
1822
|
-
const refinerData = await dataRefinerRegistry.read.refiners([
|
|
1823
|
-
BigInt(refinerId)
|
|
1824
|
-
]);
|
|
1825
|
-
if (!refinerData) {
|
|
1826
|
-
throw new Error("Refiner not found");
|
|
1827
|
-
}
|
|
1828
|
-
return {
|
|
1829
|
-
id: refinerId,
|
|
1830
|
-
dlpId: Number(refinerData.dlpId),
|
|
1831
|
-
owner: refinerData.owner,
|
|
1832
|
-
name: refinerData.name,
|
|
1833
|
-
schemaId: Number(refinerData.schemaId),
|
|
1834
|
-
refinementInstructionUrl: refinerData.refinementInstructionUrl
|
|
1835
|
-
};
|
|
1836
|
-
} catch (error) {
|
|
1837
|
-
console.error("Failed to get refiner:", error);
|
|
1838
|
-
throw new Error(
|
|
1839
|
-
`Failed to get refiner ${refinerId}: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
1840
|
-
);
|
|
1841
|
-
}
|
|
1842
|
-
}
|
|
1843
|
-
/**
|
|
1844
|
-
* Validates schema ID existence.
|
|
1845
|
-
*
|
|
1846
|
-
* @remarks
|
|
1847
|
-
* Verifies schema registration in DataRegistry.
|
|
1848
|
-
* Check before using schemas for uploads.
|
|
1849
|
-
*
|
|
1850
|
-
* @param schemaId - Numeric schema ID to validate
|
|
1851
|
-
*
|
|
1852
|
-
* @returns True if schema exists, false otherwise
|
|
1853
|
-
*
|
|
1854
|
-
* @throws {Error} Chain ID not available.
|
|
1855
|
-
* Ensure network connection.
|
|
1856
|
-
* @throws {Error} Contract read failed.
|
|
1857
|
-
* Check RPC availability.
|
|
1858
|
-
*
|
|
1859
|
-
* @example
|
|
1860
|
-
* ```typescript
|
|
1861
|
-
* const valid = await vana.data.isValidSchemaId(42);
|
|
1862
|
-
* if (valid) {
|
|
1863
|
-
* // Safe to use schema 42
|
|
1864
|
-
* await vana.data.upload({ schemaId: 42, ... });
|
|
1865
|
-
* }
|
|
1866
|
-
* ```
|
|
1867
|
-
*/
|
|
1868
|
-
async isValidSchemaId(schemaId) {
|
|
1869
|
-
try {
|
|
1870
|
-
const chainId = this.context.publicClient.chain?.id;
|
|
1871
|
-
if (!chainId) {
|
|
1872
|
-
throw new Error("Chain ID not available");
|
|
1873
|
-
}
|
|
1874
|
-
const dataRefinerRegistryAddress = getContractAddress(
|
|
1875
|
-
chainId,
|
|
1876
|
-
"DataRefinerRegistry"
|
|
1877
|
-
);
|
|
1878
|
-
const dataRefinerRegistryAbi = getAbi("DataRefinerRegistry");
|
|
1879
|
-
const dataRefinerRegistry = getContract({
|
|
1880
|
-
address: dataRefinerRegistryAddress,
|
|
1881
|
-
abi: dataRefinerRegistryAbi,
|
|
1882
|
-
client: this.context.publicClient
|
|
1883
|
-
});
|
|
1884
|
-
const isValid = await dataRefinerRegistry.read.isValidSchemaId([
|
|
1885
|
-
BigInt(schemaId)
|
|
1886
|
-
]);
|
|
1887
|
-
return isValid;
|
|
1888
|
-
} catch (error) {
|
|
1889
|
-
console.error("Failed to validate schema ID:", error);
|
|
1890
|
-
return false;
|
|
1891
|
-
}
|
|
1892
|
-
}
|
|
1893
|
-
/**
|
|
1894
|
-
* Gets the total number of refiners in the registry.
|
|
1895
|
-
*
|
|
1896
|
-
* @remarks
|
|
1897
|
-
* Queries the DataRefinerRegistry contract to get the total count of all
|
|
1898
|
-
* registered refiners across all DLPs.
|
|
1899
|
-
*
|
|
1900
|
-
* @returns Promise resolving to the total refiner count
|
|
1901
|
-
* @example
|
|
1902
|
-
* ```typescript
|
|
1903
|
-
* const count = await vana.data.getRefinersCount();
|
|
1904
|
-
* console.log(`Total refiners registered: ${count}`);
|
|
1905
|
-
* ```
|
|
1906
|
-
*/
|
|
1907
|
-
async getRefinersCount() {
|
|
1908
|
-
try {
|
|
1909
|
-
const chainId = this.context.publicClient.chain?.id;
|
|
1910
|
-
if (!chainId) {
|
|
1911
|
-
throw new Error("Chain ID not available");
|
|
1912
|
-
}
|
|
1913
|
-
const dataRefinerRegistryAddress = getContractAddress(
|
|
1914
|
-
chainId,
|
|
1915
|
-
"DataRefinerRegistry"
|
|
1916
|
-
);
|
|
1917
|
-
const dataRefinerRegistryAbi = getAbi("DataRefinerRegistry");
|
|
1918
|
-
const dataRefinerRegistry = getContract({
|
|
1919
|
-
address: dataRefinerRegistryAddress,
|
|
1920
|
-
abi: dataRefinerRegistryAbi,
|
|
1921
|
-
client: this.context.publicClient
|
|
1922
|
-
});
|
|
1923
|
-
const count = await dataRefinerRegistry.read.refinersCount();
|
|
1924
|
-
return Number(count);
|
|
1925
|
-
} catch (error) {
|
|
1926
|
-
console.error("Failed to get refiners count:", error);
|
|
1927
|
-
return 0;
|
|
1928
|
-
}
|
|
1929
|
-
}
|
|
1930
|
-
/**
|
|
1931
|
-
* Updates the schema ID for an existing refiner.
|
|
1932
|
-
*
|
|
1933
|
-
* @remarks
|
|
1934
|
-
* Allows the owner of a refiner to update its associated schema ID.
|
|
1935
|
-
* This is useful when refiner output format needs to change.
|
|
1936
|
-
*
|
|
1937
|
-
* @param params - Update parameters
|
|
1938
|
-
* @param params.refinerId - The refiner ID to update
|
|
1939
|
-
* @param params.newSchemaId - The new schema ID to set
|
|
1940
|
-
* @returns Promise resolving to the transaction hash
|
|
1941
|
-
* @throws {Error} When chain ID is not available - "Chain ID not available"
|
|
1942
|
-
* @throws {Error} When transaction fails - "Failed to update schema ID: {error}"
|
|
1943
|
-
* @example
|
|
1944
|
-
* ```typescript
|
|
1945
|
-
* const result = await vana.data.updateSchemaId({
|
|
1946
|
-
* refinerId: 1,
|
|
1947
|
-
* newSchemaId: 55
|
|
1948
|
-
* });
|
|
1949
|
-
* console.log(`Schema updated in tx ${result.transactionHash}`);
|
|
1950
|
-
* ```
|
|
1951
|
-
*/
|
|
1952
|
-
async updateSchemaId(params, options) {
|
|
1953
|
-
this.assertWallet();
|
|
1954
|
-
try {
|
|
1955
|
-
const chainId = this.context.publicClient.chain?.id;
|
|
1956
|
-
if (!chainId) {
|
|
1957
|
-
throw new Error("Chain ID not available");
|
|
1958
|
-
}
|
|
1959
|
-
const dataRefinerRegistryAddress = getContractAddress(
|
|
1960
|
-
chainId,
|
|
1961
|
-
"DataRefinerRegistry"
|
|
1962
|
-
);
|
|
1963
|
-
const dataRefinerRegistryAbi = getAbi("DataRefinerRegistry");
|
|
1964
|
-
this.assertWallet();
|
|
1965
|
-
const account = this.context.walletClient.account ?? this.context.userAddress;
|
|
1966
|
-
const hash = await this.context.walletClient.writeContract({
|
|
1967
|
-
address: dataRefinerRegistryAddress,
|
|
1968
|
-
abi: dataRefinerRegistryAbi,
|
|
1969
|
-
functionName: "updateSchemaId",
|
|
1970
|
-
args: [BigInt(params.refinerId), BigInt(params.newSchemaId)],
|
|
1971
|
-
account,
|
|
1972
|
-
chain: this.context.walletClient.chain ?? null,
|
|
1973
|
-
...this.spreadTransactionOptions(options)
|
|
1974
|
-
});
|
|
1975
|
-
await this.context.publicClient.waitForTransactionReceipt({ hash });
|
|
1976
|
-
return {
|
|
1977
|
-
transactionHash: hash
|
|
1978
|
-
};
|
|
1979
|
-
} catch (error) {
|
|
1980
|
-
console.error("Failed to update schema ID:", error);
|
|
1981
|
-
throw new Error(
|
|
1982
|
-
`Failed to update schema ID: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
1983
|
-
);
|
|
1984
|
-
}
|
|
1985
|
-
}
|
|
1986
|
-
/**
|
|
1987
|
-
* Uploads an encrypted file and grants permission to a party with a public key.
|
|
1988
|
-
*
|
|
1989
|
-
* This method handles the complete workflow:
|
|
1990
|
-
* 1. Encrypts the file with the user's encryption key
|
|
1991
|
-
* 2. Uploads the encrypted file to storage
|
|
1992
|
-
* 3. Encrypts the user's encryption key with the provided public key
|
|
1993
|
-
* 4. Registers the file with permissions
|
|
1994
|
-
*
|
|
1995
|
-
* @param params - Upload parameters including data, permissions, and options
|
|
1996
|
-
* @returns Promise resolving to upload result with file ID and storage URL
|
|
1997
|
-
*/
|
|
1998
|
-
async uploadFileWithPermissions(params) {
|
|
1999
|
-
this.assertWallet();
|
|
2000
|
-
const { data, permissions, filename, providerName } = params;
|
|
2001
|
-
try {
|
|
2002
|
-
const uploadResult = await this.uploadToStorage(
|
|
2003
|
-
data,
|
|
2004
|
-
filename,
|
|
2005
|
-
true,
|
|
2006
|
-
// Always encrypt for uploadFileWithPermissions
|
|
2007
|
-
providerName
|
|
2008
|
-
);
|
|
2009
|
-
const userAddress = this.context.userAddress;
|
|
2010
|
-
const userEncryptionKey = await generateEncryptionKey(
|
|
2011
|
-
this.context.walletClient,
|
|
2012
|
-
this.context.platform,
|
|
2013
|
-
DEFAULT_ENCRYPTION_SEED
|
|
2014
|
-
);
|
|
2015
|
-
const encryptedPermissions = await Promise.all(
|
|
2016
|
-
permissions.map(async (permission) => {
|
|
2017
|
-
const encryptedKey = await encryptWithWalletPublicKey(
|
|
2018
|
-
userEncryptionKey,
|
|
2019
|
-
permission.publicKey,
|
|
2020
|
-
this.context.platform
|
|
2021
|
-
);
|
|
2022
|
-
return {
|
|
2023
|
-
account: permission.account,
|
|
2024
|
-
key: encryptedKey
|
|
2025
|
-
};
|
|
2026
|
-
})
|
|
2027
|
-
);
|
|
2028
|
-
if (this.context.relayer) {
|
|
2029
|
-
const request = {
|
|
2030
|
-
type: "direct",
|
|
2031
|
-
operation: "submitFileAdditionWithPermissions",
|
|
2032
|
-
params: {
|
|
2033
|
-
url: uploadResult.url,
|
|
2034
|
-
userAddress,
|
|
2035
|
-
permissions: encryptedPermissions
|
|
2036
|
-
}
|
|
2037
|
-
};
|
|
2038
|
-
const response = await this.context.relayer(request);
|
|
2039
|
-
if (response.type === "error") {
|
|
2040
|
-
throw new Error(response.error);
|
|
2041
|
-
}
|
|
2042
|
-
let result;
|
|
2043
|
-
if (response.type === "pending") {
|
|
2044
|
-
result = await this.pollRelayerForConfirmation(
|
|
2045
|
-
response.operationId,
|
|
2046
|
-
void 0
|
|
2047
|
-
// TODO: Add TransactionOptions to upload method signature
|
|
2048
|
-
);
|
|
2049
|
-
} else if (response.type === "direct" && typeof response.result === "object" && response.result !== null && "fileId" in response.result) {
|
|
2050
|
-
result = response.result;
|
|
2051
|
-
} else {
|
|
2052
|
-
throw new Error("Invalid response from relayer");
|
|
2053
|
-
}
|
|
2054
|
-
return {
|
|
2055
|
-
fileId: result.fileId,
|
|
2056
|
-
url: uploadResult.url,
|
|
2057
|
-
size: uploadResult.size,
|
|
2058
|
-
transactionHash: result.transactionHash
|
|
2059
|
-
};
|
|
2060
|
-
} else {
|
|
2061
|
-
const txResult = await this.addFileWithPermissions(
|
|
2062
|
-
uploadResult.url,
|
|
2063
|
-
userAddress,
|
|
2064
|
-
encryptedPermissions
|
|
2065
|
-
);
|
|
2066
|
-
if (!this.context.waitForTransactionEvents) {
|
|
2067
|
-
throw new Error(
|
|
2068
|
-
"Cannot upload without relay: waitForTransactionEvents not configured"
|
|
2069
|
-
);
|
|
2070
|
-
}
|
|
2071
|
-
const eventResult = await this.context.waitForTransactionEvents(txResult);
|
|
2072
|
-
const fileAddedEvent = eventResult.expectedEvents.FileAdded;
|
|
2073
|
-
if (!fileAddedEvent) {
|
|
2074
|
-
throw new Error("FileAdded event not found in transaction");
|
|
2075
|
-
}
|
|
2076
|
-
return {
|
|
2077
|
-
fileId: Number(fileAddedEvent.fileId),
|
|
2078
|
-
url: uploadResult.url,
|
|
2079
|
-
size: uploadResult.size,
|
|
2080
|
-
transactionHash: txResult.hash
|
|
2081
|
-
};
|
|
2082
|
-
}
|
|
2083
|
-
} catch (error) {
|
|
2084
|
-
console.error("Failed to upload file with permissions:", error);
|
|
2085
|
-
throw new Error(
|
|
2086
|
-
`Failed to upload file with permissions: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
2087
|
-
);
|
|
2088
|
-
}
|
|
2089
|
-
}
|
|
2090
|
-
/**
|
|
2091
|
-
* Uploads content to storage without registering it on the blockchain.
|
|
2092
|
-
* This method only handles the storage upload and returns the file URL.
|
|
2093
|
-
*
|
|
2094
|
-
* @param content - The content to upload (string, Blob, Buffer, or object - objects will be JSON stringified)
|
|
2095
|
-
* @param filename - Optional filename for the uploaded file (defaults to timestamp-based name)
|
|
2096
|
-
* @param encrypt - Optional flag to encrypt the content before upload
|
|
2097
|
-
* @param providerName - Optional specific storage provider to use
|
|
2098
|
-
* @returns Promise resolving to the storage upload result with url, size, and contentType
|
|
2099
|
-
*/
|
|
2100
|
-
async uploadToStorage(content, filename, encrypt = false, providerName) {
|
|
2101
|
-
try {
|
|
2102
|
-
let blob;
|
|
2103
|
-
if (content instanceof Blob) {
|
|
2104
|
-
blob = content;
|
|
2105
|
-
} else if (typeof content === "string") {
|
|
2106
|
-
blob = new Blob([content], { type: "text/plain" });
|
|
2107
|
-
} else if (content instanceof Buffer) {
|
|
2108
|
-
const arrayBuffer = content.buffer.slice(
|
|
2109
|
-
content.byteOffset,
|
|
2110
|
-
content.byteOffset + content.byteLength
|
|
2111
|
-
);
|
|
2112
|
-
blob = new Blob([arrayBuffer], { type: "application/octet-stream" });
|
|
2113
|
-
} else {
|
|
2114
|
-
blob = new Blob([JSON.stringify(content)], {
|
|
2115
|
-
type: "application/json"
|
|
2116
|
-
});
|
|
2117
|
-
}
|
|
2118
|
-
let finalBlob = blob;
|
|
2119
|
-
if (encrypt) {
|
|
2120
|
-
this.assertWallet();
|
|
2121
|
-
const encryptionKey = await generateEncryptionKey(
|
|
2122
|
-
this.context.walletClient,
|
|
2123
|
-
this.context.platform,
|
|
2124
|
-
DEFAULT_ENCRYPTION_SEED
|
|
2125
|
-
);
|
|
2126
|
-
finalBlob = await encryptBlobWithSignedKey(
|
|
2127
|
-
blob,
|
|
2128
|
-
encryptionKey,
|
|
2129
|
-
this.context.platform
|
|
2130
|
-
);
|
|
2131
|
-
}
|
|
2132
|
-
if (!this.context.storageManager) {
|
|
2133
|
-
if (this.context.validateStorageRequired) {
|
|
2134
|
-
this.context.validateStorageRequired();
|
|
2135
|
-
throw new Error("Storage validation failed");
|
|
2136
|
-
} else {
|
|
2137
|
-
throw new Error(
|
|
2138
|
-
"Storage manager not configured. Please provide storage providers in VanaConfig."
|
|
2139
|
-
);
|
|
2140
|
-
}
|
|
2141
|
-
}
|
|
2142
|
-
const finalFilename = (() => {
|
|
2143
|
-
if (filename) {
|
|
2144
|
-
if (encrypt && !filename.endsWith(".enc")) {
|
|
2145
|
-
return `${filename}.enc`;
|
|
2146
|
-
}
|
|
2147
|
-
return filename;
|
|
2148
|
-
}
|
|
2149
|
-
return encrypt ? `upload-${Date.now()}.enc` : `upload-${Date.now()}.dat`;
|
|
2150
|
-
})();
|
|
2151
|
-
const uploadResult = await this.context.storageManager.upload(
|
|
2152
|
-
finalBlob,
|
|
2153
|
-
finalFilename,
|
|
2154
|
-
providerName
|
|
2155
|
-
);
|
|
2156
|
-
return uploadResult;
|
|
2157
|
-
} catch (error) {
|
|
2158
|
-
throw new Error(
|
|
2159
|
-
`Upload failed: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
2160
|
-
);
|
|
2161
|
-
}
|
|
2162
|
-
}
|
|
2163
|
-
/**
|
|
2164
|
-
* Adds a permission for a party to access an existing file.
|
|
2165
|
-
*
|
|
2166
|
-
* This method handles the complete workflow:
|
|
2167
|
-
* 1. Gets the user's encryption key
|
|
2168
|
-
* 2. Encrypts the user's encryption key with the provided public key
|
|
2169
|
-
* 3. Adds the permission to the file
|
|
2170
|
-
* 4. Returns the permission data from the blockchain event
|
|
2171
|
-
*
|
|
2172
|
-
* For advanced users who need more control over transaction timing,
|
|
2173
|
-
* use `submitFilePermission()` instead.
|
|
2174
|
-
*
|
|
2175
|
-
* @param params - Parameters for adding file permission
|
|
2176
|
-
* @param params.fileId - The ID of the file to grant permission for
|
|
2177
|
-
* @param params.account - The recipient's wallet address that will access the file
|
|
2178
|
-
* @param params.publicKey - The recipient's public key for encryption.
|
|
2179
|
-
* Obtain via `vana.server.getIdentity(account).publicKey`
|
|
2180
|
-
* @returns Promise resolving to permission data from PermissionGranted event
|
|
2181
|
-
* @throws {Error} "No addresses available in wallet client" - When wallet is not connected
|
|
2182
|
-
* @throws {Error} "Chain ID not available" - When wallet chain is not configured
|
|
2183
|
-
* @throws {Error} "Failed to add permission to file: {error}" - When transaction fails or user doesn't own file
|
|
2184
|
-
* @example
|
|
2185
|
-
* ```typescript
|
|
2186
|
-
* const result = await vana.data.addPermissionToFile({
|
|
2187
|
-
* fileId: 123,
|
|
2188
|
-
* account: "0xRecipientAddress...",
|
|
2189
|
-
* publicKey: "0xRecipientPublicKey..."
|
|
2190
|
-
* });
|
|
2191
|
-
* console.log(`Permission granted to ${result.account} for file ${result.fileId}`);
|
|
2192
|
-
* console.log(`Transaction: ${result.transactionHash}`);
|
|
2193
|
-
* ```
|
|
2194
|
-
*/
|
|
2195
|
-
async addPermissionToFile(params, options) {
|
|
2196
|
-
this.assertWallet();
|
|
2197
|
-
const { fileId, account, publicKey } = params;
|
|
2198
|
-
return await this.submitFilePermission(fileId, account, publicKey, options);
|
|
2199
|
-
}
|
|
2200
|
-
/**
|
|
2201
|
-
* Submits a file permission transaction to the blockchain.
|
|
2202
|
-
*
|
|
2203
|
-
* @remarks
|
|
2204
|
-
* This method supports gasless transactions via relayer callbacks when configured.
|
|
2205
|
-
* It encrypts the user's encryption key with the recipient's public key before submission.
|
|
2206
|
-
* Use this when you want to handle transaction confirmation and event parsing separately.
|
|
2207
|
-
*
|
|
2208
|
-
* @param fileId - The ID of the file to grant permission for
|
|
2209
|
-
* @param account - The recipient's wallet address that will access the file
|
|
2210
|
-
* @param publicKey - The recipient's public key for encryption.
|
|
2211
|
-
* Obtain via `vana.server.getIdentity(account).publicKey`
|
|
2212
|
-
* @returns Promise resolving to TransactionResult for tracking the transaction
|
|
2213
|
-
* @throws {Error} When chain ID is not available
|
|
2214
|
-
* @throws {Error} When encryption key generation fails
|
|
2215
|
-
* @throws {Error} When public key encryption fails
|
|
2216
|
-
*
|
|
2217
|
-
* @example
|
|
2218
|
-
* ```typescript
|
|
2219
|
-
* const tx = await vana.data.submitFilePermission(
|
|
2220
|
-
* fileId,
|
|
2221
|
-
* "0x742d35Cc6558Fd4D9e9E0E888F0462ef6919Bd36",
|
|
2222
|
-
* recipientPublicKey
|
|
2223
|
-
* );
|
|
2224
|
-
* const result = await tx.waitForEvents();
|
|
2225
|
-
* console.log(`Permission granted with ID: ${result.permissionId}`);
|
|
2226
|
-
* ```
|
|
2227
|
-
*/
|
|
2228
|
-
async submitFilePermission(fileId, account, publicKey, options) {
|
|
2229
|
-
this.assertWallet();
|
|
2230
|
-
try {
|
|
2231
|
-
const userEncryptionKey = await generateEncryptionKey(
|
|
2232
|
-
this.context.walletClient,
|
|
2233
|
-
this.context.platform,
|
|
2234
|
-
DEFAULT_ENCRYPTION_SEED
|
|
2235
|
-
);
|
|
2236
|
-
const encryptedKey = await encryptWithWalletPublicKey(
|
|
2237
|
-
userEncryptionKey,
|
|
2238
|
-
publicKey,
|
|
2239
|
-
this.context.platform
|
|
2240
|
-
);
|
|
2241
|
-
const chainId = this.context.publicClient.chain?.id;
|
|
2242
|
-
if (!chainId) {
|
|
2243
|
-
throw new Error("Chain ID not available");
|
|
2244
|
-
}
|
|
2245
|
-
const dataRegistryAddress = getContractAddress(chainId, "DataRegistry");
|
|
2246
|
-
const dataRegistryAbi = getAbi("DataRegistry");
|
|
2247
|
-
this.assertWallet();
|
|
2248
|
-
const walletAccount = this.context.walletClient.account ?? this.context.userAddress;
|
|
2249
|
-
const txHash = await this.context.walletClient.writeContract({
|
|
2250
|
-
address: dataRegistryAddress,
|
|
2251
|
-
abi: dataRegistryAbi,
|
|
2252
|
-
functionName: "addFilePermission",
|
|
2253
|
-
args: [BigInt(fileId), account, encryptedKey],
|
|
2254
|
-
account: walletAccount,
|
|
2255
|
-
chain: this.context.walletClient.chain ?? null,
|
|
2256
|
-
...this.spreadTransactionOptions(options)
|
|
2257
|
-
});
|
|
2258
|
-
const { tx } = await import("../utils/transactionHelpers");
|
|
2259
|
-
return tx({
|
|
2260
|
-
hash: txHash,
|
|
2261
|
-
from: typeof walletAccount === "string" ? walletAccount : walletAccount.address,
|
|
2262
|
-
contract: "DataRegistry",
|
|
2263
|
-
fn: "addFilePermission"
|
|
2264
|
-
});
|
|
2265
|
-
} catch (error) {
|
|
2266
|
-
console.error("Failed to add permission to file:", error);
|
|
2267
|
-
throw new Error(
|
|
2268
|
-
`Failed to add permission to file: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
2269
|
-
);
|
|
2270
|
-
}
|
|
2271
|
-
}
|
|
2272
|
-
/**
|
|
2273
|
-
* Gets the encrypted key for a specific account's permission to access a file.
|
|
2274
|
-
*
|
|
2275
|
-
* @param fileId - The ID of the file
|
|
2276
|
-
* @param account - The account address to get the permission for
|
|
2277
|
-
* @returns Promise resolving to the encrypted key for that account
|
|
2278
|
-
*/
|
|
2279
|
-
async getFilePermission(fileId, account) {
|
|
2280
|
-
try {
|
|
2281
|
-
const chainId = this.context.publicClient.chain?.id;
|
|
2282
|
-
if (!chainId) {
|
|
2283
|
-
throw new Error("Chain ID not available");
|
|
2284
|
-
}
|
|
2285
|
-
const dataRegistryAddress = getContractAddress(chainId, "DataRegistry");
|
|
2286
|
-
const dataRegistryAbi = getAbi("DataRegistry");
|
|
2287
|
-
const dataRegistry = getContract({
|
|
2288
|
-
address: dataRegistryAddress,
|
|
2289
|
-
abi: dataRegistryAbi,
|
|
2290
|
-
client: this.context.publicClient
|
|
2291
|
-
});
|
|
2292
|
-
const encryptedKey = await dataRegistry.read.filePermissions([
|
|
2293
|
-
BigInt(fileId),
|
|
2294
|
-
account
|
|
2295
|
-
]);
|
|
2296
|
-
return encryptedKey;
|
|
2297
|
-
} catch (error) {
|
|
2298
|
-
console.error("Failed to get file permission:", error);
|
|
2299
|
-
throw new Error(
|
|
2300
|
-
`Failed to get file permission: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
2301
|
-
);
|
|
2302
|
-
}
|
|
2303
|
-
}
|
|
2304
|
-
/**
|
|
2305
|
-
* Decrypts a file that the user has permission to access using their private key.
|
|
2306
|
-
*
|
|
2307
|
-
* This method handles the complete workflow for servers or other permitted parties:
|
|
2308
|
-
* 1. Gets the encrypted encryption key from file permissions
|
|
2309
|
-
* 2. Decrypts the encryption key using the provided private key
|
|
2310
|
-
* 3. Downloads and decrypts the file data
|
|
2311
|
-
*
|
|
2312
|
-
* @param file - The file to decrypt
|
|
2313
|
-
* @param privateKey - The private key to decrypt the user's encryption key
|
|
2314
|
-
* @param options - Optional decryption configuration
|
|
2315
|
-
* @param options.account - The account address that has permission (defaults to current wallet account)
|
|
2316
|
-
* @returns Promise resolving to the decrypted file data
|
|
2317
|
-
*/
|
|
2318
|
-
async decryptFileWithPermission(file, privateKey, options) {
|
|
2319
|
-
try {
|
|
2320
|
-
const permissionAccount = options?.account ?? this.context.userAddress;
|
|
2321
|
-
const encryptedKey = await this.getFilePermission(
|
|
2322
|
-
file.id,
|
|
2323
|
-
permissionAccount
|
|
2324
|
-
);
|
|
2325
|
-
if (!encryptedKey) {
|
|
2326
|
-
throw new Error(
|
|
2327
|
-
`No permission found for account ${permissionAccount} to access file ${file.id}`
|
|
2328
|
-
);
|
|
2329
|
-
}
|
|
2330
|
-
const userEncryptionKey = await decryptWithWalletPrivateKey(
|
|
2331
|
-
encryptedKey,
|
|
2332
|
-
privateKey,
|
|
2333
|
-
this.context.platform
|
|
2334
|
-
);
|
|
2335
|
-
let encryptedData;
|
|
2336
|
-
if (file.url.startsWith("ipfs://")) {
|
|
2337
|
-
encryptedData = await this.fetchFromIPFS(file.url);
|
|
2338
|
-
} else {
|
|
2339
|
-
encryptedData = await this.fetch(file.url);
|
|
2340
|
-
}
|
|
2341
|
-
const decryptedData = await decryptBlobWithSignedKey(
|
|
2342
|
-
encryptedData,
|
|
2343
|
-
userEncryptionKey,
|
|
2344
|
-
this.context.platform
|
|
2345
|
-
);
|
|
2346
|
-
return decryptedData;
|
|
2347
|
-
} catch (error) {
|
|
2348
|
-
console.error("Failed to decrypt file with permission:", error);
|
|
2349
|
-
throw new Error(
|
|
2350
|
-
`Failed to decrypt file with permission: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
2351
|
-
);
|
|
2352
|
-
}
|
|
2353
|
-
}
|
|
2354
|
-
/**
|
|
2355
|
-
* Simple network-agnostic fetch utility for retrieving file content.
|
|
2356
|
-
*
|
|
2357
|
-
* @remarks
|
|
2358
|
-
* This is a thin wrapper around the global fetch API that returns the response as a Blob.
|
|
2359
|
-
* It provides a consistent interface for fetching encrypted content before decryption.
|
|
2360
|
-
* For IPFS URLs, consider using fetchFromIPFS for better reliability.
|
|
2361
|
-
*
|
|
2362
|
-
* @param url - The URL to fetch content from
|
|
2363
|
-
* @returns Promise resolving to the fetched content as a Blob
|
|
2364
|
-
* @throws {Error} "HTTP error! status: {status} {statusText}" - When server returns error status
|
|
2365
|
-
* @throws {Error} "Empty response" - When server returns no content
|
|
2366
|
-
* @throws {Error} "Network error: Failed to fetch from {url}" - When network request fails
|
|
2367
|
-
*
|
|
2368
|
-
* @example
|
|
2369
|
-
* ```typescript
|
|
2370
|
-
* // Fetch and decrypt a file
|
|
2371
|
-
* const encryptionKey = await generateEncryptionKey(walletClient);
|
|
2372
|
-
* const encryptedBlob = await vana.data.fetch(file.url);
|
|
2373
|
-
* const decryptedBlob = await decryptBlob(encryptedBlob, encryptionKey, platform);
|
|
2374
|
-
*
|
|
2375
|
-
* // With custom headers for authentication
|
|
2376
|
-
* const response = await fetch(file.url, {
|
|
2377
|
-
* headers: { 'Authorization': 'Bearer token' }
|
|
2378
|
-
* });
|
|
2379
|
-
* const encryptedBlob = await response.blob();
|
|
2380
|
-
* ```
|
|
2381
|
-
*/
|
|
2382
|
-
async fetch(url) {
|
|
2383
|
-
try {
|
|
2384
|
-
const { universalFetch } = await import("../utils/download");
|
|
2385
|
-
const response = await universalFetch(url, this.context.downloadRelayer);
|
|
2386
|
-
if (!response.ok) {
|
|
2387
|
-
throw new Error(
|
|
2388
|
-
`HTTP error! status: ${response.status} ${response.statusText}`
|
|
2389
|
-
);
|
|
2390
|
-
}
|
|
2391
|
-
const blob = await response.blob();
|
|
2392
|
-
if (blob.size === 0) {
|
|
2393
|
-
throw new Error("Empty response");
|
|
2394
|
-
}
|
|
2395
|
-
return blob;
|
|
2396
|
-
} catch (error) {
|
|
2397
|
-
if (error instanceof TypeError && error.message.includes("fetch")) {
|
|
2398
|
-
throw new Error(
|
|
2399
|
-
`Network error: Failed to fetch from ${url}. The URL may be invalid or the server may not be accessible.`
|
|
2400
|
-
);
|
|
2401
|
-
}
|
|
2402
|
-
throw error;
|
|
2403
|
-
}
|
|
2404
|
-
}
|
|
2405
|
-
/**
|
|
2406
|
-
* Specialized IPFS fetcher with gateway fallback mechanism.
|
|
2407
|
-
*
|
|
2408
|
-
* @remarks
|
|
2409
|
-
* This method provides robust IPFS content fetching by trying multiple gateways
|
|
2410
|
-
* in sequence until one succeeds. It supports both ipfs:// URLs and raw CIDs.
|
|
2411
|
-
*
|
|
2412
|
-
* The default gateway list includes public gateways, but you should provide
|
|
2413
|
-
* your own gateways for production use to ensure reliability and privacy.
|
|
2414
|
-
*
|
|
2415
|
-
* @param url - The IPFS URL (ipfs://...) or CID to fetch
|
|
2416
|
-
* @param options - Optional configuration
|
|
2417
|
-
* @param options.gateways - Array of IPFS gateway URLs to try (must end with /)
|
|
2418
|
-
* @returns Promise resolving to the fetched content as a Blob
|
|
2419
|
-
* @throws {Error} "Invalid IPFS URL format" - When URL is not ipfs:// or valid CID
|
|
2420
|
-
* @throws {Error} "Empty response" - When gateway returns no content
|
|
2421
|
-
* @throws {Error} "HTTP error! status: {status}" - When gateway returns error status
|
|
2422
|
-
* @throws {Error} "Failed to fetch IPFS content {cid} from all gateways" - When all gateways fail
|
|
2423
|
-
*
|
|
2424
|
-
* @example
|
|
2425
|
-
* ```typescript
|
|
2426
|
-
* // Fetch from IPFS with custom gateways
|
|
2427
|
-
* const encryptedBlob = await vana.data.fetchFromIPFS(file.url, {
|
|
2428
|
-
* gateways: [
|
|
2429
|
-
* 'https://my-private-gateway.com/ipfs/',
|
|
2430
|
-
* 'https://dweb.link/ipfs/',
|
|
2431
|
-
* 'https://ipfs.io/ipfs/'
|
|
2432
|
-
* ]
|
|
2433
|
-
* });
|
|
2434
|
-
*
|
|
2435
|
-
* // Decrypt the fetched content
|
|
2436
|
-
* const encryptionKey = await generateEncryptionKey(walletClient);
|
|
2437
|
-
* const decryptedBlob = await decryptBlob(encryptedBlob, encryptionKey, platform);
|
|
2438
|
-
*
|
|
2439
|
-
* // With raw CID
|
|
2440
|
-
* const blob = await vana.data.fetchFromIPFS('QmXxx...', {
|
|
2441
|
-
* gateways: ['https://ipfs.io/ipfs/']
|
|
2442
|
-
* });
|
|
2443
|
-
* ```
|
|
2444
|
-
*/
|
|
2445
|
-
async fetchFromIPFS(url, options) {
|
|
2446
|
-
const defaultGateways = [
|
|
2447
|
-
"https://dweb.link/ipfs/",
|
|
2448
|
-
"https://ipfs.io/ipfs/"
|
|
2449
|
-
];
|
|
2450
|
-
const gateways = options?.gateways ?? this.context.ipfsGateways ?? defaultGateways;
|
|
2451
|
-
const { extractIpfsHash } = await import("../utils/ipfs");
|
|
2452
|
-
const cid = extractIpfsHash(url);
|
|
2453
|
-
if (!cid) {
|
|
2454
|
-
throw new Error(
|
|
2455
|
-
`Invalid IPFS URL format. Expected ipfs://... or a raw CID, got: ${url}`
|
|
2456
|
-
);
|
|
2457
|
-
}
|
|
2458
|
-
const errors = [];
|
|
2459
|
-
for (let i = 0; i < gateways.length; i++) {
|
|
2460
|
-
const gateway = gateways[i];
|
|
2461
|
-
const isLastGateway = i === gateways.length - 1;
|
|
2462
|
-
const gatewayUrl = gateway.endsWith("/") ? `${gateway}${cid}` : `${gateway}/${cid}`;
|
|
2463
|
-
try {
|
|
2464
|
-
console.debug(`Trying IPFS gateway: ${gatewayUrl}`);
|
|
2465
|
-
const response = await fetch(gatewayUrl);
|
|
2466
|
-
if (response.ok) {
|
|
2467
|
-
const blob = await response.blob();
|
|
2468
|
-
if (blob.size > 0) {
|
|
2469
|
-
console.debug(`Successfully fetched from gateway: ${gateway}`);
|
|
2470
|
-
return blob;
|
|
2471
|
-
} else {
|
|
2472
|
-
if (isLastGateway) {
|
|
2473
|
-
throw new Error("Empty response");
|
|
2474
|
-
}
|
|
2475
|
-
errors.push({
|
|
2476
|
-
gateway,
|
|
2477
|
-
error: "Empty response"
|
|
2478
|
-
});
|
|
2479
|
-
}
|
|
2480
|
-
} else {
|
|
2481
|
-
if (isLastGateway) {
|
|
2482
|
-
if (response.status === 403) {
|
|
2483
|
-
throw new Error(`HTTP error! status: 403 ${response.statusText}`);
|
|
2484
|
-
} else if (response.status === 404) {
|
|
2485
|
-
throw new Error(`HTTP error! status: 404 ${response.statusText}`);
|
|
2486
|
-
} else {
|
|
2487
|
-
throw new Error(
|
|
2488
|
-
`HTTP error! status: ${response.status} ${response.statusText}`
|
|
2489
|
-
);
|
|
2490
|
-
}
|
|
2491
|
-
}
|
|
2492
|
-
errors.push({
|
|
2493
|
-
gateway,
|
|
2494
|
-
error: `HTTP ${response.status} ${response.statusText}`
|
|
2495
|
-
});
|
|
2496
|
-
}
|
|
2497
|
-
} catch (error) {
|
|
2498
|
-
if (isLastGateway && error instanceof Error && (error.message.includes("Empty response") || error.message.includes("HTTP error!"))) {
|
|
2499
|
-
throw error;
|
|
2500
|
-
}
|
|
2501
|
-
errors.push({
|
|
2502
|
-
gateway,
|
|
2503
|
-
error: error instanceof Error ? error.message : "Unknown error"
|
|
2504
|
-
});
|
|
2505
|
-
}
|
|
2506
|
-
}
|
|
2507
|
-
if (this.context.downloadRelayer && gateways.length > 0) {
|
|
2508
|
-
try {
|
|
2509
|
-
const relayerUrl = gateways[0].endsWith("/") ? `${gateways[0]}${cid}` : `${gateways[0]}/${cid}`;
|
|
2510
|
-
return await this.context.downloadRelayer.proxyDownload(relayerUrl);
|
|
2511
|
-
} catch (relayerError) {
|
|
2512
|
-
errors.push({
|
|
2513
|
-
gateway: "download-relayer",
|
|
2514
|
-
error: `Proxy failed: ${relayerError instanceof Error ? relayerError.message : "Unknown error"}`
|
|
2515
|
-
});
|
|
2516
|
-
}
|
|
2517
|
-
}
|
|
2518
|
-
const errorDetails = errors.map((e) => `${e.gateway}: ${e.error}`).join("\n ");
|
|
2519
|
-
throw new Error(
|
|
2520
|
-
`Failed to fetch IPFS content ${cid} from all gateways:
|
|
2521
|
-
${errorDetails}`
|
|
2522
|
-
);
|
|
2523
|
-
}
|
|
2524
|
-
/**
|
|
2525
|
-
* Validates a data schema definition against the Vana meta-schema.
|
|
2526
|
-
*
|
|
2527
|
-
* @param schema - The data schema definition to validate
|
|
2528
|
-
* @returns The validated DataSchema
|
|
2529
|
-
* @throws SchemaValidationError if invalid
|
|
2530
|
-
* @example
|
|
2531
|
-
* ```typescript
|
|
2532
|
-
* const schema = {
|
|
2533
|
-
* name: "User Profile",
|
|
2534
|
-
* version: "1.0.0",
|
|
2535
|
-
* dialect: "json",
|
|
2536
|
-
* schema: {
|
|
2537
|
-
* type: "object",
|
|
2538
|
-
* properties: {
|
|
2539
|
-
* name: { type: "string" },
|
|
2540
|
-
* age: { type: "number" }
|
|
2541
|
-
* }
|
|
2542
|
-
* }
|
|
2543
|
-
* };
|
|
2544
|
-
*
|
|
2545
|
-
* const validatedSchema = vana.data.validateDataSchemaAgainstMetaSchema(schema);
|
|
2546
|
-
* ```
|
|
2547
|
-
*/
|
|
2548
|
-
validateDataSchemaAgainstMetaSchema(schema) {
|
|
2549
|
-
return validateDataSchemaAgainstMetaSchema(schema);
|
|
2550
|
-
}
|
|
2551
|
-
/**
|
|
2552
|
-
* Validates data against a JSON Schema from a data schema.
|
|
2553
|
-
*
|
|
2554
|
-
* @param data - The data to validate
|
|
2555
|
-
* @param schema - The data schema containing the schema
|
|
2556
|
-
* @returns Void (throws if validation fails)
|
|
2557
|
-
* @throws SchemaValidationError if invalid
|
|
2558
|
-
* @example
|
|
2559
|
-
* ```typescript
|
|
2560
|
-
* const schema = {
|
|
2561
|
-
* name: "User Profile",
|
|
2562
|
-
* version: "1.0.0",
|
|
2563
|
-
* dialect: "json",
|
|
2564
|
-
* schema: {
|
|
2565
|
-
* type: "object",
|
|
2566
|
-
* properties: {
|
|
2567
|
-
* name: { type: "string" },
|
|
2568
|
-
* age: { type: "number" }
|
|
2569
|
-
* },
|
|
2570
|
-
* required: ["name"]
|
|
2571
|
-
* }
|
|
2572
|
-
* };
|
|
2573
|
-
*
|
|
2574
|
-
* const userData = { name: "Alice", age: 30 };
|
|
2575
|
-
* vana.data.validateDataAgainstSchema(userData, schema);
|
|
2576
|
-
* ```
|
|
2577
|
-
*/
|
|
2578
|
-
validateDataAgainstSchema(data, schema) {
|
|
2579
|
-
validateDataAgainstSchema(data, schema);
|
|
2580
|
-
}
|
|
2581
|
-
/**
|
|
2582
|
-
* Fetches and validates a data schema from a URL, then returns the parsed data schema.
|
|
2583
|
-
*
|
|
2584
|
-
* @param url - The URL to fetch the data schema from
|
|
2585
|
-
* @returns The validated data schema
|
|
2586
|
-
* @throws SchemaValidationError if invalid or fetch fails
|
|
2587
|
-
* @example
|
|
2588
|
-
* ```typescript
|
|
2589
|
-
* // Fetch and validate a schema from IPFS or HTTP
|
|
2590
|
-
* const schema = await vana.data.fetchAndValidateSchema("https://example.com/schema.json");
|
|
2591
|
-
* console.log(schema.name, schema.dialect);
|
|
2592
|
-
*
|
|
2593
|
-
* // Use the schema to validate user data
|
|
2594
|
-
* if (schema.dialect === "json") {
|
|
2595
|
-
* vana.data.validateDataAgainstSchema(userData, schema);
|
|
2596
|
-
* }
|
|
2597
|
-
* ```
|
|
2598
|
-
*/
|
|
2599
|
-
async fetchAndValidateSchema(url) {
|
|
2600
|
-
return fetchAndValidateSchema(url);
|
|
2601
|
-
}
|
|
2602
|
-
/**
|
|
2603
|
-
* Polls for confirmation of a relayer operation.
|
|
2604
|
-
* @internal
|
|
2605
|
-
*/
|
|
2606
|
-
async pollRelayerForConfirmation(operationId, options) {
|
|
2607
|
-
if (!this.context.relayer) {
|
|
2608
|
-
throw new Error("Relayer not configured for polling");
|
|
2609
|
-
}
|
|
2610
|
-
const pollingManager = new PollingManager(this.context.relayer);
|
|
2611
|
-
const result = await pollingManager.startPolling(operationId, {
|
|
2612
|
-
signal: options?.signal,
|
|
2613
|
-
onStatusUpdate: options?.onStatusUpdate,
|
|
2614
|
-
...options?.pollingOptions
|
|
2615
|
-
});
|
|
2616
|
-
return {
|
|
2617
|
-
fileId: 0,
|
|
2618
|
-
// This would need to be extracted from transaction events
|
|
2619
|
-
transactionHash: result.hash
|
|
2620
|
-
};
|
|
2621
|
-
}
|
|
2622
|
-
}
|
|
2623
|
-
export {
|
|
2624
|
-
DataController
|
|
2625
|
-
};
|
|
2626
|
-
//# sourceMappingURL=data.js.map
|