import Wallet = require ( "ethereumjs-wallet" ) ; import passworder = require ( "browser-passworder" ) ; import { Buffer } from "buffer" ; interface BufferLike { type : string ; data : string ; } function isBufferLike ( something ) { return ( something as BufferLike ) . type === "Buffer" ; } export default class Keyring { wallet : Wallet ; static serialize ( keyring , password ) { let privateKey = keyring . wallet . getPrivateKey ( ) ; return passworder . encrypt ( password , privateKey ) . then ( ( string ) => { return Buffer . from ( string ) . toString ( "base64" ) ; } ) ; } static deserialize ( string , password ) { let unbase64 = Buffer . from ( string , "base64" ) . toString ( ) ; return passworder . decrypt ( password , unbase64 ) . then ( ( privateKey ) => { if ( isBufferLike ( privateKey ) ) { return new Keyring ( Buffer . from ( privateKey . data ) ) ; } else { return new Keyring ( privateKey ) ; } } ) ; } constructor ( privateKey ) { this . wallet = Wallet . fromPrivateKey ( privateKey ) ; } }