1

I'm making a full-service wallet, I already made the UTXOs selection algorithm and now I want to push the transaction hex bytes. It works when I push only a transaction with only 1 input, but if I try to push the hex from a tx I made with 2 inputs from the same account, I get this:

OP_EQUALVERIFY: non-equal data

Here is my decoded hex:

{
   "lock_time":0,
   "size":339,
   "inputs":[
      {
         "prev_out":{
            "index":0,
            "hash":"3a896625fce1f18ce4c3139863359631d5677cff4e4fd8dac25ca6327c0dadd8"
         },
         "script":"473044022070f3b03cca6a75038ffe150be347a97d0e07adc857cc93e245739db55e3a6d36022051eb72b21e35a5e7309e1864f00e1b107a5b5262d80b1128e9fb45fbe7971c7b012102bb05325000709d3a3d8063f9913777859b8fa30b83e4c4f981bb2d9781b7c91a"
      },
      {
         "prev_out":{
            "index":1,
            "hash":"436094120d4412c32b97738f9051a32a237c037abe31cafbdc8a02d16f2a659c"
         },
         "script":"483045022100da7abc59e24d2afea9219ee24d1f3df4e1b9a2efa154d9704ed903210abfdb40022015a8237d38e18f558172872af1aa5c244b38c3ad8f594463c0dcad18dbfcf5f5012102bb05325000709d3a3d8063f9913777859b8fa30b83e4c4f981bb2d9781b7c91a"
      }
   ],
   "version":1,
   "vin_sz":2,
   "hash":"c5991f9f044f9d829fc01eeda76f36a514271270f9f891ea7b584f504ed404ea",
   "vout_sz":1,
   "out":[
      {
         "script_string":"OP_DUP OP_HASH160 7ff85ee854794422531b7b5c3d380c5f50011a4e OP_EQUALVERIFY OP_CHECKSIG",
         "address":"1CfeMTtDP9HNjc8vsuuHGgUfABexvW5Qkf",
         "value":350000,
         "script":"76a9147ff85ee854794422531b7b5c3d380c5f50011a4e88ac"
      }
   ]
}

Links for the transactions being used:

My address is 1FebYgpxQ1exSNdJENkbmNPmSfsiP3kZiT.

And my code using the BitcoinJS library is the following:

key = bitcoin.ECPair.fromWIF(privatekey);
var tx = new bitcoin.TransactionBuilder()
var sum_outputs = 0;

var input_utxos = defineInputs(utxos, amount);

for(var a = 0; a < input_utxos.length ; a++){
    tx.addInput(input_utxos[a].tx,a)
    sum_outputs+=parseInt(input_utxos[a].amount);
}

tx.addOutput(to, parseInt(amount));
var change_amount = sum_outputs - parseInt(amount);
if(change_amount > 0) {
    tx.addOutput(from, change_amount);
}

for(var a = 0; a < input_utxos.length; a++) {
    tx.sign(a, key);
}

result = tx.build().toHex();

console.log(result);

So my questions are:

  1. Why is this working with 1 input from my address and not for 2 inputs?
  2. How can I correctly sign the second input that I assume is the cause for the error?
  • At the current moment, I looked at both of the source transactions and the indexes have been spent already. Did you successfully solve your problem? – Nayuki Sep 16 '15 at 23:20
  • @NayukiMinase yes. thanks. i will post my own answer – caxco93 Sep 17 '15 at 14:59
2

So... I found out what was wrong. The problem was in this part of the code when i was adding the inputs. I was adding an index to each input based on which number of input it was.

for(var a = 0; a < input_utxos.length ; a++){
    tx.addInput(input_utxos[a].tx,a)
    sum_outputs+=parseInt(input_utxos[a].amount);
}

but the index i was supossed to send was actually the index on the transaction outputs. so it had to be like this

for(var a = 0; a < input_utxos.length ; a++){
    tx.addInput(input_utxos[a].tx,input_utxos[a].n)
    sum_outputs+=parseInt(input_utxos[a].amount);
}

The index was gettable from the api that provided me all the utxos for an account. In this case, it was called n and was inside the transaction object itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.