import { Keystore } from '@pigi/core-utils' ; import { ethers } from 'ethers' ; import { KeyValueStore , WalletDB } from '../../types' ; export class DefaultWalletDB implements WalletDB { param db constructor ( private db ) { } param keystore returns public async putKeystore ( keystore ) { const key = Buffer . from ( ethers . utils . getAddress ( keystore . address ) ) ; const value = Buffer . from ( JSON . stringify ( keystore ) ) ; await this . db . put ( key , value ) ; } param address returns public async getKeystore ( address ) { const key = Buffer . from ( ethers . utils . getAddress ( address ) ) ; const value = await this . db . get ( key ) ; if ( value === null ) { throw new Error ( 'Keystore file does not exist.' ) ; } return JSON . parse ( value . toString ( ) ) ; } param address returns public async hasKeystore ( address ) { try { await this . getKeystore ( address ) ; } catch ( err ) { if ( err . message . includes ( 'Keystore file does not exist.' ) ) { return false ; } else { throw err ; } } return true ; } returns public async listAccounts ( ) { const keystores = await this . db . iterator ( ) . values ( ) ; return keystores . map ( ( keystore ) => { const address = JSON . parse ( keystore . toString ( ) ) . address ; return ethers . utils . getAddress ( address ) ; } ) ; } }