UNPKG

chaingate

Version:

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

54 lines 2.43 kB
import { HDWallet } from './HDWallet/HDWallet'; import { WalletEncryption, WalletIsNotEncrypted, } from '../entities/WalletEncryption/WalletEncryption'; import { Encrypted } from '../entities/WalletEncryption/Encrypted'; import { bytesToHex, mapToRecord, transformMap } from '../../InternalUtils/Utils'; import { AllCurrencies } from '../../Currencies/CurrencyModules'; export class SeedableWallet extends HDWallet { supportedCurrencies = AllCurrencies; walletEncryption; walletUniqueId; constructor(context, secret, askForPassword) { const transports = { getPublicKeyProvider: async (currencyId) => { const derivationPath = this.getDerivationPath(currencyId); return this.deriveFromPathUsingCache.bind(this, derivationPath); }, getPrivateKeyProvider: async (currencyId) => { const derivationPath = this.getDerivationPath(currencyId); const seed = await this.getSeed(); return seed.getPrivateKey.bind(seed, derivationPath); }, }; super(context, transports); this.walletEncryption = new WalletEncryption(secret instanceof Encrypted ? secret : secret.raw, askForPassword); } async deriveFromPath(derivationPath) { const seed = await this.getSeed(); return seed.getExtendedPublicKey(derivationPath); } async internalSerialize(walletType) { const secret = this.walletEncryption.getSecret(); if (!(secret instanceof Encrypted)) throw new WalletIsNotEncrypted(); const publicKeysStr = transformMap(this.derivationResults, (e) => e.xpub); return { format: 'ChainGate Serialize Wallet Format Version 2', walletUniqueId: await this.getWalletUniqueId(), walletType: walletType, secret: { iterations: secret.iterations, dkLen: secret.dkLen, nonce: bytesToHex(secret.nonce, false), salt: bytesToHex(secret.salt, false), data: bytesToHex(secret.data, false), cipher: secret.cipher, }, publicKeys: mapToRecord(publicKeysStr), derivationPaths: mapToRecord(this.derivationPaths), }; } async getWalletUniqueId() { return this.walletUniqueId; } } //# sourceMappingURL=SeedableWallet.js.map