5

So I am creating a simple P2PKH transaction and after I researched about this found few useful posts and answers here; I was able to successfully spend a a single P2PKH UTXO but it is really getting complicated when I try to spend transaction with multiple inputs. I have spent a day entirely on this.

All transactions are on Bitcoin Testnet.

Outputs I am redeeming:

  1. Tx Hash: 9742ed783bfec215ec6484d82dae20fba582229e54c49e06d662a91ead3f6a54 Index: 0 scriptPubKey: OP_DUP OP_HASH160 81f52d4061313b6f63549efc03f3df6cb6f0149e OP_EQUALVERIFY OP_CHECKSIG

  2. Tx Hash: 40dbd3c25073cb7222becead1eb9bb2195cf309cf5b24af559f3391168ec6318 Index: 0 scriptPubKey: OP_DUP OP_HASH160 81f52d4061313b6f63549efc03f3df6cb6f0149e OP_EQUALVERIFY OP_CHECKSIG

Now I read an answer from here that suggested if using multiple inputs, one should replace scriptSig for rest of inputs (the ones not being signed) to empty; And I have tried these variations so far:

1. Replacing scriptSig for inputs with "00"

So only the input (UTXO) that is being signed will have its scriptPubKey, and other input(s) should have "00" in place.

Serialized input #1 when signed:
01000000
02
546a3fad1ea962d6069ec4549e2282a5fb20ae2dd88464ec15c2fe3b78ed4297
00000000
19
76a91481F52D4061313B6F63549EFC03F3DF6CB6F0149E88ac
ffffffff
1863ec681139f359f54ab2f59c30cf9521bbb91eadcebe2272cb7350c2d3db40
00000000
00
ffffffff
01
c0fb390000000000
19
76a914DD02C23FF4C3FFC6DA1C74B7EA5BCB0891B0E98288ac
00000000
01000000

and...

Serialized input #2 when signed:
01000000
02
546a3fad1ea962d6069ec4549e2282a5fb20ae2dd88464ec15c2fe3b78ed4297
00000000
00
ffffffff
1863ec681139f359f54ab2f59c30cf9521bbb91eadcebe2272cb7350c2d3db40
00000000
19
76a914A17C43FE0E1F8E660B044C9538E2CEF4ABFDCED288ac
ffffffff
01
c0fb390000000000
19
76a914DD02C23FF4C3FFC6DA1C74B7EA5BCB0891B0E98288ac
00000000
01000000

main transaction with both inputs signed: (signatures without 01 hash code type suffix and public keys are bold)

01000000
02
546a3fad1ea962d6069ec4549e2282a5fb20ae2dd88464ec15c2fe3b78ed4297
00000000
6a
4730440220E4A366646391B3CFB06C0C4B0343E678B560BE8A2D20418233DEE863986105E502207245C5BA3DB516E59B95764C1716F5B39850E20429F08B91389C4780CE45430E0121025F69830D2BA35D04CA9EFB3EA46AA2645BBBDCB592A189A539480657C9696137
ffffffff
1863ec681139f359f54ab2f59c30cf9521bbb91eadcebe2272cb7350c2d3db40
00000000
6a
4730440220C41C498D0CA55E38FE85DD158FDE82BB451068124E2B3AA2391D861BA9281EDD0220165712ADF71B19DF525583D8F48B82DFF06B2E556C17A583F4D5C54E99412C6B0121039F9AF4A84A8D5C35DD7A5628F8C2DC1894C2B4BDD4815DAFFDCBC9102FE2BA26
ffffffff
01
c0fb390000000000
19
76a914DD02C23FF4C3FFC6DA1C74B7EA5BCB0891B0E98288ac
00000000

now decode raw transaction works but when trying to broadcast it, bitcoind gives these errors:

mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation) (code 16)

or

non-mandatory-script-verify-flag (Non-canonical DER signature) (code 64)

I have tried different variants:

  1. Replace scriptPubKey of inputs that are not being signed with just "00"
  2. Replace scriptPubKey of inputs that are not being signed with just ""
  3. Remove sequence number from inputs "ffffff"

but nothing seems to work. What am I doing wrong? Your help will be appreciated :)

1 Answer 1

3

and other input(s) should have "00" in place.

Keep in mind that you replace the "scriptsig" with "empty" and the 0x00 is the size of the empty script.

non-mandatory-script-verify-flag (Non-canonical DER signature) (code 64)

The problem here is the DER encoding used in your signatures. r values in a signature are positive and since each r value that you have has its most significant bit set they need a preceding 0x00 to tell the DER-decoder that the numbers are positive:

0xE4 = 0b11100100 
0xC4 = 0b11000100 

Each signature should change to this: 483045022100E4A3...

1
  • You are right!!! I was going nuts over transaction preparing while the issue was from ECDSA lib side. Thank you mate! Commented Dec 7, 2019 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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