mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
move to utils folder and cleanup
This commit is contained in:
parent
c15d814d2f
commit
cca7b269f0
@ -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()
|
||||
}
|
||||
}
|
9
src/models/DIDRecord.ts
Normal file
9
src/models/DIDRecord.ts
Normal file
@ -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
|
||||
}
|
48
src/utils/DIDResolved.ts
Normal file
48
src/utils/DIDResolved.ts
Normal file
@ -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
|
||||
}
|
||||
}
|
88
src/utils/DIDResolver.ts
Normal file
88
src/utils/DIDResolver.ts
Normal file
@ -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<DIDResolved> {
|
||||
|
||||
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<DIDRecord> {
|
||||
|
||||
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
|
||||
}
|
||||
}
|
@ -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 () => {
|
||||
@ -35,19 +35,17 @@ describe("DIDResolver", () => {
|
||||
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)
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
|
||||
})
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user