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