From a9ba23d2c3947a9376f212417626617caf39817a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Guti=C3=A9rrez?= Date: Tue, 16 Jul 2019 14:56:56 +0200 Subject: [PATCH] Resolve the DDO using the service endpoint on registration event. --- src/aquarius/Aquarius.ts | 15 ++++++++++++--- src/keeper/contracts/DIDRegistry.ts | 22 +++++++++++++++++++++- src/ocean/DID.ts | 5 ++++- src/ocean/OceanAssets.ts | 6 ++++-- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/aquarius/Aquarius.ts b/src/aquarius/Aquarius.ts index 03f06ac..81b315d 100644 --- a/src/aquarius/Aquarius.ts +++ b/src/aquarius/Aquarius.ts @@ -178,11 +178,16 @@ export class Aquarius extends Instantiable { /** * Retrieves a DDO by DID. - * @param {DID} did DID of the asset. + * @param {DID | string} did DID of the asset. * @return {Promise} DDO of the asset. */ - public async retrieveDDO(did: DID): Promise { - const fullUrl = `${this.url}${apiPath}/${did.getDid()}` + public async retrieveDDO( + did: DID | string, + metadataServiceEndpoint?: string + ): Promise { + did = did && DID.parse(did) + const fullUrl = + metadataServiceEndpoint || `${this.url}${apiPath}/${did.getDid()}` const result = await this.ocean.utils.fetch .get(fullUrl) .then((response: any) => { @@ -208,6 +213,10 @@ export class Aquarius extends Instantiable { return result } + public async retrieveDDOByUrl(metadataServiceEndpoint?: string) { + return this.retrieveDDO(undefined, metadataServiceEndpoint) + } + public getServiceEndpoint(did: DID) { return `${this.url}/api/v1/aquarius/assets/ddo/did:op:${did.getId()}` } diff --git a/src/keeper/contracts/DIDRegistry.ts b/src/keeper/contracts/DIDRegistry.ts index e3c9c91..abf441b 100644 --- a/src/keeper/contracts/DIDRegistry.ts +++ b/src/keeper/contracts/DIDRegistry.ts @@ -1,6 +1,6 @@ import Web3Provider from '../Web3Provider' import ContractBase from './ContractBase' -import { zeroX, didPrefixed } from '../../utils' +import { zeroX, didPrefixed, didZeroX } from '../../utils' import { InstantiableConfig } from '../../Instantiable.abstract' export default class DIDRegistry extends ContractBase { @@ -42,4 +42,24 @@ export default class DIDRegistry extends ContractBase { .map(({ returnValues }) => returnValues._did) .map(didPrefixed) } + + public async getAttributesByDid( + did: string + ): Promise<{ did: string; serviceEndpoint: string; checksum: string }> { + return (await this.getPastEvents('DIDAttributeRegistered', { + _did: didZeroX(did) + })).map( + ({ + returnValues: { + _did, + _checksum: checksum, + _value: serviceEndpoint + } + }) => ({ + did: didPrefixed(_did), + serviceEndpoint, + checksum + }) + )[0] + } } diff --git a/src/ocean/DID.ts b/src/ocean/DID.ts index 4aeba22..4bc41f3 100644 --- a/src/ocean/DID.ts +++ b/src/ocean/DID.ts @@ -11,7 +11,10 @@ export default class DID { * @param {string} didString DID in string. * @return {DID} */ - public static parse(didString: string): DID { + public static parse(didString: string | DID): DID { + if (didString instanceof DID) { + didString = didString.getDid() + } let did: DID const didMatch = didString.match(/^did:op:([a-f0-9]{64})$/i) diff --git a/src/ocean/OceanAssets.ts b/src/ocean/OceanAssets.ts index 0e92d26..d0b0457 100644 --- a/src/ocean/OceanAssets.ts +++ b/src/ocean/OceanAssets.ts @@ -53,8 +53,10 @@ export class OceanAssets extends Instantiable { * @return {Promise} */ public async resolve(did: string): Promise { - const d: DID = DID.parse(did) - return this.ocean.aquarius.retrieveDDO(d) + const { + serviceEndpoint + } = await this.ocean.keeper.didRegistry.getAttributesByDid(did) + return this.ocean.aquarius.retrieveDDOByUrl(serviceEndpoint) } /**