How to Create a Raw Bitcoin Transaction — Step by Step

Jordan Baczuk
Coinmonks
5 min readAug 14, 2018

--

bitcoinchaser.com

Introduction

What makes up a bitcoin transaction? Well, there is more than one kind of bitcoin transaction, and consequently, there are multiple types of addresses to which Bitcoin can be sent (to the same person’s key pair).

The current default address type on bitcoin-core is a P2SH-P2WPKH which is a Pay-to-Witness-Public-Key-Hash wrapped in a Pay-to-Script-Hash. For more info, see this previous article: How to Generate a Bitcoin Address — Step by Step.

Learn more and join people in 22 countries around the world in my course on how to Become a Bitcoin + Blockchain Programmer.

Discover and review best Bitcoin products

Transaction Format

Let’s look at a transaction from my private bitcoin network:

It’s obviously self-explanatory, got it now?

If you’re still wondering, let’s break it down (from left to right):

Note: all values are little endian. Here is a description of all of the fields:

Transaction Properties

Create a raw transaction

Lets create a transaction from scratch! Let’s use Output 1 from the transaction above:

ScriptSig

Notice the format of the scriptPubkey is a P2SH :

OP_HASH160 0x14 <script hash> OP_EQUAL

In order to spend this output, we must provide the script that hashes to the value in <script hash> so the OP_EQUAL succeeds. Our scriptSig needs to take the form:

<0 0x14 <20-byte-key-hash>>

But, since this is a witness transaction, the witness part of the transaction also includes:<signature> <pubkey> , which will be evaluated using <signature> <pubkey> CHECKSIG .

To get the 20-byte-key-hash we need to know our public key. Because I own this address, I can get the public key for my address:

Notes:

  1. <ef> and <0> indicate testnet and a compressed key, so they are removed.

Notice the final key hash matches the redeemScript in the output above. Our script sig becomes:

Outputs

Let’s send 1 BTC to address:

1 BTC = 100,000,000 satoshi so Output 1 Value: 00e1f50500000000 (remember, little endian).

We can get the script hash from the address by decoding it and removing the testnet prefix 0xc4:

Remember the script is OP_HASH160 0x14 <script hash> OP_EQUAL , and c4 is just an address prefix we need to remove, so our Output 1 public key script becomes: a914a860f76561c85551594c18eecceffaee8c4822d787 .

We’ll do the same for output 2, which represents our 9 BTCchange (back to the original address) minus a 0.0001 BTC transaction fee. Output 2 Value: F0C1A43500000000 (899990000 satoshi). The Output 2 public key script is a914d8b6fcc85a383261df05423ddf068a8987bf028787 .

Signatures & Witnesses

Now for our signature and public key. We already have our public key from before:

To sign this, we serialize it first, but without the Flag and the scriptSig becomes 00:

Serialized, this becomes:

To sign, we will use the Bitcoin-core RPC call signrawtransaction , which will create the scriptSig for our input as well as the Witnesses and populate the Flag.

Finally, our signed transaction looks like this:

If you liked the article, check out my course on how to Become a Bitcoin + Blockchain Programmer.

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

--

--