From cca7b269f039335d507e61cbf00bfbdcf963aebc Mon Sep 17 00:00:00 2001 From: Bill Barman Date: Thu, 22 Nov 2018 16:35:37 +0800 Subject: [PATCH] move to utils folder and cleanup --- src/DIDResolver.ts | 49 ------------------ src/models/DIDRecord.ts | 9 ++++ src/utils/DIDResolved.ts | 48 ++++++++++++++++++ src/utils/DIDResolver.ts | 88 +++++++++++++++++++++++++++++++++ test/keeper/DIDResolver.test.ts | 20 ++++---- 5 files changed, 154 insertions(+), 60 deletions(-) delete mode 100644 src/DIDResolver.ts create mode 100644 src/models/DIDRecord.ts create mode 100644 src/utils/DIDResolved.ts create mode 100644 src/utils/DIDResolver.ts diff --git a/src/DIDResolver.ts b/src/DIDResolver.ts deleted file mode 100644 index 4011b4d..0000000 --- a/src/DIDResolver.ts +++ /dev/null @@ -1,49 +0,0 @@ -/* - -DIDResolver module to resolve Ocean DID's off the block chain - -*/ - -import DIDRegistry from "./keeper/contracts/DIDRegistry" -// import Web3Provider from "./keeper/Web3Provider" -// import * as Web3 from "web3" - - -class DIDResolved { - public items: string[] - public value: string - - public constructor() { - this.items = [] - } - - public addData(data: string, value: string) { - this.items.push(data) - } -} - -/* - * - * Resolve a DID to an URL/DDO or later an internal/extrenal DID - * :param did: 32 byte value or DID string to resolver, this is part of the ocean DID did:op:<32 byte value> - * :param max_hop_count: max number of hops allowed to find the destination URL/DDO - * :return DIDResolved object: URL or DDO of the resolved DID - * :return null: if the DID cannot be resolved - * :raises TypeError: on non 32byte value as the DID - * :raises TypeError: on any of the resolved values are not string/DID bytes. - * :raises OceanDIDCircularReference: on the chain being pointed back to itself. - * :raises OceanDIDNotFound: if no DID can be found to resolve. - * - */ -export default class DIDResolver { - - public didRegistry - public constructor(didRegistry: DIDRegistry) { - this.didRegistry = didRegistry - } - - public resolve(did: string, maxHopCount?: number): DIDResolved { - - return new DIDResolved() - } -} diff --git a/src/models/DIDRecord.ts b/src/models/DIDRecord.ts new file mode 100644 index 0000000..6b61e76 --- /dev/null +++ b/src/models/DIDRecord.ts @@ -0,0 +1,9 @@ + +export default class DIDRecord { + public didId: string + public blockNumber: number + public owner: string + public valueType: ValueType + public key: string + public value: string +} diff --git a/src/utils/DIDResolved.ts b/src/utils/DIDResolved.ts new file mode 100644 index 0000000..2ac3006 --- /dev/null +++ b/src/utils/DIDResolved.ts @@ -0,0 +1,48 @@ +/* + +DIDResolver module to resolve Ocean DID's off the block chain + +*/ + +// import DIDRegistry from "../keeper/contracts/DIDRegistry" +import DIDRecord from "../models/DIDRecord" +// import ValueType from "../models/ValueType" + +// import * as Web3 from "web3" +import * as DIDTools from "../DID" + +export default class DIDResolved { + public items: DIDRecord[] + public value: string + + public constructor() { + this.items = [] + } + + public addData(data: DIDRecord) { + this.items.push(data) + } + public hopCount(): number { + return this.items.length + } + public getLastItem(): DIDRecord { + let result: DIDRecord = null + if ( this.items.length > 0 ) { + result = this.items[this.items.length - 1] + } + return result + } + + public getValue(): string { + const item = this.getLastItem() + let result: string = null + if ( item ) { + if ( item.valueType === "DID" ) { + result = DIDTools.idToDID(item.value) + } else { + result = item.value + } + } + return result + } +} diff --git a/src/utils/DIDResolver.ts b/src/utils/DIDResolver.ts new file mode 100644 index 0000000..f1b0dc9 --- /dev/null +++ b/src/utils/DIDResolver.ts @@ -0,0 +1,88 @@ +/* + +DIDResolver module to resolve Ocean DID's off the block chain + +*/ +import {assert} from "chai" +import DIDRecord from "../models/DIDRecord" +import DIDResolved from "./DIDResolved" + +import DIDRegistry from "../keeper/contracts/DIDRegistry" +import ValueType from "../models/ValueType" + +import * as Web3 from "web3" +import * as DIDTools from "../DID" + +/* + * + * Resolve a DID to an URL/DDO or later an internal/extrenal DID + * :param did: 32 byte value or DID string to resolver, this is part of the ocean DID did:op:<32 byte value> + * :param max_hop_count: max number of hops allowed to find the destination URL/DDO + * :return DIDResolved object: URL or DDO of the resolved DID + * :return null: if the DID cannot be resolved + * :raises TypeError: on non 32byte value as the DID + * :raises TypeError: on any of the resolved values are not string/DID bytes. + * :raises OceanDIDCircularReference: on the chain being pointed back to itself. + * :raises OceanDIDNotFound: if no DID can be found to resolve. + * + */ +export default class DIDResolver { + + public didRegistry + public constructor(didRegistry: DIDRegistry) { + this.didRegistry = didRegistry + } + + public async resolve(did: string, maxHopCount?: number): Promise { + + maxHopCount = maxHopCount === undefined ? 0 : maxHopCount + + let didId = DIDTools.DIDToId(did) + const resolved = new DIDResolved() + let data: DIDRecord = await this.getDID(didId) + while ( data && (maxHopCount === 0 || resolved.hopCount() < maxHopCount) ) { + resolved.addData(data) + if (data.valueType === "URL" || data.valueType === "DDO" ) { + data = null + break + } else { + didId = Web3.utils.toHex(data.value).substring(2) + data = await this.getDID(didId) + } + } + return resolved + } + + public async getDID(didId): Promise { + + let record: DIDRecord = null + + const blockNumber: number = await this.didRegistry.getUpdateAt(didId) + const filterOwner: string = await this.didRegistry.getOwner(didId) + assert(blockNumber > 0 ) + + // filter on the blockNumber only + const filterOptions = { + fromBlock: blockNumber, + toBlock: blockNumber, + filter: { + did: Web3.utils.toHex("0x" + didId), + owner: filterOwner, + }, + } + const events = await this.didRegistry.getEventData("DIDAttributeRegistered", filterOptions) + if ( events && events.length > 0 ) { + const event = events[events.length - 1] + record = { + didId: event.returnValues.did, + blockNumber: event.returnValues.updateAt, + owner: event.returnValues.owner, + valueType: ValueType[event.returnValues.valueType], + key: event.returnValues.key, + value: event.returnValues.value, + + } as DIDRecord + } + return record + } +} diff --git a/test/keeper/DIDResolver.test.ts b/test/keeper/DIDResolver.test.ts index 6dd0a90..e4f13dd 100644 --- a/test/keeper/DIDResolver.test.ts +++ b/test/keeper/DIDResolver.test.ts @@ -7,16 +7,16 @@ import ValueType from "../../src/models/ValueType" import Account from "../../src/ocean/Account" import IdGenerator from "../../src/ocean/IdGenerator" import Ocean from "../../src/ocean/Ocean" -//import Logger from "../src/utils/Logger" import config from "../config" import TestContractHandler from "./TestContractHandler" -import DIDResolver from "../../src/DIDResolver" + import * as DIDTools from "../../src/DID" +import DIDResolver from "../../src/utils/DIDResolver" + let ocean: Ocean let didRegistry: DIDRegistry - describe("DIDResolver", () => { before(async () => { @@ -30,24 +30,22 @@ describe("DIDResolver", () => { it("should register an attribute as a new DID and resolve", async () => { const testId = IdGenerator.generateId() - + const did = DIDTools.idToDID(testId) const didId = DIDTools.DIDToId(did) const ownerAccount: Account = (await ocean.getAccounts())[0] const providerKey = Web3Provider.getWeb3().utils.fromAscii("provider") - const data = "my nice provider, is nice" - const receipt = await didRegistry.registerAttribute(didId, ValueType.DID, providerKey, - data, ownerAccount.getId()) + const testURL = "http://localhost:5000" + const receipt = await didRegistry.registerAttribute(didId, ValueType.URL, providerKey, + testURL, ownerAccount.getId()) assert(receipt.status) assert(receipt.events.DIDAttributeRegistered) const didResolver = new DIDResolver(didRegistry) - didResolver.resolve(did) + const didResolved = await didResolver.resolve(did) }) - + }) - }) -