UNPKG

chaingate

Version:

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

55 lines 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PhraseWallet = void 0; const SeedableWallet_1 = require("../../abstract/SeedableWallet"); const chaingate_client_1 = require("chaingate-client"); const Encrypted_1 = require("../../entities/WalletEncryption/Encrypted"); const Phrase_1 = require("../../entities/Secret/implementations/Phrase"); const Utils_1 = require("../../../Utils/Utils"); const ExtendedPublicKey_1 = require("../../entities/Secret/implementations/ExtendedPublicKey"); class PhraseWallet extends SeedableWallet_1.SeedableWallet { constructor(apiClient, secret, askForPassword) { super(apiClient, secret, askForPassword); } async serializeInternal() { return await this.internalSerialize('phrase'); } async getPhrase() { const phrase = (new TextDecoder()).decode(await this.walletEncryption.getSecretDecrypted()); return new Phrase_1.Phrase(phrase); } async getSeed() { return (await this.getPhrase()).getSeed(); } static async new(apiKey, phrase, warnAboutUnencrypted, encrypt) { const chainGateClient = new chaingate_client_1.ChainGateClient(apiKey); const newPhrase = await Phrase_1.Phrase.new(phrase); const wallet = new PhraseWallet(chainGateClient, 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(apiKey, serialized, askForPassword) { const encrypted = new Encrypted_1.Encrypted({ iterations: serialized.secret.iterations, dkLen: serialized.secret.dkLen, nonce: (0, Utils_1.hexToBytes)(serialized.secret.nonce), salt: (0, Utils_1.hexToBytes)(serialized.secret.salt), data: (0, Utils_1.hexToBytes)(serialized.secret.data), cipher: serialized.secret.cipher }); if (!(serialized.walletType == 'phrase')) throw new Error('Wallet format error'); const wallet = new PhraseWallet(new chaingate_client_1.ChainGateClient(apiKey), encrypted, askForPassword); wallet.walletUniqueId = serialized.walletUniqueId; wallet.derivationPaths = (0, Utils_1.recordToMap)(serialized.derivationPaths); const derivationResultsStr = (0, Utils_1.recordToMap)(serialized.publicKeys); wallet.derivationResults = (0, Utils_1.transformMap)(derivationResultsStr, (t) => new ExtendedPublicKey_1.ExtendedPublicKey(t)); return wallet; } } exports.PhraseWallet = PhraseWallet; //# sourceMappingURL=PhraseWallet.js.map