diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 04e431ca..9733957c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -61,7 +61,7 @@ jobs: run: | bash -x start_ocean.sh --no-aquarius --no-elasticsearch --no-provider --no-dashboard 2>&1 > start_ocean.log & env: - CONTRACTS_VERSION: v1.1.4 + CONTRACTS_VERSION: v1.1.8 - run: npm ci - name: Wait for contracts deployment working-directory: ${{ github.workspace }}/barge @@ -119,7 +119,7 @@ jobs: run: | bash -x start_ocean.sh --with-provider2 --no-dashboard --with-c2d 2>&1 > start_ocean.log & env: - CONTRACTS_VERSION: v1.1.3 + CONTRACTS_VERSION: v1.1.8 - run: npm ci - run: npm run build:metadata diff --git a/CodeExamples.md b/CodeExamples.md index 32940c54..c8912bb8 100644 --- a/CodeExamples.md +++ b/CodeExamples.md @@ -108,7 +108,8 @@ import { amountToUnits, ValidateMetadata, getEventFromTx, - DDO + DDO, + LoggerInstance } from '@oceanprotocol/lib' ``` @@ -564,6 +565,7 @@ Lets check that the download URL was successfully received const fileData = await downloadFile(downloadURL) console.log(fileData) } catch (e) { + LoggerInstance.error('Download failed', e) assert.fail('Download failed') } diff --git a/src/services/Provider.ts b/src/services/Provider.ts index 764d6319..a1591f1b 100644 --- a/src/services/Provider.ts +++ b/src/services/Provider.ts @@ -100,10 +100,13 @@ export class Provider { headers: { 'Content-Type': 'application/json' }, signal }) - return (await response.json()).nonce.toString() + const { nonce } = await response.json() + console.log(`[getNonce] Consumer: ${consumerAddress} nonce: ${nonce}`) + const sanitizedNonce = !nonce || nonce === null ? '0' : nonce + return sanitizedNonce } catch (e) { LoggerInstance.error(e) - throw new Error('HTTP request failed calling Provider') + throw new Error(e.message) } } @@ -487,14 +490,24 @@ export class Provider { ? this.getEndpointURL(serviceEndpoints, 'download').urlPath : null if (!downloadUrl) return null - const nonce = Date.now() + const consumerAddress = await signer.getAddress() + const nonce = ( + (await this.getNonce( + providerUri, + consumerAddress, + null, + providerEndpoints, + serviceEndpoints + )) + 1 + ).toString() + const signature = await this.signProviderRequest(signer, did + nonce) let consumeUrl = downloadUrl consumeUrl += `?fileIndex=${fileIndex}` consumeUrl += `&documentId=${did}` consumeUrl += `&transferTxId=${transferTxId}` consumeUrl += `&serviceId=${serviceId}` - consumeUrl += `&consumerAddress=${await signer.getAddress()}` + consumeUrl += `&consumerAddress=${consumerAddress}` consumeUrl += `&nonce=${nonce}` consumeUrl += `&signature=${signature}` if (userCustomParameters) @@ -532,13 +545,23 @@ export class Provider { ? this.getEndpointURL(serviceEndpoints, 'computeStart').urlPath : null - const nonce = Date.now() - let signatureMessage = await consumer.getAddress() + const consumerAddress = await consumer.getAddress() + const nonce = ( + (await this.getNonce( + providerUri, + consumerAddress, + signal, + providerEndpoints, + serviceEndpoints + )) + 1 + ).toString() + + let signatureMessage = consumerAddress signatureMessage += dataset.documentId signatureMessage += nonce const signature = await this.signProviderRequest(consumer, signatureMessage) const payload = Object() - payload.consumerAddress = await consumer.getAddress() + payload.consumerAddress = consumerAddress payload.signature = signature payload.nonce = nonce payload.environment = computeEnv @@ -601,13 +624,15 @@ export class Provider { ? this.getEndpointURL(serviceEndpoints, 'computeStop').urlPath : null - const nonce = await this.getNonce( - providerUri, - consumerAddress, - signal, - providerEndpoints, - serviceEndpoints - ) + const nonce = ( + (await this.getNonce( + providerUri, + consumerAddress, + signal, + providerEndpoints, + serviceEndpoints + )) + 1 + ).toString() let signatureMessage = consumerAddress signatureMessage += jobId || '' @@ -737,7 +762,15 @@ export class Provider { ? this.getEndpointURL(serviceEndpoints, 'computeResult').urlPath : null - const nonce = Date.now() + const nonce = ( + (await this.getNonce( + providerUri, + await consumer.getAddress(), + null, + providerEndpoints, + serviceEndpoints + )) + 1 + ).toString() let signatureMessage = await consumer.getAddress() signatureMessage += jobId signatureMessage += index.toString() @@ -777,13 +810,15 @@ export class Provider { ? this.getEndpointURL(serviceEndpoints, 'computeDelete').urlPath : null - const nonce = await this.getNonce( - providerUri, - await consumer.getAddress(), - signal, - providerEndpoints, - serviceEndpoints - ) + const nonce = ( + (await this.getNonce( + providerUri, + await consumer.getAddress(), + signal, + providerEndpoints, + serviceEndpoints + )) + 1 + ).toString() let signatureMessage = await consumer.getAddress() signatureMessage += jobId || '' diff --git a/test/integration/CodeExamples.test.ts b/test/integration/CodeExamples.test.ts index 341b3ea0..15979f4d 100644 --- a/test/integration/CodeExamples.test.ts +++ b/test/integration/CodeExamples.test.ts @@ -108,7 +108,8 @@ import { amountToUnits, ValidateMetadata, getEventFromTx, - DDO + DDO, + LoggerInstance } from '../../src' /// ``` @@ -564,6 +565,7 @@ describe('Marketplace flow tests', async () => { const fileData = await downloadFile(downloadURL) console.log(fileData) } catch (e) { + LoggerInstance.error('Download failed', e) assert.fail('Download failed') } }) /// diff --git a/test/integration/Provider.test.ts b/test/integration/Provider.test.ts index b395e0ff..2aa69f2a 100644 --- a/test/integration/Provider.test.ts +++ b/test/integration/Provider.test.ts @@ -1,7 +1,7 @@ import { assert } from 'chai' import { getTestConfig, provider } from '../config' import { Config, Provider } from '../../src' -import { ethers, Signer } from 'ethers' +import { Signer } from 'ethers' import { FileInfo } from '../../src/@types' describe('Provider tests', async () => { @@ -55,4 +55,12 @@ describe('Provider tests', async () => { const computeEnvs = await providerInstance.getComputeEnvironments(config.providerUri) assert(computeEnvs, 'No Compute environments found') }) + + it('Alice tests getNonce', async () => { + const nonce = await providerInstance.getNonce( + config.providerUri, + '0xe2DD09d719Da89e5a3D0F2549c7E24566e947260' + ) + assert(nonce, 'could not get nonce for the sent address') + }) })