chaingate
Version:
A complete TypeScript library for connecting to and making transactions on different blockchains
40 lines • 1.7 kB
JavaScript
import { hexToBytes } from '../../../../InternalUtils/Utils';
import * as btc from '@scure/btc-signer';
import { OutScript } from '@scure/btc-signer';
import { UtxoTransaction } from '../UtxoWallet/UtxoTransaction';
import { toSatoshi } from '../UtxoWallet/UtxoUtils';
export class LegacyUtxoPreparedTransaction extends UtxoTransaction {
constructor(utils, fromAddress, toAddress, amount, networkParams, privateKeyProvider) {
super(utils, fromAddress, toAddress, amount, networkParams, privateKeyProvider);
}
toLegacyAddress(address) {
return address;
}
async sign(inputs, outputs) {
const txVins = inputs.map((vin) => ({
txid: hexToBytes(vin.txid),
index: vin.n,
witnessUtxo: {
script: vin.script,
amount: BigInt(toSatoshi(vin.amount.toString()).toString()),
},
}));
const txVouts = outputs.map((vout) => ({
script: OutScript.encode(btc.Address(this.networkParams).decode(vout.address)),
amount: BigInt(toSatoshi(vout.amount).toString()),
}));
const transaction = new btc.Transaction({
allowLegacyWitnessUtxo: true,
});
for (const txVin of txVins)
transaction.addInput(txVin);
for (const txVout of txVouts)
transaction.addOutput(txVout);
const privateKeyRaw = (await this.privateKeyProvider()).raw;
for (let i = 0; i < transaction.inputsLength; i++)
transaction.signIdx(privateKeyRaw, i);
transaction.finalize();
return hexToBytes(transaction.hex);
}
}
//# sourceMappingURL=LegacyUtxoPreparedTransaction.js.map