UNPKG

chaingate

Version:

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

88 lines 4.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PrivateKeyWallet = void 0; const Utils_1 = require("../../../InternalUtils/Utils"); const PublicKey_1 = require("../../entities/PublicKey"); const PrivateKey_1 = require("../../entities/Secret/implementations/PrivateKey"); const Encrypted_1 = require("../../entities/WalletEncryption/Encrypted"); const WalletEncryption_1 = require("../../entities/WalletEncryption/WalletEncryption"); const Wallet_1 = require("../../Wallet"); const CurrencyModules_1 = require("../../../Currencies/CurrencyModules"); class PrivateKeyWallet extends Wallet_1.Wallet { supportedCurrencies = CurrencyModules_1.AllCurrencies; publicKey; walletUniqueId; walletEncryption; async getPublicKey() { if (!this.publicKey) this.publicKey = (await this.getPrivateKey()).publicKey; return Promise.resolve(this.publicKey); } async getPrivateKey() { return new PrivateKey_1.PrivateKey(await this.walletEncryption.getSecretDecrypted()); } constructor(client, markets, secret, askForPassword) { const transports = { // eslint-disable-next-line @typescript-eslint/no-unused-vars getPrivateKeyProvider: async (_currencyInfo) => { return this.getPrivateKey.bind(this); }, // eslint-disable-next-line @typescript-eslint/no-unused-vars getPublicKeyProvider: async (_currencyInfo) => { return this.getPublicKey.bind(this); }, }; super(client, transports, markets); this.walletEncryption = new WalletEncryption_1.WalletEncryption(secret instanceof PrivateKey_1.PrivateKey ? secret.raw : secret, askForPassword); } static async new(client, markets, privateKey, warnAboutUnencrypted, encrypt) { const newPrivateKey = new PrivateKey_1.PrivateKey(privateKey); const wallet = new PrivateKeyWallet(client, markets, newPrivateKey, encrypt?.askForPassword); wallet.walletUniqueId = newPrivateKey.uniqueId; wallet.publicKey = newPrivateKey.publicKey; if (encrypt) await wallet.walletEncryption.encrypt(encrypt.password); wallet.walletEncryption.warnAboutUnencrypted = warnAboutUnencrypted; return wallet; } static async import(client, markets, exported, askForPassword) { const encrypted = new Encrypted_1.Encrypted({ iterations: exported.secret.iterations, dkLen: exported.secret.dkLen, nonce: (0, Utils_1.hexToBytes)(exported.secret.nonce), salt: (0, Utils_1.hexToBytes)(exported.secret.salt), data: (0, Utils_1.hexToBytes)(exported.secret.data), cipher: exported.secret.cipher, }); if (!(exported.walletType == 'privateKey')) throw new Error('Wallet format error'); const wallet = new PrivateKeyWallet(client, markets, encrypted, askForPassword); wallet.walletUniqueId = exported.walletUniqueId; wallet.publicKey = new PublicKey_1.PublicKey((0, Utils_1.hexToBytes)(exported.publicKey)); return wallet; } async getWalletUniqueId() { return this.walletUniqueId; } async serializeInternal() { const secret = this.walletEncryption.getSecret(); if (!(secret instanceof Encrypted_1.Encrypted)) throw new WalletEncryption_1.WalletIsNotEncrypted(); return { format: 'ChainGate Serialize Wallet Format Version 2', walletUniqueId: await this.getWalletUniqueId(), walletType: 'privateKey', secret: { iterations: secret.iterations, dkLen: secret.dkLen, nonce: (0, Utils_1.bytesToHex)(secret.nonce, false), salt: (0, Utils_1.bytesToHex)(secret.salt, false), data: (0, Utils_1.bytesToHex)(secret.data, false), cipher: secret.cipher, }, publicKey: this.publicKey.hex, }; } } exports.PrivateKeyWallet = PrivateKeyWallet; //# sourceMappingURL=PrivateKeyWallet.js.map