bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.62k stars 2.09k forks source link

Invlaid checksum (debian9, apache2) #1567

Closed KhandakerBD closed 4 years ago

KhandakerBD commented 4 years ago

Hello i am trying to get RAW TX HEX, here is my code on run.js:

let bitcoin = require("bitcoinjs-lib");
let network = bitcoin.networks.bitcoin;
let txb = new bitcoin.TransactionBuilder(network);

txb.addInput(process.argv[2], Number(process.argv[3]));
txb.addOutput(process.argv[4], Number(process.argv[5]));
txb.addOutput(process.argv[6], Number(process.argv[7]));

let keypairSpend = bitcoin.ECPair.fromWIF(process.argv[8], network);
txb.sign(0, keypairSpend);

let tx = txb.build();
let txhex = tx.toHex();

console.log(txhex);

on console am running:

node run.js 1d6c0754692307890b8574e685fb4ee2c011150c953b744ee38116a6ac8f1969 0 1Hoz7pndjePNw3YmoRBuTQwrv57CKNGExr 2000 1348GDrPAysz76PZJstdzFTLWkckTR3xKd 7580 INPUT_ADDRESS_PRIVATE_KEY

Getting this error:

Error: Invalid checksum
    at Object.decode (/var/www/test/yyyBtcLib/node_modules/bs58check/base.js:41:25)
    at Object.decode (/var/www/test/yyyBtcLib/node_modules/wif/index.js:43:30)
    at Object.fromWIF (/var/www/test/yyyBtcLib/node_modules/bitcoinjs-lib/src/ecpair.js:75:23)
    at Object.<anonymous> (/var/www/test/yyyBtcLib/ok.js:23:35)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)

I have no knowledge about js language, the error must be in my code, Because i can't figure out the error asking here.

Thank You.

junderw commented 4 years ago

Whatever you put in place of INPUT_ADDRESS_PRIVATE_KEY is not a valid WIF key.

It must be base58check encoded according to the WIF protocol.

junderw commented 4 years ago
this is the error your got >>>                                   Error: Invalid checksum
this is the base58check library which is throwing the error >>>    at Object.decode (/var/www/test/yyyBtcLib/node_modules/bs58check/base.js:41:25)
this is the wif library >>>                                        at Object.decode (/var/www/test/yyyBtcLib/node_modules/wif/index.js:43:30)
fromWIF in your code >>>                                           at Object.fromWIF (/var/www/test/yyyBtcLib/node_modules/bitcoinjs-lib/src/ecpair.js:75:23)
junderw commented 4 years ago

The WIF key should look something like this: (Obviously do not use these keys, but just showing you some examples. Valid WIF bitcoin keys start with K L or 5.

# Compressed key WIF (This is good)
L13AeJxBxTgkzyrMNFoV5oX1nz7HAp8f8X6HjzNEvsFyvPq9eFhw
Ky1vMqcf99YF94scEbZRY2vPVjGBTi1885ggy7WTN5nVAua5qxGj

# Uncompressed key WIF (This is old, and won't work with segwit)
5JDtxvcGSQghp98PV18M4QYaeKdyHvhMeQGLUfdacbAQ9bVATyP
KhandakerBD commented 4 years ago

The WIF key should look something like this: (Obviously do not use these keys, but just showing you some examples. Valid WIF bitcoin keys start with K L or 5.

# Compressed key WIF (This is good)
L13AeJxBxTgkzyrMNFoV5oX1nz7HAp8f8X6HjzNEvsFyvPq9eFhw
Ky1vMqcf99YF94scEbZRY2vPVjGBTi1885ggy7WTN5nVAua5qxGj

# Uncompressed key WIF (This is old, and won't work with segwit)
5JDtxvcGSQghp98PV18M4QYaeKdyHvhMeQGLUfdacbAQ9bVATyP

Oh, okay.

Can i know when i am trying to create new raw transaction am i in need of to use the full amount of previous transactions output? or i can just use any amount less than previous transactions output to this transactions input?

junderw commented 4 years ago

inputs spend 100% of the referenced output's amount.

You have to create an extra output sending money back to you, this is called "change"

So if you have a 10k satoshi output you wish to spend but only want to send 3k satoshi to a friend and you want to add a 400 satoshi fee to the tx, your tx would look like this:

Inputs Outputs
10000 satoshi 3000 satoshi to friend
(only one input) 6600 satoshi to self
KhandakerBD commented 4 years ago

@junderw Hello sir,,

i have another question, Can i use the same address as change address? let me explain:

inputs => 32T8ff96iUQov1DGq6kEXRC1iK9QTpasCJ - 10000 satoshi output => 1155HZhXLJxjQg65Vgq3429y5snXVqNLad - 3000 satoshi output => 32T8ff96iUQov1DGq6kEXRC1iK9QTpasCJ - 6600 satoshi

like using the input address as change address!

junderw commented 4 years ago

Yes you can, but it is not recommended for privacy reasons.