From ece08c6963d6b47986f687b1d31fb74b420b72c4 Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Wed, 24 Jun 2020 18:50:34 +0200 Subject: [PATCH] add initialize to provider --- src/ocean/Assets.ts | 3 +- src/provider/Provider.ts | 95 ++++++++++++++++++++++++++-------------- 2 files changed, 62 insertions(+), 36 deletions(-) diff --git a/src/ocean/Assets.ts b/src/ocean/Assets.ts index 827d6b14..43717d8f 100644 --- a/src/ocean/Assets.ts +++ b/src/ocean/Assets.ts @@ -90,8 +90,7 @@ export class Assets extends Instantiable { const encryptedFiles = await this.ocean.provider.encrypt( did.getId(), metadata.main.files, - publisher, - dtAddress + publisher ) this.logger.log('Files encrypted') observer.next(CreateProgressStep.FilesEncrypted) diff --git a/src/provider/Provider.ts b/src/provider/Provider.ts index 784b4dd3..36fb9db3 100644 --- a/src/provider/Provider.ts +++ b/src/provider/Provider.ts @@ -1,6 +1,7 @@ import Account from '../ocean/Account' import { noZeroX } from '../utils' import { Instantiable, InstantiableConfig } from '../Instantiable.abstract' +import { DDO } from '../ddo/DDO' const apiPath = '/api/v1/services/' @@ -20,34 +21,6 @@ export class Provider extends Instantiable { this.setInstanceConfig(config) } - public async getVersionInfo() { - return (await this.ocean.utils.fetch.get(this.url)).json() - } - - public getURI() { - return `${this.url}` - } - - public getPurchaseEndpoint() { - return `${this.url}${apiPath}/access/initialize` - } - - public getConsumeEndpoint() { - return `${this.url}${apiPath}/consume` - } - - public getEncryptEndpoint() { - return `${this.url}${apiPath}/encrypt` - } - - public getPublishEndpoint() { - return `${this.url}${apiPath}/publish` - } - - public getComputeEndpoint() { - return `${this.url}${apiPath}/compute` - } - public async createSignature(account: Account, agreementId: string): Promise { const signature = await this.ocean.utils.signature.signText( noZeroX(agreementId), @@ -66,12 +39,7 @@ export class Provider extends Instantiable { return signature } - public async encrypt( - did: string, - document: any, - account: Account, - dtAddress: string - ): Promise { + public async encrypt(did: string, document: any, account: Account): Promise { const signature = this.ocean.utils.signature.signWithHash( did, account.getId(), @@ -99,4 +67,63 @@ export class Provider extends Instantiable { throw new Error('HTTP request failed') } } + + public async initialize( + did: string, + serviceIndex: number, + serviceType: string, + consumerAddress: string + ): Promise { + const DDO = await this.ocean.assets.resolve(did) + const { dtAddress } = DDO + const args = { + did, + dtAddress, + serviceIndex, + serviceType, + consumerAddress + } + + try { + return await this.ocean.utils.fetch.post( + this.getInitializeEndpoint(), + decodeURI(JSON.stringify(args)) + ) + } catch (e) { + this.logger.error(e) + throw new Error('HTTP request failed') + } + } + + public async getVersionInfo() { + return (await this.ocean.utils.fetch.get(this.url)).json() + } + + public getURI() { + return `${this.url}` + } + + public getInitializeEndpoint() { + return `${this.url}${apiPath}/initialize` + } + + public getConsumeEndpoint() { + return `${this.url}${apiPath}/consume` + } + + public getEncryptEndpoint() { + return `${this.url}${apiPath}/encrypt` + } + + public getPublishEndpoint() { + return `${this.url}${apiPath}/publish` + } + + public getComputeEndpoint() { + return `${this.url}${apiPath}/compute` + } + + public getDownloadEndpoint() { + return `${this.url}${apiPath}/download` + } }