I am trying to run this bsv academy code:
const {
Address,
TxBuilder,
TxOut,
KeyPair,
Bn,
PrivKey
} = require('bsv')
// Parameters needed for TxBuilder and filling them with the
// spending output/UTXO values
// Use https://api.mattercloud.net/api/v3/main/address/19Hv2xm2xypAPsnz7NhXu3b9YRU9USn93i/utxo
// to get the details of values below for a given UTXO
const addressUtxoStr = '19Hv2xm2xypAPsnz7NhXu3b9YRU9USn93i'
const satoshisUtxoNumber = 8119
const utxoVoutNumber = 0
const utxoTxidStr = '221b42c696587aaca6df870a2d95b5cfd1dd18825d2732136e6d7fa88091695f'
const utxoPrivKeyWifStr = 'L1H2gUHKEQcjGAxMnispD5d1StASoyhv6zCsPeCriAfZgBPZehF1'
const changeAddressStr = '19Hv2xm2xypAPsnz7NhXu3b9YRU9USn93i'
const satoshisToSendNumber = 5000
const targetAddresStr = '1HayBXYGb7AFPoAAM5P3dNibiQANVve7wN'
// Start to build the tx. Everything about Building transactions is present at
// Github : bsv/lib/tx-builder.js
const builder = new TxBuilder()
// add an input
const fundTxOut = TxOut.fromProperties(
Bn().fromNumber(satoshisUtxoNumber),
Address.fromString(addressUtxoStr).toTxOutScript()
)
const fundTxHashBuf = Buffer.from(utxoTxidStr, 'hex').reverse()
builder.inputFromPubKeyHash(fundTxHashBuf, utxoVoutNumber, fundTxOut)
// Add output to address
builder.outputToAddress(Bn().fromNumber(satoshisToSendNumber), Address.fromString(targetAddresStr))
// Set change address
builder.setChangeAddress(Address.fromString(changeAddressStr))
// build tx
builder.build() // This saves the tx inside the "tx" attribute
// sign
const keyPairs = [ KeyPair.fromPrivKey(PrivKey.fromWif(utxoPrivKeyWifStr)) ]
builder.signWithKeyPairs(keyPairs)
console.log(builder.tx.toHex()) // Signed tx
// run the JS file on node terminal and it will display -
//01000000015f699180a87f6d6e1332275d8218ddd1cfb5952d0a87dfa6ac7a5896c6421b22000000006a473044022024d4b7
But when I run it, it says:
const builder = new TxBuilder()
^
TypeError: TxBuilder is not a constructor
at Object.
What do I do wrong??
I am trying to run this bsv academy code:
const {
Address,
TxBuilder,
TxOut,
KeyPair,
Bn,
PrivKey
} = require('bsv')
// Parameters needed for TxBuilder and filling them with the
// spending output/UTXO values
// Use https://api.mattercloud.net/api/v3/main/address/19Hv2xm2xypAPsnz7NhXu3b9YRU9USn93i/utxo
// to get the details of values below for a given UTXO
const addressUtxoStr = '19Hv2xm2xypAPsnz7NhXu3b9YRU9USn93i'
const satoshisUtxoNumber = 8119
const utxoVoutNumber = 0
const utxoTxidStr = '221b42c696587aaca6df870a2d95b5cfd1dd18825d2732136e6d7fa88091695f'
const utxoPrivKeyWifStr = 'L1H2gUHKEQcjGAxMnispD5d1StASoyhv6zCsPeCriAfZgBPZehF1'
const changeAddressStr = '19Hv2xm2xypAPsnz7NhXu3b9YRU9USn93i'
const satoshisToSendNumber = 5000
const targetAddresStr = '1HayBXYGb7AFPoAAM5P3dNibiQANVve7wN'
// Start to build the tx. Everything about Building transactions is present at
// Github : bsv/lib/tx-builder.js
const builder = new TxBuilder()
// add an input
const fundTxOut = TxOut.fromProperties(
Bn().fromNumber(satoshisUtxoNumber),
Address.fromString(addressUtxoStr).toTxOutScript()
)
const fundTxHashBuf = Buffer.from(utxoTxidStr, 'hex').reverse()
builder.inputFromPubKeyHash(fundTxHashBuf, utxoVoutNumber, fundTxOut)
// Add output to address
builder.outputToAddress(Bn().fromNumber(satoshisToSendNumber), Address.fromString(targetAddresStr))
// Set change address
builder.setChangeAddress(Address.fromString(changeAddressStr))
// build tx
builder.build() // This saves the tx inside the "tx" attribute
// sign
const keyPairs = [ KeyPair.fromPrivKey(PrivKey.fromWif(utxoPrivKeyWifStr)) ]
builder.signWithKeyPairs(keyPairs)
console.log(builder.tx.toHex()) // Signed tx
// run the JS file on node terminal and it will display -
//01000000015f699180a87f6d6e1332275d8218ddd1cfb5952d0a87dfa6ac7a5896c6421b22000000006a473044022024d4b7
But when I run it, it says:
const builder = new TxBuilder()
^
TypeError: TxBuilder is not a constructor
at Object.
What do I do wrong??