mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
add circular checks in resolver
This commit is contained in:
parent
757e6884c3
commit
c808b1dd4a
@ -43,6 +43,11 @@ export default class DIDResolved {
|
|||||||
return item && item.valueType === "DDO"
|
return item && item.valueType === "DDO"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public isDID(): boolean {
|
||||||
|
const item = this.getLastItem()
|
||||||
|
return item && (item.valueType === "DID" || item.valueType === "DIDRef")
|
||||||
|
}
|
||||||
|
|
||||||
public getValue(): string {
|
public getValue(): string {
|
||||||
const item = this.getLastItem()
|
const item = this.getLastItem()
|
||||||
let result: string = null
|
let result: string = null
|
||||||
@ -55,4 +60,13 @@ export default class DIDResolved {
|
|||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public isDIDIdVisited(didId: string): boolean {
|
||||||
|
for ( let item of this.items) {
|
||||||
|
if ( item.didId === didId ) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,9 +35,17 @@ export default class DIDResolver {
|
|||||||
|
|
||||||
let didId = DIDTools.didToId(did)
|
let didId = DIDTools.didToId(did)
|
||||||
const resolved = new DIDResolved()
|
const resolved = new DIDResolved()
|
||||||
|
|
||||||
let data: DIDRecord = await this.getDID(didId)
|
let data: DIDRecord = await this.getDID(didId)
|
||||||
while ( data && (maxHopCount === 0 || resolved.hopCount() < maxHopCount) ) {
|
while ( data && (maxHopCount === 0 || resolved.hopCount() < maxHopCount) ) {
|
||||||
|
// need to search after the result
|
||||||
|
if ( resolved.isDIDIdVisited(data.didId) ) {
|
||||||
|
// error circular ref
|
||||||
|
// TODO: raise an error
|
||||||
|
break
|
||||||
|
}
|
||||||
resolved.addData(data)
|
resolved.addData(data)
|
||||||
|
|
||||||
didId = null
|
didId = null
|
||||||
if (data.valueType === "URL" || data.valueType === "DDO" ) {
|
if (data.valueType === "URL" || data.valueType === "DDO" ) {
|
||||||
data = null
|
data = null
|
||||||
@ -57,6 +65,9 @@ export default class DIDResolver {
|
|||||||
if ( didId ) {
|
if ( didId ) {
|
||||||
data = await this.getDID(didId)
|
data = await this.getDID(didId)
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
data = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return resolved
|
return resolved
|
||||||
|
@ -83,8 +83,21 @@ describe("DIDResolver", () => {
|
|||||||
assert(didResolved.isURL())
|
assert(didResolved.isURL())
|
||||||
assert(didResolved.getValue() === testURL)
|
assert(didResolved.getValue() === testURL)
|
||||||
|
|
||||||
|
// test circular link
|
||||||
|
const didIdFirst = DIDTools.didToId(didList[0])
|
||||||
|
|
||||||
|
const receiptLastCircular = await didRegistry.registerAttribute(didIdLast, ValueType.DID, providerKey,
|
||||||
|
didIdFirst, ownerAccount.getId())
|
||||||
|
assert(receiptLastCircular)
|
||||||
|
|
||||||
|
// resolve from the first DID in the chain
|
||||||
|
const didResolvedCircular = await didResolver.resolve(didList[0])
|
||||||
|
assert(didResolvedCircular)
|
||||||
|
assert(didResolvedCircular.isDID())
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user