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) • 1.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SigningWallet = void 0;
const Wallet_1 = require("../Wallet");
const SERIALIZE_WARNING = 'WARNING: You are serializing unencrypted private key material. ' +
'Anyone with access to this data can steal your funds. ' +
'Pass { acknowledge: true } to suppress this warning.';
/**
* Base class for wallets that hold private key material. Supports encryption.
*
* @typeParam T - The secret type this wallet holds.
*/
class SigningWallet extends Wallet_1.Wallet {
/** @internal */
constructor(secret) {
super();
this.secret = secret;
}
/** Whether the wallet's secret material is currently encrypted. */
get encrypted() {
return this.secret.encrypted;
}
/**
* Encrypts the wallet with a password. After encryption, operations that
* need the secret will prompt for the password automatically.
*
* @param password - The encryption password.
* @param askForPassword - Callback for future password prompts.
* @throws {@link AlreadyEncryptedError} if already encrypted.
*/
async encrypt(password, askForPassword) {
await this.secret.encrypt(password, askForPassword);
}
/**
* Serializes the wallet for storage. Warns if serializing unencrypted
* unless `options.acknowledge` is `true`.
*
* @param options - Serialization options.
*/
async serialize(options) {
if (this.secret.encrypted) {
const { ciphertext, iv, salt } = this.secret.getEncryptedExport();
return { type: this.walletType, ciphertext, iv, salt };
}
if (!options?.acknowledge) {
console.warn(SERIALIZE_WARNING);
}
return this.secret.withDecrypted(() => this.doSerialize());
}
}
exports.SigningWallet = SigningWallet;