UNPKG

chaingate

Version:

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

49 lines 2.38 kB
import { SeedableWallet } from '../../abstract/SeedableWallet'; import { Encrypted } from '../../entities/WalletEncryption/Encrypted'; import { Phrase } from '../../entities/Secret/implementations/Phrase'; import { hexToBytes, recordToMap, transformMap } from '../../../InternalUtils/Utils'; import { ExtendedPublicKey } from '../../entities/Secret/implementations/ExtendedPublicKey'; export class PhraseWallet extends SeedableWallet { constructor(context, secret, askForPassword) { super(context, secret, askForPassword); } async serializeInternal() { return await this.internalSerialize('phrase'); } async getPhrase() { const phrase = new TextDecoder().decode(await this.walletEncryption.getSecretDecrypted()); return new Phrase(phrase); } async getSeed() { return (await this.getPhrase()).getSeed(); } static async new(context, phrase, warnAboutUnencrypted, encrypt) { const newPhrase = await Phrase.new(phrase); const wallet = new PhraseWallet(context, newPhrase, encrypt?.askForPassword); wallet.walletUniqueId = newPhrase.uniqueId; await wallet.generateAllCurrencyDefaultDerivations(); if (encrypt) await wallet.walletEncryption.encrypt(encrypt.password); wallet.walletEncryption.warnAboutUnencrypted = warnAboutUnencrypted; return wallet; } static async import(context, serialized, askForPassword) { const encrypted = new Encrypted({ iterations: serialized.secret.iterations, dkLen: serialized.secret.dkLen, nonce: hexToBytes(serialized.secret.nonce), salt: hexToBytes(serialized.secret.salt), data: hexToBytes(serialized.secret.data), cipher: serialized.secret.cipher, }); if (!(serialized.walletType == 'phrase')) throw new Error('Wallet format error'); const wallet = new PhraseWallet(context, encrypted, askForPassword); wallet.walletUniqueId = serialized.walletUniqueId; wallet.derivationPaths = recordToMap(serialized.derivationPaths); const derivationResultsStr = recordToMap(serialized.publicKeys); wallet.derivationResults = transformMap(derivationResultsStr, (t) => new ExtendedPublicKey(t)); return wallet; } } //# sourceMappingURL=PhraseWallet.js.map