mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
Provider int tests
This commit is contained in:
parent
dd72fa4d44
commit
3ed382ce84
@ -30,7 +30,7 @@
|
|||||||
"test": "npm run lint && npm run test:unit:cover && npm run test:integration:cover",
|
"test": "npm run lint && npm run test:unit:cover && npm run test:integration:cover",
|
||||||
"test:unit": "npm run mocha -- 'test/unit/**/*.test.ts'",
|
"test:unit": "npm run mocha -- 'test/unit/**/*.test.ts'",
|
||||||
"test:unit:cover": "nyc --report-dir coverage/unit --exclude 'src/@types/**/*' npm run test:unit",
|
"test:unit:cover": "nyc --report-dir coverage/unit --exclude 'src/@types/**/*' npm run test:unit",
|
||||||
"test:integration": "npm run mocha -- 'test/integration/**/*.test.ts'",
|
"test:integration": "npm run mocha -- 'test/integration/Provider.test.ts'",
|
||||||
"test:integration:cover": "nyc --report-dir coverage/integration --no-clean npm run test:integration",
|
"test:integration:cover": "nyc --report-dir coverage/integration --no-clean npm run test:integration",
|
||||||
"create:guide": "./scripts/createCodeExamples.sh test/integration/CodeExamples.test.ts",
|
"create:guide": "./scripts/createCodeExamples.sh test/integration/CodeExamples.test.ts",
|
||||||
"create:guidec2d": "./scripts/createCodeExamples.sh test/integration/ComputeExamples.test.ts",
|
"create:guidec2d": "./scripts/createCodeExamples.sh test/integration/ComputeExamples.test.ts",
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import Web3 from 'web3'
|
|
||||||
import fetch from 'cross-fetch'
|
import fetch from 'cross-fetch'
|
||||||
|
import { ethers, Signer, providers } from 'ethers'
|
||||||
import { LoggerInstance } from '../utils'
|
import { LoggerInstance } from '../utils'
|
||||||
import {
|
import {
|
||||||
Arweave,
|
Arweave,
|
||||||
@ -98,21 +98,25 @@ export class Provider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// aici trebuie modificat
|
||||||
public async signProviderRequest(
|
public async signProviderRequest(
|
||||||
web3: Web3,
|
signer: Signer,
|
||||||
accountId: string,
|
|
||||||
message: string,
|
message: string,
|
||||||
password?: string
|
password?: string
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const consumerMessage = web3.utils.soliditySha3({
|
const consumerMessage = ethers.utils.solidityKeccak256(
|
||||||
t: 'bytes',
|
['bytes'],
|
||||||
v: web3.utils.utf8ToHex(message)
|
[ethers.utils.hexlify(ethers.utils.toUtf8Bytes(message))]
|
||||||
})
|
)
|
||||||
const isMetaMask =
|
|
||||||
web3 && web3.currentProvider && (web3.currentProvider as any).isMetaMask
|
// const isMetaMask =
|
||||||
if (isMetaMask)
|
// web3 && web3.currentProvider && (web3.currentProvider as any).isMetaMask
|
||||||
return await web3.eth.personal.sign(consumerMessage, accountId, password)
|
|
||||||
else return await web3.eth.sign(consumerMessage, accountId)
|
// return await web3.eth.personal.sign(consumerMessage, accountId, password)
|
||||||
|
// if (isMetaMask)
|
||||||
|
// (signer as providers.JsonRpcSigner)._legacySignMessage(consumerMessage)
|
||||||
|
// else
|
||||||
|
return await (signer as providers.JsonRpcSigner)._legacySignMessage(consumerMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Encrypt data using the Provider's own symmetric key
|
/** Encrypt data using the Provider's own symmetric key
|
||||||
@ -371,7 +375,7 @@ export class Provider {
|
|||||||
* @param {string} serviceId
|
* @param {string} serviceId
|
||||||
* @param {number} fileIndex
|
* @param {number} fileIndex
|
||||||
* @param {string} providerUri
|
* @param {string} providerUri
|
||||||
* @param {Web3} web3
|
* @param {Signer} signer
|
||||||
* @param {UserCustomParameters} userCustomParameters
|
* @param {UserCustomParameters} userCustomParameters
|
||||||
* @return {Promise<string>}
|
* @return {Promise<string>}
|
||||||
*/
|
*/
|
||||||
@ -382,7 +386,7 @@ export class Provider {
|
|||||||
fileIndex: number,
|
fileIndex: number,
|
||||||
transferTxId: string,
|
transferTxId: string,
|
||||||
providerUri: string,
|
providerUri: string,
|
||||||
web3: Web3,
|
signer: Signer,
|
||||||
userCustomParameters?: UserCustomParameters
|
userCustomParameters?: UserCustomParameters
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
const providerEndpoints = await this.getEndpoints(providerUri)
|
const providerEndpoints = await this.getEndpoints(providerUri)
|
||||||
@ -395,7 +399,7 @@ export class Provider {
|
|||||||
: null
|
: null
|
||||||
if (!downloadUrl) return null
|
if (!downloadUrl) return null
|
||||||
const nonce = Date.now()
|
const nonce = Date.now()
|
||||||
const signature = await this.signProviderRequest(web3, accountId, did + nonce)
|
const signature = await this.signProviderRequest(signer, did + nonce)
|
||||||
let consumeUrl = downloadUrl
|
let consumeUrl = downloadUrl
|
||||||
consumeUrl += `?fileIndex=${fileIndex}`
|
consumeUrl += `?fileIndex=${fileIndex}`
|
||||||
consumeUrl += `&documentId=${did}`
|
consumeUrl += `&documentId=${did}`
|
||||||
@ -409,20 +413,22 @@ export class Provider {
|
|||||||
return consumeUrl
|
return consumeUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// need to update
|
||||||
|
// this as well
|
||||||
/** Instruct the provider to start a compute job
|
/** Instruct the provider to start a compute job
|
||||||
* @param {string} did
|
* @param {string} did
|
||||||
* @param {string} consumerAddress
|
* @param {string} consumerAddress
|
||||||
* @param {string} computeEnv
|
* @param {string} computeEnv
|
||||||
* @param {ComputeAlgorithm} algorithm
|
* @param {ComputeAlgorithm} algorithm
|
||||||
* @param {string} providerUri
|
* @param {string} providerUri
|
||||||
* @param {Web3} web3
|
* @param {Signer} signer
|
||||||
* @param {AbortSignal} signal abort signal
|
* @param {AbortSignal} signal abort signal
|
||||||
* @param {ComputeOutput} output
|
* @param {ComputeOutput} output
|
||||||
* @return {Promise<ComputeJob | ComputeJob[]>}
|
* @return {Promise<ComputeJob | ComputeJob[]>}
|
||||||
*/
|
*/
|
||||||
public async computeStart(
|
public async computeStart(
|
||||||
providerUri: string,
|
providerUri: string,
|
||||||
web3: Web3,
|
signer: Signer,
|
||||||
consumerAddress: string,
|
consumerAddress: string,
|
||||||
computeEnv: string,
|
computeEnv: string,
|
||||||
dataset: ComputeAsset,
|
dataset: ComputeAsset,
|
||||||
@ -444,11 +450,7 @@ export class Provider {
|
|||||||
let signatureMessage = consumerAddress
|
let signatureMessage = consumerAddress
|
||||||
signatureMessage += dataset.documentId
|
signatureMessage += dataset.documentId
|
||||||
signatureMessage += nonce
|
signatureMessage += nonce
|
||||||
const signature = await this.signProviderRequest(
|
const signature = await this.signProviderRequest(signer, signatureMessage)
|
||||||
web3,
|
|
||||||
consumerAddress,
|
|
||||||
signatureMessage
|
|
||||||
)
|
|
||||||
const payload = Object()
|
const payload = Object()
|
||||||
payload.consumerAddress = consumerAddress
|
payload.consumerAddress = consumerAddress
|
||||||
payload.signature = signature
|
payload.signature = signature
|
||||||
@ -487,12 +489,13 @@ export class Provider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this as well
|
||||||
/** Instruct the provider to Stop the execution of a to stop a compute job.
|
/** Instruct the provider to Stop the execution of a to stop a compute job.
|
||||||
* @param {string} did
|
* @param {string} did
|
||||||
* @param {string} consumerAddress
|
* @param {string} consumerAddress
|
||||||
* @param {string} jobId
|
* @param {string} jobId
|
||||||
* @param {string} providerUri
|
* @param {string} providerUri
|
||||||
* @param {Web3} web3
|
* @param {Signer} signer
|
||||||
* @param {AbortSignal} signal abort signal
|
* @param {AbortSignal} signal abort signal
|
||||||
* @return {Promise<ComputeJob | ComputeJob[]>}
|
* @return {Promise<ComputeJob | ComputeJob[]>}
|
||||||
*/
|
*/
|
||||||
@ -501,7 +504,7 @@ export class Provider {
|
|||||||
consumerAddress: string,
|
consumerAddress: string,
|
||||||
jobId: string,
|
jobId: string,
|
||||||
providerUri: string,
|
providerUri: string,
|
||||||
web3: Web3,
|
signer: Signer,
|
||||||
signal?: AbortSignal
|
signal?: AbortSignal
|
||||||
): Promise<ComputeJob | ComputeJob[]> {
|
): Promise<ComputeJob | ComputeJob[]> {
|
||||||
const providerEndpoints = await this.getEndpoints(providerUri)
|
const providerEndpoints = await this.getEndpoints(providerUri)
|
||||||
@ -525,11 +528,7 @@ export class Provider {
|
|||||||
signatureMessage += jobId || ''
|
signatureMessage += jobId || ''
|
||||||
signatureMessage += (did && `${this.noZeroX(did)}`) || ''
|
signatureMessage += (did && `${this.noZeroX(did)}`) || ''
|
||||||
signatureMessage += nonce
|
signatureMessage += nonce
|
||||||
const signature = await this.signProviderRequest(
|
const signature = await this.signProviderRequest(signer, signatureMessage)
|
||||||
web3,
|
|
||||||
consumerAddress,
|
|
||||||
signatureMessage
|
|
||||||
)
|
|
||||||
const payload = Object()
|
const payload = Object()
|
||||||
payload.signature = signature
|
payload.signature = signature
|
||||||
payload.documentId = this.noZeroX(did)
|
payload.documentId = this.noZeroX(did)
|
||||||
@ -614,7 +613,7 @@ export class Provider {
|
|||||||
|
|
||||||
/** Get compute result url
|
/** Get compute result url
|
||||||
* @param {string} providerUri The URI of the provider we want to query
|
* @param {string} providerUri The URI of the provider we want to query
|
||||||
* @param {Web3} web3 Web3 instance
|
* @param {Signer} signer Web3 instance
|
||||||
* @param {string} consumerAddress The consumer ethereum address
|
* @param {string} consumerAddress The consumer ethereum address
|
||||||
* @param {string} jobId The ID of a compute job.
|
* @param {string} jobId The ID of a compute job.
|
||||||
* @param {number} index Result index
|
* @param {number} index Result index
|
||||||
@ -622,7 +621,7 @@ export class Provider {
|
|||||||
*/
|
*/
|
||||||
public async getComputeResultUrl(
|
public async getComputeResultUrl(
|
||||||
providerUri: string,
|
providerUri: string,
|
||||||
web3: Web3,
|
signer: Signer,
|
||||||
consumerAddress: string,
|
consumerAddress: string,
|
||||||
jobId: string,
|
jobId: string,
|
||||||
index: number
|
index: number
|
||||||
@ -641,11 +640,7 @@ export class Provider {
|
|||||||
signatureMessage += jobId
|
signatureMessage += jobId
|
||||||
signatureMessage += index.toString()
|
signatureMessage += index.toString()
|
||||||
signatureMessage += nonce
|
signatureMessage += nonce
|
||||||
const signature = await this.signProviderRequest(
|
const signature = await this.signProviderRequest(signer, signatureMessage)
|
||||||
web3,
|
|
||||||
consumerAddress,
|
|
||||||
signatureMessage
|
|
||||||
)
|
|
||||||
if (!computeResultUrl) return null
|
if (!computeResultUrl) return null
|
||||||
let resultUrl = computeResultUrl
|
let resultUrl = computeResultUrl
|
||||||
resultUrl += `?consumerAddress=${consumerAddress}`
|
resultUrl += `?consumerAddress=${consumerAddress}`
|
||||||
@ -656,6 +651,7 @@ export class Provider {
|
|||||||
return resultUrl
|
return resultUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// needs update
|
||||||
/** Deletes a compute job.
|
/** Deletes a compute job.
|
||||||
* @param {string} did
|
* @param {string} did
|
||||||
* @param {string} consumerAddress
|
* @param {string} consumerAddress
|
||||||
@ -670,7 +666,7 @@ export class Provider {
|
|||||||
consumerAddress: string,
|
consumerAddress: string,
|
||||||
jobId: string,
|
jobId: string,
|
||||||
providerUri: string,
|
providerUri: string,
|
||||||
web3: Web3,
|
signer: Signer,
|
||||||
signal?: AbortSignal
|
signal?: AbortSignal
|
||||||
): Promise<ComputeJob | ComputeJob[]> {
|
): Promise<ComputeJob | ComputeJob[]> {
|
||||||
const providerEndpoints = await this.getEndpoints(providerUri)
|
const providerEndpoints = await this.getEndpoints(providerUri)
|
||||||
@ -694,11 +690,7 @@ export class Provider {
|
|||||||
signatureMessage += jobId || ''
|
signatureMessage += jobId || ''
|
||||||
signatureMessage += (did && `${this.noZeroX(did)}`) || ''
|
signatureMessage += (did && `${this.noZeroX(did)}`) || ''
|
||||||
signatureMessage += nonce
|
signatureMessage += nonce
|
||||||
const signature = await this.signProviderRequest(
|
const signature = await this.signProviderRequest(signer, signatureMessage)
|
||||||
web3,
|
|
||||||
consumerAddress,
|
|
||||||
signatureMessage
|
|
||||||
)
|
|
||||||
const payload = Object()
|
const payload = Object()
|
||||||
payload.documentId = this.noZeroX(did)
|
payload.documentId = this.noZeroX(did)
|
||||||
payload.consumerAddress = consumerAddress
|
payload.consumerAddress = consumerAddress
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
import { assert } from 'chai'
|
import { assert } from 'chai'
|
||||||
import { web3, getTestConfig } from '../config'
|
import { getTestConfig, provider } from '../config'
|
||||||
import { Config, Provider } from '../../src'
|
import { Config, Provider } from '../../src'
|
||||||
|
import { ethers, Signer, providers } from 'ethers'
|
||||||
import { FileInfo } from '../../src/@types'
|
import { FileInfo } from '../../src/@types'
|
||||||
|
|
||||||
describe('Provider tests', async () => {
|
describe('Provider tests', async () => {
|
||||||
let config: Config
|
let config: Config
|
||||||
|
let signer: Signer
|
||||||
let providerInstance: Provider
|
let providerInstance: Provider
|
||||||
|
|
||||||
before(async () => {
|
before(async () => {
|
||||||
config = await getTestConfig(web3)
|
signer = (await provider.getSigner(0)) as Signer
|
||||||
|
config = await getTestConfig(signer)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Initialize Ocean', async () => {
|
it('Initialize Ocean', async () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user