UNPKG

chaingate

Version:

A complete TypeScript library for connecting to and making transactions on different blockchains

44 lines 1.79 kB
import { bytesToHex, hexToBytes } from '../../../../InternalUtils/Utils'; import bch from 'bitcore-lib-cash'; import { toCashAddress, toLegacyAddress } from 'bchaddrjs'; import { UtxoTransaction } from '../../abstract/UtxoWallet/UtxoTransaction'; import { toSatoshi } from '../../abstract/UtxoWallet/UtxoUtils'; Object.defineProperty(global, '_bitcoreCash', { get() { return undefined; }, set() { }, configurable: true, }); export class BitcoinCashPreparedTransaction extends UtxoTransaction { constructor(utils, fromAddress, toAddress, amount, privateKeyProvider) { super(utils, fromAddress, toAddress, amount, { bech32: 'bc', pubKeyHash: 0x00, scriptHash: 0x05, wif: 0x80, }, privateKeyProvider); } toLegacyAddress(address) { return toLegacyAddress(address); } async sign(inputs, outputs) { let transaction = new bch.Transaction(); transaction = transaction.from(inputs.map((t) => new bch.Transaction.UnspentOutput({ txId: t.txid, outputIndex: t.n, script: bch.Script.fromHex(bytesToHex(t.script, false)), satoshis: toSatoshi(t.amount).toNumber(), }))); outputs.forEach((t) => { transaction = transaction.addOutput(new bch.Transaction.Output({ satoshis: toSatoshi(t.amount).toNumber(), script: bch.Script.fromAddress(bch.Address.fromString(toCashAddress(t.address))), })); }); const privateKey = (await this.privateKeyProvider()).raw; transaction = transaction.sign(bytesToHex(privateKey, false)); return hexToBytes(transaction.serialize()); } } //# sourceMappingURL=BitcoinCashPreparedTransaction.js.map