From a15c6554200743e15bf40e2bc9d8be9817a6f134 Mon Sep 17 00:00:00 2001 From: Bill Barman Date: Tue, 20 Nov 2018 14:40:37 +0800 Subject: [PATCH] validate proof upto crypto lib --- src/libDDO/Authentication.ts | 2 ++ src/libDDO/DDO.ts | 56 ++++++++++++++++++++++++++++++++++++ src/libDDO/PublicKey.ts | 26 +++++++++++++++++ test/ddo_lib/DDO.test.ts | 9 ++++++ 4 files changed, 93 insertions(+) diff --git a/src/libDDO/Authentication.ts b/src/libDDO/Authentication.ts index 55d3330..01f3ed3 100644 --- a/src/libDDO/Authentication.ts +++ b/src/libDDO/Authentication.ts @@ -6,6 +6,8 @@ interface IAuthentication { export default class Authentication { + public static TYPE_RSA = "RsaVerificationKey2018" + public publicKeyId: string public type: string public value: string diff --git a/src/libDDO/DDO.ts b/src/libDDO/DDO.ts index f1b06ce..83f6f1b 100644 --- a/src/libDDO/DDO.ts +++ b/src/libDDO/DDO.ts @@ -6,6 +6,8 @@ import Service from "./Service" import * as Web3 from "web3" +//const crypto = require('crypto') + interface IDDO { id: string created?: string @@ -18,6 +20,12 @@ interface IDDO { export default class DDO { + public static validateSignature(text: string, keyValue: string, signature: string, authenticationType: string) { + if ( authenticationType === Authentication.TYPE_RSA ) { + } + return true + } + public static CONTEXT: string = "https://w3id.org/future-method/v1" public context: string = DDO.CONTEXT public did: string @@ -219,7 +227,55 @@ export default class DDO { const values = this.hashTextList() return Web3.utils.sha3(values.join()) } + + public getPublicKey(keyId: string): PublicKey { + const result = {publicKey: null } + this.publicKeys.forEach(function(publicKey) { + if ( publicKey.did === keyId ) { + this.publicKey = publicKey + } + }, result) + return result.publicKey + } + public getAuthentication(publicKeyId: string): Authentication { + const result = {authentication: null } + this.authentications.forEach(function(authentication) { + if ( authentication.publicKeyId === publicKeyId ) { + this.authentication = authentication + } + }, result) + return result.authentication + } + + public validateFromKey(keyId: string, signatureText: string, signatureValue: string): boolean { + const publicKey = this.getPublicKey(keyId) + if ( ! publicKey) { + return false + } + console.log(publicKey) + const keyValue = publicKey.decodeValue() + console.log(keyValue) + + const authentication = this.getAuthentication(publicKey.did) + + return DDO.validateSignature(signatureText, keyValue, signatureValue, authentication.type) + } + + public validateProof(signatureText?: string): boolean { + if ( signatureText == null ) { + signatureText = this.hashTextList().join() + } + if ( !this.isProofDefined() ) { + return false + } + if ( !this.proof.isValid() ) { + return false + } + const signature = new Buffer(this.proof.signatureValue, "base64") + return this.validateFromKey(this.proof.creator, signatureText, signature.toString("ascii")) + } + public isEmpty(): boolean { return this.did && this.did.length === 0 && this.publicKeys.length === 0 diff --git a/src/libDDO/PublicKey.ts b/src/libDDO/PublicKey.ts index 5d7a91e..3166951 100644 --- a/src/libDDO/PublicKey.ts +++ b/src/libDDO/PublicKey.ts @@ -1,4 +1,6 @@ +import * as Web3 from "web3" + interface IPublicKey { id?: string owner?: string @@ -42,4 +44,28 @@ export default class PublicKey { && this.value && this.value.length > 0 } + public decodeValue(): string { + var value = this.value + var buffer + switch(this.type) { + case PublicKey.PEM: + value = this.value + break; + case PublicKey.JWK: + // TODO: implement + break; + case PublicKey.HEX: + value = Web3.utils.hexToAscii(this.value) + break; + case PublicKey.BASE64: + buffer = new Buffer(this.value, 'base64') + value = buffer.toString('ascii') + break; + case PublicKey.BASE85: + buffer = new Buffer(this.value, 'base85') + value = buffer.toString('ascii') + break; + } + return value + } } diff --git a/test/ddo_lib/DDO.test.ts b/test/ddo_lib/DDO.test.ts index 8e3b060..8b63690 100644 --- a/test/ddo_lib/DDO.test.ts +++ b/test/ddo_lib/DDO.test.ts @@ -109,4 +109,13 @@ describe("libDDO", () => { }) }) + describe('DDO validate proof', () => { + it("should have a valid ddo proof", async () => { + var ddo = new DDO(jsonDDO) + assert(ddo) + assert(ddo.validate()) + assert(ddo.validateProof()) + }) + }) + })