11import { OceanP2P } from './components/P2P/index.js'
22import { OceanProvider } from './components/Provider/index.js'
33import { OceanIndexer } from './components/Indexer/index.js'
4- import { OceanNodeConfig , P2PCommandResponse } from './@types/OceanNode.js'
4+ import {
5+ AccessListContract ,
6+ OceanNodeConfig ,
7+ P2PCommandResponse
8+ } from './@types/OceanNode.js'
9+ import { ValidateChainId } from './@types/commands.js'
10+
511import { Database } from './components/database/index.js'
612import { Escrow } from './components/core/utils/escrow.js'
713import { CoreHandlersRegistry } from './components/core/handler/coreHandlersRegistry.js'
@@ -15,6 +21,8 @@ import { BlockchainRegistry } from './components/BlockchainRegistry/index.js'
1521import { Blockchain } from './utils/blockchain.js'
1622import { createPersistentStorage } from './components/persistentStorage/createPersistentStorage.js'
1723import { PersistentStorageFactory } from './components/persistentStorage/PersistentStorageFactory.js'
24+ import { isAddress , FallbackProvider , ethers } from 'ethers'
25+ import { create256Hash } from './utils/crypt.js'
1826
1927export interface RequestLimiter {
2028 requester : string | string [ ] // IP address or peer ID
@@ -40,6 +48,7 @@ export class OceanNode {
4048 private requestMap : Map < string , RequestLimiter >
4149 private auth : Auth
4250 private persistentStorage : PersistentStorageFactory
51+ private database : Database
4352
4453 // eslint-disable-next-line no-useless-constructor
4554 private constructor (
@@ -64,8 +73,9 @@ export class OceanNode {
6473 this . coreHandlers = CoreHandlersRegistry . getInstance ( this )
6574 this . requestMap = new Map < string , RequestLimiter > ( )
6675 this . config = config
76+ this . database = db
6777 if ( this . db && this . db ?. authToken ) {
68- this . auth = new Auth ( this . db . authToken )
78+ this . auth = new Auth ( this . db . authToken , config )
6979 }
7080 if ( node ) {
7181 node . setCoreHandlers ( this . coreHandlers )
@@ -161,10 +171,6 @@ export class OceanNode {
161171 return this . indexer
162172 }
163173
164- public getDatabase ( ) : Database {
165- return this . db
166- }
167-
168174 public getC2DEngines ( ) : C2DEngines {
169175 return this . c2dEngines
170176 }
@@ -259,4 +265,81 @@ export class OceanNode {
259265 }
260266 }
261267 }
268+
269+ getAdminAddresses ( ) : { addresses : string [ ] ; accessLists : any } {
270+ const ret = {
271+ addresses : [ ] as string [ ] ,
272+ accessLists : undefined as AccessListContract | undefined
273+ }
274+
275+ if ( this . config . allowedAdmins && this . config . allowedAdmins . length > 0 ) {
276+ for ( const admin of this . config . allowedAdmins ) {
277+ if ( isAddress ( admin ) === true ) {
278+ ret . addresses . push ( admin )
279+ }
280+ }
281+ }
282+ ret . accessLists = this . config . allowedAdminsList
283+ return ret
284+ }
285+
286+ checkSupportedChainId ( chainId : number ) : ValidateChainId {
287+ if ( ! chainId || ! ( `${ chainId . toString ( ) } ` in this . config . supportedNetworks ) ) {
288+ OCEAN_NODE_LOGGER . error ( `Chain ID ${ chainId } is not supported` )
289+ return {
290+ validation : false ,
291+ networkRpc : ''
292+ }
293+ }
294+ return {
295+ validation : true ,
296+ networkRpc : this . config . supportedNetworks [ chainId . toString ( ) ] . rpc
297+ }
298+ }
299+
300+ async getJsonRpcProvider ( chainId : number ) : Promise < FallbackProvider > {
301+ const checkResult = this . checkSupportedChainId ( chainId )
302+ if ( ! checkResult . validation ) {
303+ return null
304+ }
305+ const blockchain = this . getBlockchain ( chainId )
306+ if ( ! blockchain ) return null
307+ return await blockchain . getProvider ( )
308+ }
309+
310+ hasP2PInterface ( ) {
311+ return this . config . hasP2P || false
312+ }
313+
314+ async getDatabase ( forceReload : boolean = false ) : Promise < Database > {
315+ if ( ! this . database || forceReload ) {
316+ const { dbConfig } = this . config
317+ if ( dbConfig && dbConfig . url ) {
318+ this . database = await Database . init ( dbConfig )
319+ }
320+ }
321+ return this . database
322+ }
323+
324+ async getValidationSignature ( ddo : string ) : Promise < any > {
325+ try {
326+ const hashedDDO = create256Hash ( ddo )
327+ const providerWallet = await this . keyManager . getEthWallet ( )
328+ const messageHash = ethers . solidityPackedKeccak256 (
329+ [ 'bytes' ] ,
330+ [ ethers . hexlify ( ethers . toUtf8Bytes ( hashedDDO ) ) ]
331+ )
332+ const signed32Bytes = await providerWallet . signMessage (
333+ new Uint8Array ( ethers . toBeArray ( messageHash ) )
334+ )
335+ const signatureSplitted = ethers . Signature . from ( signed32Bytes )
336+ const v = signatureSplitted . v <= 1 ? signatureSplitted . v + 27 : signatureSplitted . v
337+ const r = ethers . hexlify ( signatureSplitted . r ) // 32 bytes
338+ const s = ethers . hexlify ( signatureSplitted . s )
339+ return { hash : hashedDDO , publicKey : providerWallet . address , r, s, v }
340+ } catch ( error ) {
341+ OCEAN_NODE_LOGGER . logMessage ( `Validation signature error: ${ error } ` , true )
342+ return { hash : '' , publicKey : '' , r : '' , s : '' , v : '' }
343+ }
344+ }
262345}
0 commit comments