mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
104 lines
4.0 KiB
TypeScript
104 lines
4.0 KiB
TypeScript
|
|
import {assert} from "chai"
|
|
import ConfigProvider from "../../src/ConfigProvider"
|
|
import DIDRegistry from "../../src/keeper/contracts/DIDRegistry"
|
|
import Web3Provider from "../../src/keeper/Web3Provider"
|
|
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 config from "../config"
|
|
import TestContractHandler from "./TestContractHandler"
|
|
|
|
import * as DIDTools from "../../src/utils/DIDTools"
|
|
|
|
import DIDResolver from "../../src/utils/DIDResolver"
|
|
|
|
let ocean: Ocean
|
|
let didRegistry: DIDRegistry
|
|
|
|
describe("DIDResolver", () => {
|
|
|
|
before(async () => {
|
|
ConfigProvider.setConfig(config)
|
|
await TestContractHandler.prepareContracts()
|
|
ocean = await Ocean.getInstance(config)
|
|
didRegistry = await DIDRegistry.getInstance()
|
|
})
|
|
|
|
describe("register and resolve", () => {
|
|
|
|
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 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)
|
|
assert(didResolver)
|
|
|
|
const didResolved = await didResolver.resolve(did)
|
|
assert(didResolved)
|
|
assert(didResolved.isURL())
|
|
assert(didResolved.getValue() === testURL)
|
|
|
|
})
|
|
it("should register chain of attributes and resolve", async () => {
|
|
const didList: string[] = []
|
|
const chainLength = 10
|
|
const testURL = "http://localhost:5000"
|
|
const ownerAccount: Account = (await ocean.getAccounts())[0]
|
|
const providerKey = Web3Provider.getWeb3().utils.fromAscii("provider")
|
|
|
|
for ( let index = 0; index < chainLength; index ++ ) {
|
|
const did = DIDTools.idToDID(IdGenerator.generateId())
|
|
didList.push(did)
|
|
}
|
|
|
|
for ( let index = 0; index < chainLength - 1; index ++ ) {
|
|
const didId = DIDTools.didToId(didList[index])
|
|
const nextDIDId = DIDTools.didToId(didList[index + 1])
|
|
const receipt = await didRegistry.registerAttribute(didId, ValueType.DID, providerKey,
|
|
nextDIDId, ownerAccount.getId())
|
|
assert(receipt)
|
|
}
|
|
const didIdLast = DIDTools.didToId(didList[didList.length - 1])
|
|
const receiptLast = await didRegistry.registerAttribute(didIdLast, ValueType.URL, providerKey,
|
|
testURL, ownerAccount.getId())
|
|
assert(receiptLast)
|
|
|
|
const didResolver = new DIDResolver(didRegistry)
|
|
assert(didResolver)
|
|
|
|
// resolve from the first DID in the chain
|
|
const didResolved = await didResolver.resolve(didList[0])
|
|
assert(didResolved)
|
|
assert(didResolved.isURL())
|
|
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())
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
})
|