import { pki } from "node-forge" ; import { computeHash } from '../fact/hash' ; import { Keystore , UserIdentity } from "../keystore" ; import { FactRecord , FactSignature , PredecessorCollection } from "../storage" ; export class MemoryKeystore implements Keystore { private keyPairs : { [ key ] : { publicKey : string ; privateKey : string ; } ; } = { } ; getUserFact ( userIdentity ) { return Promise . resolve ( this . getIdentityFact ( 'Jinaga.User' , userIdentity ) ) ; } getDeviceFact ( userIdentity ) { return Promise . resolve ( this . getIdentityFact ( 'Jinaga.Device' , userIdentity ) ) ; } signFact ( userIdentity , fact ) { return Promise . resolve ( [ ] ) ; } private getIdentityFact ( type , identity ) { if ( ! identity ) { return null ; } const publicKey = this . getPublicKey ( identity ) ; const predecessors = { } ; const fields = { publicKey : publicKey } ; const hash = computeHash ( fields , predecessors ) ; return { type , hash , predecessors , fields } ; } private getPublicKey ( userIdentity ) { const key = `${ userIdentity . provider }:${ userIdentity . id }` ; const keyPair = this . keyPairs [ key ] ; if ( keyPair ) { return keyPair . publicKey ; } else { return this . generateKeyPair ( key ) ; } } private generateKeyPair ( key ) { const keypair = pki . rsa . generateKeyPair ( { bits : 1024 } ) ; const privateKey = pki . privateKeyToPem ( keypair . privateKey ) ; const publicKey = pki . publicKeyToPem ( keypair . publicKey ) ; this . keyPairs [ key ] = { publicKey , privateKey } ; return publicKey ; } }