1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

Merge pull request #1120 from oceanprotocol/feature/v4-provider-class

Helper class for provider interaction
This commit is contained in:
Bogdan Fazakas 2021-12-07 21:03:49 +02:00 committed by GitHub
commit b5dcdd5150
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 805 additions and 66 deletions

429
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,26 @@
export interface FileMetadata {
/**
* File format, if applicable.
* @type {string}
* @example "text/csv"
*/
contentType: string
/**
* File content length.
* @type {[type]}
*/
contentLength?: string
/**
* File index.
* @type {number}
*/
index?: number
/**
* File URL.
* @type {string}
*/
url?: string
}

295
src/provider/Provider.ts Normal file
View File

@ -0,0 +1,295 @@
import Web3 from 'web3'
import { Config } from '../models'
import { LoggerInstance } from '../utils'
import { Asset } from '../ddo/Asset'
import { FileMetadata } from '../interfaces/FileMetadata'
import { noZeroX } from '../utils/ConversionTypeHelper'
import { signText } from '../utils/SignatureUtils'
export interface ServiceEndpoint {
serviceName: string
method: string
urlPath: string
}
export interface UserCustomParameters {
[key: string]: any
}
export class Provider {
/**
* Returns the provider endpoints
* @param {any} fetchMethod
* @return {Promise<ServiceEndpoint[]>}
*/
async getEndpoints(providerUri: string, fetchMethod: any): Promise<any> {
try {
const endpoints = await await fetchMethod(providerUri).json()
return endpoints
} catch (e) {
LoggerInstance.error('Finding the service endpoints failed:', e)
return null
}
}
getEndpointURL(
servicesEndpoints: ServiceEndpoint[],
serviceName: string
): ServiceEndpoint {
if (!servicesEndpoints) return null
return servicesEndpoints.find((s) => s.serviceName === serviceName) as ServiceEndpoint
}
/**
* Returns the service endpoints that exist in provider.
* @param {any} endpoints
* @return {Promise<ServiceEndpoint[]>}
*/
public async getServiceEndpoints(providerEndpoint: string, endpoints: any) {
const serviceEndpoints: ServiceEndpoint[] = []
for (const i in endpoints.serviceEndpoints) {
const endpoint: ServiceEndpoint = {
serviceName: i,
method: endpoints.serviceEndpoints[i][0],
urlPath: providerEndpoint + endpoints.serviceEndpoints[i][1]
}
serviceEndpoints.push(endpoint)
}
return serviceEndpoints
}
/** Encrypt DDO using the Provider's own symmetric key
* @param {string} providerUri provider uri address
* @param {string} consumerAddress Publisher address
* @param {string} fetchMethod fetch client instance
* @param {string} providerEndpoints Identifier of the asset to be registered in ocean
* @param {string} serviceEndpoints document description object (DDO)=
* @return {Promise<string>} urlDetails
*/
public async getNonce(
providerUri: string,
consumerAddress: string,
fetchMethod: any,
providerEndpoints?: any,
serviceEndpoints?: ServiceEndpoint[]
): Promise<string> {
if (!providerEndpoints) {
providerEndpoints = await this.getEndpoints(providerUri, fetchMethod)
}
if (!serviceEndpoints) {
serviceEndpoints = await this.getServiceEndpoints(providerUri, providerEndpoints)
}
const path = this.getEndpointURL(serviceEndpoints, 'nonce')
? this.getEndpointURL(serviceEndpoints, 'nonce').urlPath
: null
if (!path) return null
try {
const response = await fetchMethod(path + `?userAddress=${consumerAddress}`)
return String((await response.json()).nonce)
} catch (e) {
LoggerInstance.error(e)
throw new Error('HTTP request failed')
}
}
/** Encrypt DDO using the Provider's own symmetric key
* @param {string} did Identifier of the asset to be registered in ocean
* @param {string} accountId Publisher address
* @param {string} document document description object (DDO)
* @param {string} providerUri provider uri address
* @param {string} fetchMethod fetch client instance
* @return {Promise<string>} urlDetails
*/
public async encrypt(
did: string,
accountId: string,
document: any,
providerUri: string,
fetchMethod: any
): Promise<string> {
const providerEndpoints = await this.getEndpoints(providerUri, fetchMethod)
const serviceEndpoints = await this.getServiceEndpoints(
providerUri,
providerEndpoints
)
const args = {
documentId: did,
document: JSON.stringify(document),
publisherAddress: accountId
}
const path = this.getEndpointURL(serviceEndpoints, 'encrypt')
? this.getEndpointURL(serviceEndpoints, 'encrypt').urlPath
: null
if (!path) return null
try {
const response = await fetchMethod(path, decodeURI(JSON.stringify(args)))
return (await response.json()).encryptedDocument
} catch (e) {
LoggerInstance.error(e)
throw new Error('HTTP request failed')
}
}
/** Get URL details (if possible)
* @param {string | DID} url or did
* @param {string} providerUri Identifier of the asset to be registered in ocean
* @param {string} fetchMethod fetch client instance
* @return {Promise<FileMetadata[]>} urlDetails
*/
public async fileinfo(
url: string,
providerUri: string,
fetchMethod: any
): Promise<FileMetadata[]> {
const providerEndpoints = await this.getEndpoints(providerUri, fetchMethod)
const serviceEndpoints = await this.getServiceEndpoints(
providerUri,
providerEndpoints
)
const args = { url }
const files: FileMetadata[] = []
const path = this.getEndpointURL(serviceEndpoints, 'fileinfo')
? this.getEndpointURL(serviceEndpoints, 'fileinfo').urlPath
: null
if (!path) return null
try {
const response = await fetchMethod(path, JSON.stringify(args))
const results: FileMetadata[] = await response.json()
for (const result of results) {
files.push(result)
}
return files
} catch (e) {
return null
}
}
/** Initialize a service request.
* @param {DDO | string} asset
* @param {number} serviceIndex
* @param {string} serviceType
* @param {string} consumerAddress
* @param {UserCustomParameters} userCustomParameters
* @param {string} providerUri Identifier of the asset to be registered in ocean
* @param {string} fetchMethod fetch client instance
* @return {Promise<FileMetadata[]>} urlDetails
*/
public async initialize(
asset: Asset,
serviceIndex: number,
serviceType: string,
consumerAddress: string,
providerUri: string,
fetchMethod: any,
userCustomParameters?: UserCustomParameters
): Promise<string> {
const providerEndpoints = await this.getEndpoints(providerUri, fetchMethod)
const serviceEndpoints = await this.getServiceEndpoints(
providerUri,
providerEndpoints
)
let initializeUrl = this.getEndpointURL(serviceEndpoints, 'initialize')
? this.getEndpointURL(serviceEndpoints, 'initialize').urlPath
: null
if (!initializeUrl) return null
initializeUrl += `?documentId=${asset.id}`
initializeUrl += `&serviceId=${serviceIndex}`
initializeUrl += `&serviceType=${serviceType}`
initializeUrl += `&dataToken=${asset.datatokens[0]}` // to check later
initializeUrl += `&consumerAddress=${consumerAddress}`
if (userCustomParameters)
initializeUrl += '&userdata=' + encodeURI(JSON.stringify(userCustomParameters))
try {
const response = await fetchMethod(initializeUrl)
return await response.text()
} catch (e) {
LoggerInstance.error(e)
throw new Error('Asset URL not found or not available.')
}
}
public async download(
did: string,
destination: string,
accountId: string,
files: FileMetadata[],
index = -1,
providerUri: string,
web3: Web3,
fetchMethod: any,
userCustomParameters?: UserCustomParameters
): Promise<any> {
const providerEndpoints = await this.getEndpoints(providerUri, fetchMethod)
const serviceEndpoints = await this.getServiceEndpoints(
providerUri,
providerEndpoints
)
const downloadUrl = this.getEndpointURL(serviceEndpoints, 'download')
? this.getEndpointURL(serviceEndpoints, 'download').urlPath
: null
const nonce = await this.getNonce(
providerUri,
accountId,
fetchMethod,
providerEndpoints,
serviceEndpoints
)
const signature = await this.createSignature(web3, accountId, did + nonce)
if (!downloadUrl) return null
const filesPromises = files
.filter((_, i) => index === -1 || i === index)
.map(async ({ index: i, url: fileUrl }) => {
let consumeUrl = downloadUrl
consumeUrl += `?index=${i}`
consumeUrl += `&documentId=${did}`
consumeUrl += `&consumerAddress=${accountId}`
consumeUrl += `&url=${fileUrl}`
consumeUrl += `&signature=${signature}`
if (userCustomParameters)
consumeUrl += '&userdata=' + encodeURI(JSON.stringify(userCustomParameters))
try {
!destination
? await fetchMethod.downloadFileBrowser(consumeUrl)
: await fetchMethod.downloadFile(consumeUrl, destination, i)
} catch (e) {
LoggerInstance.error('Error consuming assets', e)
throw e
}
})
await Promise.all(filesPromises)
return destination
}
public async createSignature(
web3: Web3,
accountId: string,
agreementId: string
): Promise<string> {
const signature = await signText(web3, noZeroX(agreementId), accountId)
return signature
}
/** Check for a valid provider at URL
* @param {String} url provider uri address
* @param {String} fetchMethod fetch client instance
* @return {Promise<boolean>} string
*/
public async isValidProvider(url: string, fetchMethod: any): Promise<boolean> {
try {
const response = await fetchMethod(url)
if (response?.ok) {
const params = await response.json()
if (params && params.providerAddress) return true
}
return false
} catch (error) {
LoggerInstance.error(`Error validating provider: ${error.message}`)
return false
}
}
}
export const ProviderInstance = new Provider()
export default ProviderInstance

1
src/provider/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './Provider'

View File

@ -0,0 +1,27 @@
import { LoggerInstance } from './Logger'
export const zeroX = (input: string): string => zeroXTransformer(input, true)
export const noZeroX = (input: string): string => zeroXTransformer(input, false)
export function zeroXTransformer(input = '', zeroOutput: boolean): string {
const { valid, output } = inputMatch(input, /^(?:0x)*([a-f0-9]+)$/i, 'zeroXTransformer')
return (zeroOutput && valid ? '0x' : '') + output
}
// Shared functions
function inputMatch(
input: string,
regexp: RegExp,
conversorName: string
): { valid: boolean; output: string } {
if (typeof input !== 'string') {
LoggerInstance.debug('Not input string:')
LoggerInstance.debug(input)
throw new Error(`[${conversorName}] Expected string, input type: ${typeof input}`)
}
const match = input.match(regexp)
if (!match) {
LoggerInstance.warn(`[${conversorName}] Input transformation failed.`)
return { valid: false, output: input }
}
return { valid: true, output: match[1] }
}

12
src/utils/FetchHelper.ts Normal file
View File

@ -0,0 +1,12 @@
import fetch from 'cross-fetch'
import LoggerInstance from './Logger'
export async function fetchData(url: string, opts: RequestInit): Promise<Response> {
const result = await fetch(url, opts)
if (!result.ok) {
LoggerInstance.error(`Error requesting [${opts.method}] ${url}`)
LoggerInstance.error(`Response message: \n${await result.text()}`)
throw result
}
return result
}

View File

@ -0,0 +1,28 @@
import Web3 from 'web3'
import { LoggerInstance } from './Logger'
export async function signText(
web3: Web3,
text: string,
publicKey: string,
password?: string
): Promise<string> {
const isMetaMask =
web3 && web3.currentProvider && (web3.currentProvider as any).isMetaMask
try {
return await web3.eth.personal.sign(text, publicKey, password)
} catch (e) {
if (isMetaMask) {
throw e
}
LoggerInstance.warn('Error on personal sign.')
LoggerInstance.warn(e)
try {
return await web3.eth.sign(text, publicKey)
} catch (e2) {
LoggerInstance.error('Error on sign.')
LoggerInstance.error(e2)
throw new Error('Error executing personal sign')
}
}
}

View File

@ -3,3 +3,4 @@ export * from './GasUtils'
export * from './Logger'
export * from './DatatokenName'
export * from './ContractParams'
export * from './FetchHelper'

View File

@ -0,0 +1,6 @@
{
"require": ["ts-node/register", "source-map-support/register", "mock-local-storage"],
"full-trace": true,
"exit": true,
"timeout": "300000"
}

View File

@ -0,0 +1,25 @@
import config from './config'
import { Provider } from '../../src/provider/Provider'
import { assert } from 'chai'
import { fetchData } from '../../src/utils'
describe('Provider tests', () => {
let providerInstance: Provider
it('Initialize Ocean', async () => {
providerInstance = new Provider()
})
it('Alice tests invalid provider', async () => {
const valid = await providerInstance.isValidProvider('http://example.net', fetchData)
assert(valid === false)
})
it('Alice tests valid provider', async () => {
const valid = await providerInstance.isValidProvider(
'http://127.0.0.1:8030',
fetchData
)
assert(valid === true)
})
})

View File

@ -1,15 +1,12 @@
import { Config } from '../../src/models/Config'
import { LoggerInstance, LogLevel } from '../../src/utils'
import Web3 from 'web3'
LoggerInstance.setLevel(LogLevel.Error)
const web3 = new Web3('http://127.0.0.1:8545')
export default {
metadataCacheUri: 'http://aquarius:5000',
providerUri: 'http://localhost:8030',
nodeUri: `http://localhost:${process.env.ETH_PORT || 8545}`,
verbose: LogLevel.Error,
web3Provider: web3
} as Config

View File

View File

@ -12,17 +12,17 @@ import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedR
import OPFCollector from '@oceanprotocol/contracts/artifacts/contracts/communityFee/OPFCommunityFeeCollector.sol/OPFCommunityFeeCollector.json'
import MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/MockERC20Decimals.sol/MockERC20Decimals.json'
import { TestContractHandler } from '../TestContractHandler'
import { NFTFactory, NFTCreateData } from '../../src/factories/NFTFactory'
import { TestContractHandler } from '../../TestContractHandler'
import { NFTFactory, NFTCreateData } from '../../../src/factories/NFTFactory'
import {
Datatoken,
NFTDatatoken,
OrderParams,
DispenserParams
} from '../../src/datatokens'
} from '../../../src/datatokens'
import { AbiItem } from 'web3-utils'
import { LoggerInstance } from '../../src/utils'
import { FreCreationParams, FreOrderParams } from '../../src/interfaces'
import { LoggerInstance } from '../../../src/utils'
import { FreCreationParams, FreOrderParams } from '../../../src/interfaces'
const web3 = new Web3('http://127.0.0.1:8545')

View File

@ -12,11 +12,11 @@ import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedR
import OPFCollector from '@oceanprotocol/contracts/artifacts/contracts/communityFee/OPFCommunityFeeCollector.sol/OPFCommunityFeeCollector.json'
import MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/MockERC20Decimals.sol/MockERC20Decimals.json'
import { TestContractHandler } from '../TestContractHandler'
import { NFTFactory, NFTCreateData } from '../../src/factories/NFTFactory'
import { NFTDatatoken } from '../../src/datatokens/NFTDatatoken'
import { TestContractHandler } from '../../TestContractHandler'
import { NFTFactory, NFTCreateData } from '../../../src/factories/NFTFactory'
import { NFTDatatoken } from '../../../src/datatokens/NFTDatatoken'
import { AbiItem } from 'web3-utils'
import { LoggerInstance } from '../../src/utils'
import { LoggerInstance } from '../../../src/utils'
const web3 = new Web3('http://127.0.0.1:8545')