2018-11-20 04:54:50 +01:00
|
|
|
import {assert} from "chai"
|
2018-11-23 05:09:56 +01:00
|
|
|
import ConfigProvider from "../../src/ConfigProvider"
|
2018-11-20 04:54:50 +01:00
|
|
|
import DDO from "../../src/libDDO/DDO"
|
2018-11-23 05:09:56 +01:00
|
|
|
import Account from "../../src/ocean/Account"
|
2018-11-23 01:53:39 +01:00
|
|
|
import IdGenerator from "../../src/ocean/IdGenerator"
|
2018-11-23 05:09:56 +01:00
|
|
|
import Ocean from "../../src/ocean/Ocean"
|
|
|
|
import config from "../config"
|
|
|
|
import TestContractHandler from "../keeper/TestContractHandler"
|
2018-11-20 04:54:50 +01:00
|
|
|
|
2018-11-23 05:09:56 +01:00
|
|
|
import PublicKey from "../../src/libDDO/PublicKey"
|
2018-11-21 01:35:41 +01:00
|
|
|
import * as jsonDDO from "../testdata/ddoSample1.json"
|
2018-11-20 04:54:50 +01:00
|
|
|
|
2018-11-23 05:09:56 +01:00
|
|
|
let ocean: Ocean
|
|
|
|
|
2018-11-20 06:01:52 +01:00
|
|
|
describe("libDDO", () => {
|
2018-11-20 04:54:50 +01:00
|
|
|
|
2018-11-23 05:09:56 +01:00
|
|
|
before(async () => {
|
|
|
|
ConfigProvider.setConfig(config)
|
|
|
|
await TestContractHandler.prepareContracts()
|
|
|
|
ocean = await Ocean.getInstance(config)
|
|
|
|
})
|
|
|
|
|
2018-11-20 04:54:50 +01:00
|
|
|
describe("#constructor()", () => {
|
|
|
|
|
|
|
|
it("should create an empty ddo", async () => {
|
|
|
|
|
|
|
|
const ddo = new DDO()
|
|
|
|
assert(ddo)
|
|
|
|
assert(ddo.did == null)
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
2018-11-21 01:35:41 +01:00
|
|
|
describe("JSON serialization unserialization", () => {
|
2018-11-20 04:54:50 +01:00
|
|
|
it("should create ddo with the sample JSON", async () => {
|
|
|
|
|
|
|
|
assert(jsonDDO)
|
2018-11-21 01:35:41 +01:00
|
|
|
const ddo = new DDO(jsonDDO)
|
2018-11-20 04:54:50 +01:00
|
|
|
assert(ddo)
|
|
|
|
|
|
|
|
assert(ddo.validate())
|
|
|
|
|
2018-11-21 01:35:41 +01:00
|
|
|
const jsonText = ddo.toJSON()
|
2018-11-20 04:54:50 +01:00
|
|
|
assert(jsonText)
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
2018-11-21 01:35:41 +01:00
|
|
|
describe("validation", () => {
|
2018-11-20 04:54:50 +01:00
|
|
|
it("should test ddo core validation", async () => {
|
|
|
|
|
|
|
|
// core ddo values
|
|
|
|
assert(jsonDDO)
|
2018-11-21 01:35:41 +01:00
|
|
|
const ddo = new DDO(jsonDDO)
|
2018-11-20 04:54:50 +01:00
|
|
|
assert(ddo)
|
|
|
|
|
|
|
|
assert(ddo.validate())
|
2018-11-21 01:35:41 +01:00
|
|
|
ddo.did = ""
|
2018-11-20 04:54:50 +01:00
|
|
|
assert(!ddo.validate())
|
|
|
|
})
|
|
|
|
it("should test ddo public key validation", async () => {
|
|
|
|
|
|
|
|
// public key
|
2018-11-21 01:35:41 +01:00
|
|
|
const ddo = new DDO(jsonDDO)
|
2018-11-20 04:54:50 +01:00
|
|
|
assert(ddo)
|
|
|
|
assert(ddo.validate())
|
|
|
|
|
2018-11-21 01:35:41 +01:00
|
|
|
ddo.publicKeys[0].id = ""
|
2018-11-20 04:54:50 +01:00
|
|
|
assert(!ddo.validate())
|
|
|
|
|
|
|
|
})
|
|
|
|
it("should test ddo authentication validation", async () => {
|
|
|
|
|
|
|
|
// authentication
|
2018-11-21 01:35:41 +01:00
|
|
|
const ddo = new DDO(jsonDDO)
|
2018-11-20 04:54:50 +01:00
|
|
|
assert(ddo)
|
|
|
|
assert(ddo.validate())
|
|
|
|
|
2018-11-21 01:35:41 +01:00
|
|
|
ddo.authentications[0].type = ""
|
2018-11-20 04:54:50 +01:00
|
|
|
assert(!ddo.validate())
|
|
|
|
})
|
|
|
|
it("should test ddo service validation", async () => {
|
|
|
|
// service
|
2018-11-21 01:35:41 +01:00
|
|
|
const ddo = new DDO(jsonDDO)
|
2018-11-20 04:54:50 +01:00
|
|
|
assert(ddo)
|
|
|
|
assert(ddo.validate())
|
|
|
|
|
2018-11-21 01:35:41 +01:00
|
|
|
ddo.services[0].endpoint = ""
|
2018-11-20 04:54:50 +01:00
|
|
|
assert(!ddo.validate())
|
|
|
|
})
|
|
|
|
it("should test ddo proof validation", async () => {
|
|
|
|
// proof
|
2018-11-21 01:35:41 +01:00
|
|
|
const ddo = new DDO(jsonDDO)
|
2018-11-20 04:54:50 +01:00
|
|
|
assert(ddo)
|
|
|
|
assert(ddo.validate())
|
|
|
|
|
2018-11-21 01:35:41 +01:00
|
|
|
ddo.proof.signatureValue = ""
|
2018-11-20 04:54:50 +01:00
|
|
|
assert(!ddo.validate())
|
|
|
|
})
|
|
|
|
})
|
2018-11-21 01:35:41 +01:00
|
|
|
describe("DDO access data", () => {
|
2018-11-23 01:53:39 +01:00
|
|
|
it("should find the correct public key", async () => {
|
|
|
|
const did = "did:op:" + IdGenerator.generateId()
|
|
|
|
const ddo = new DDO(did)
|
|
|
|
assert(ddo)
|
2018-11-23 02:36:08 +01:00
|
|
|
for ( let i = 0; i < 5; i ++ ) {
|
2018-11-23 01:53:39 +01:00
|
|
|
const privateKey = ddo.addSignature()
|
|
|
|
assert(privateKey)
|
|
|
|
}
|
|
|
|
const publicKey = ddo.getPublicKey(4)
|
|
|
|
assert(publicKey)
|
2018-11-23 02:30:44 +01:00
|
|
|
|
2018-11-23 01:53:39 +01:00
|
|
|
const publicKeyId = ddo.getPublicKey(did + "#keys=5")
|
|
|
|
assert(publicKeyId)
|
2018-11-23 02:30:44 +01:00
|
|
|
assert(publicKeyId.id === publicKey.id)
|
2018-11-23 01:53:39 +01:00
|
|
|
})
|
2018-11-23 02:30:44 +01:00
|
|
|
|
2018-11-20 04:54:50 +01:00
|
|
|
it("should find a service in the ddo", async () => {
|
2018-11-21 01:35:41 +01:00
|
|
|
const ddo = new DDO(jsonDDO)
|
2018-11-20 04:54:50 +01:00
|
|
|
assert(ddo)
|
|
|
|
assert(ddo.validate())
|
|
|
|
|
2018-11-21 01:55:02 +01:00
|
|
|
const serviceFound = ddo.getService("Metadata")
|
|
|
|
assert(serviceFound)
|
2018-11-20 04:54:50 +01:00
|
|
|
|
2018-11-21 01:55:02 +01:00
|
|
|
const serviceNotFound = ddo.getService("MetadataCannotFind")
|
|
|
|
assert(serviceNotFound == null)
|
2018-11-21 01:35:41 +01:00
|
|
|
|
|
|
|
// var item = ddo.findServiceKeyValue("serviceDefinitionId", "test")
|
2018-11-20 04:54:50 +01:00
|
|
|
})
|
|
|
|
})
|
2018-11-21 01:35:41 +01:00
|
|
|
describe("DDO hashing", () => {
|
2018-11-20 04:54:50 +01:00
|
|
|
it("should hash a valid ddo", async () => {
|
2018-11-21 01:35:41 +01:00
|
|
|
const ddo = new DDO(jsonDDO)
|
2018-11-20 04:54:50 +01:00
|
|
|
assert(ddo)
|
|
|
|
assert(ddo.validate())
|
|
|
|
|
2018-11-21 01:35:41 +01:00
|
|
|
const hash = ddo.calculateHash()
|
2018-11-20 04:54:50 +01:00
|
|
|
assert(hash)
|
2018-11-20 09:59:04 +01:00
|
|
|
// console.log(hash)
|
2018-11-20 04:54:50 +01:00
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-11-21 01:35:41 +01:00
|
|
|
describe("DDO validate proof from JSON", () => {
|
2018-11-20 07:40:37 +01:00
|
|
|
it("should have a valid ddo proof", async () => {
|
2018-11-21 01:35:41 +01:00
|
|
|
const ddo = new DDO(jsonDDO)
|
2018-11-20 07:40:37 +01:00
|
|
|
assert(ddo)
|
2018-11-21 01:35:41 +01:00
|
|
|
assert(ddo.validate())
|
2018-11-20 12:28:02 +01:00
|
|
|
ddo.validateProof()
|
2018-11-20 07:40:37 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-11-21 01:35:41 +01:00
|
|
|
describe("DDO creation", () => {
|
2018-11-20 09:59:04 +01:00
|
|
|
it("should add a signature", async () => {
|
2018-11-21 01:35:41 +01:00
|
|
|
const ddo = new DDO()
|
2018-11-20 09:59:04 +01:00
|
|
|
assert(ddo)
|
|
|
|
const privateKey = ddo.addSignature()
|
2018-11-21 01:35:41 +01:00
|
|
|
assert(privateKey.match("-----BEGIN RSA PRIVATE KEY-----"))
|
2018-11-20 09:59:04 +01:00
|
|
|
})
|
2018-11-21 01:35:41 +01:00
|
|
|
|
2018-11-20 09:59:04 +01:00
|
|
|
it("should add a service", async () => {
|
2018-11-23 01:53:39 +01:00
|
|
|
const did = "did:op:" + IdGenerator.generateId()
|
2018-11-21 01:35:41 +01:00
|
|
|
const ddo = new DDO(did)
|
2018-11-20 09:59:04 +01:00
|
|
|
assert(ddo)
|
2018-11-23 05:09:56 +01:00
|
|
|
const testServiceType = "metatrippy"
|
|
|
|
const testServiceURL = "http://localhost:5555"
|
|
|
|
const service = ddo.addService({
|
|
|
|
type: testServiceType,
|
|
|
|
serviceEndpoint: testServiceURL,
|
|
|
|
})
|
2018-11-20 09:59:04 +01:00
|
|
|
assert(service)
|
|
|
|
assert(service.id === did)
|
2018-11-23 05:09:56 +01:00
|
|
|
assert(service.type === testServiceType )
|
|
|
|
assert(service.endpoint === testServiceURL )
|
2018-11-20 09:59:04 +01:00
|
|
|
})
|
2018-11-20 11:24:38 +01:00
|
|
|
it("should add a static proof and validate", async () => {
|
2018-11-23 01:53:39 +01:00
|
|
|
const did = "did:op:" + IdGenerator.generateId()
|
2018-11-21 01:35:41 +01:00
|
|
|
const ddo = new DDO(did)
|
2018-11-20 11:24:38 +01:00
|
|
|
assert(ddo)
|
|
|
|
const privateKey = ddo.addSignature()
|
2018-11-21 01:35:41 +01:00
|
|
|
assert(privateKey.match("-----BEGIN RSA PRIVATE KEY-----"))
|
2018-11-22 16:30:07 +01:00
|
|
|
ddo.addProof(privateKey)
|
2018-11-21 04:24:53 +01:00
|
|
|
// console.log(ddo.toJSON())
|
2018-11-20 12:24:18 +01:00
|
|
|
assert(ddo.validateProof())
|
2018-11-20 11:24:38 +01:00
|
|
|
})
|
2018-11-21 04:24:53 +01:00
|
|
|
|
|
|
|
it("should add a static embedded proof and validate", async () => {
|
2018-11-23 01:53:39 +01:00
|
|
|
const did = "did:op:" + IdGenerator.generateId()
|
2018-11-21 04:24:53 +01:00
|
|
|
const ddo = new DDO(did)
|
|
|
|
assert(ddo)
|
|
|
|
const privateKey = ddo.addSignature("pem", true)
|
|
|
|
assert(privateKey.match("-----BEGIN RSA PRIVATE KEY-----"))
|
2018-11-22 16:30:07 +01:00
|
|
|
ddo.addProof(privateKey)
|
2018-11-21 04:24:53 +01:00
|
|
|
// console.log(ddo.toJSON())
|
|
|
|
assert(ddo.validateProof())
|
|
|
|
})
|
2018-11-23 05:09:56 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe("DDO signing", () => {
|
|
|
|
it("should add an Ethereum account public key", async () => {
|
2018-11-21 04:24:53 +01:00
|
|
|
|
2018-11-23 05:09:56 +01:00
|
|
|
const ownerAccount: Account = (await ocean.getAccounts())[0]
|
|
|
|
assert(ownerAccount)
|
|
|
|
|
|
|
|
const did = "did:op:" + IdGenerator.generateId()
|
|
|
|
const ddo = new DDO(did)
|
|
|
|
assert(ddo)
|
|
|
|
// add the public key
|
|
|
|
const publicKey = ddo.addPublicKey({
|
|
|
|
value: await ownerAccount.getPublicKey(),
|
|
|
|
storeType: "hex",
|
|
|
|
owner: ownerAccount.getId(),
|
|
|
|
})
|
|
|
|
|
|
|
|
// add the authentication record
|
|
|
|
const authentication = ddo.addAuthentication({ publicKey: publicKey.id })
|
|
|
|
|
|
|
|
assert(publicKey.id === authentication.publicKeyId)
|
|
|
|
assert(publicKey.owner === ownerAccount.getId())
|
|
|
|
assert(publicKey.type === PublicKey.TYPE_RSA)
|
|
|
|
const data: any = publicKey.toData()
|
|
|
|
assert(data.hex === publicKey.value)
|
|
|
|
})
|
2018-11-20 09:59:04 +01:00
|
|
|
})
|
|
|
|
|
2018-11-20 04:54:50 +01:00
|
|
|
})
|