diff --git a/src/brizo/Brizo.ts b/src/brizo/Brizo.ts index f093ce5..b77dda9 100644 --- a/src/brizo/Brizo.ts +++ b/src/brizo/Brizo.ts @@ -2,6 +2,8 @@ import { File, MetaData } from '../ddo/MetaData' import Account from '../ocean/Account' import { noZeroX } from '../utils' import { Instantiable, InstantiableConfig } from '../Instantiable.abstract' +import { DDO } from '../ddo/DDO' +import { ServiceType } from '../ddo/Service' const apiPath = '/api/v1/brizo/services' @@ -39,6 +41,18 @@ export class Brizo extends Instantiable { return `${this.url}${apiPath}/compute` } + public async getEndpointFromAgreement( + type: ServiceType, + agreementId: string + ): Promise { + const { assets, keeper } = this.ocean + const { did } = await keeper.agreementStoreManager.getAgreement(agreementId) + const ddo: DDO = await assets.resolve(did) + const { serviceEndpoint } = ddo.findServiceByType(type) + + return serviceEndpoint + } + public async initializeServiceAgreement( did: string, serviceAgreementId: string, @@ -105,9 +119,19 @@ export class Brizo extends Instantiable { ): Promise { const signature = await this.createSignature(consumerAccount, serviceAgreementId) const address = consumerAccount.getId() + const serviceEndpoint = await this.getEndpointFromAgreement( + 'compute', + serviceAgreementId + ) + + if (!serviceEndpoint) { + throw new Error( + 'Computing on asset failed, service definition is missing the `serviceEndpoint`.' + ) + } // construct Brizo URL - let url = this.getComputeEndpoint() + let url = serviceEndpoint url += `&signature=${signature}` url += `&consumerAddress=${address}` url += `&serviceAgreementId=${noZeroX(serviceAgreementId)}`