From 327557949ecc75e2d49a6efc1096c8bbcb06d880 Mon Sep 17 00:00:00 2001 From: Bill Barman Date: Fri, 23 Nov 2018 08:53:39 +0800 Subject: [PATCH] move to typescript features --- src/libDDO/DDO.ts | 136 +++++++++++++++++++--------------------- test/libDDO/DDO.test.ts | 24 +++++-- 2 files changed, 84 insertions(+), 76 deletions(-) diff --git a/src/libDDO/DDO.ts b/src/libDDO/DDO.ts index d791991..c618f46 100644 --- a/src/libDDO/DDO.ts +++ b/src/libDDO/DDO.ts @@ -15,10 +15,10 @@ interface IDDO { id: string created?: string ["@context"]: string - publicKey?: [] - authentication?: [] - service?: [] - proof?: {} + publicKey?: any[] + authentication?: any[] + service?: any[] + proof?: any } export default class DDO { @@ -117,22 +117,26 @@ export default class DDO { this.context = data["@context"] } + this.publicKeys = [] + this.authentications = [] + this.services = [] + if ( data.hasOwnProperty("publicKey") ) { - data.publicKey.forEach(function(value) { + for (const value of data.publicKey) { this.publicKeys.push(new PublicKey(value)) - }, this) + } } if ( data.hasOwnProperty("authentication") ) { - data.authentication.forEach(function(value) { + for (const value of data.authentication) { this.authentications.push(new Authentication(value)) - }, this) + } } if ( data.hasOwnProperty("service") ) { - data.service.forEach(function(value) { + for (const value of data.service) { this.services.push(new Service(value)) - }, this) + } } if ( data.hasOwnProperty("proof") ) { @@ -152,23 +156,23 @@ export default class DDO { } if ( this.publicKeys.length > 0 ) { data.publicKey = [] - this.publicKeys.forEach(function(publicKey) { - this.push(publicKey.toData()) - }, data.publicKey) + for ( const publicKey of this.publicKeys ) { + data.publicKey.push(publicKey.toData()) + } } if ( this.authentications.length > 0 ) { data.authentication = [] - this.authentications.forEach(function(authentication) { - this.push(authentication.toData()) - }, data.authentication ) + for ( const authentication of this.authentications) { + data.authentication.push(authentication.toData()) + } } if ( this.services.length > 0 ) { data.service = [] - this.services.forEach(function(service) { - this.push(service.toData()) - }, data.service) + for ( const service of this.services ) { + data.service.push(service.toData()) + } } if ( this.isProofDefined() ) { @@ -315,32 +319,20 @@ export default class DDO { return false } - const result = { isValid: true } - this.publicKeys.forEach(function(publicKey) { + for ( const publicKey of this.publicKeys) { if ( !publicKey.isValid() ) { - this.isValid = false + return false } - }, result) - if ( ! result.isValid ) { - return false } - - this.authentications.forEach(function(authentication) { + for ( const authentication of this.authentications) { if ( !authentication.isValid() ) { - this.isValid = false + return false } - }, result) - if ( ! result.isValid ) { - return false } - - this.services.forEach(function(service) { + for ( const service of this.services) { if ( !service.isValid() ) { - this.isValid = false + return false } - }, result) - if ( ! result.isValid ) { - return false } if ( this.isProofDefined() ) { @@ -356,13 +348,12 @@ export default class DDO { * :return a valid service object if found, else null */ public getService(serviceType: string): Service { - const result = { service: null } - this.services.forEach(function(service) { + for ( const service of this.services) { if (service.type === serviceType ) { - this.service = service + return service } - }, result) - return result.service + } + return null } /* @@ -381,13 +372,12 @@ export default class DDO { * :return a service object if found else return null */ public findServiceKeyValue(key: string, value: string): Service { - const result = { service: null } - this.services.forEach(function(service) { + for ( const service of this.services) { if (service.values[key] === value) { - this.service = service + return service } - }, result) - return result.service + } + return null } /* @@ -403,20 +393,21 @@ export default class DDO { values.push(this.created) } - this.publicKeys.forEach(function(publicKey) { - this.push(publicKey.type) - this.push(publicKey.value) - }, values) + for ( const publicKey of this.publicKeys) { + values.push(publicKey.type) + values.push(publicKey.value) + } - this.authentications.forEach(function(authentication) { - this.push(authentication.type) - this.push(authentication.value) - }, values) + for ( const authentication of this.authentications) { + values.push(authentication.type) + values.push(authentication.value) + } + + for ( const service of this.services) { + values.push(service.type) + values.push(service.endpoint) + } - this.services.forEach(function(service) { - this.push(service.type) - this.push(service.endpoint) - }, values) return values } @@ -435,14 +426,16 @@ export default class DDO { * :param keyId: public key id to find * :return the PublicKey object or null for not found */ - public getPublicKey(keyId: string): PublicKey { - const result = {publicKey: null } - this.publicKeys.forEach(function(publicKey) { + public getPublicKey(keyId: any): PublicKey { + if ( typeof keyId === "number" ) { + return this.publicKeys[keyId] + } + for ( const publicKey of this.publicKeys ) { if ( publicKey.id === keyId ) { - this.publicKey = publicKey + return publicKey } - }, result) - return result.publicKey + } + return null } /* Get an authentication object based on it's public key id @@ -453,18 +446,17 @@ export default class DDO { * :param publicKeyId: the public key id to use to search * :return if found return the Authentication object or null */ + public getAuthentication(publicKeyId: string): Authentication { - const result = {authentication: null } - this.authentications.forEach(function(authentication) { + for ( const authentication of this.authentications ) { if ( authentication.publicKeyId === publicKeyId ) { - this.authentication = authentication + return authentication } - // look for embedded public keys in the authentication record if ( authentication.publicKey && authentication.publicKey.id === publicKeyId ) { - this.authentication = authentication + return authentication } - }, result) - return result.authentication + } + return null } /* diff --git a/test/libDDO/DDO.test.ts b/test/libDDO/DDO.test.ts index d9ce173..2ab4428 100644 --- a/test/libDDO/DDO.test.ts +++ b/test/libDDO/DDO.test.ts @@ -1,7 +1,7 @@ import {assert} from "chai" import DDO from "../../src/libDDO/DDO" +import IdGenerator from "../../src/ocean/IdGenerator" -import * as Web3 from "web3" import * as jsonDDO from "../testdata/ddoSample1.json" describe("libDDO", () => { @@ -83,6 +83,22 @@ describe("libDDO", () => { }) }) describe("DDO access data", () => { + it("should find the correct public key", async () => { + const did = "did:op:" + IdGenerator.generateId() + const ddo = new DDO(did) + assert(ddo) + for( let i = 0; i < 5; i ++ ) { + const privateKey = ddo.addSignature() + assert(privateKey) + } + const publicKey = ddo.getPublicKey(4) + assert(publicKey) + + const publicKeyId = ddo.getPublicKey(did + "#keys=5") + assert(publicKeyId) + assert(publicKeyId.id == publicKey.id) + }) + it("should find a service in the ddo", async () => { const ddo = new DDO(jsonDDO) assert(ddo) @@ -128,7 +144,7 @@ describe("libDDO", () => { }) it("should add a service", async () => { - const did = "did:op:" + Web3.utils.randomHex(32).substr(2) + const did = "did:op:" + IdGenerator.generateId() const ddo = new DDO(did) assert(ddo) const service = ddo.addService({type: "metatrippy", serviceEndpoint: "http://localhost:5000"}) @@ -136,7 +152,7 @@ describe("libDDO", () => { assert(service.id === did) }) it("should add a static proof and validate", async () => { - const did = "did:op:" + Web3.utils.randomHex(32).substr(2) + const did = "did:op:" + IdGenerator.generateId() const ddo = new DDO(did) assert(ddo) const privateKey = ddo.addSignature() @@ -147,7 +163,7 @@ describe("libDDO", () => { }) it("should add a static embedded proof and validate", async () => { - const did = "did:op:" + Web3.utils.randomHex(32).substr(2) + const did = "did:op:" + IdGenerator.generateId() const ddo = new DDO(did) assert(ddo) const privateKey = ddo.addSignature("pem", true)