1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

validate proof upto crypto lib

This commit is contained in:
Bill Barman 2018-11-20 14:40:37 +08:00
parent 752787528e
commit a15c655420
4 changed files with 93 additions and 0 deletions

View File

@ -6,6 +6,8 @@ interface IAuthentication {
export default class Authentication {
public static TYPE_RSA = "RsaVerificationKey2018"
public publicKeyId: string
public type: string
public value: string

View File

@ -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

View File

@ -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
}
}

View File

@ -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())
})
})
})