However, his method only works for transactions that have a single input, as he is only signing transaction.vin. 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)