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

Merge pull request #194 from oceanprotocol/feature/provider_nonce

add nonce to provider signature
This commit is contained in:
Ahmed 2020-07-30 10:09:02 +02:00 committed by GitHub
commit 98d0f49594
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,6 +15,7 @@ const apiPath = '/api/v1/services'
* data services. * data services.
*/ */
export class Provider extends Instantiable { export class Provider extends Instantiable {
public nonce: string
private get url() { private get url() {
return this.config.providerUri return this.config.providerUri
} }
@ -22,6 +23,7 @@ export class Provider extends Instantiable {
constructor(config: InstantiableConfig) { constructor(config: InstantiableConfig) {
super() super()
this.setInstanceConfig(config) this.setInstanceConfig(config)
this.nonce = '0'
} }
public async createSignature(account: Account, agreementId: string): Promise<string> { public async createSignature(account: Account, agreementId: string): Promise<string> {
@ -43,8 +45,9 @@ export class Provider extends Instantiable {
} }
public async encrypt(did: string, document: any, account: Account): Promise<string> { public async encrypt(did: string, document: any, account: Account): Promise<string> {
await this.getNonce(account.getId())
const signature = await this.ocean.utils.signature.signWithHash( const signature = await this.ocean.utils.signature.signWithHash(
did, did + this.nonce,
account.getId(), account.getId(),
account.getPassword() account.getPassword()
) )
@ -67,6 +70,23 @@ export class Provider extends Instantiable {
} }
} }
/** Get nonce from provider
* @param {String} consumerAddress
* @return {Promise<string>} string
*/
public async getNonce(consumerAddress: string): Promise<string> {
let initializeUrl = this.getNonceEndpoint()
initializeUrl += `?userAddress=${consumerAddress}`
try {
const response = await this.ocean.utils.fetch.get(initializeUrl)
this.nonce = String((await response.json()).nonce)
return this.nonce
} catch (e) {
this.logger.error(e)
throw new Error('HTTP request failed')
}
}
public async initialize( public async initialize(
did: string, did: string,
serviceIndex: number, serviceIndex: number,
@ -107,7 +127,8 @@ export class Provider extends Instantiable {
files: File[], files: File[],
index: number = -1 index: number = -1
): Promise<any> { ): Promise<any> {
const signature = await this.createSignature(account, did) await this.getNonce(account.getId())
const signature = await this.createSignature(account, did + this.nonce)
const filesPromises = files const filesPromises = files
.filter((_, i) => index === -1 || i === index) .filter((_, i) => index === -1 || i === index)
.map(async ({ index: i }) => { .map(async ({ index: i }) => {
@ -149,9 +170,11 @@ export class Provider extends Instantiable {
algorithmDataToken?: string algorithmDataToken?: string
): Promise<ComputeJob | ComputeJob[]> { ): Promise<ComputeJob | ComputeJob[]> {
const address = consumerAccount.getId() const address = consumerAccount.getId()
await this.getNonce(consumerAccount.getId())
let signatureMessage = address let signatureMessage = address
signatureMessage += jobId || '' signatureMessage += jobId || ''
signatureMessage += (did && `${noZeroX(did)}`) || '' signatureMessage += (did && `${noZeroX(did)}`) || ''
signatureMessage += this.nonce
const signature = await this.createHashSignature( const signature = await this.createHashSignature(
consumerAccount, consumerAccount,
signatureMessage signatureMessage
@ -244,6 +267,10 @@ export class Provider extends Instantiable {
return `${this.url}${apiPath}/initialize` return `${this.url}${apiPath}/initialize`
} }
public getNonceEndpoint() {
return `${this.url}${apiPath}/nonce`
}
public getConsumeEndpoint() { public getConsumeEndpoint() {
return `${this.url}${apiPath}/consume` return `${this.url}${apiPath}/consume`
} }