chaingate
Version:
Multi-chain cryptocurrency SDK for TypeScript — unified API for Bitcoin, Ethereum, Litecoin, Dogecoin, Bitcoin Cash, Polygon, Arbitrum, and any EVM-compatible chain. Create wallets, query balances, send transactions, and manage tokens and NFTs across UTXO
52 lines (51 loc) • 2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrivateKeyWallet = void 0;
const SigningWallet_1 = require("../SigningWallet");
const PrivateKey_1 = require("./PrivateKey");
const PublicKey_1 = require("../../ViewOnlyWallet/PublicKeyWallet/PublicKey");
const utils_1 = require("../../../utils");
/**
* Wallet backed by a single private key (no HD derivation).
*
* @example
* ```ts
* const wallet = new PrivateKeyWallet(new PrivateKey('deadbeef...'));
* console.log(wallet.publicKey);
* ```
*/
class PrivateKeyWallet extends SigningWallet_1.SigningWallet {
/** @param privateKey - The private key. */
constructor(privateKey) {
super(privateKey);
this.walletType = 'privateKey';
// Store the public key hex eagerly (before any encryption) so it's always available.
// When restoring an already-encrypted wallet, the factory must supply the publicKey separately.
this._publicKey = privateKey.encrypted ? '' : (0, utils_1.bytesToHex)(privateKey.publicKey);
}
/** Compressed public key as hex. Available even when encrypted. */
get publicKey() {
return this._publicKey;
}
/** Returns the {@link PrivateKey}. Prompts for password if encrypted. */
async getPrivateKey() {
if (!this.secret.encrypted)
return this.secret;
return this.secret.withDecrypted(() => new PrivateKey_1.PrivateKey(new Uint8Array(this.secret.raw)));
}
/** Returns the corresponding {@link PublicKey}. */
async getPublicKey() {
if (this._publicKey)
return new PublicKey_1.PublicKey(this._publicKey);
return this.secret.withDecrypted(() => new PublicKey_1.PublicKey(this.secret.publicKey));
}
/** @internal */
doSerialize() {
return {
type: 'privateKey',
privateKey: this.secret.hex,
publicKey: this._publicKey || undefined,
};
}
}
exports.PrivateKeyWallet = PrivateKeyWallet;