2

If I have the public key and signature for a transaction how do I get the hash value that is used in verify(hash, sig, pubkey). I want to know how to work backwards from a raw transaction to get it, not how to build it.

My question is basically this one

How to verify the signature of a raw transaction

But the answer given I think is for getting the Transaction ID hash which I have tried and it doesn't work.

4

1 Answer 1

0

Here is how things can be done backwards. Unfortunately, the bitcoin-explorer (bx) verify mechanism works a little differently but somewhat similar to what you were expecting. Step 4 below might provide details that might be useful for what you are doing. Step 5 below completes the validation offline.

1. Here is the easy way to compute transaction IDs from the raw hexadecimal transactions.

% echo 01000000018a8a9f938c251be60da2711c6bf168c66213fae8e26251fcec47f9a41d0e71e3020000006b483045022100ad775acd2d0b14904771c28754e0e510ecd8af842732c5c15a0899d1e28ed42902204efb34805a958174f6a6273444cde7f97cf38cec0d96d17c7921490f58e6379d012103c0f217eb4428c61d47d8aa084a28c3d3d26f43b6569283f08fec5451a5fc8c98ffffffff0200803801000000001976a914d2c6b9c7f146ea4039e6ae67904eb94d479b7ed688ac06d79e460e0000001976a914b85f8cde95fe9b30872e49d5b961a4af5d07518688ac00000000 | bx bitcoin256

9b4912711de5e258a74a529227f43647bc3b2304212fb508c5f8e910d5986270

2. Here are two means (1st is offline, 2nd is online) to extract the Endorsement/Signature and associated Public Key used for the TX_ID 9b4912711de5e258a74a529227f43647bc3b2304212fb508c5f8e910d5986270 input:

% echo 01000000018a8a9f938c251be60da2711c6bf168c66213fae8e26251fcec47f9a41d0e71e3020000006b483045022100ad775acd2d0b14904771c28754e0e510ecd8af842732c5c15a0899d1e28ed42902204efb34805a958174f6a6273444cde7f97cf38cec0d96d17c7921490f58e6379d012103c0f217eb4428c61d47d8aa084a28c3d3d26f43b6569283f08fec5451a5fc8c98ffffffff0200803801000000001976a914d2c6b9c7f146ea4039e6ae67904eb94d479b7ed688ac06d79e460e0000001976a914b85f8cde95fe9b30872e49d5b961a4af5d07518688ac00000000 | bx tx-decode

or

% bx fetch-tx -c ~/bitcoin-explorer/bx-mainnet-remote.cfg 9b4912711de5e258a74a529227f43647bc3b2304212fb508c5f8e910d5986270

that both yield:

transaction
{
    hash 9b4912711de5e258a74a529227f43647bc3b2304212fb508c5f8e910d5986270
    inputs
    {
        input
        {
            address_hash a0d48c129cfde8f124a16f4d69bc47fd7fb5780d
            previous_output
            {
                hash e3710e1da4f947ecfc5162e2e8fa1362c668f16b1c71a20de61b258c939f8a8a
                index 2
            }
            script "[3045022100ad775acd2d0b14904771c28754e0e510ecd8af842732c5c15a0899d1e28ed42902204efb34805a958174f6a6273444cde7f97cf38cec0d96d17c7921490f58e6379d01] [03c0f217eb4428c61d47d8aa084a28c3d3d26f43b6569283f08fec5451a5fc8c98]"
            sequence 4294967295
        }
    }
    lock_time 0
    outputs
    {
        output
        {
            address_hash d2c6b9c7f146ea4039e6ae67904eb94d479b7ed6
            script "dup hash160 [d2c6b9c7f146ea4039e6ae67904eb94d479b7ed6] equalverify checksig"
            value 20480000
        }
        output
        {
            address_hash b85f8cde95fe9b30872e49d5b961a4af5d075186
            script "dup hash160 [b85f8cde95fe9b30872e49d5b961a4af5d075186] equalverify checksig"
            value 61314356998
        }
    }
    version 1
}

Distinguished Encoding Rules (DER) formatted Signature = 3045022100ad775acd2d0b14904771c28754e0e510ecd8af842732c5c15a0899d1e28ed42902204efb34805a958174f6a6273444cde7f97cf38cec0d96d17c7921490f58e6379d01

R =

% echo 3045022100ad775acd2d0b14904771c28754e0e510ecd8af842732c5c15a0899d1e28ed42902204efb34805a958174f6a6273444cde7f97cf38cec0d96d17c7921490f58e6379d01 | cut -c 9-74 00ad775acd2d0b14904771c28754e0e510ecd8af842732c5c15a0899d1e28ed429

S =

% echo 3045022100ad775acd2d0b14904771c28754e0e510ecd8af842732c5c15a0899d1e28ed42902204efb34805a958174f6a6273444cde7f97cf38cec0d96d17c7921490f58e6379d01 | cut -c 79-142 4efb34805a958174f6a6273444cde7f97cf38cec0d96d17c7921490f58e6379d

Pubkey = 03c0f217eb4428c61d47d8aa084a28c3d3d26f43b6569283f08fec5451a5fc8c98

3. The 3rd output of TXID e3710e1da4f947ecfc5162e2e8fa1362c668f16b1c71a20de61b258c939f8a8a contains the original input script that was signed, requires an additional online query:

% bx fetch-tx -c ~/bitcoin-explorer/bx-mainnet-remote.cfg e3710e1da4f947ecfc5162e2e8fa1362c668f16b1c71a20de61b258c939f8a8a

transaction
{
    hash e3710e1da4f947ecfc5162e2e8fa1362c668f16b1c71a20de61b258c939f8a8a
    inputs
    {
        input
        {
            address_hash 3480cd7a92d09974aacc473512ca80a686249498
            previous_output
            {
                hash 4b7b945088ad38de258490d02bdca724548818e1ad184d0e00a2d4d1e82229c8
                index 0
            }
            script "[3046022100da2122351174582ca4f8b9cfc2567bd2c95c3f30be13ce0f20422ba6c2d55806022100af211740671144cc8cbce04e8028e60eb1028af661a8eba77430e4a8615925da01] [02416953ece00bbc3a1ebca108c8961abae0bcc613ecf838bb0e5799202fbe7ae4]"
            sequence 4294967295
        }
    }
    lock_time 0
    outputs
    {
        output
        {
            address_hash ead65b0c5d7abc0af467ae2f34ed1b84a6662fda
            script "dup hash160 [ead65b0c5d7abc0af467ae2f34ed1b84a6662fda] equalverify checksig"
            value 12557630
        }
        output
        {
            address_hash 59cca2f8c91a9784c6c38da5a10f00644238b527
            script "dup hash160 [59cca2f8c91a9784c6c38da5a10f00644238b527] equalverify checksig"
            value 10100000
        }
        output
        {
            address_hash a0d48c129cfde8f124a16f4d69bc47fd7fb5780d
            script "dup hash160 [a0d48c129cfde8f124a16f4d69bc47fd7fb5780d] equalverify checksig"
            value 61334886998
        }
    }
    version 1
}

ASCII encoded script = "dup hash160 [a0d48c129cfde8f124a16f4d69bc47fd7fb5780d] equalverify checksig"

and computing the corresponding P2PKH address:

% echo a0d48c129cfde8f124a16f4d69bc47fd7fb5780d | bx address-encode -v 0

1FfPjjFG1uoA62yiTsje8biYYvEhf8puQP

4. For self awareness, compute Hex encoded script and associated sha256 value:

% echo "dup hash160 [a0d48c129cfde8f124a16f4d69bc47fd7fb5780d] equalverify checksig" | bx script-encode

76a914a0d48c129cfde8f124a16f4d69bc47fd7fb5780d88ac

% echo 76a914a0d48c129cfde8f124a16f4d69bc47fd7fb5780d88ac | bx sha256 0d9288118ccdc74e6ce85ced746aa48afbd30eb70f58e4ce009a78dd96faf33d

5. Confirm signature is valid using an offline command:

% echo 01000000018a8a9f938c251be60da2711c6bf168c66213fae8e26251fcec47f9a41d0e71e3020000006b483045022100ad775acd2d0b14904771c28754e0e510ecd8af842732c5c15a0899d1e28ed42902204efb34805a958174f6a6273444cde7f97cf38cec0d96d17c7921490f58e6379d012103c0f217eb4428c61d47d8aa084a28c3d3d26f43b6569283f08fec5451a5fc8c98ffffffff0200803801000000001976a914d2c6b9c7f146ea4039e6ae67904eb94d479b7ed688ac06d79e460e0000001976a914b85f8cde95fe9b30872e49d5b961a4af5d07518688ac00000000 | bx input-validate -i 0 03c0f217eb4428c61d47d8aa084a28c3d3d26f43b6569283f08fec5451a5fc8c98 "dup hash160 [a0d48c129cfde8f124a16f4d69bc47fd7fb5780d] equalverify checksig" 3045022100ad775acd2d0b14904771c28754e0e510ecd8af842732c5c15a0899d1e28ed42902204efb34805a958174f6a6273444cde7f97cf38cec0d96d17c7921490f58e6379d01

The endorsement is valid.

2
  • 1
    This Q does NOT want txid but rather the hash used in the signature (and verification), which is different, as the Q says and the linked dupes 4 years ago correctly explain. Feb 24, 2019 at 23:20
  • @dave_thompson_085 - Is this better?
    – skaht
    Feb 25, 2019 at 22:21

Your Answer

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

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