import { ethers } from 'ethers' ; import { DefaultSignatureProvider , SignatureProvider , Wallet , } from '@pigi/core-utils' ; import { WalletDB } from '../../types' ; /**
 * Simple Wallet implementation.
 */ export class DefaultWallet implements Wallet { private unlocked : Record < string , ethers . Wallet > = { } ; param walletdb constructor ( private walletdb ) { } returns public async listAccounts ( ) { return this . walletdb . listAccounts ( ) ; } param password returns public async createAccount ( password ) { const wallet = ethers . Wallet . createRandom ( ) ; const keystore = await wallet . encrypt ( password ) ; await this . walletdb . putKeystore ( JSON . parse ( keystore ) ) ; return wallet . address ; } param address param password public async unlockAccount ( address , password ) { if ( ! ( await this . walletdb . hasKeystore ( address ) ) ) { throw new Error ( 'Account does not exist.' ) ; } const keystore = await this . walletdb . getKeystore ( address ) ; let wallet ; try { wallet = await ethers . Wallet . fromEncryptedJson ( JSON . stringify ( keystore ) , password ) ; } catch ( err ) { throw new Error ( 'Invalid account password.' ) ; } this . unlocked [ address ] = wallet ; } param address public async lockAccount ( address ) { delete this . unlocked [ address ] ; } public async getSignatureProvider ( address ) { if ( ! ( await this . walletdb . hasKeystore ( address ) ) ) { throw new Error ( 'Account does not exist.' ) ; } if ( ! ( address in this . unlocked ) ) { throw new Error ( 'Account is not unlocked.' ) ; } return new DefaultSignatureProvider ( this . unlocked [ address ] ) ; } param address param message returns public async sign ( address , message ) { if ( ! ( await this . walletdb . hasKeystore ( address ) ) ) { throw new Error ( 'Account does not exist.' ) ; } if ( ! ( address in this . unlocked ) ) { throw new Error ( 'Account is not unlocked.' ) ; } return this . unlocked [ address ] . signMessage ( message ) ; } }