Skip to content

Commit

Permalink
Updated sign transaction to use bitcoinjs-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
SDargarh committed Jan 23, 2024
1 parent 524ee81 commit 7d4472e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@

### 2.0.5 (2024-01-23)

- Replaced blockcyper api with sochain api to fetch fees in satoshi
- Replaced blockcyper api with sochain api to fetch fees in satoshi
- Updated sign transaction to use bitcoinjs-lib
11 changes: 5 additions & 6 deletions src/helper/calculateFeeAndInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ async function getTransactionSize(URL, headers){
let inputs = [];
utxos.data.data.outputs.forEach(async (element) => {
let utxo = {};
utxo.satoshis = sb.toSatoshi(parseFloat(element.value));
utxo.script = element.script;
utxo.address = element.address;
utxo.txId = element.hash;
utxo.outputIndex = element.index;
totalAmountAvailable += utxo.satoshis;
utxo.value = sb.toSatoshi(parseFloat(element.value));
utxo.scriptPubKey = element.script;
utxo.txid = element.hash;
utxo.vout = element.index;
totalAmountAvailable += utxo.value;
inputCount += 1;
inputs.push(utxo);
});
Expand Down
51 changes: 32 additions & 19 deletions src/helper/signTransaction.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
const bitcore = require("bitcore-lib")
const bitcoin = require("bitcoinjs-lib")

const {getFeeAndInput} = require('./calculateFeeAndInput')

async function signTransaction(from, to, amountToSend, URL, privateKey, satPerByte, headers) {

const transaction = new bitcore.Transaction();
async function signTransaction(from, to, amountToSend, URL, privateKey, satPerByte, headers, network) {
const psbt = new bitcoin.Psbt({ network: network })

const { totalAmountAvailable, inputs, fee } = await getFeeAndInput(URL, satPerByte, headers)

if (totalAmountAvailable - amountToSend - fee < 0) {
throw new Error("Balance is too low for this transaction");
}

//Set transaction input
transaction.from(inputs);

// set the recieving address and the amount to send
transaction.to(to, Math.floor(amountToSend));

// Set change address - Address to receive the left over funds after transfer
transaction.change(from);
for (const unspentOutput of inputs) {
psbt.addInput({
hash: unspentOutput.txid,
index: unspentOutput.vout,
witnessUtxo: {
script: Buffer.from(unspentOutput.scriptPubKey, 'hex'),
value: unspentOutput.value // value in satoshi
},
})
}

//manually set transaction fees: 20 satoshis per byte
transaction.fee(fee);
psbt.addOutput({address: to, value: amountToSend});

// Sign transaction with your private key
transaction.sign(privateKey);
const change = totalAmountAvailable - amountToSend - fee
psbt.addOutput({address: from, value: change});

for (let i = 0; i < inputs.length; i++) {
psbt.signInput(i, bitcoin.ECPair.fromWIF(privateKey, bitcoin.networks.testnet))
}

// serialize Transactions
const serializedTransaction = transaction.serialize();
const isValid = psbt.validateSignaturesOfAllInputs()

return serializedTransaction;
if (isValid) {
psbt.finalizeAllInputs()
// signed transaction hex
const transaction = psbt.extractTransaction()
const signedTransaction = transaction.toHex()
const transactionId = transaction.getId()

return signedTransaction
}

}


module.exports = signTransaction
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class KeyringController {
const headers = { "API-KEY": SOCHAIN_API_KEY}

try {
const signedTransaction = await helpers.signTransaction(from, to, amount, URL, privateKey, satPerByte, headers)
const signedTransaction = await helpers.signTransaction(from, to, amount, URL, privateKey, satPerByte, headers, network)
return { signedTransaction };
} catch (err) {
throw err
Expand Down

0 comments on commit 7d4472e

Please sign in to comment.