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

add initialize to provider

This commit is contained in:
Ahmed Ali 2020-06-24 18:50:34 +02:00
parent 9be591a067
commit ece08c6963
2 changed files with 62 additions and 36 deletions

View File

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

View File

@ -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<string> {
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<string> {
public async encrypt(did: string, document: any, account: Account): Promise<string> {
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<any> {
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`
}
}