mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
cleanup
This commit is contained in:
parent
cd7cd1e337
commit
99f215300e
@ -7,7 +7,7 @@ interface IAuthentication {
|
|||||||
export default class Authentication {
|
export default class Authentication {
|
||||||
|
|
||||||
public static TYPE_RSA = "RsaVerificationKey2018"
|
public static TYPE_RSA = "RsaVerificationKey2018"
|
||||||
|
|
||||||
public publicKeyId: string
|
public publicKeyId: string
|
||||||
public type: string
|
public type: string
|
||||||
public value: string
|
public value: string
|
||||||
|
@ -4,10 +4,9 @@ import Proof from "./Proof"
|
|||||||
import PublicKey from "./PublicKey"
|
import PublicKey from "./PublicKey"
|
||||||
import Service from "./Service"
|
import Service from "./Service"
|
||||||
|
|
||||||
|
import * as ursa from "ursa"
|
||||||
import * as Web3 from "web3"
|
import * as Web3 from "web3"
|
||||||
|
|
||||||
const ursa = require('ursa');
|
|
||||||
|
|
||||||
interface IDDO {
|
interface IDDO {
|
||||||
id: string
|
id: string
|
||||||
created?: string
|
created?: string
|
||||||
@ -20,16 +19,18 @@ interface IDDO {
|
|||||||
|
|
||||||
export default class DDO {
|
export default class DDO {
|
||||||
|
|
||||||
|
public static CONTEXT: string = "https://w3id.org/future-method/v1"
|
||||||
|
|
||||||
public static validateSignature(text: string, keyValue: string, signature: string, authenticationType: string) {
|
public static validateSignature(text: string, keyValue: string, signature: string, authenticationType: string) {
|
||||||
if ( authenticationType === Authentication.TYPE_RSA ) {
|
if ( authenticationType === Authentication.TYPE_RSA ) {
|
||||||
// var key = ursa.createPublicKey(keyValue, "utf8")
|
// var key = ursa.createPublicKey(keyValue, "utf8")
|
||||||
// var signatureBuffer = new Buffer(signature, 'ascii')
|
// var signatureBuffer = new Buffer(signature, 'ascii')
|
||||||
// var textBuffer = new Buffer(text, 'ascii')
|
// var textBuffer = new Buffer(text, 'ascii')
|
||||||
|
|
||||||
// return key.hashAndVerify("sha256", text, signature, "utf8", false)
|
// return key.hashAndVerify("sha256", text, signature, "utf8", false)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// setup with SHA256 RSA
|
// setup with SHA256 RSA
|
||||||
var sig = new jsr.crypto.Signature({"alg": "SHA256withRSA"})
|
var sig = new jsr.crypto.Signature({"alg": "SHA256withRSA"})
|
||||||
// init with public PEM key
|
// init with public PEM key
|
||||||
sig.init(keyValue)
|
sig.init(keyValue)
|
||||||
@ -43,8 +44,7 @@ export default class DDO {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CONTEXT: string = "https://w3id.org/future-method/v1"
|
|
||||||
public context: string = DDO.CONTEXT
|
public context: string = DDO.CONTEXT
|
||||||
public did: string
|
public did: string
|
||||||
public created: string
|
public created: string
|
||||||
@ -57,14 +57,14 @@ export default class DDO {
|
|||||||
this.publicKeys = []
|
this.publicKeys = []
|
||||||
this.authentications = []
|
this.authentications = []
|
||||||
this.services = []
|
this.services = []
|
||||||
|
|
||||||
if (typeof did === "string") {
|
if (typeof did === "string") {
|
||||||
this.did = did
|
this.did = did
|
||||||
}
|
}
|
||||||
if (typeof did === "object") {
|
if (typeof did === "object") {
|
||||||
this.readFromData(did)
|
this.readFromData(did)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public readFromData(data: IDDO) {
|
public readFromData(data: IDDO) {
|
||||||
@ -145,27 +145,25 @@ export default class DDO {
|
|||||||
public toJSON(): string {
|
public toJSON(): string {
|
||||||
return JSON.stringify(this.toData(), null, 2)
|
return JSON.stringify(this.toData(), null, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
public addSignature(keyType?: string): string {
|
public addSignature(keyType?: string): string {
|
||||||
if ( keyType == null ) {
|
if ( keyType == null ) {
|
||||||
keyType = PublicKey.PEM
|
keyType = PublicKey.PEM
|
||||||
}
|
}
|
||||||
if (keyType === PublicKey.PEM ) {
|
if (keyType === PublicKey.PEM ) {
|
||||||
var keys = ursa.generatePrivateKey(1024, 65537)
|
const keys = ursa.generatePrivateKey(1024, 65537)
|
||||||
const nextIndex = this.publicKeys.length + 1
|
const nextIndex = this.publicKeys.length + 1
|
||||||
const keyId = (this.did ? this.did : '' ) + "#keys=" + nextIndex
|
const keyId = (this.did ? this.did : "" ) + "#keys=" + nextIndex
|
||||||
var publicKey = new PublicKey({id: keyId, owner: keyId, type: keyType})
|
const publicKey = new PublicKey({id: keyId, owner: keyId, type: keyType})
|
||||||
publicKey.value = keys.toPublicPem("utf8")
|
publicKey.value = keys.toPublicPem("utf8")
|
||||||
this.publicKeys.push(publicKey)
|
this.publicKeys.push(publicKey)
|
||||||
|
|
||||||
|
|
||||||
return keys.toPrivatePem("utf8")
|
return keys.toPrivatePem("utf8")
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
public addService(data): Service {
|
public addService(data): Service {
|
||||||
var service = new Service(data)
|
const service = new Service(data)
|
||||||
if (service.id == null ) {
|
if (service.id == null ) {
|
||||||
service.id = this.did
|
service.id = this.did
|
||||||
}
|
}
|
||||||
@ -277,7 +275,7 @@ export default class DDO {
|
|||||||
const values = this.hashTextList()
|
const values = this.hashTextList()
|
||||||
return Web3.utils.sha3(values.join())
|
return Web3.utils.sha3(values.join())
|
||||||
}
|
}
|
||||||
|
|
||||||
public getPublicKey(keyId: string): PublicKey {
|
public getPublicKey(keyId: string): PublicKey {
|
||||||
const result = {publicKey: null }
|
const result = {publicKey: null }
|
||||||
this.publicKeys.forEach(function(publicKey) {
|
this.publicKeys.forEach(function(publicKey) {
|
||||||
@ -305,10 +303,10 @@ export default class DDO {
|
|||||||
}
|
}
|
||||||
const keyValue = publicKey.decodeValue()
|
const keyValue = publicKey.decodeValue()
|
||||||
const authentication = this.getAuthentication(publicKey.id)
|
const authentication = this.getAuthentication(publicKey.id)
|
||||||
|
|
||||||
return DDO.validateSignature(signatureText, keyValue, signatureValue, authentication.type)
|
return DDO.validateSignature(signatureText, keyValue, signatureValue, authentication.type)
|
||||||
}
|
}
|
||||||
|
|
||||||
public validateProof(signatureText?: string): boolean {
|
public validateProof(signatureText?: string): boolean {
|
||||||
if ( signatureText == null ) {
|
if ( signatureText == null ) {
|
||||||
signatureText = this.hashTextList().join()
|
signatureText = this.hashTextList().join()
|
||||||
@ -322,7 +320,7 @@ export default class DDO {
|
|||||||
const signature = new Buffer(this.proof.signatureValue, "base64")
|
const signature = new Buffer(this.proof.signatureValue, "base64")
|
||||||
return this.validateFromKey(this.proof.creator, signatureText, signature.toString("ascii"))
|
return this.validateFromKey(this.proof.creator, signatureText, signature.toString("ascii"))
|
||||||
}
|
}
|
||||||
|
|
||||||
public isEmpty(): boolean {
|
public isEmpty(): boolean {
|
||||||
return this.did && this.did.length === 0
|
return this.did && this.did.length === 0
|
||||||
&& this.publicKeys.length === 0
|
&& this.publicKeys.length === 0
|
||||||
|
@ -45,26 +45,26 @@ export default class PublicKey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public decodeValue(): string {
|
public decodeValue(): string {
|
||||||
var value = this.value
|
let value = this.value
|
||||||
var buffer
|
let buffer = null
|
||||||
switch(this.type) {
|
switch (this.type) {
|
||||||
case PublicKey.PEM:
|
case PublicKey.PEM:
|
||||||
value = this.value
|
value = this.value
|
||||||
break;
|
break
|
||||||
case PublicKey.JWK:
|
case PublicKey.JWK:
|
||||||
// TODO: implement
|
// TODO: implement
|
||||||
break;
|
break
|
||||||
case PublicKey.HEX:
|
case PublicKey.HEX:
|
||||||
value = Web3.utils.hexToAscii(this.value)
|
value = Web3.utils.hexToAscii(this.value)
|
||||||
break;
|
break
|
||||||
case PublicKey.BASE64:
|
case PublicKey.BASE64:
|
||||||
buffer = new Buffer(this.value, 'base64')
|
buffer = new Buffer(this.value, "base64")
|
||||||
value = buffer.toString('ascii')
|
value = buffer.toString("ascii")
|
||||||
break;
|
break
|
||||||
case PublicKey.BASE85:
|
case PublicKey.BASE85:
|
||||||
buffer = new Buffer(this.value, 'base85')
|
buffer = new Buffer(this.value, "base85")
|
||||||
value = buffer.toString('ascii')
|
value = buffer.toString("ascii")
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user