@oceanprotocol/lib 6.1.2 → 7.0.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,180 @@
1
+ import { type Libp2pOptions } from 'libp2p';
2
+ import { Signer } from 'ethers';
3
+ import { StorageObject, FileInfo, ComputeJob, ComputeOutput, ComputeAlgorithm, ComputeAsset, ComputeEnvironment, ProviderInitialize, ProviderComputeInitializeResults, UserCustomParameters, ComputeResourceRequest, ComputeJobMetadata, PolicyServerInitializeCommand, PolicyServerPassthroughCommand, dockerRegistryAuth, DownloadResponse, ComputeResultStream } from '../../@types/index.js';
4
+ import { type DDO, type ValidateMetadata } from '@oceanprotocol/ddo-js';
5
+ export declare const OCEAN_P2P_PROTOCOL = "/ocean/nodes/1.0.0";
6
+ export interface P2PConfig {
7
+ /**
8
+ * Bootstrap peer multiaddrs for DHT peer discovery.
9
+ * Required when dialing bare peer IDs; defaults to Ocean Protocol's
10
+ * public bootstrap nodes. Ignored if `libp2p.peerDiscovery` is set.
11
+ */
12
+ bootstrapPeers?: string[];
13
+ /** Timeout per dial + stream operation in ms. Default: 10000 */
14
+ dialTimeout?: number;
15
+ /** Max retry attempts on connection errors. Default: 5 */
16
+ maxRetries?: number;
17
+ /** Base delay between retries in ms. Default: 1000 */
18
+ retryDelay?: number;
19
+ /**
20
+ * Timeout for DHT peer lookup when dialing a bare peer ID, in ms. Default: 60000.
21
+ * Intentionally separate from dialTimeout — DHT resolution needs more time than
22
+ * a direct dial. Once a peer is found and connected, subsequent calls skip this.
23
+ */
24
+ dhtLookupTimeout?: number;
25
+ /**
26
+ * mDNS discovery interval in ms. Set to 0 to disable. Default: 20000
27
+ * Useful for local development — discovers peers on the same LAN without bootstrap nodes.
28
+ */
29
+ mDNSInterval?: number;
30
+ /**
31
+ * Enable TCP transport in addition to WebSockets. Default: false.
32
+ * Required in Node.js/Electron environments to reach nodes over plain TCP.
33
+ * Do NOT enable in browser builds — TCP is not available in browsers.
34
+ */
35
+ enableTcp?: boolean;
36
+ /**
37
+ * Full libp2p node configuration. Fields provided here override ocean.js
38
+ * defaults (transports, encrypters, services, connectionManager, etc.).
39
+ * Unset fields keep ocean.js defaults.
40
+ */
41
+ libp2p?: Partial<Libp2pOptions>;
42
+ }
43
+ export declare class P2pProvider {
44
+ private p2pConfig;
45
+ private libp2pNode;
46
+ private discoveredNodes;
47
+ /**
48
+ * Configure the internal libp2p node used for P2P transport.
49
+ * Call this once before making P2P requests, e.g.:
50
+ * ProviderInstance.setupP2P({ bootstrapPeers: ['/ip4/1.2.3.4/tcp/9000/ws/p2p/16Uiu2...'] })
51
+ *
52
+ * Required when using bare peer IDs as nodeUri — the bootstrap peers
53
+ * provide DHT entry points so the peer can be located.
54
+ */
55
+ setupP2P(config: P2PConfig): Promise<void>;
56
+ /** Returns all peers discovered via mDNS or DHT bootstrap. */
57
+ getDiscoveredNodes(): Array<{
58
+ peerId: string;
59
+ multiaddrs: string[];
60
+ }>;
61
+ private bufToHex;
62
+ private getOrCreateLibp2pNode;
63
+ private toUint8Array;
64
+ private getConnection;
65
+ protected getConsumerAddress(s: Signer | string): Promise<string>;
66
+ protected getSignature(s: Signer | string, nonce: string, command: string): Promise<string>;
67
+ private getNodePublicKey;
68
+ protected getAuthorization(s: Signer | string): string;
69
+ private dialAndStream;
70
+ private sendP2pCommand;
71
+ /**
72
+ * Returns node status via P2P STATUS command.
73
+ * @param {string} nodeUri - multiaddr of the node
74
+ */
75
+ getEndpoints(nodeUri: string): Promise<any>;
76
+ /**
77
+ * Get current nonce from the node via P2P.
78
+ */
79
+ getNonce(nodeUri: string, consumerAddress: string, signal?: AbortSignal): Promise<number>;
80
+ /**
81
+ * Encrypt data via P2P ENCRYPT command.
82
+ */
83
+ encrypt(data: any, chainId: number, nodeUri: string, signerOrAuthToken: Signer | string, _policyServer?: any, signal?: AbortSignal): Promise<string>;
84
+ /**
85
+ * Get file details for a given DID and service ID via P2P.
86
+ */
87
+ checkDidFiles(did: string, serviceId: string, nodeUri: string, withChecksum?: boolean, signal?: AbortSignal): Promise<FileInfo[]>;
88
+ /**
89
+ * Get File details via P2P.
90
+ */
91
+ getFileInfo(file: StorageObject, nodeUri: string, withChecksum?: boolean, signal?: AbortSignal): Promise<FileInfo[]>;
92
+ /**
93
+ * Returns compute environments via P2P.
94
+ */
95
+ getComputeEnvironments(nodeUri: string, signal?: AbortSignal): Promise<ComputeEnvironment[]>;
96
+ /**
97
+ * Initializes the provider for a service (download) request via P2P.
98
+ */
99
+ initialize(did: string, serviceId: string, fileIndex: number, consumerAddress: string, nodeUri: string, signal?: AbortSignal, userCustomParameters?: UserCustomParameters, computeEnv?: string, validUntil?: number): Promise<ProviderInitialize>;
100
+ /**
101
+ * Initializes compute request via P2P.
102
+ */
103
+ initializeCompute(assets: ComputeAsset[], algorithm: ComputeAlgorithm, computeEnv: string, token: string, validUntil: number, nodeUri: string, signerOrAuthToken: Signer | string, resources: ComputeResourceRequest[], chainId: number, policyServer?: any, signal?: AbortSignal, output?: ComputeOutput, dockerRegistryAuth?: dockerRegistryAuth): Promise<ProviderComputeInitializeResults>;
104
+ /**
105
+ * Sends a DOWNLOAD command to the peer via P2P, reads the binary stream
106
+ * directly from the lpStream, and returns a DownloadResponse.
107
+ * The node decrypts the service file and streams raw file data back.
108
+ */
109
+ getDownloadUrl(did: string, serviceId: string, fileIndex: number, transferTxId: string, nodeUri: string, signerOrAuthToken: Signer | string, policyServer?: any, userCustomParameters?: UserCustomParameters): Promise<DownloadResponse>;
110
+ /**
111
+ * Start a paid compute job via P2P.
112
+ */
113
+ computeStart(nodeUri: string, signerOrAuthToken: Signer | string, computeEnv: string, datasets: ComputeAsset[], algorithm: ComputeAlgorithm, maxJobDuration: number, token: string, resources: ComputeResourceRequest[], chainId: number, metadata?: ComputeJobMetadata, additionalViewers?: string[], output?: ComputeOutput, policyServer?: any, signal?: AbortSignal, queueMaxWaitTime?: number, dockerRegistryAuth?: dockerRegistryAuth): Promise<ComputeJob | ComputeJob[]>;
114
+ /**
115
+ * Start a free compute job via P2P.
116
+ */
117
+ freeComputeStart(nodeUri: string, signerOrAuthToken: Signer | string, computeEnv: string, datasets: ComputeAsset[], algorithm: ComputeAlgorithm, resources?: ComputeResourceRequest[], metadata?: ComputeJobMetadata, additionalViewers?: string[], output?: ComputeOutput, policyServer?: any, signal?: AbortSignal, queueMaxWaitTime?: number, dockerRegistryAuth?: dockerRegistryAuth): Promise<ComputeJob | ComputeJob[]>;
118
+ /**
119
+ * Get streamable compute logs via P2P. Returns an async generator of Uint8Array chunks.
120
+ */
121
+ computeStreamableLogs(nodeUri: string, signerOrAuthToken: Signer | string, jobId: string, signal?: AbortSignal): Promise<any>;
122
+ /**
123
+ * Stop a compute job via P2P.
124
+ */
125
+ computeStop(jobId: string, nodeUri: string, signerOrAuthToken: Signer | string, agreementId?: string, signal?: AbortSignal): Promise<ComputeJob | ComputeJob[]>;
126
+ /**
127
+ * Get compute status via P2P.
128
+ */
129
+ computeStatus(nodeUri: string, signerOrAuthToken: Signer | string, jobId?: string, agreementId?: string, signal?: AbortSignal): Promise<ComputeJob | ComputeJob[]>;
130
+ /**
131
+ * Get compute result as an async generator of Uint8Array chunks via P2P.
132
+ * Supports resumable downloads via `offset` (byte position to resume from).
133
+ */
134
+ getComputeResult(nodeUri: string, signerOrAuthToken: Signer | string, jobId: string, index: number, offset?: number): Promise<ComputeResultStream>;
135
+ getComputeResultUrl(nodeUri: string, signerOrAuthToken: Signer | string, jobId: string, index: number): Promise<string>;
136
+ /**
137
+ * Generate an auth token via P2P.
138
+ */
139
+ generateAuthToken(consumer: Signer, nodeUri: string, signal?: AbortSignal): Promise<string>;
140
+ /**
141
+ * Resolve a DDO by DID via P2P GET_DDO command.
142
+ */
143
+ resolveDdo(nodeUri: string, did: string, signal?: AbortSignal): Promise<any>;
144
+ /**
145
+ * Validate a DDO via P2P VALIDATE_DDO command.
146
+ */
147
+ validateDdo(nodeUri: string, ddo: DDO, signer: Signer, signal?: AbortSignal): Promise<ValidateMetadata>;
148
+ /**
149
+ * Invalidate an auth token via P2P.
150
+ */
151
+ invalidateAuthToken(consumer: Signer, token: string, nodeUri: string, signal?: AbortSignal): Promise<{
152
+ success: boolean;
153
+ }>;
154
+ /**
155
+ * Check if a P2P node is reachable by calling STATUS.
156
+ */
157
+ isValidProvider(nodeUri: string, signal?: AbortSignal): Promise<boolean>;
158
+ /**
159
+ * PolicyServer passthrough via P2P.
160
+ */
161
+ PolicyServerPassthrough(nodeUri: string, request: PolicyServerPassthroughCommand, signal?: AbortSignal): Promise<any>;
162
+ /**
163
+ * Initialize Policy Server verification via P2P.
164
+ */
165
+ initializePSVerification(nodeUri: string, request: PolicyServerInitializeCommand, signal?: AbortSignal): Promise<any>;
166
+ /**
167
+ * Download node logs via P2P.
168
+ */
169
+ downloadNodeLogs(nodeUri: string, signer: Signer, startTime: string, endTime: string, maxLogs?: number, moduleName?: string, level?: string, page?: number, signal?: AbortSignal): Promise<any>;
170
+ /**
171
+ * Fetch node configuration via P2P. Accepts a pre-signed payload —
172
+ * the caller is responsible for nonce retrieval and signing.
173
+ */
174
+ fetchConfig(nodeUri: string, payload: Record<string, any>): Promise<any>;
175
+ /**
176
+ * Push node configuration via P2P. Accepts a pre-signed payload —
177
+ * the caller is responsible for nonce retrieval and signing.
178
+ */
179
+ pushConfig(nodeUri: string, payload: Record<string, any>): Promise<any>;
180
+ }
@@ -6,9 +6,10 @@ import { DownloadResponse } from '../@types/index.js';
6
6
  */
7
7
  export declare function downloadFileBrowser(url: string): void;
8
8
  /**
9
- * Triggers a file download from the specified URL when called from a browser context.
10
- * @param {string} url - The URL of the file to download
11
- * @param {number} [index] - The file index
12
- * @returns {Promise<void>} - A Promise that resolves when the file has been downloaded
9
+ * Downloads a file from a URL, or passes through an already-collected DownloadResponse
10
+ * (returned by P2P transport).
11
+ * @param {string | DownloadResponse} urlOrData - HTTP URL or pre-collected P2P response
12
+ * @param {number} [index] - The file index, used as fallback filename for URL downloads
13
+ * @returns {Promise<DownloadResponse>}
13
14
  */
14
- export declare function downloadFile(url: string, index?: number): Promise<DownloadResponse>;
15
+ export declare function downloadFile(urlOrData: string | DownloadResponse, index?: number): Promise<DownloadResponse>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@oceanprotocol/lib",
3
3
  "source": "./src/index.ts",
4
- "version": "6.1.2",
4
+ "version": "7.0.0-next.0",
5
5
  "description": "JavaScript client library for Ocean Protocol",
6
6
  "main": "./dist/lib.cjs",
7
7
  "umd:main": "dist/lib.umd.js",
@@ -56,6 +56,14 @@
56
56
  "web3": "^1.8.0"
57
57
  },
58
58
  "dependencies": {
59
+ "@chainsafe/libp2p-noise": "^17.0.0",
60
+ "@chainsafe/libp2p-yamux": "^8.0.1",
61
+ "@libp2p/bootstrap": "^12.0.14",
62
+ "@libp2p/identify": "^4.0.13",
63
+ "@libp2p/kad-dht": "^16.1.6",
64
+ "@libp2p/tcp": "^11.0.13",
65
+ "@libp2p/websockets": "^10.1.6",
66
+ "@multiformats/multiaddr": "^13.0.1",
59
67
  "@oasisprotocol/sapphire-paratime": "^1.3.2",
60
68
  "@oceanprotocol/contracts": "^2.6.0",
61
69
  "@oceanprotocol/ddo-js": "^0.1.4",
@@ -69,7 +77,9 @@
69
77
  "eciesjs": "^0.4.5",
70
78
  "ethers": "^6.15.0",
71
79
  "form-data": "^2.3.3",
72
- "jsonwebtoken": "^9.0.2"
80
+ "jsonwebtoken": "^9.0.2",
81
+ "libp2p": "^3.1.6",
82
+ "uint8arrays": "^4.0.10"
73
83
  },
74
84
  "devDependencies": {
75
85
  "@istanbuljs/nyc-config-typescript": "^1.0.2",