Wednesday, July 24, 2024
HomeBitcointestnet - Constructing Signed Bitcoin Transaction in Java

testnet – Constructing Signed Bitcoin Transaction in Java

Rapidly decoding the tx:

 VERSION      01000000
 TX_IN COUNT  hex=01, decimal=1
  TX_IN[0]    OutPoint hash 22F389655A7916085318876070450A0BFCB0B5B762CCBC3B3F71CF70328368E4 
  TX_IN[0]    OutPoint index hex=00000000, reversed=00000000, decimal=0
  TX_IN[0]    Script Size hex=8A, decimal=138
  TX_IN[0]    Script Sig  473044022051646B77924F6BB7C411C5AA890110AB55DB8812B8998FE24C8BDCE39795EBD602200BC4DE18FD5524AD8B946EE57604424E2B943EF2A14FC7199A7853DDA0743CBE014104B97C679207532E0F4EE2515AEDABA5F87700BBE0138F7457BAA58E89A53153823AB29632E6C3C804ECAAB5913656512339792479A1B898B7E5DC31F075FF8660
        47: OP_DATA_0x47:     push hex 47 (decimal 71) bytes as knowledge
        30: OP_SEQUENCE_0x30: kind tag indicating SEQUENCE, start sigscript
        44: OP_LENGTH_0x44:   size of R + S
        02: OP_INT_0x02:      kind tag indicating INTEGER
        20: OP_LENGTH_0x20:   that is SIG R
        02: OP_S_INT_0x02
        20: OP_LENGTH_0x20:   that is SIG S
        01: OP_SIGHASHALL:    this terminates the ECDSA signature (ASN1-DER construction)
        Minimal and most measurement constraints                        - okay
        scriptsig at all times begins with 0x30                           - okay
        size 136 chars is lower than precise sig size (140 chars) - okay
               (hex 0x44, decimal 68, 136 chars)
        size of R coordinate (64) >= 0                            - okay
        size of S coordinate (64) >= 0                            - okay
        S-Worth is inside scriptsig boundaries                      - okay
        Be sure that the R & S size covers your entire signature      - okay
        S-value have to be smaller than N/2                            - okay
        strictly examine DER-encoded signature                        - okay
        41: OP_DATA_0x41:     push hex 41 (decimal 65) bytes as knowledge
        04: OP_LENGTH_0X04
        * This terminates the Public Key, corresponding bitcoin handle is:    mnAEswb3Aiz5YTsfCJc8vQETnSe19mc5Am 
 TX_IN[0] Sequence FFFFFFFF

TX_OUT COUNT, hex=01, decimal=1
 TX_OUT[0] Worth hex=76DF171000000000, rev_hex=000000001017DF76, dec=269999990 
 TX_OUT[0] PK_Script Size hex=19, dec=25 
 TX_OUT[0] pk_script 76A91448DDFD3891F3F422D5C3C9C25E35B382667FC6E688AC
        76: OP_DUP
        A9: OP_HASH160
        14: OP_Data14 (= decimal 20)
        88: OP_EQUALVERIFY
        AC: OP_CHECKSIG   It is a P2PKH script and interprets base58 encoded into this bitcoin handle:    mnAEswb3Aiz5YTsfCJc8vQETnSe19mc5Am 
 LOCK_TIME 00000000

So the transaction is right, and the signature is assembled accurately. It additionally follows the “strict sig examine” guidelines. The tx is attempting to spend from mnAEswb3Aiz5YTsfCJc8vQETnSe19mc5Am to mnAEswb3Aiz5YTsfCJc8vQETnSe19mc5Am.
So I checked the signature towards the double hash of the tx (see the well-known pizza transaction run-through: How does the ECDSA verification algorithm work throughout transaction?):

The raw_tx.txt is:


Eliminated sig, and entered your pubkey script into raw_tx_SIGHASH_ALL.txt:


and double hash (the binary values, therefore some conversion first!)

  end result=$( cat raw_tx_SIGHASH_ALL.txt | sed 's/[[:xdigit:]]{2}/x&/g' )
  printf $end result > raw_tx.hex
  hexdump -C raw_tx.hex 
  openssl dgst -binary -sha256 <raw_tx.hex >ssha256.hex
  openssl dgst -binary -sha256 <ssha256.hex >dsha256.hex
  hexdump -C dsha256.hex 

So the dsha256 is in readable type:

I take advantage of openssl to confirm the double hash hex file (dsha256.hex), with the signature and a “pem” key (must convert pubkey from hex to pem), I at all times get an error:

openssl pkeyutl -verify -pubin -inkey pubkey.pem -sigfile tmp_sig.hex -in dsha256.hex

whereas working via the pizza instance, it returns a “Signature Verified Efficiently”.


Most Popular

Recent Comments