From d67ed4bc15f636e46d4bdda1b9418af3f176a559 Mon Sep 17 00:00:00 2001 From: Bill Barman Date: Wed, 21 Nov 2018 08:55:02 +0800 Subject: [PATCH] change PublicKey labels --- src/libDDO/DDO.ts | 4 ++-- src/libDDO/PublicKey.ts | 37 ++++++++++++++++++------------------- test/libDDO/DDO.test.ts | 8 ++++---- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/libDDO/DDO.ts b/src/libDDO/DDO.ts index 9128f88..fb8447e 100644 --- a/src/libDDO/DDO.ts +++ b/src/libDDO/DDO.ts @@ -139,9 +139,9 @@ export default class DDO { public addSignature(keyType?: string): string { if ( keyType == null ) { - keyType = PublicKey.PEM + keyType = PublicKey.STORE_AS_PEM } - if (keyType === PublicKey.PEM ) { + if (keyType === PublicKey.STORE_AS_PEM ) { // generate the key pairs const keys = ursa.generatePrivateKey(1024, 65537) diff --git a/src/libDDO/PublicKey.ts b/src/libDDO/PublicKey.ts index 52558b9..a3c8d7d 100644 --- a/src/libDDO/PublicKey.ts +++ b/src/libDDO/PublicKey.ts @@ -1,6 +1,4 @@ -import * as Web3 from "web3" - interface IPublicKey { id?: string owner?: string @@ -10,11 +8,11 @@ interface IPublicKey { export default class PublicKey { public static TYPE_RSA: string = "RsaSignatureAuthentication2018" - public static PEM: string = "publicKeyPem" - public static JWK: string = "publicKeyJwk" - public static HEX: string = "publicKeyHex" - public static BASE64: string = "publicKeyBase64" - public static BASE85: string = "publicKeyBase85" + public static STORE_AS_PEM: string = "publicKeyPem" + public static STORE_AS_JWK: string = "publicKeyJwk" + public static STORE_AS_HEX: string = "publicKeyHex" + public static STORE_AS_BASE64: string = "publicKeyBase64" + public static STORE_AS_BASE85: string = "publicKeyBase85" public id: string public owner: string @@ -25,7 +23,7 @@ export default class PublicKey { this.id = data.id this.owner = data.owner this.type = data.type - this.value = data[PublicKey.PEM] + this.value = data[PublicKey.STORE_AS_PEM] } public toData(): IPublicKey { @@ -33,7 +31,7 @@ export default class PublicKey { id: this.id, owner: this.owner, type: this.type, - [PublicKey.PEM]: this.value, + [PublicKey.STORE_AS_PEM]: this.value, } as IPublicKey } @@ -48,22 +46,23 @@ export default class PublicKey { let value = this.value let buffer = null switch (this.type) { - case PublicKey.PEM: + case PublicKey.STORE_AS_PEM: value = this.value break - case PublicKey.JWK: + case PublicKey.STORE_AS_JWK: // TODO: implement break - case PublicKey.HEX: - value = Web3.utils.hexToAscii(this.value) + case PublicKey.STORE_AS_HEX: + buffer = Buffer.from(this.value, "hex") + value = buffer.toString("binary") break - case PublicKey.BASE64: - buffer = new Buffer(this.value, "base64") - value = buffer.toString("ascii") + case PublicKey.STORE_AS_BASE64: + buffer = Buffer.from(this.value, "base64") + value = buffer.toString("binary") break - case PublicKey.BASE85: - buffer = new Buffer(this.value, "base85") - value = buffer.toString("ascii") + case PublicKey.STORE_AS_BASE85: + buffer = Buffer.from(this.value, "base85") + value = buffer.toString("binary") break } return value diff --git a/test/libDDO/DDO.test.ts b/test/libDDO/DDO.test.ts index c5ef8ad..97cc079 100644 --- a/test/libDDO/DDO.test.ts +++ b/test/libDDO/DDO.test.ts @@ -88,11 +88,11 @@ describe("libDDO", () => { assert(ddo) assert(ddo.validate()) - const service = ddo.getService("Metadata") - assert(service) + const serviceFound = ddo.getService("Metadata") + assert(serviceFound) - const service = ddo.getService("MetadataCannotFind") - assert(service == null) + const serviceNotFound = ddo.getService("MetadataCannotFind") + assert(serviceNotFound == null) // var item = ddo.findServiceKeyValue("serviceDefinitionId", "test") })