chaingate
Version:
A complete TypeScript library for connecting to and making transactions on different blockchains
114 lines • 6.53 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrivateKeyWallet = void 0;
const chaingate_client_1 = require("chaingate-client");
const Utils_1 = require("../../../Utils/Utils");
const PublicKey_1 = require("../../entities/PublicKey");
const PrivateKey_1 = require("../../entities/Secret/implementations/PrivateKey");
const Wallet_1 = require("../../Wallet");
const Arbitrum_1 = require("../../entities/Currency/implementations/Arbitrum");
const Avalanche_1 = require("../../entities/Currency/implementations/Avalanche");
const Base_1 = require("../../entities/Currency/implementations/Base");
const BNBChain_1 = require("../../entities/Currency/implementations/BNBChain");
const Ethereum_1 = require("../../entities/Currency/implementations/Ethereum/Ethereum");
const FantomOpera_1 = require("../../entities/Currency/implementations/FantomOpera");
const Polygon_1 = require("../../entities/Currency/implementations/Polygon");
const Bitcoin_1 = require("../../entities/Currency/implementations/Bitcoin/Bitcoin");
const BitcoinTestnet_1 = require("../../entities/Currency/implementations/BitcoinTestnet/BitcoinTestnet");
const Dogecoin_1 = require("../../entities/Currency/implementations/Dogecoin/Dogecoin");
const Litecoin_1 = require("../../entities/Currency/implementations/Litecoin/Litecoin");
const BitcoinCash_1 = require("../../entities/Currency/implementations/BitcoinCash/BitcoinCash");
const Encrypted_1 = require("../../entities/WalletEncryption/Encrypted");
const WalletEncryption_1 = require("../../entities/WalletEncryption/WalletEncryption");
class PrivateKeyWallet extends Wallet_1.Wallet {
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(apiClient, secret, askForPassword) {
const keyProvider = {
// 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(apiClient, keyProvider);
this.walletEncryption = new WalletEncryption_1.WalletEncryption(secret instanceof PrivateKey_1.PrivateKey ? secret.raw : secret, askForPassword);
}
static async new(apiKey, privateKey, warnAboutUnencrypted, encrypt) {
const chainGateClient = new chaingate_client_1.ChainGateClient(apiKey);
const newPrivateKey = new PrivateKey_1.PrivateKey(privateKey);
const wallet = new PrivateKeyWallet(chainGateClient, 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(apiKey, 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(new chaingate_client_1.ChainGateClient(apiKey), encrypted, askForPassword);
wallet.walletUniqueId = exported.walletUniqueId;
wallet.publicKey = new PublicKey_1.PublicKey((0, Utils_1.hexToBytes)(exported.publicKey));
return wallet;
}
currency(currency) {
const currencyMap = {
'arbitrum': new Arbitrum_1.Arbitrum(this.apiClient.ArbitrumApi, this.currencyProviders),
'avalanche': new Avalanche_1.Avalanche(this.apiClient.AvalancheApi, this.currencyProviders),
'base': new Base_1.Base(this.apiClient.BaseApi, this.currencyProviders),
'bnbChain': new BNBChain_1.BNBChain(this.apiClient.BNBChainApi, this.currencyProviders),
'ethereum': new Ethereum_1.Ethereum(this.apiClient.EthereumApi, this.currencyProviders),
'fantomOpera': new FantomOpera_1.FantomOpera(this.apiClient.FantomOperaApi, this.currencyProviders),
'polygon': new Polygon_1.Polygon(this.apiClient.PolygonApi, this.currencyProviders),
'bitcoin': new Bitcoin_1.Bitcoin(this.apiClient.BitcoinApi, this.currencyProviders),
'bitcoinTestnet': new BitcoinTestnet_1.BitcoinTestnet(this.apiClient.BitcoinTestnetApi, this.currencyProviders),
'dogecoin': new Dogecoin_1.Dogecoin(this.apiClient.DogecoinApi, this.currencyProviders),
'litecoin': new Litecoin_1.Litecoin(this.apiClient.LitecoinApi, this.currencyProviders),
'bitcoinCash': new BitcoinCash_1.BitcoinCash(this.apiClient.BitcoinCashApi, this.currencyProviders)
};
return currencyMap[currency];
}
supportedCurrencies = Wallet_1.Currencies;
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