Search
 Coin Explorers
Search
 Coin Explorers

Portfolio

Markets

Project Reviews

Founder Stories

Features

Guides

News

Videos

Let’s stay in touch:

News

Understanding a Raw Bitcoin Transaction

I wrote some code that outlines how to create a transaction from scratch. First I want to explain the concepts I found difficult to grasp, and the information that took me a while to find. Then I…

Dec 16, 2018 · 3 min read
  • Share on X
  • Share on Facebook
  • Share on Linkedin
Understanding a Raw Bitcoin Transaction

Code Here it is. To generate a key pair we first need a private key. You can either use a random number or wallet import format. ECmultiply finds the public key using the most significant bit first. We need the binary form of the private key to know exactly how to add and double. The ECmultiply algorithm(double and add)is similar to square and multiply. The counter helps illustrate how the private key doubles and adds with the public key. The ECmultiply loop starts at one because the first bit is always a 1. This implied addition counts as one addition to the private key. The generator point is the public key when the private key is one. This code does not allow addition of the same point. Public Key Generation Step 1 generates the compressed and uncompressed public key. Any point in the cyclic group has only one other point with the same X value. The Y coordinates are also the same except one is flipped over the X-axis. No information is lost when reducing the Y coordinate to only a positive or negative value. Decoding hex means going from hex to ascii. Encoding hex means going to ascii to hex. At step 2 before we can start hashing we have to convert hex to ascii. Hashing has to be done on ascii characters to work with this code. Step 3 is called the public key hash, it is where coins are sent to in a raw TX. Step 9 is smaller and more readable, but easily reverted to step 3. The address is a checksum and encoding of step 3. You can’t go backwards from step 3 to 1 because hashes are a one way function. Bitcoin, Litecoin, and Vertcoin work the same for this kind of TX except for step 4. I used Vertcoin because it is cheap. Here you can see all the components that make up a signing message and transaction. Signing Message We reference the transaction ID, the output from that transaction we want to redeem(input), and the public key hash we want to send to(output). We sign the hash of the message with a random number(1 to N), and the private key. If someone knows the random number you used they can calculate your private key. The signature proves you possess the private key to the address coins were sent to(input) without revealing the private key. This is verified by putting together your public key, the signature, and the message. We only use our private key to sign transactions, and produce public keys. Little endian means bytes are ordered from smallest to largest. Byte 3f2e1d would become 1d2e3f. Big endian means it would stay the same. Input scriptpubkey is where someone would have sent coins to if I gave them the public address from my private key. They can calculate it from my address, I can calculate it from my address or private key. Transaction lock time is the earliest time that miners can include the transaction in the blockchain. Sequence number can be used if your transaction is not final and you plan on updating it. This is not commonly used. If it is at its maximum value the transaction is considered final even if block time is not reached. The signature produces an R and S value. These two values along with the public key, and the hash of the signing message verify the signature is authentic. This is where the step 1 public key is revealed. Final Transaction The final transaction is the same as our signing message except in our signing message is previous scriptpubkey. In our final TX this is replaced with scriptsig. Scriptsig confirms we know the private key to our public address. If any of the data in the TX changed the signature would not be valid because the hash would be different. Transmitting I used Ubuntu in VirtualBox and then installed the Vertcoin daemon. I used this to view raw transactions, and broadcast them. I hope you learned something. I would appreciate any feedback, this is my first attempt publishing something.


  • Share on X
  • Share on Facebook
  • Share on Linkedin

Related News

Bitcoin has officially entered the Guinness World Records for a number of entries, the first of which is being recognized as the First Decentralized Cryptocurrency
News

Bitcoin has officially entered the Guinness World Records for a number of entries, the first of which is being recognized as the First Decentralized Cryptocurrency

Bitcoin now has multiple entries in the Guinness Book of World Records, including most valuable and the first decentralized cryptocurrency.

Oct 19, 2022

740 Million in Bitcoin exits exchanges, the biggest outflow since June's BTC price crash
News

740 Million in Bitcoin exits exchanges, the biggest outflow since June's BTC price crash

The technical outlook, however, remains bearish for Bitcoin, with the price eyeing a run-down toward $14,000 in Q4/2022.

Oct 18, 2022

Bitcoin Wins the Guinness World Record for First Decentralized Cryptocurrency
News

Bitcoin Wins the Guinness World Record for First Decentralized Cryptocurrency

Bitcoin has been honored as the oldest and most valuable crypto, while El Salvador is recognized as the first country to adopt it as legal tender. 

Oct 18, 2022

 Coin Explorers

PortfolioMarketsProject ReviewsFounder StoriesFeaturesGuidesNewsVideosTerms & ConditionsPrivacy Policy

Powered by

 Coin Explorers

Copyright © 2025 - All Rights Reserved