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

Update provider nonce handling (#1747)

* Updates the usage of nonce

* nonce method updates

* fix lint

* remove double resolve for the response

* use provider invalid-signature image and comm update docs

* print errro

* set a default value if nonce received is null

* fix issue with consumer address on download

* remove parse int

* skip compute tests

* Updating CodeExamples.md

* put back compute tests

* prepare for release

* use older contracts

---------

Co-authored-by: GitHub Actions Bot <>
This commit is contained in:
Bogdan Fazakas 2023-07-31 13:21:05 +03:00 committed by GitHub
parent ff5fcd7f22
commit 808178291d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 27 deletions

View File

@ -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

View File

@ -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')
}

View File

@ -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 || ''

View File

@ -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')
}
}) ///

View File

@ -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')
})
})