Create Bitcoin SV transaction with moneybutton/bsv js library

878 Views Asked by At

I want to create a raw transaction using the BSV JavaScript library from MoneyButton (https://github.com/moneybutton/bsv/) When creating a bitcoin Satoshi Vision (BSV) transaction I always get an error.

'node_modules/bsv/lib/encoding/base58check.js:58 if (csum.toString('hex') !== hash4.toString('hex')) { throw new Error('Checksum mismatch') }'

I have also tried to generate the transaction using the JavaScript BitbossIO/keyring library and I was also not able to generate a raw transaction. I don't know which part I'm getting wrong.

const bsv = require('bsv');


var privateKey = new bsv.PrivateKey.fromWIF('pL3yyzZEc96qU8PUyAtk3TBzyosTVGhA1eMWc6icZzS2ZKTnHGuAh');


var utxo = new bsv.UnspentOutput({
  "txId" : "600fee0e6eca8eb19c40f5bfae5871446e617d44c39a3ad44782c571dbf59650",
  "outputIndex" : 1,
  "address" : "12cyVmfJVwkBA4MUSUDarUL2jXiM98JEoe",
  "script" : "76a91411c5d84f5eca47921b0b92042de543f209c301a188ac",
  "satoshis" : 6691
});


var transaction = new bsv.Transaction()

console.log(transaction)
  .from(utxo)
  .to('1PM2zxJArgHFxqYkrFqN7aKQV8nfnEGA56', 5000)
  .change('1PM2zxJArgHFxqYkrFqN7aKQV8nfnEGA56')
  .sign(privateKey);

console.log(transaction.toString());´

I wish I could generate a transaction. Also, please find above the private key to the transaction. You may use the 10cents, but please help me with the transaction. ;)

2

There are 2 best solutions below

0
On

It's a while ago. Found it when I was searching for building transactions.

Yes, the key is incorrect. I have figured out which is the correct key, and processed the transaction.

Here is the code with the correct key:

"use strict";

const bsv = require("bsv");

//const privateKey = new bsv.PrivateKey.fromWIF('pL3yyzZEc96qU8PUyAtk3TBzyosTVGhA1eMWc6icZzS2ZKTnHGuAh');
const privateKey = bsv.PrivateKey.fromWIF('L3yyzZEc96qU8PUyAtk3TBzyosTVGhA1eMWc6icZzS2ZKTnHGuAh');

const utxo = new bsv.Transaction.UnspentOutput({
  "txId" : "600fee0e6eca8eb19c40f5bfae5871446e617d44c39a3ad44782c571dbf59650",
  "outputIndex" : 1,
  "address" : "12cyVmfJVwkBA4MUSUDarUL2jXiM98JEoe",
  "script" : "76a91411c5d84f5eca47921b0b92042de543f209c301a188ac",
  "satoshis" : 6691
});

const transaction = new bsv.Transaction()
.from(utxo)
.to('1PM2zxJArgHFxqYkrFqN7aKQV8nfnEGA56', 5000)
.change('1PM2zxJArgHFxqYkrFqN7aKQV8nfnEGA56')
.sign(privateKey);

console.log(transaction.toString());
1
On

I believe the key you're using is incorrect, as that's the line that's causing the error because the format is incorrect.

Make sure you're using the correct WIF key.