1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

Resolve the DDO using the service endpoint on registration event.

This commit is contained in:
Pedro Gutiérrez 2019-07-16 14:56:56 +02:00
parent df0ced1f1a
commit a9ba23d2c3
4 changed files with 41 additions and 7 deletions

View File

@ -178,11 +178,16 @@ export class Aquarius extends Instantiable {
/** /**
* Retrieves a DDO by DID. * Retrieves a DDO by DID.
* @param {DID} did DID of the asset. * @param {DID | string} did DID of the asset.
* @return {Promise<DDO>} DDO of the asset. * @return {Promise<DDO>} DDO of the asset.
*/ */
public async retrieveDDO(did: DID): Promise<DDO> { public async retrieveDDO(
const fullUrl = `${this.url}${apiPath}/${did.getDid()}` did: DID | string,
metadataServiceEndpoint?: string
): Promise<DDO> {
did = did && DID.parse(did)
const fullUrl =
metadataServiceEndpoint || `${this.url}${apiPath}/${did.getDid()}`
const result = await this.ocean.utils.fetch const result = await this.ocean.utils.fetch
.get(fullUrl) .get(fullUrl)
.then((response: any) => { .then((response: any) => {
@ -208,6 +213,10 @@ export class Aquarius extends Instantiable {
return result return result
} }
public async retrieveDDOByUrl(metadataServiceEndpoint?: string) {
return this.retrieveDDO(undefined, metadataServiceEndpoint)
}
public getServiceEndpoint(did: DID) { public getServiceEndpoint(did: DID) {
return `${this.url}/api/v1/aquarius/assets/ddo/did:op:${did.getId()}` return `${this.url}/api/v1/aquarius/assets/ddo/did:op:${did.getId()}`
} }

View File

@ -1,6 +1,6 @@
import Web3Provider from '../Web3Provider' import Web3Provider from '../Web3Provider'
import ContractBase from './ContractBase' import ContractBase from './ContractBase'
import { zeroX, didPrefixed } from '../../utils' import { zeroX, didPrefixed, didZeroX } from '../../utils'
import { InstantiableConfig } from '../../Instantiable.abstract' import { InstantiableConfig } from '../../Instantiable.abstract'
export default class DIDRegistry extends ContractBase { export default class DIDRegistry extends ContractBase {
@ -42,4 +42,24 @@ export default class DIDRegistry extends ContractBase {
.map(({ returnValues }) => returnValues._did) .map(({ returnValues }) => returnValues._did)
.map(didPrefixed) .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]
}
} }

View File

@ -11,7 +11,10 @@ export default class DID {
* @param {string} didString DID in string. * @param {string} didString DID in string.
* @return {DID} * @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 let did: DID
const didMatch = didString.match(/^did:op:([a-f0-9]{64})$/i) const didMatch = didString.match(/^did:op:([a-f0-9]{64})$/i)

View File

@ -53,8 +53,10 @@ export class OceanAssets extends Instantiable {
* @return {Promise<DDO>} * @return {Promise<DDO>}
*/ */
public async resolve(did: string): Promise<DDO> { public async resolve(did: string): Promise<DDO> {
const d: DID = DID.parse(did) const {
return this.ocean.aquarius.retrieveDDO(d) serviceEndpoint
} = await this.ocean.keeper.didRegistry.getAttributesByDid(did)
return this.ocean.aquarius.retrieveDDOByUrl(serviceEndpoint)
} }
/** /**