mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
remove optional fetch method, add abort signal (#1235)
* remove optional fetch method, add abort signal * fix encrypt * fix encrypt * fix lint,tests * test * fix adr * test * fix asset * t * t
This commit is contained in:
parent
b19ada4eaf
commit
b880bee007
@ -1,7 +1,6 @@
|
|||||||
import { LoggerInstance, crossFetchGeneric } from '../utils'
|
import { LoggerInstance } from '../utils'
|
||||||
import { Asset, DDO, Metadata, ValidateMetadata } from '../@types/'
|
import { Asset, DDO, ValidateMetadata } from '../@types/'
|
||||||
import { json } from 'stream/consumers'
|
import fetch from 'cross-fetch'
|
||||||
|
|
||||||
export class Aquarius {
|
export class Aquarius {
|
||||||
public aquariusURL
|
public aquariusURL
|
||||||
/**
|
/**
|
||||||
@ -14,19 +13,23 @@ export class Aquarius {
|
|||||||
|
|
||||||
/** Resolves a DID
|
/** Resolves a DID
|
||||||
* @param {string} did
|
* @param {string} did
|
||||||
* @param {string} fetchMethod fetch client instance
|
* @param {AbortSignal} signal abort signal
|
||||||
* @return {Promise<DDO>} DDO
|
* @return {Promise<Asset>} Asset
|
||||||
*/
|
*/
|
||||||
public async resolve(did: string, fetchMethod?: any): Promise<DDO> {
|
public async resolve(did: string, signal?: AbortSignal): Promise<Asset> {
|
||||||
const preferedFetch = fetchMethod || crossFetchGeneric
|
|
||||||
const path = this.aquariusURL + '/api/aquarius/assets/ddo/' + did
|
const path = this.aquariusURL + '/api/aquarius/assets/ddo/' + did
|
||||||
try {
|
try {
|
||||||
const response = await preferedFetch('GET', path, null, {
|
const response = await fetch(path, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
signal: signal
|
||||||
})
|
})
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const raw = response.data ? response.data : await response.json()
|
const raw = await response.json()
|
||||||
return raw as DDO
|
return raw as Asset
|
||||||
} else {
|
} else {
|
||||||
throw new Error('HTTP request failed with status ' + response.status)
|
throw new Error('HTTP request failed with status ' + response.status)
|
||||||
}
|
}
|
||||||
@ -50,24 +53,27 @@ export class Aquarius {
|
|||||||
|
|
||||||
* @param {string} did DID of the asset.
|
* @param {string} did DID of the asset.
|
||||||
* @param {string} txid used when the did exists and we expect an update with that txid.
|
* @param {string} txid used when the did exists and we expect an update with that txid.
|
||||||
* @param {string} fetchMethod fetch client instance
|
* @param {AbortSignal} signal abort signal
|
||||||
* @return {Promise<DDO>} DDO of the asset.
|
* @return {Promise<DDO>} DDO of the asset.
|
||||||
*/
|
*/
|
||||||
public async waitForAqua(
|
public async waitForAqua(
|
||||||
did: string,
|
did: string,
|
||||||
txid?: string,
|
txid?: string,
|
||||||
fetchMethod?: any
|
signal?: AbortSignal
|
||||||
): Promise<Asset> {
|
): Promise<Asset> {
|
||||||
let tries = 0
|
let tries = 0
|
||||||
do {
|
do {
|
||||||
try {
|
try {
|
||||||
const preferedFetch = fetchMethod || crossFetchGeneric
|
|
||||||
const path = this.aquariusURL + '/api/aquarius/assets/ddo/' + did
|
const path = this.aquariusURL + '/api/aquarius/assets/ddo/' + did
|
||||||
const response = await preferedFetch('GET', path, null, {
|
const response = await fetch(path, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
signal: signal
|
||||||
})
|
})
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const ddo = response.data ? response.data : await response.json()
|
const ddo = await response.json()
|
||||||
if (txid) {
|
if (txid) {
|
||||||
// check tx
|
// check tx
|
||||||
if (ddo.event && ddo.event.txid === txid) return ddo as Asset
|
if (ddo.event && ddo.event.txid === txid) return ddo as Asset
|
||||||
@ -85,21 +91,27 @@ export class Aquarius {
|
|||||||
/**
|
/**
|
||||||
* Validate DDO content
|
* Validate DDO content
|
||||||
* @param {DDO} ddo DID Descriptor Object content.
|
* @param {DDO} ddo DID Descriptor Object content.
|
||||||
* @param {string} fetchMethod fetch client instance
|
* @param {AbortSignal} signal abort signal
|
||||||
* @return {Promise<ValidateMetadata>}.
|
* @return {Promise<ValidateMetadata>}.
|
||||||
*/
|
*/
|
||||||
public async validate(ddo: DDO, fetchMethod: any): Promise<ValidateMetadata> {
|
public async validate(ddo: DDO, signal?: AbortSignal): Promise<ValidateMetadata> {
|
||||||
const preferedFetch = fetchMethod || crossFetchGeneric
|
|
||||||
const status: ValidateMetadata = {
|
const status: ValidateMetadata = {
|
||||||
valid: false
|
valid: false
|
||||||
}
|
}
|
||||||
let jsonResponse
|
let jsonResponse
|
||||||
try {
|
try {
|
||||||
const path = this.aquariusURL + '/api/aquarius/assets/ddo/validate'
|
const path = this.aquariusURL + '/api/aquarius/assets/ddo/validate'
|
||||||
const response = await preferedFetch('POST', path, JSON.stringify(ddo), {
|
|
||||||
|
const response = await fetch(path, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify(ddo),
|
||||||
|
headers: {
|
||||||
'Content-Type': 'application/octet-stream'
|
'Content-Type': 'application/octet-stream'
|
||||||
|
},
|
||||||
|
signal: signal
|
||||||
})
|
})
|
||||||
jsonResponse = response.data ? response.data : await response.json()
|
|
||||||
|
jsonResponse = await response.json()
|
||||||
if (response.status === 200) {
|
if (response.status === 200) {
|
||||||
status.valid = true
|
status.valid = true
|
||||||
status.hash = jsonResponse.hash
|
status.hash = jsonResponse.hash
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import Web3 from 'web3'
|
import Web3 from 'web3'
|
||||||
import { LoggerInstance, getData, crossFetchGeneric } from '../utils'
|
import { LoggerInstance, getData, downloadFile, downloadFileBrowser } from '../utils'
|
||||||
import {
|
import {
|
||||||
FileMetadata,
|
FileMetadata,
|
||||||
ComputeJob,
|
ComputeJob,
|
||||||
@ -10,6 +10,10 @@ import {
|
|||||||
} from '../@types/'
|
} from '../@types/'
|
||||||
import { noZeroX } from '../utils/ConversionTypeHelper'
|
import { noZeroX } from '../utils/ConversionTypeHelper'
|
||||||
import { signText, signWithHash } from '../utils/SignatureUtils'
|
import { signText, signWithHash } from '../utils/SignatureUtils'
|
||||||
|
import fetch from 'cross-fetch'
|
||||||
|
export interface HttpCallback {
|
||||||
|
(httpMethod: string, url: string, body: string, header: any): Promise<any>
|
||||||
|
}
|
||||||
|
|
||||||
export interface ServiceEndpoint {
|
export interface ServiceEndpoint {
|
||||||
serviceName: string
|
serviceName: string
|
||||||
@ -23,7 +27,6 @@ export interface UserCustomParameters {
|
|||||||
export class Provider {
|
export class Provider {
|
||||||
/**
|
/**
|
||||||
* Returns the provider endpoints
|
* Returns the provider endpoints
|
||||||
* @param {any} fetchMethod
|
|
||||||
* @return {Promise<ServiceEndpoint[]>}
|
* @return {Promise<ServiceEndpoint[]>}
|
||||||
*/
|
*/
|
||||||
async getEndpoints(providerUri: string): Promise<any> {
|
async getEndpoints(providerUri: string): Promise<any> {
|
||||||
@ -65,7 +68,7 @@ export class Provider {
|
|||||||
/** Encrypt DDO using the Provider's own symmetric key
|
/** Encrypt DDO using the Provider's own symmetric key
|
||||||
* @param {string} providerUri provider uri address
|
* @param {string} providerUri provider uri address
|
||||||
* @param {string} consumerAddress Publisher address
|
* @param {string} consumerAddress Publisher address
|
||||||
* @param {string} fetchMethod fetch client instance
|
* @param {AbortSignal} signal abort signal
|
||||||
* @param {string} providerEndpoints Identifier of the asset to be registered in ocean
|
* @param {string} providerEndpoints Identifier of the asset to be registered in ocean
|
||||||
* @param {string} serviceEndpoints document description object (DDO)=
|
* @param {string} serviceEndpoints document description object (DDO)=
|
||||||
* @return {Promise<string>} urlDetails
|
* @return {Promise<string>} urlDetails
|
||||||
@ -73,11 +76,10 @@ export class Provider {
|
|||||||
public async getNonce(
|
public async getNonce(
|
||||||
providerUri: string,
|
providerUri: string,
|
||||||
consumerAddress: string,
|
consumerAddress: string,
|
||||||
fetchMethod?: any,
|
signal?: AbortSignal,
|
||||||
providerEndpoints?: any,
|
providerEndpoints?: any,
|
||||||
serviceEndpoints?: ServiceEndpoint[]
|
serviceEndpoints?: ServiceEndpoint[]
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const preferedFetch = fetchMethod || crossFetchGeneric
|
|
||||||
if (!providerEndpoints) {
|
if (!providerEndpoints) {
|
||||||
providerEndpoints = await this.getEndpoints(providerUri)
|
providerEndpoints = await this.getEndpoints(providerUri)
|
||||||
}
|
}
|
||||||
@ -89,14 +91,13 @@ export class Provider {
|
|||||||
: null
|
: null
|
||||||
if (!path) return null
|
if (!path) return null
|
||||||
try {
|
try {
|
||||||
const response = await preferedFetch(
|
const response = await fetch(path + `?userAddress=${consumerAddress}`, {
|
||||||
'GET',
|
method: 'GET',
|
||||||
path + `?userAddress=${consumerAddress}`,
|
headers: {
|
||||||
null,
|
|
||||||
{
|
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
}
|
},
|
||||||
)
|
signal: signal
|
||||||
|
})
|
||||||
return String((await response.json()).nonce)
|
return String((await response.json()).nonce)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
LoggerInstance.error(e)
|
LoggerInstance.error(e)
|
||||||
@ -125,11 +126,14 @@ export class Provider {
|
|||||||
/** Encrypt data using the Provider's own symmetric key
|
/** Encrypt data using the Provider's own symmetric key
|
||||||
* @param {string} data data in json format that needs to be sent , it can either be a DDO or a File array
|
* @param {string} data data in json format that needs to be sent , it can either be a DDO or a File array
|
||||||
* @param {string} providerUri provider uri address
|
* @param {string} providerUri provider uri address
|
||||||
* @param {string} postMethod http post method
|
* @param {AbortSignal} signal abort signal
|
||||||
* @return {Promise<string>} urlDetails
|
* @return {Promise<string>} urlDetails
|
||||||
*/
|
*/
|
||||||
public async encrypt(data: any, providerUri: string, postMethod?: any): Promise<any> {
|
public async encrypt(
|
||||||
const preferedFetch = postMethod || crossFetchGeneric
|
data: any,
|
||||||
|
providerUri: string,
|
||||||
|
signal?: AbortSignal
|
||||||
|
): Promise<string> {
|
||||||
const providerEndpoints = await this.getEndpoints(providerUri)
|
const providerEndpoints = await this.getEndpoints(providerUri)
|
||||||
const serviceEndpoints = await this.getServiceEndpoints(
|
const serviceEndpoints = await this.getServiceEndpoints(
|
||||||
providerUri,
|
providerUri,
|
||||||
@ -141,15 +145,15 @@ export class Provider {
|
|||||||
|
|
||||||
if (!path) return null
|
if (!path) return null
|
||||||
try {
|
try {
|
||||||
const response = await preferedFetch(
|
const response = await fetch(path, {
|
||||||
'POST',
|
method: 'POST',
|
||||||
path,
|
body: decodeURI(JSON.stringify(data)),
|
||||||
decodeURI(JSON.stringify(data)),
|
headers: {
|
||||||
{
|
|
||||||
'Content-Type': 'application/octet-stream'
|
'Content-Type': 'application/octet-stream'
|
||||||
}
|
},
|
||||||
)
|
signal: signal
|
||||||
return response
|
})
|
||||||
|
return await response.text()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
LoggerInstance.error(e)
|
LoggerInstance.error(e)
|
||||||
throw new Error('HTTP request failed')
|
throw new Error('HTTP request failed')
|
||||||
@ -160,16 +164,15 @@ export class Provider {
|
|||||||
* @param {string} did did
|
* @param {string} did did
|
||||||
* @param {number} serviceId the id of the service for which to check the files
|
* @param {number} serviceId the id of the service for which to check the files
|
||||||
* @param {string} providerUri uri of the provider that will be used to check the file
|
* @param {string} providerUri uri of the provider that will be used to check the file
|
||||||
* @param {string} fetchMethod fetch client instance
|
* @param {AbortSignal} signal abort signal
|
||||||
* @return {Promise<FileMetadata[]>} urlDetails
|
* @return {Promise<FileMetadata[]>} urlDetails
|
||||||
*/
|
*/
|
||||||
public async checkDidFiles(
|
public async checkDidFiles(
|
||||||
did: string,
|
did: string,
|
||||||
serviceId: number,
|
serviceId: number,
|
||||||
providerUri: string,
|
providerUri: string,
|
||||||
fetchMethod?: any
|
signal?: AbortSignal
|
||||||
): Promise<FileMetadata[]> {
|
): Promise<FileMetadata[]> {
|
||||||
const preferedFetch = fetchMethod || crossFetchGeneric
|
|
||||||
const providerEndpoints = await this.getEndpoints(providerUri)
|
const providerEndpoints = await this.getEndpoints(providerUri)
|
||||||
const serviceEndpoints = await this.getServiceEndpoints(
|
const serviceEndpoints = await this.getServiceEndpoints(
|
||||||
providerUri,
|
providerUri,
|
||||||
@ -182,12 +185,15 @@ export class Provider {
|
|||||||
: null
|
: null
|
||||||
if (!path) return null
|
if (!path) return null
|
||||||
try {
|
try {
|
||||||
const response = await preferedFetch('POST', path, JSON.stringify(args), {
|
const response = await fetch(path, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify(args),
|
||||||
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
signal: signal
|
||||||
})
|
})
|
||||||
const results: FileMetadata[] = response.data
|
const results: FileMetadata[] = await response.json()
|
||||||
? response.data
|
|
||||||
: await response.json()
|
|
||||||
for (const result of results) {
|
for (const result of results) {
|
||||||
files.push(result)
|
files.push(result)
|
||||||
}
|
}
|
||||||
@ -200,15 +206,14 @@ export class Provider {
|
|||||||
/** Get URL details (if possible)
|
/** Get URL details (if possible)
|
||||||
* @param {string} url or did
|
* @param {string} url or did
|
||||||
* @param {string} providerUri uri of the provider that will be used to check the file
|
* @param {string} providerUri uri of the provider that will be used to check the file
|
||||||
* @param {string} fetchMethod fetch client instance
|
* @param {AbortSignal} signal abort signal
|
||||||
* @return {Promise<FileMetadata[]>} urlDetails
|
* @return {Promise<FileMetadata[]>} urlDetails
|
||||||
*/
|
*/
|
||||||
public async checkFileUrl(
|
public async checkFileUrl(
|
||||||
url: string,
|
url: string,
|
||||||
providerUri: string,
|
providerUri: string,
|
||||||
fetchMethod?: any
|
signal?: AbortSignal
|
||||||
): Promise<FileMetadata[]> {
|
): Promise<FileMetadata[]> {
|
||||||
const preferedFetch = fetchMethod || crossFetchGeneric
|
|
||||||
const providerEndpoints = await this.getEndpoints(providerUri)
|
const providerEndpoints = await this.getEndpoints(providerUri)
|
||||||
const serviceEndpoints = await this.getServiceEndpoints(
|
const serviceEndpoints = await this.getServiceEndpoints(
|
||||||
providerUri,
|
providerUri,
|
||||||
@ -221,12 +226,15 @@ export class Provider {
|
|||||||
: null
|
: null
|
||||||
if (!path) return null
|
if (!path) return null
|
||||||
try {
|
try {
|
||||||
const response = await preferedFetch('POST', path, JSON.stringify(args), {
|
const response = await fetch(path, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify(args),
|
||||||
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
signal: signal
|
||||||
})
|
})
|
||||||
const results: FileMetadata[] = response.data
|
const results: FileMetadata[] = await response.json()
|
||||||
? response.data
|
|
||||||
: await response.json()
|
|
||||||
for (const result of results) {
|
for (const result of results) {
|
||||||
files.push(result)
|
files.push(result)
|
||||||
}
|
}
|
||||||
@ -243,7 +251,7 @@ export class Provider {
|
|||||||
* @param {string} consumerAddress
|
* @param {string} consumerAddress
|
||||||
* @param {UserCustomParameters} userCustomParameters
|
* @param {UserCustomParameters} userCustomParameters
|
||||||
* @param {string} providerUri Identifier of the asset to be registered in ocean
|
* @param {string} providerUri Identifier of the asset to be registered in ocean
|
||||||
* @param {string} fetchMethod fetch client instance
|
* @param {AbortSignal} signal abort signal
|
||||||
* @return {Promise<ProviderInitialize>} ProviderInitialize data
|
* @return {Promise<ProviderInitialize>} ProviderInitialize data
|
||||||
*/
|
*/
|
||||||
public async initialize(
|
public async initialize(
|
||||||
@ -252,12 +260,11 @@ export class Provider {
|
|||||||
fileIndex: number,
|
fileIndex: number,
|
||||||
consumerAddress: string,
|
consumerAddress: string,
|
||||||
providerUri: string,
|
providerUri: string,
|
||||||
fetchMethod?: any,
|
signal?: AbortSignal,
|
||||||
userCustomParameters?: UserCustomParameters,
|
userCustomParameters?: UserCustomParameters,
|
||||||
computeEnv?: string,
|
computeEnv?: string,
|
||||||
validUntil?: number
|
validUntil?: number
|
||||||
): Promise<ProviderInitialize> {
|
): Promise<ProviderInitialize> {
|
||||||
const preferedFetch = fetchMethod || crossFetchGeneric
|
|
||||||
const providerEndpoints = await this.getEndpoints(providerUri)
|
const providerEndpoints = await this.getEndpoints(providerUri)
|
||||||
const serviceEndpoints = await this.getServiceEndpoints(
|
const serviceEndpoints = await this.getServiceEndpoints(
|
||||||
providerUri,
|
providerUri,
|
||||||
@ -277,12 +284,14 @@ export class Provider {
|
|||||||
if (computeEnv) initializeUrl += '&computeEnv=' + encodeURI(computeEnv)
|
if (computeEnv) initializeUrl += '&computeEnv=' + encodeURI(computeEnv)
|
||||||
if (validUntil) initializeUrl += '&validUntil=' + validUntil
|
if (validUntil) initializeUrl += '&validUntil=' + validUntil
|
||||||
try {
|
try {
|
||||||
const response = await preferedFetch('GET', initializeUrl, null, {
|
const response = await fetch(initializeUrl, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
signal: signal
|
||||||
})
|
})
|
||||||
const results: ProviderInitialize = response.data
|
const results: ProviderInitialize = await response.json()
|
||||||
? response.data
|
|
||||||
: await response.json()
|
|
||||||
return results
|
return results
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
LoggerInstance.error(e)
|
LoggerInstance.error(e)
|
||||||
@ -297,7 +306,6 @@ export class Provider {
|
|||||||
* @param {number} fileIndex
|
* @param {number} fileIndex
|
||||||
* @param {string} providerUri
|
* @param {string} providerUri
|
||||||
* @param {Web3} web3
|
* @param {Web3} web3
|
||||||
* @param {any} fetchMethod
|
|
||||||
* @param {UserCustomParameters} userCustomParameters
|
* @param {UserCustomParameters} userCustomParameters
|
||||||
* @return {Promise<string>}
|
* @return {Promise<string>}
|
||||||
*/
|
*/
|
||||||
@ -342,7 +350,7 @@ export class Provider {
|
|||||||
* @param {ComputeAlgorithm} algorithm
|
* @param {ComputeAlgorithm} algorithm
|
||||||
* @param {string} providerUri
|
* @param {string} providerUri
|
||||||
* @param {Web3} web3
|
* @param {Web3} web3
|
||||||
* @param {any} fetchMethod
|
* @param {AbortSignal} signal abort signal
|
||||||
* @param {ComputeOutput} output
|
* @param {ComputeOutput} output
|
||||||
* @return {Promise<ComputeJob | ComputeJob[]>}
|
* @return {Promise<ComputeJob | ComputeJob[]>}
|
||||||
*/
|
*/
|
||||||
@ -353,11 +361,10 @@ export class Provider {
|
|||||||
computeEnv: string,
|
computeEnv: string,
|
||||||
dataset: ComputeAsset,
|
dataset: ComputeAsset,
|
||||||
algorithm: ComputeAlgorithm,
|
algorithm: ComputeAlgorithm,
|
||||||
fetchMethod?: any,
|
signal?: AbortSignal,
|
||||||
additionalDatasets?: ComputeAsset[],
|
additionalDatasets?: ComputeAsset[],
|
||||||
output?: ComputeOutput
|
output?: ComputeOutput
|
||||||
): Promise<ComputeJob | ComputeJob[]> {
|
): Promise<ComputeJob | ComputeJob[]> {
|
||||||
const preferedFetch = fetchMethod || crossFetchGeneric
|
|
||||||
const providerEndpoints = await this.getEndpoints(providerUri)
|
const providerEndpoints = await this.getEndpoints(providerUri)
|
||||||
const serviceEndpoints = await this.getServiceEndpoints(
|
const serviceEndpoints = await this.getServiceEndpoints(
|
||||||
providerUri,
|
providerUri,
|
||||||
@ -388,16 +395,17 @@ export class Provider {
|
|||||||
if (output) payload.output = output
|
if (output) payload.output = output
|
||||||
if (!computeStartUrl) return null
|
if (!computeStartUrl) return null
|
||||||
try {
|
try {
|
||||||
const response = await preferedFetch(
|
const response = await fetch(computeStartUrl, {
|
||||||
'POST',
|
method: 'POST',
|
||||||
computeStartUrl,
|
body: JSON.stringify(payload),
|
||||||
JSON.stringify(payload),
|
headers: {
|
||||||
{
|
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
}
|
},
|
||||||
)
|
signal: signal
|
||||||
|
})
|
||||||
|
|
||||||
if (response?.ok) {
|
if (response?.ok) {
|
||||||
const params = response.data ? response.data : await response.json()
|
const params = await response.json()
|
||||||
return params
|
return params
|
||||||
}
|
}
|
||||||
console.error('Compute start failed:', response.status, response.statusText)
|
console.error('Compute start failed:', response.status, response.statusText)
|
||||||
@ -417,7 +425,7 @@ export class Provider {
|
|||||||
* @param {string} jobId
|
* @param {string} jobId
|
||||||
* @param {string} providerUri
|
* @param {string} providerUri
|
||||||
* @param {Web3} web3
|
* @param {Web3} web3
|
||||||
* @param {any} fetchMethod
|
* @param {AbortSignal} signal abort signal
|
||||||
* @return {Promise<ComputeJob | ComputeJob[]>}
|
* @return {Promise<ComputeJob | ComputeJob[]>}
|
||||||
*/
|
*/
|
||||||
public async computeStop(
|
public async computeStop(
|
||||||
@ -426,9 +434,8 @@ export class Provider {
|
|||||||
jobId: string,
|
jobId: string,
|
||||||
providerUri: string,
|
providerUri: string,
|
||||||
web3: Web3,
|
web3: Web3,
|
||||||
fetchMethod?: any
|
signal?: AbortSignal
|
||||||
): Promise<ComputeJob | ComputeJob[]> {
|
): Promise<ComputeJob | ComputeJob[]> {
|
||||||
const preferedFetch = fetchMethod || crossFetchGeneric
|
|
||||||
const providerEndpoints = await this.getEndpoints(providerUri)
|
const providerEndpoints = await this.getEndpoints(providerUri)
|
||||||
const serviceEndpoints = await this.getServiceEndpoints(
|
const serviceEndpoints = await this.getServiceEndpoints(
|
||||||
providerUri,
|
providerUri,
|
||||||
@ -441,7 +448,7 @@ export class Provider {
|
|||||||
const nonce = await this.getNonce(
|
const nonce = await this.getNonce(
|
||||||
providerUri,
|
providerUri,
|
||||||
consumerAddress,
|
consumerAddress,
|
||||||
fetchMethod,
|
signal,
|
||||||
providerEndpoints,
|
providerEndpoints,
|
||||||
serviceEndpoints
|
serviceEndpoints
|
||||||
)
|
)
|
||||||
@ -464,16 +471,17 @@ export class Provider {
|
|||||||
|
|
||||||
if (!computeStopUrl) return null
|
if (!computeStopUrl) return null
|
||||||
try {
|
try {
|
||||||
const response = await preferedFetch(
|
const response = await fetch(computeStopUrl, {
|
||||||
'PUT',
|
method: 'PUT',
|
||||||
computeStopUrl,
|
body: JSON.stringify(payload),
|
||||||
JSON.stringify(payload),
|
headers: {
|
||||||
{
|
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
}
|
},
|
||||||
)
|
signal: signal
|
||||||
|
})
|
||||||
|
|
||||||
if (response?.ok) {
|
if (response?.ok) {
|
||||||
const params = response.data ? response.data : await response.json()
|
const params = await response.json()
|
||||||
return params
|
return params
|
||||||
}
|
}
|
||||||
LoggerInstance.error('Compute stop failed:', response.status, response.statusText)
|
LoggerInstance.error('Compute stop failed:', response.status, response.statusText)
|
||||||
@ -492,13 +500,13 @@ export class Provider {
|
|||||||
* @param {string} consumerAddress
|
* @param {string} consumerAddress
|
||||||
* @param {string} providerUri
|
* @param {string} providerUri
|
||||||
* @param {Web3} web3
|
* @param {Web3} web3
|
||||||
* @param {any} fetchMethod
|
* @param {AbortSignal} signal abort signal
|
||||||
* @param {string} jobId
|
* @param {string} jobId
|
||||||
* @return {Promise<ComputeJob | ComputeJob[]>}
|
* @return {Promise<ComputeJob | ComputeJob[]>}
|
||||||
*/
|
*/
|
||||||
public async computeStatus(
|
public async computeStatus(
|
||||||
providerUri: string,
|
providerUri: string,
|
||||||
fetchMethod?: any,
|
signal?: AbortSignal,
|
||||||
jobId?: string,
|
jobId?: string,
|
||||||
did?: string,
|
did?: string,
|
||||||
consumerAddress?: string
|
consumerAddress?: string
|
||||||
@ -506,7 +514,6 @@ export class Provider {
|
|||||||
if (!jobId && !did && !consumerAddress) {
|
if (!jobId && !did && !consumerAddress) {
|
||||||
throw new Error('You need at least one of jobId, did, consumerAddress')
|
throw new Error('You need at least one of jobId, did, consumerAddress')
|
||||||
}
|
}
|
||||||
const preferedFetch = fetchMethod || crossFetchGeneric
|
|
||||||
|
|
||||||
const providerEndpoints = await this.getEndpoints(providerUri)
|
const providerEndpoints = await this.getEndpoints(providerUri)
|
||||||
const serviceEndpoints = await this.getServiceEndpoints(
|
const serviceEndpoints = await this.getServiceEndpoints(
|
||||||
@ -523,11 +530,16 @@ export class Provider {
|
|||||||
|
|
||||||
if (!computeStatusUrl) return null
|
if (!computeStatusUrl) return null
|
||||||
try {
|
try {
|
||||||
const response = await preferedFetch('GET', computeStatusUrl + url, null, {
|
const response = await fetch(computeStatusUrl + url, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
signal: signal
|
||||||
})
|
})
|
||||||
|
|
||||||
if (response?.ok) {
|
if (response?.ok) {
|
||||||
const params = response.data ? response.data : await response.json()
|
const params = await response.json()
|
||||||
return params
|
return params
|
||||||
}
|
}
|
||||||
LoggerInstance.error(
|
LoggerInstance.error(
|
||||||
@ -549,7 +561,7 @@ export class Provider {
|
|||||||
* @param {string} providerUri
|
* @param {string} providerUri
|
||||||
* @param {string} destination
|
* @param {string} destination
|
||||||
* @param {Web3} web3
|
* @param {Web3} web3
|
||||||
* @param {any} fetchMethod
|
* @param {AbortSignal} signal abort signal
|
||||||
* @return {Promise<ComputeJob | ComputeJob[]>}
|
* @return {Promise<ComputeJob | ComputeJob[]>}
|
||||||
*/
|
*/
|
||||||
public async computeResult(
|
public async computeResult(
|
||||||
@ -559,7 +571,7 @@ export class Provider {
|
|||||||
accountId: string,
|
accountId: string,
|
||||||
providerUri: string,
|
providerUri: string,
|
||||||
web3: Web3,
|
web3: Web3,
|
||||||
fetchMethod: any
|
signal?: AbortSignal
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const providerEndpoints = await this.getEndpoints(providerUri)
|
const providerEndpoints = await this.getEndpoints(providerUri)
|
||||||
const serviceEndpoints = await this.getServiceEndpoints(
|
const serviceEndpoints = await this.getServiceEndpoints(
|
||||||
@ -573,7 +585,7 @@ export class Provider {
|
|||||||
const nonce = await this.getNonce(
|
const nonce = await this.getNonce(
|
||||||
providerUri,
|
providerUri,
|
||||||
accountId,
|
accountId,
|
||||||
fetchMethod,
|
signal,
|
||||||
providerEndpoints,
|
providerEndpoints,
|
||||||
serviceEndpoints
|
serviceEndpoints
|
||||||
)
|
)
|
||||||
@ -593,8 +605,8 @@ export class Provider {
|
|||||||
if (!computeResultUrl) return null
|
if (!computeResultUrl) return null
|
||||||
try {
|
try {
|
||||||
!destination
|
!destination
|
||||||
? await fetchMethod.downloadFileBrowser(consumeUrl)
|
? await downloadFileBrowser(consumeUrl)
|
||||||
: await fetchMethod.downloadFile(consumeUrl, destination, index)
|
: await downloadFile(consumeUrl, destination, index)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
LoggerInstance.error('Error getting job result')
|
LoggerInstance.error('Error getting job result')
|
||||||
LoggerInstance.error(e)
|
LoggerInstance.error(e)
|
||||||
@ -609,7 +621,7 @@ export class Provider {
|
|||||||
* @param {string} jobId
|
* @param {string} jobId
|
||||||
* @param {string} providerUri
|
* @param {string} providerUri
|
||||||
* @param {Web3} web3
|
* @param {Web3} web3
|
||||||
* @param {any} fetchMethod
|
* @param {AbortSignal} signal abort signal
|
||||||
* @return {Promise<ComputeJob | ComputeJob[]>}
|
* @return {Promise<ComputeJob | ComputeJob[]>}
|
||||||
*/
|
*/
|
||||||
public async computeDelete(
|
public async computeDelete(
|
||||||
@ -618,9 +630,8 @@ export class Provider {
|
|||||||
jobId: string,
|
jobId: string,
|
||||||
providerUri: string,
|
providerUri: string,
|
||||||
web3: Web3,
|
web3: Web3,
|
||||||
fetchMethod?: any
|
signal?: AbortSignal
|
||||||
): Promise<ComputeJob | ComputeJob[]> {
|
): Promise<ComputeJob | ComputeJob[]> {
|
||||||
const preferedFetch = fetchMethod || crossFetchGeneric
|
|
||||||
const providerEndpoints = await this.getEndpoints(providerUri)
|
const providerEndpoints = await this.getEndpoints(providerUri)
|
||||||
const serviceEndpoints = await this.getServiceEndpoints(
|
const serviceEndpoints = await this.getServiceEndpoints(
|
||||||
providerUri,
|
providerUri,
|
||||||
@ -633,7 +644,7 @@ export class Provider {
|
|||||||
const nonce = await this.getNonce(
|
const nonce = await this.getNonce(
|
||||||
providerUri,
|
providerUri,
|
||||||
consumerAddress,
|
consumerAddress,
|
||||||
fetchMethod,
|
signal,
|
||||||
providerEndpoints,
|
providerEndpoints,
|
||||||
serviceEndpoints
|
serviceEndpoints
|
||||||
)
|
)
|
||||||
@ -656,16 +667,17 @@ export class Provider {
|
|||||||
|
|
||||||
if (!computeDeleteUrl) return null
|
if (!computeDeleteUrl) return null
|
||||||
try {
|
try {
|
||||||
const response = await preferedFetch(
|
const response = await fetch(computeDeleteUrl, {
|
||||||
'DELETE',
|
method: 'DELETE',
|
||||||
computeDeleteUrl,
|
body: JSON.stringify(payload),
|
||||||
JSON.stringify(payload),
|
headers: {
|
||||||
{
|
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
}
|
},
|
||||||
)
|
signal: signal
|
||||||
|
})
|
||||||
|
|
||||||
if (response?.ok) {
|
if (response?.ok) {
|
||||||
const params = response.data ? response.data : await response.json()
|
const params = await response.json()
|
||||||
return params
|
return params
|
||||||
}
|
}
|
||||||
LoggerInstance.error(
|
LoggerInstance.error(
|
||||||
@ -685,17 +697,20 @@ export class Provider {
|
|||||||
|
|
||||||
/** Check for a valid provider at URL
|
/** Check for a valid provider at URL
|
||||||
* @param {String} url provider uri address
|
* @param {String} url provider uri address
|
||||||
* @param {String} fetchMethod fetch client instance
|
* @param {AbortSignal} signal abort signal
|
||||||
* @return {Promise<boolean>} string
|
* @return {Promise<boolean>} string
|
||||||
*/
|
*/
|
||||||
public async isValidProvider(url: string, fetchMethod?: any): Promise<boolean> {
|
public async isValidProvider(url: string, signal?: AbortSignal): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
const preferedFetch = fetchMethod || crossFetchGeneric
|
const response = await fetch(url, {
|
||||||
const response = await preferedFetch('GET', url, null, {
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
signal: signal
|
||||||
})
|
})
|
||||||
if (response?.ok) {
|
if (response?.ok) {
|
||||||
const params = response.data ? response.data : await response.json()
|
const params = await response.json()
|
||||||
if (params && params.providerAddress) return true
|
if (params && params.providerAddress) return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
@ -13,6 +13,13 @@ export async function fetchData(url: string, opts: RequestInit): Promise<Respons
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function downloadFileBrowser(url: string): Promise<void> {
|
||||||
|
const anchor = document.createElement('a')
|
||||||
|
anchor.download = ''
|
||||||
|
anchor.href = url
|
||||||
|
anchor.click()
|
||||||
|
}
|
||||||
|
|
||||||
export async function downloadFile(
|
export async function downloadFile(
|
||||||
url: string,
|
url: string,
|
||||||
destination?: string,
|
destination?: string,
|
||||||
@ -77,17 +84,3 @@ export async function postData(url: string, payload: BodyInit): Promise<Response
|
|||||||
}
|
}
|
||||||
return postWithHeaders(url, payload, headers)
|
return postWithHeaders(url, payload, headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// simple fetch function used in tests
|
|
||||||
export async function crossFetchGeneric(
|
|
||||||
method: string,
|
|
||||||
url: string,
|
|
||||||
body: string,
|
|
||||||
headers: any
|
|
||||||
) {
|
|
||||||
return fetch(url, {
|
|
||||||
method: method,
|
|
||||||
body: body,
|
|
||||||
headers: headers
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
@ -11,7 +11,7 @@ import Web3 from 'web3'
|
|||||||
import { algo, SHA256 } from 'crypto-js'
|
import { algo, SHA256 } from 'crypto-js'
|
||||||
import { homedir } from 'os'
|
import { homedir } from 'os'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import { downloadFile, crossFetchGeneric } from '../../src/utils/FetchHelper'
|
import { downloadFile } from '../../src/utils/FetchHelper'
|
||||||
import console from 'console'
|
import console from 'console'
|
||||||
import { ProviderFees } from '../../src/@types'
|
import { ProviderFees } from '../../src/@types'
|
||||||
|
|
||||||
@ -24,7 +24,6 @@ const data = JSON.parse(
|
|||||||
)
|
)
|
||||||
|
|
||||||
const addresses = data.development
|
const addresses = data.development
|
||||||
console.log(addresses)
|
|
||||||
const aquarius = new Aquarius('http://127.0.0.1:5000')
|
const aquarius = new Aquarius('http://127.0.0.1:5000')
|
||||||
const web3 = new Web3('http://127.0.0.1:8545')
|
const web3 = new Web3('http://127.0.0.1:8545')
|
||||||
const providerUrl = 'http://172.15.0.4:8030'
|
const providerUrl = 'http://172.15.0.4:8030'
|
||||||
@ -156,14 +155,9 @@ describe('Simple compute tests', async () => {
|
|||||||
)
|
)
|
||||||
const erc721AddressAsset = result.events.NFTCreated.returnValues[0]
|
const erc721AddressAsset = result.events.NFTCreated.returnValues[0]
|
||||||
const datatokenAddressAsset = result.events.TokenCreated.returnValues[0]
|
const datatokenAddressAsset = result.events.TokenCreated.returnValues[0]
|
||||||
|
|
||||||
// create the files encrypted string
|
// create the files encrypted string
|
||||||
let providerResponse = await ProviderInstance.encrypt(
|
let providerResponse = await ProviderInstance.encrypt(assetUrl, providerUrl)
|
||||||
assetUrl,
|
ddo.services[0].files = await providerResponse
|
||||||
providerUrl,
|
|
||||||
crossFetchGeneric
|
|
||||||
)
|
|
||||||
ddo.services[0].files = await providerResponse.text()
|
|
||||||
ddo.services[0].datatokenAddress = datatokenAddressAsset
|
ddo.services[0].datatokenAddress = datatokenAddressAsset
|
||||||
// update ddo and set the right did
|
// update ddo and set the right did
|
||||||
ddo.nftAddress = erc721AddressAsset
|
ddo.nftAddress = erc721AddressAsset
|
||||||
@ -171,8 +165,9 @@ describe('Simple compute tests', async () => {
|
|||||||
'did:op:' +
|
'did:op:' +
|
||||||
SHA256(web3.utils.toChecksumAddress(erc721AddressAsset) + chain.toString(10))
|
SHA256(web3.utils.toChecksumAddress(erc721AddressAsset) + chain.toString(10))
|
||||||
|
|
||||||
providerResponse = await ProviderInstance.encrypt(ddo, providerUrl, crossFetchGeneric)
|
providerResponse = await ProviderInstance.encrypt(ddo, providerUrl)
|
||||||
let encryptedResponse = await providerResponse.text()
|
|
||||||
|
let encryptedResponse = await providerResponse
|
||||||
let metadataHash = getHash(JSON.stringify(ddo))
|
let metadataHash = getHash(JSON.stringify(ddo))
|
||||||
let res = await nft.setMetadata(
|
let res = await nft.setMetadata(
|
||||||
erc721AddressAsset,
|
erc721AddressAsset,
|
||||||
@ -184,7 +179,7 @@ describe('Simple compute tests', async () => {
|
|||||||
encryptedResponse,
|
encryptedResponse,
|
||||||
'0x' + metadataHash
|
'0x' + metadataHash
|
||||||
)
|
)
|
||||||
|
console.log('setMetadata tx', res)
|
||||||
// let's publish the algorithm as well
|
// let's publish the algorithm as well
|
||||||
const nftParamsAlgo: NftCreateData = {
|
const nftParamsAlgo: NftCreateData = {
|
||||||
name: 'testNFT',
|
name: 'testNFT',
|
||||||
@ -210,12 +205,8 @@ describe('Simple compute tests', async () => {
|
|||||||
const datatokenAddressAlgo = resultAlgo.events.TokenCreated.returnValues[0]
|
const datatokenAddressAlgo = resultAlgo.events.TokenCreated.returnValues[0]
|
||||||
|
|
||||||
// create the files encrypted string
|
// create the files encrypted string
|
||||||
providerResponse = await ProviderInstance.encrypt(
|
providerResponse = await ProviderInstance.encrypt(algoAssetUrl, providerUrl)
|
||||||
algoAssetUrl,
|
algoDdo.services[0].files = await providerResponse
|
||||||
providerUrl,
|
|
||||||
crossFetchGeneric
|
|
||||||
)
|
|
||||||
algoDdo.services[0].files = await providerResponse.text()
|
|
||||||
algoDdo.services[0].datatokenAddress = datatokenAddressAlgo
|
algoDdo.services[0].datatokenAddress = datatokenAddressAlgo
|
||||||
// update ddo and set the right did
|
// update ddo and set the right did
|
||||||
algoDdo.nftAddress = erc721AddressAlgo
|
algoDdo.nftAddress = erc721AddressAlgo
|
||||||
@ -224,12 +215,8 @@ describe('Simple compute tests', async () => {
|
|||||||
'did:op:' +
|
'did:op:' +
|
||||||
SHA256(web3.utils.toChecksumAddress(erc721AddressAlgo) + chain.toString(10))
|
SHA256(web3.utils.toChecksumAddress(erc721AddressAlgo) + chain.toString(10))
|
||||||
|
|
||||||
providerResponse = await ProviderInstance.encrypt(
|
providerResponse = await ProviderInstance.encrypt(algoDdo, providerUrl)
|
||||||
algoDdo,
|
encryptedResponse = await providerResponse
|
||||||
providerUrl,
|
|
||||||
crossFetchGeneric
|
|
||||||
)
|
|
||||||
encryptedResponse = await providerResponse.text()
|
|
||||||
metadataHash = getHash(JSON.stringify(algoDdo))
|
metadataHash = getHash(JSON.stringify(algoDdo))
|
||||||
res = await nft.setMetadata(
|
res = await nft.setMetadata(
|
||||||
erc721AddressAlgo,
|
erc721AddressAlgo,
|
||||||
@ -241,14 +228,12 @@ describe('Simple compute tests', async () => {
|
|||||||
encryptedResponse,
|
encryptedResponse,
|
||||||
'0x' + metadataHash
|
'0x' + metadataHash
|
||||||
)
|
)
|
||||||
|
|
||||||
|
console.log('starting to wait for aqua')
|
||||||
// let's wait
|
// let's wait
|
||||||
const resolvedDDOAsset = await aquarius.waitForAqua(ddo.id, null, crossFetchGeneric)
|
const resolvedDDOAsset = await aquarius.waitForAqua(ddo.id)
|
||||||
assert(resolvedDDOAsset, 'Cannot fetch DDO from Aquarius')
|
assert(resolvedDDOAsset, 'Cannot fetch DDO from Aquarius')
|
||||||
const resolvedDDOAlgo = await aquarius.waitForAqua(
|
const resolvedDDOAlgo = await aquarius.waitForAqua(algoDdo.id)
|
||||||
algoDdo.id,
|
|
||||||
null,
|
|
||||||
crossFetchGeneric
|
|
||||||
)
|
|
||||||
assert(resolvedDDOAlgo, 'Cannot fetch DDO from Aquarius')
|
assert(resolvedDDOAlgo, 'Cannot fetch DDO from Aquarius')
|
||||||
// mint 1 ERC20 and send it to the consumer
|
// mint 1 ERC20 and send it to the consumer
|
||||||
await datatoken.mint(datatokenAddressAsset, publisherAccount, '1', consumerAccount)
|
await datatoken.mint(datatokenAddressAsset, publisherAccount, '1', consumerAccount)
|
||||||
@ -260,8 +245,7 @@ describe('Simple compute tests', async () => {
|
|||||||
resolvedDDOAlgo.services[0].id,
|
resolvedDDOAlgo.services[0].id,
|
||||||
0,
|
0,
|
||||||
consumerAccount,
|
consumerAccount,
|
||||||
providerUrl,
|
providerUrl
|
||||||
crossFetchGeneric
|
|
||||||
)
|
)
|
||||||
const providerAlgoFees: ProviderFees = {
|
const providerAlgoFees: ProviderFees = {
|
||||||
providerFeeAddress: initializeDataAlgo.providerFee.providerFeeAddress,
|
providerFeeAddress: initializeDataAlgo.providerFee.providerFeeAddress,
|
||||||
@ -294,7 +278,7 @@ describe('Simple compute tests', async () => {
|
|||||||
0,
|
0,
|
||||||
consumerAccount,
|
consumerAccount,
|
||||||
providerUrl,
|
providerUrl,
|
||||||
crossFetchGeneric,
|
null,
|
||||||
null,
|
null,
|
||||||
'env1',
|
'env1',
|
||||||
providerValidUntil.getTime()
|
providerValidUntil.getTime()
|
||||||
@ -333,13 +317,12 @@ describe('Simple compute tests', async () => {
|
|||||||
documentId: resolvedDDOAlgo.id,
|
documentId: resolvedDDOAlgo.id,
|
||||||
serviceId: resolvedDDOAlgo.services[0].id,
|
serviceId: resolvedDDOAlgo.services[0].id,
|
||||||
transferTxId: txidAlgo.transactionHash
|
transferTxId: txidAlgo.transactionHash
|
||||||
},
|
}
|
||||||
crossFetchGeneric
|
|
||||||
)
|
)
|
||||||
assert(computeJobs, 'Cannot start compute job')
|
assert(computeJobs, 'Cannot start compute job')
|
||||||
const jobStatus = await ProviderInstance.computeStatus(
|
const jobStatus = await ProviderInstance.computeStatus(
|
||||||
providerUrl,
|
providerUrl,
|
||||||
crossFetchGeneric,
|
null,
|
||||||
computeJobs[0].jobId
|
computeJobs[0].jobId
|
||||||
)
|
)
|
||||||
assert(jobStatus)
|
assert(jobStatus)
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import { Provider } from '../../src/provider/Provider'
|
import { Provider } from '../../src/provider/Provider'
|
||||||
import { assert } from 'chai'
|
import { assert } from 'chai'
|
||||||
import { fetchData, crossFetchGeneric } from '../../src/utils'
|
|
||||||
import { FileMetadata } from '../../src/@types'
|
import { FileMetadata } from '../../src/@types'
|
||||||
|
|
||||||
describe('Provider tests', () => {
|
describe('Provider tests', () => {
|
||||||
@ -11,26 +10,19 @@ describe('Provider tests', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('Alice tests invalid provider', async () => {
|
it('Alice tests invalid provider', async () => {
|
||||||
const valid = await providerInstance.isValidProvider(
|
const valid = await providerInstance.isValidProvider('http://example.net')
|
||||||
'http://example.net',
|
|
||||||
crossFetchGeneric
|
|
||||||
)
|
|
||||||
assert(valid === false)
|
assert(valid === false)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Alice tests valid provider', async () => {
|
it('Alice tests valid provider', async () => {
|
||||||
const valid = await providerInstance.isValidProvider(
|
const valid = await providerInstance.isValidProvider('http://127.0.0.1:8030')
|
||||||
'http://127.0.0.1:8030',
|
|
||||||
crossFetchGeneric
|
|
||||||
)
|
|
||||||
assert(valid === true)
|
assert(valid === true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Alice checks fileinfo', async () => {
|
it('Alice checks fileinfo', async () => {
|
||||||
const fileinfo: FileMetadata[] = await providerInstance.checkFileUrl(
|
const fileinfo: FileMetadata[] = await providerInstance.checkFileUrl(
|
||||||
'https://dumps.wikimedia.org/enwiki/latest/enwiki-latest-abstract.xml.gz-rss.xml',
|
'https://dumps.wikimedia.org/enwiki/latest/enwiki-latest-abstract.xml.gz-rss.xml',
|
||||||
'http://127.0.0.1:8030',
|
'http://127.0.0.1:8030'
|
||||||
crossFetchGeneric
|
|
||||||
)
|
)
|
||||||
assert(fileinfo[0].valid === true, 'Sent file is not valid')
|
assert(fileinfo[0].valid === true, 'Sent file is not valid')
|
||||||
})
|
})
|
||||||
|
@ -9,7 +9,7 @@ import {
|
|||||||
FreCreationParams,
|
FreCreationParams,
|
||||||
DispenserCreationParams
|
DispenserCreationParams
|
||||||
} from '../../src/interfaces'
|
} from '../../src/interfaces'
|
||||||
import { getHash, crossFetchGeneric, ZERO_ADDRESS } from '../../src/utils'
|
import { getHash, ZERO_ADDRESS } from '../../src/utils'
|
||||||
import { Nft } from '../../src/tokens/NFT'
|
import { Nft } from '../../src/tokens/NFT'
|
||||||
import Web3 from 'web3'
|
import Web3 from 'web3'
|
||||||
import { SHA256 } from 'crypto-js'
|
import { SHA256 } from 'crypto-js'
|
||||||
@ -27,7 +27,6 @@ const data = JSON.parse(
|
|||||||
)
|
)
|
||||||
|
|
||||||
const addresses = data.development
|
const addresses = data.development
|
||||||
console.log(addresses)
|
|
||||||
const aquarius = new Aquarius('http://127.0.0.1:5000')
|
const aquarius = new Aquarius('http://127.0.0.1:5000')
|
||||||
const web3 = new Web3('http://127.0.0.1:8545')
|
const web3 = new Web3('http://127.0.0.1:8545')
|
||||||
const providerUrl = 'http://172.15.0.4:8030'
|
const providerUrl = 'http://172.15.0.4:8030'
|
||||||
@ -130,14 +129,10 @@ describe('Publish tests', async () => {
|
|||||||
const datatokenAddress = bundleNFT.events.TokenCreated.returnValues[0]
|
const datatokenAddress = bundleNFT.events.TokenCreated.returnValues[0]
|
||||||
const poolAdress = bundleNFT.events.NewPool.returnValues[0]
|
const poolAdress = bundleNFT.events.NewPool.returnValues[0]
|
||||||
|
|
||||||
const encryptedFiles = await ProviderInstance.encrypt(
|
const encryptedFiles = await ProviderInstance.encrypt(files, providerUrl)
|
||||||
files,
|
|
||||||
providerUrl,
|
|
||||||
crossFetchGeneric
|
|
||||||
)
|
|
||||||
|
|
||||||
poolDdo.metadata.name = 'test-dataset-pool'
|
poolDdo.metadata.name = 'test-dataset-pool'
|
||||||
poolDdo.services[0].files = await encryptedFiles.text()
|
poolDdo.services[0].files = await encryptedFiles
|
||||||
poolDdo.services[0].datatokenAddress = datatokenAddress
|
poolDdo.services[0].datatokenAddress = datatokenAddress
|
||||||
|
|
||||||
poolDdo.nftAddress = nftAddress
|
poolDdo.nftAddress = nftAddress
|
||||||
@ -146,18 +141,11 @@ describe('Publish tests', async () => {
|
|||||||
poolDdo.id =
|
poolDdo.id =
|
||||||
'did:op:' + SHA256(web3.utils.toChecksumAddress(nftAddress) + chain.toString(10))
|
'did:op:' + SHA256(web3.utils.toChecksumAddress(nftAddress) + chain.toString(10))
|
||||||
|
|
||||||
const AssetValidation: ValidateMetadata = await aquarius.validate(
|
const AssetValidation: ValidateMetadata = await aquarius.validate(poolDdo)
|
||||||
poolDdo,
|
|
||||||
crossFetchGeneric
|
|
||||||
)
|
|
||||||
assert(AssetValidation.valid === true, 'Published asset is not valid')
|
assert(AssetValidation.valid === true, 'Published asset is not valid')
|
||||||
|
|
||||||
const encryptedDdo = await ProviderInstance.encrypt(
|
const encryptedDdo = await ProviderInstance.encrypt(poolDdo, providerUrl)
|
||||||
poolDdo,
|
const encryptedResponse = await encryptedDdo
|
||||||
providerUrl,
|
|
||||||
crossFetchGeneric
|
|
||||||
)
|
|
||||||
const encryptedResponse = await encryptedDdo.text()
|
|
||||||
const metadataHash = getHash(JSON.stringify(poolDdo))
|
const metadataHash = getHash(JSON.stringify(poolDdo))
|
||||||
// just to make sure that our hash matches one computed by aquarius
|
// just to make sure that our hash matches one computed by aquarius
|
||||||
assert(AssetValidation.hash === '0x' + metadataHash, 'Metadata hash is a missmatch')
|
assert(AssetValidation.hash === '0x' + metadataHash, 'Metadata hash is a missmatch')
|
||||||
@ -173,7 +161,7 @@ describe('Publish tests', async () => {
|
|||||||
[AssetValidation.proof]
|
[AssetValidation.proof]
|
||||||
)
|
)
|
||||||
|
|
||||||
const resolvedDDO = await aquarius.waitForAqua(poolDdo.id, null, crossFetchGeneric)
|
const resolvedDDO = await aquarius.waitForAqua(poolDdo.id)
|
||||||
assert(resolvedDDO, 'Cannot fetch DDO from Aquarius')
|
assert(resolvedDDO, 'Cannot fetch DDO from Aquarius')
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -219,14 +207,10 @@ describe('Publish tests', async () => {
|
|||||||
const datatokenAddress = bundleNFT.events.TokenCreated.returnValues[0]
|
const datatokenAddress = bundleNFT.events.TokenCreated.returnValues[0]
|
||||||
const fixedPrice = bundleNFT.events.NewFixedRate.returnValues[0]
|
const fixedPrice = bundleNFT.events.NewFixedRate.returnValues[0]
|
||||||
|
|
||||||
const encryptedFiles = await ProviderInstance.encrypt(
|
const encryptedFiles = await ProviderInstance.encrypt(files, providerUrl)
|
||||||
files,
|
|
||||||
providerUrl,
|
|
||||||
crossFetchGeneric
|
|
||||||
)
|
|
||||||
|
|
||||||
fixedPriceDdo.metadata.name = 'test-dataset-fixedPrice'
|
fixedPriceDdo.metadata.name = 'test-dataset-fixedPrice'
|
||||||
fixedPriceDdo.services[0].files = await encryptedFiles.text()
|
fixedPriceDdo.services[0].files = await encryptedFiles
|
||||||
fixedPriceDdo.services[0].datatokenAddress = datatokenAddress
|
fixedPriceDdo.services[0].datatokenAddress = datatokenAddress
|
||||||
|
|
||||||
fixedPriceDdo.nftAddress = nftAddress
|
fixedPriceDdo.nftAddress = nftAddress
|
||||||
@ -235,18 +219,11 @@ describe('Publish tests', async () => {
|
|||||||
fixedPriceDdo.id =
|
fixedPriceDdo.id =
|
||||||
'did:op:' + SHA256(web3.utils.toChecksumAddress(nftAddress) + chain.toString(10))
|
'did:op:' + SHA256(web3.utils.toChecksumAddress(nftAddress) + chain.toString(10))
|
||||||
|
|
||||||
const isAssetValid: ValidateMetadata = await aquarius.validate(
|
const isAssetValid: ValidateMetadata = await aquarius.validate(fixedPriceDdo)
|
||||||
fixedPriceDdo,
|
|
||||||
crossFetchGeneric
|
|
||||||
)
|
|
||||||
assert(isAssetValid.valid === true, 'Published asset is not valid')
|
assert(isAssetValid.valid === true, 'Published asset is not valid')
|
||||||
|
|
||||||
const encryptedDdo = await ProviderInstance.encrypt(
|
const encryptedDdo = await ProviderInstance.encrypt(fixedPriceDdo, providerUrl)
|
||||||
fixedPriceDdo,
|
const encryptedResponse = await encryptedDdo
|
||||||
providerUrl,
|
|
||||||
crossFetchGeneric
|
|
||||||
)
|
|
||||||
const encryptedResponse = await encryptedDdo.text()
|
|
||||||
const metadataHash = getHash(JSON.stringify(fixedPriceDdo))
|
const metadataHash = getHash(JSON.stringify(fixedPriceDdo))
|
||||||
// this is publishing with an explicit empty metadataProofs
|
// this is publishing with an explicit empty metadataProofs
|
||||||
const res = await nft.setMetadata(
|
const res = await nft.setMetadata(
|
||||||
@ -260,11 +237,7 @@ describe('Publish tests', async () => {
|
|||||||
'0x' + metadataHash,
|
'0x' + metadataHash,
|
||||||
[]
|
[]
|
||||||
)
|
)
|
||||||
const resolvedDDO = await aquarius.waitForAqua(
|
const resolvedDDO = await aquarius.waitForAqua(fixedPriceDdo.id)
|
||||||
fixedPriceDdo.id,
|
|
||||||
null,
|
|
||||||
crossFetchGeneric
|
|
||||||
)
|
|
||||||
assert(resolvedDDO, 'Cannot fetch DDO from Aquarius')
|
assert(resolvedDDO, 'Cannot fetch DDO from Aquarius')
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -305,13 +278,9 @@ describe('Publish tests', async () => {
|
|||||||
const datatokenAddress = bundleNFT.events.TokenCreated.returnValues[0]
|
const datatokenAddress = bundleNFT.events.TokenCreated.returnValues[0]
|
||||||
const dispenserAddress = bundleNFT.events.DispenserCreated.returnValues[0]
|
const dispenserAddress = bundleNFT.events.DispenserCreated.returnValues[0]
|
||||||
|
|
||||||
const encryptedFiles = await ProviderInstance.encrypt(
|
const encryptedFiles = await ProviderInstance.encrypt(files, providerUrl)
|
||||||
files,
|
|
||||||
providerUrl,
|
|
||||||
crossFetchGeneric
|
|
||||||
)
|
|
||||||
dispenserDdo.metadata.name = 'test-dataset-dispenser'
|
dispenserDdo.metadata.name = 'test-dataset-dispenser'
|
||||||
dispenserDdo.services[0].files = await encryptedFiles.text()
|
dispenserDdo.services[0].files = await encryptedFiles
|
||||||
dispenserDdo.services[0].datatokenAddress = datatokenAddress
|
dispenserDdo.services[0].datatokenAddress = datatokenAddress
|
||||||
|
|
||||||
dispenserDdo.nftAddress = nftAddress
|
dispenserDdo.nftAddress = nftAddress
|
||||||
@ -320,18 +289,11 @@ describe('Publish tests', async () => {
|
|||||||
dispenserDdo.id =
|
dispenserDdo.id =
|
||||||
'did:op:' + SHA256(web3.utils.toChecksumAddress(nftAddress) + chain.toString(10))
|
'did:op:' + SHA256(web3.utils.toChecksumAddress(nftAddress) + chain.toString(10))
|
||||||
|
|
||||||
const isAssetValid: ValidateMetadata = await aquarius.validate(
|
const isAssetValid: ValidateMetadata = await aquarius.validate(dispenserDdo)
|
||||||
dispenserDdo,
|
|
||||||
crossFetchGeneric
|
|
||||||
)
|
|
||||||
assert(isAssetValid.valid === true, 'Published asset is not valid')
|
assert(isAssetValid.valid === true, 'Published asset is not valid')
|
||||||
|
|
||||||
const encryptedDdo = await ProviderInstance.encrypt(
|
const encryptedDdo = await ProviderInstance.encrypt(dispenserDdo, providerUrl)
|
||||||
dispenserDdo,
|
const encryptedResponse = await encryptedDdo
|
||||||
providerUrl,
|
|
||||||
crossFetchGeneric
|
|
||||||
)
|
|
||||||
const encryptedResponse = await encryptedDdo.text()
|
|
||||||
const metadataHash = getHash(JSON.stringify(dispenserDdo))
|
const metadataHash = getHash(JSON.stringify(dispenserDdo))
|
||||||
// this is publishing with any explicit metadataProofs
|
// this is publishing with any explicit metadataProofs
|
||||||
const res = await nft.setMetadata(
|
const res = await nft.setMetadata(
|
||||||
@ -344,11 +306,7 @@ describe('Publish tests', async () => {
|
|||||||
encryptedResponse,
|
encryptedResponse,
|
||||||
'0x' + metadataHash
|
'0x' + metadataHash
|
||||||
)
|
)
|
||||||
const resolvedDDO = await aquarius.waitForAqua(
|
const resolvedDDO = await aquarius.waitForAqua(dispenserDdo.id)
|
||||||
dispenserDdo.id,
|
|
||||||
null,
|
|
||||||
crossFetchGeneric
|
|
||||||
)
|
|
||||||
assert(resolvedDDO, 'Cannot fetch DDO from Aquarius')
|
assert(resolvedDDO, 'Cannot fetch DDO from Aquarius')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import ProviderInstance, { Provider } from '../../src/provider/Provider'
|
import ProviderInstance from '../../src/provider/Provider'
|
||||||
import Aquarius from '../../src/aquarius/Aquarius'
|
import Aquarius from '../../src/aquarius/Aquarius'
|
||||||
import { assert } from 'chai'
|
import { assert } from 'chai'
|
||||||
import { NftFactory, NftCreateData } from '../../src/factories/index'
|
import { NftFactory, NftCreateData } from '../../src/factories/index'
|
||||||
@ -10,7 +10,7 @@ import Web3 from 'web3'
|
|||||||
import { SHA256 } from 'crypto-js'
|
import { SHA256 } from 'crypto-js'
|
||||||
import { homedir } from 'os'
|
import { homedir } from 'os'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import { downloadFile, crossFetchGeneric } from '../../src/utils/FetchHelper'
|
import { downloadFile } from '../../src/utils/FetchHelper'
|
||||||
import console from 'console'
|
import console from 'console'
|
||||||
import { ProviderFees } from '../../src/@types/Provider'
|
import { ProviderFees } from '../../src/@types/Provider'
|
||||||
|
|
||||||
@ -97,12 +97,8 @@ describe('Simple Publish & consume test', async () => {
|
|||||||
const datatokenAddress = result.events.TokenCreated.returnValues[0]
|
const datatokenAddress = result.events.TokenCreated.returnValues[0]
|
||||||
|
|
||||||
// create the files encrypted string
|
// create the files encrypted string
|
||||||
let providerResponse = await ProviderInstance.encrypt(
|
let providerResponse = await ProviderInstance.encrypt(assetUrl, providerUrl)
|
||||||
assetUrl,
|
ddo.services[0].files = await providerResponse
|
||||||
providerUrl,
|
|
||||||
crossFetchGeneric
|
|
||||||
)
|
|
||||||
ddo.services[0].files = await providerResponse.text()
|
|
||||||
ddo.services[0].datatokenAddress = datatokenAddress
|
ddo.services[0].datatokenAddress = datatokenAddress
|
||||||
// update ddo and set the right did
|
// update ddo and set the right did
|
||||||
ddo.nftAddress = erc721Address
|
ddo.nftAddress = erc721Address
|
||||||
@ -110,8 +106,8 @@ describe('Simple Publish & consume test', async () => {
|
|||||||
ddo.id =
|
ddo.id =
|
||||||
'did:op:' + SHA256(web3.utils.toChecksumAddress(erc721Address) + chain.toString(10))
|
'did:op:' + SHA256(web3.utils.toChecksumAddress(erc721Address) + chain.toString(10))
|
||||||
|
|
||||||
providerResponse = await ProviderInstance.encrypt(ddo, providerUrl, crossFetchGeneric)
|
providerResponse = await ProviderInstance.encrypt(ddo, providerUrl)
|
||||||
const encryptedResponse = await providerResponse.text()
|
const encryptedResponse = await providerResponse
|
||||||
const metadataHash = getHash(JSON.stringify(ddo))
|
const metadataHash = getHash(JSON.stringify(ddo))
|
||||||
const res = await nft.setMetadata(
|
const res = await nft.setMetadata(
|
||||||
erc721Address,
|
erc721Address,
|
||||||
@ -123,7 +119,7 @@ describe('Simple Publish & consume test', async () => {
|
|||||||
encryptedResponse,
|
encryptedResponse,
|
||||||
'0x' + metadataHash
|
'0x' + metadataHash
|
||||||
)
|
)
|
||||||
const resolvedDDO = await aquarius.waitForAqua(ddo.id, null, crossFetchGeneric)
|
const resolvedDDO = await aquarius.waitForAqua(ddo.id)
|
||||||
assert(resolvedDDO, 'Cannot fetch DDO from Aquarius')
|
assert(resolvedDDO, 'Cannot fetch DDO from Aquarius')
|
||||||
// mint 1 ERC20 and send it to the consumer
|
// mint 1 ERC20 and send it to the consumer
|
||||||
await datatoken.mint(datatokenAddress, publisherAccount, '1', consumerAccount)
|
await datatoken.mint(datatokenAddress, publisherAccount, '1', consumerAccount)
|
||||||
@ -133,8 +129,7 @@ describe('Simple Publish & consume test', async () => {
|
|||||||
resolvedDDO.services[0].id,
|
resolvedDDO.services[0].id,
|
||||||
0,
|
0,
|
||||||
consumerAccount,
|
consumerAccount,
|
||||||
providerUrl,
|
providerUrl
|
||||||
crossFetchGeneric
|
|
||||||
)
|
)
|
||||||
const providerFees: ProviderFees = {
|
const providerFees: ProviderFees = {
|
||||||
providerFeeAddress: initializeData.providerFee.providerFeeAddress,
|
providerFeeAddress: initializeData.providerFee.providerFeeAddress,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user