3

I am currently learning bitcoin technology. So I came up around the term raw transaction. There are many tools to create a raw transaction but I want to do it all by myself.

My Idea is to write a raw transaction and broadcast it over network without cli. All I want is to deeply understand the Bitcoin Raw transaction.

To make it simple my transaction has one input and one output. I have got all the input as follows:

#All the values entered here are for example. None need to be true.

Version: 010000 Reverse of previous Tx hash: 8602122a7044b8795b5829b6b48fb1960a124f42ab1c003e769bbaad31cb2 Previous output index: 000000 Script size: 6a Script sig: 3044022............................. Output Count: 01 Value(in satoshi): 1000000 //Please check if it is written in correct format or not Script size: //To be known scriptPubkey: //To be known locktime: 00000000

The problem is I don't know how to generate the scriptPubKey and how to write the value in Satoshi.

The info you may need to generate scriptPubKey is:

Private Key: 94C54DFCAE5E4E64A82B21478A9A243039AA57650EC6395FEC4250ACF90C3902
Public Key: 033c6f7a45e7892df81329e453b9faf5bafef648dfd9bec321eecc8e65512b3c367

Okay don't rush to block explorer. The wallets are empty.

meshcollider
  • 11,815
  • 4
  • 25
  • 53
madhurkant
  • 31
  • 4
  • Not sure if I understand your question correctly, are you looking for createrawtransaction? –  Jan 06 '22 at 07:23
  • 1
    I want to know how the sriptPubKey is generated? the scriptPubKey here: https://medium.datadriveninvestor.com/bitcoin-raw-transaction-breakdown-c0a5a3aa8688 is very long as compared to just a ripemd160 hash of public key. – madhurkant Jan 06 '22 at 07:29
  • It sounds like you may be working with actual funds on mainnet. I couldn't find the transaction you are referring to in your question, but you should consider doing this sort of exploratory work on testnet instead in case you're currently using actual funds. – Murch Jan 07 '22 at 17:14
  • related: https://bitcoin.stackexchange.com/questions/13936/does-createrawtransaction-have-the-ability-to-create-a-transaction-with-custom-s – Ciro Santilli OurBigBook.com Feb 08 '24 at 09:07

1 Answers1

2

A common scriptPubKey format, known as Pay-to-Public-Key-Hash (P2PKH), has the following form:

OP_DUP OP_HASH160 <push of 20-byte public key hash> OP_EQUALVERIFY OP_CHECKSIG

The hash of the public key is formed by ripemd-160(sha-256(compressed public key)).

You can find the hex encoding of the opcodes here: https://en.bitcoin.it/wiki/Script

The amount should be encoded as 8 bytes, in little-endian format. If you want to send 1 million satoshis, this would be 0x40420F0000000000.

By the way, you are missing "Input Count: 01" and sequence number in your example transaction.


The scriptPubKey in the article you linked is:

76a914f76bc4190f3d8e2315e5c11c59cfc8be9df747e388ac

Decoded, this is:

OP_DUP OP_HASH160 [push 20 bytes 76bc4190f3d8e2315e5c11c59cfc8be9df747e3] OP_EQUALVERIFY OP_CHECKSIG

This is exactly the same as above - a P2PKH output spending to the public key whose hash is 76bc4190f3d8e2315e5c11c59cfc8be9df74.

The article you linked is incorrect, when it says:

Locking Script — This is the hash of the locking script that specifies the conditions that must be met in order to spend this output.

It is referring to a P2SH output, but the output in the example is a P2PKH output.

meshcollider
  • 11,815
  • 4
  • 25
  • 53