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
61 lines (60 loc) • 2.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BchTransaction = void 0;
const BaseUtxoTransaction_1 = require("../BaseUtxoTransaction");
const cashaddr_1 = require("./cashaddr");
const bch_1 = require("./bch");
/**
* An unsigned Bitcoin Cash transaction prepared by {@link BchConnector.transfer}.
*
* @example
* ```ts
* const amount = cg.networks.bitcoincash.amount('0.01');
* const tx = await bch.transfer(amount, 'bitcoincash:qq...');
* const fees = tx.recommendedFees();
* tx.setFee(fees.high);
* const broadcasted = await tx.signAndBroadcast();
* ```
*/
class BchTransaction extends BaseUtxoTransaction_1.BaseUtxoTransaction {
/**
* Creates a BCH transaction, converting addresses to legacy for UTXO selection.
* @internal
*/
static async create(params) {
const { explorer, fromAddress: rawFrom, toAddress: rawTo, valueSat, networkParams, getPrivateKey, } = params;
// Convert addresses to legacy format for @scure/btc-signer compatibility
// (it doesn't understand CashAddr encoding).
const fromAddress = (0, cashaddr_1.toLegacyAddress)(rawFrom);
const toAddress = (0, cashaddr_1.toLegacyAddress)(rawTo);
// Fetch fee rates.
const feeRateResult = await explorer.getFeeRate();
// Gather UTXOs and compute fee estimates for each tier.
const cachedUnspent = explorer.global.utxoCache.getUnspent(fromAddress);
const state = { utxos: [...cachedUnspent], page: 0, crawled: false };
const feeRates = await (0, BaseUtxoTransaction_1.buildRecommendedFees)(feeRateResult, explorer, fromAddress, toAddress, valueSat, networkParams, state);
return new BchTransaction({
explorer,
fromAddress,
toAddress,
valueSat,
networkParams,
feeRates,
getPrivateKey,
state,
});
}
// ---------------------------------------------------------------------------
// Transaction signing
// ---------------------------------------------------------------------------
/** Signs the transaction and returns the serialized raw bytes. */
signTransaction(inputs, outputs, privateKey) {
return (0, bch_1.signBchTransaction)(inputs.map((input) => ({
txid: input.txid,
n: input.n,
script: input.script,
amount: input.amount.min(),
})), outputs, privateKey, this.networkParams);
}
}
exports.BchTransaction = BchTransaction;