UNPKG

chaingate

Version:

A complete TypeScript library for connecting to and making transactions on different blockchains

39 lines 1.34 kB
import { ethers } from 'ethers'; import { IncorrectPassword } from './errors'; import { hexToBytes } from '../../../InternalUtils/Utils'; import { Keystore } from './Keystore'; export class Web3Keystore extends Keystore { keystoreData; constructor(keystoreData) { super(); this.keystoreData = keystoreData; } async checkPassword(password) { try { await ethers.Wallet.fromEncryptedJson(JSON.stringify(this.keystoreData), password); return true; } catch (ex) { if (ex instanceof TypeError && 'argument' in ex && ex.argument === 'password') { return false; } throw new Error('Invalid keystore'); } } async decrypt(password) { try { const ethersWallet = await ethers.Wallet.fromEncryptedJson(JSON.stringify(this.keystoreData), password); return hexToBytes(ethersWallet.privateKey); } catch (ex) { if (ex instanceof TypeError && 'argument' in ex && ex.argument === 'password') { throw new IncorrectPassword(); } throw new Error('Invalid keystore'); } } static isKeystore(obj) { return 'version' in obj && obj.version == 3; } } //# sourceMappingURL=Web3Keystore.js.map