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

add docs and fix lint

This commit is contained in:
Bill Barman 2018-11-23 09:54:37 +08:00
parent fa5c8ac260
commit 6776088be3
2 changed files with 75 additions and 12 deletions

View File

@ -4,11 +4,7 @@ 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 "../utils/DIDTools"
export default class DIDResolved {
@ -18,13 +14,27 @@ export default class DIDResolved {
public constructor() {
this.items = []
}
public addData(data: DIDRecord) {
/*
* Add the DIDRecord to the collection of visited DID's
*
* :param data: DIDRecord to add
*/
public addData(data: DIDRecord): void {
this.items.push(data)
}
/*
* Number of hops performed
* :return hop count
*/
public hopCount(): number {
return this.items.length
}
/* Get the last DID record added by the resolver
*
* :return DIDRecord of the last resolved item
*/
public getLastItem(): DIDRecord {
let result: DIDRecord = null
if ( this.items.length > 0 ) {
@ -33,21 +43,67 @@ export default class DIDResolved {
return result
}
/*
* :return true if the resolved result is a URL
*/
public isURL(): boolean {
const item = this.getLastItem()
return item && item.valueType === "URL"
}
/*
* :return true if the resolved result is an on chain DDO
*/
public isDDO(): boolean {
const item = this.getLastItem()
return item && item.valueType === "DDO"
}
/*
* :return true if the resolved result is another DID
*/
public isDID(): boolean {
const item = this.getLastItem()
return item && (item.valueType === "DID" || item.valueType === "DIDRef")
}
/*
* :return the stored key value for this DID resolved record
*/
public getKey(): string {
const item = this.getLastItem()
if ( item ) {
return item.key
}
return null
}
/*
* :return the owner of the resolved record
*/
public getOwner(): string {
const item = this.getLastItem()
if ( item ) {
return item.owner
}
return null
}
/*
* :return the ValueType ( URL, DDO, DID, DIDRef )
*/
public getValueType(): ValueType {
const item = this.getLastItem()
if ( item ) {
return item.valueType
}
return null
}
/*
* :return the value of the resolved record, if it's a DID
* this call will return an ocean DID instead of in internal Id.
*/
public getValue(): string {
const item = this.getLastItem()
let result: string = null
@ -60,9 +116,17 @@ export default class DIDResolved {
}
return result
}
/*
* Used by the resolver to check to see if the DID record has been
* visited before, to stop circular links.
*
* :param didId: 32 byte id that has been visited
*
* :return true if this 32 byte value is already in the list of DID records
*/
public isDIDIdVisited(didId: string): boolean {
for ( let item of this.items) {
for ( const item of this.items) {
if ( item.didId === didId ) {
return true
}

View File

@ -30,7 +30,7 @@ export default class DIDResolver {
}
public async resolve(did: string, maxHopCount?: number): Promise<DIDResolved> {
maxHopCount = maxHopCount ? maxHopCount : 0
let didId = DIDTools.didToId(did)
@ -45,7 +45,7 @@ export default class DIDResolver {
break
}
resolved.addData(data)
didId = null
if (data.valueType === "URL" || data.valueType === "DDO" ) {
data = null
@ -64,8 +64,7 @@ export default class DIDResolver {
// only look if we have another id to find
if ( didId ) {
data = await this.getDID(didId)
}
else {
} else {
data = null
}
}