Manually signing a transaction with multiple inputs

+1 vote

Hi,

After referencing the discussion thread from https://www.multichain.com/qa/1923/manually-sign-a-raw-transaction, I've been using daanporon's method for signing multichain transactions using javascript, as provided here: https://gist.github.com/daanporon/28087f042fb83507f5dc9389119a0508

However, his method only works for transactions that have a single input, as he is only signing transaction.vin[0]. So I encountered a problem when trying to sign a transaction with 2 inputs.

I tried to modify his implementation to sign all the inputs. I assumed it's similar to bitcoin, so I used the answer from this thread as reference: https://bitcoin.stackexchange.com/questions/41209/how-to-sign-a-transaction-with-multiple-inputs

Basically, my algorithm is:

For each vin

  • Remove the scriptSig from all other vins
  • Set this vin.scriptSig = OP_DUP+OP_HASH160+pubkeyhash+OP_EQUAL_VERIFY+OP_CHECKSIG (to mimic the output of the prevtx; this is what daan's code already does)
  • Generate the hash from this new version of the transaction
  • Sign the hash and store the signature in a separate array (so that it doesnt get lost when the next vin is processed)
After all vins are processed, I copy the generated signatures from the separate array in the last step to their respective vins.
 
However, when I try this approach with my transaction which has two inputs, the transaction is still invalid (raises ConnectInputs error when sending it). I compared it to the equivalent output of signrawtransaction using the multichain API, and found that the scriptSig of vin #0 is wrong, but the scriptSig of vin#1 is correct. Both vins reference prevtx that are sent to the same address and I'm signing them using the same private key.
 
Given that I'm getting one of the signatures right, I'm assuming I'm kind of on the right track, but maybe I'm just missing a step or two somewhere?
 
Would anyone be able to advice if the approach I'm using is incorrect, or how else I can update it? (Actually, I'm hoping daanporon would be able to comment as well if he sees this - or anyone who can help)

 

 

 

asked Aug 31, 2018 by sphearman

1 Answer

0 votes

The exact details of the payload you need to sign varies with the type of signature, e.g. SIGHASH_ALL, SIGHASH_SINGLE, etc...

May I recommend reading the bitcoin documentation about this, because MultiChain follows the same logic:

https://en.bitcoin.it/wiki/OP_CHECKSIG

answered Aug 31, 2018 by MultiChain
...