How do I sign a raw transaction using Javascript or Any Javascript Library?

+1 vote

# multichain

I have created a raw transaction using mutlichain-cli. This is that raw transaction. 
 

Raw-Transaction

Now I have signed this using bitcoin-js library. I have also set following params so that bitcoin addresses are compatible here.

 "address-pubkeyhash-version" : "00",
    "address-scripthash-version" : "05",
    "private-key-version" : "80",
    "address-checksum-value" : "00000000",

This is the signed transaction using bitcoinjs library.

Signed-Transaction

When I convert this transaction to hex and submit to chain, it says Script failed an OP_CHECKSIGVERIFY operation. I feel that scriptSig generated by bitcoinjs library is wrong. 

How do I generate correct signatures using any javascript module?

asked Jan 29, 2018 by akshay_111meher

1 Answer

0 votes

The next step to analyze the cause of the problem is to sign the transaction using MultiChain (signrawtransaction API) and show the decoded hexadecimal of that signed transaction for comparison. For example, are you sure you are using the right address / private key for signing its input?

answered Jan 29, 2018 by MultiChain
I did the same. For every transaction

scriptSig:{
         asm:"*****",
         hex:"******"
}

were different compared to that generated with 'signrawtransaction API'.
The tried with multiple libraries.

Can you elaborate step by step how to generate signatures.
Please share your bitcoin-js code and we'll see if we can see anything to help you.
@MultiChain, I have attempted to do the same using bitcoinjs-lib. I'm sharing my script below. Same problem as others have reported, the signed transaction hex don't match.

Here's the script:
```
var bitcoin = require('bitcoinjs-lib')

function signTransaction(txHex, privateKey) {
    // Read the transaction
    var tx = bitcoin.Transaction.fromHex(txHex)
    // Load the private key
    var keyPair = bitcoin.ECPair.fromWIF(privateKey)
    // ----------------------
    // Build the transaction:
    // ----------------------
    var txb = new bitcoin.TransactionBuilder()
    // Add the inputs
    tx.ins.forEach(function(value, index){
        txb.addInput(value.hash, value.index)
        txb.sign(index, keyPair)
    })
    // Add the outputs
    tx.outs.forEach(function(value, index){
        txb.tx.outs.push(value)
    })
    return txb.build().toHex()
}

// Let's use the function above to sign a transaction:
// The txHex below was generated by the `createrawsendfrom` RPC call
let txHex = '0100000001956431812011527d7b357c683f8fccf8070aa193ddf15b433acad4ecd74a25ff0100000000ffffffff0200000000000000003776a914e37bcb36f9f9e07f063310265ccf0c937831e22988ac1c73706b7131903e9cf63ab2184db38033a79b8ca7a0252600000000007500000000000000003776a914a0d8d5bbb65b912fd8bb6809b3eb2ac349912e8f88ac1c73706b7131903e9cf63ab2184db38033a79b8ca7fc7d8005000000007500000000'
let privateKey = 'KwZFBkdzBEVNvwm2kHX8Abs2o9PZcoWnKbhAvaQGQdk3RY2WVxmG'
var signed_tx = signTransaction(txHex, privateKey)

console.log('\nSigned transaction using the function:')
console.log(signed_tx)

// The actual signed transaction from the `signrawtransaction` RPC call is this:
var api_signed_tx = '0100000001956431812011527d7b357c683f8fccf8070aa193ddf15b433acad4ecd74a25ff010000006b48304502210080ac8c015ca8ab92461860d4f7c6b9d531858c030116ebc586eb41e6401c80b20220435a4d4c0d4278b3167a128d4c0374800d7d70291c2e82fea196523c58ec67bc01210389c7fd04b9a3fc138b439f494df359a42c4e323bd6fecc96293f8a0a117c858effffffff0200000000000000003776a914e37bcb36f9f9e07f063310265ccf0c937831e22988ac1c73706b7131903e9cf63ab2184db38033a79b8ca7a0252600000000007500000000000000003776a914a0d8d5bbb65b912fd8bb6809b3eb2ac349912e8f88ac1c73706b7131903e9cf63ab2184db38033a79b8ca7fc7d8005000000007500000000'
console.log('\nSigned transaction with API:')
console.log(api_signed_tx)
```
What happens if you try to broadcast the transaction signed by bitcoinjs-lib by passing it to sendrawtransaction in the MultiChain API?
...