How to Create Raw Bitcoin Transaction and Sign It with Golang

How to Create Raw Bitcoin Transaction and Sign It with Golang

This tutorial explains how to create a raw Bitcoin transaction with Go, using btcsuite library, and then push it to the testnet, or main net. How to Create Raw Bitcoin Transaction and Sign It with Golang

This tutorial explains how to create a raw Bitcoin transaction with Go, using btcsuite library, and then push it to the testnet, or main net.

Following concepts will be covered by the tutorial:

  • UTXO
  • Public, Private key pair
  • Transaction Structure
  • Create Transaction
  • Sign Transaction
  • Push to network
  • Fee and some other concepts

UTXO

Unspent Output Transaction (UTXO) is referred to an output of a transaction that is not spent yet. The balance of and account is the sum of the amounts of its UTXOs. For example, you can search “3D2oetdNuZUqQHPJmcMDDHYoqkyNVsFk9r” in blockcypher and then click on “Advanced Details”, “unpent outputs” to see UTXOs of this address. Can also use other addresses or other blockchain explorers.

Image for post

UTXO (blockchain explorer)

Private, Public key and Address

We don’t dive deep in private and public key, and just touch address concept. To interact with Bitcoin blockchain, you have to provide a private, public key pair and extract your address from it. All Bitcoin wallets do this job for you and if working with the main net store your backup file or the seed in a secure place.

For testnet, any wallet that supports testnet can be used, or can use this link to get private key and address.

Main net and testnet addresses have a little difference, because of the generating address algorithm.

Address generating algorithm is as follows:

(You can skip address generating algorithm, since every wallet does it for automatically)

  • Get you public key
  • Perform Sha256 on your Public key
  • Perform RIPEMD160 hashing on the result of Sha256 hash
  • Add version byte in front of RIPEMD160 hash (0x00 for main net, 0x6f for testnet)
  • Perform Sha256 hash on the extended RIPEMD160 hash
  • Perform Sha256 hash on the result of the previous step
  • Take the first 4 byte of the second Sha256 result, it’s the checksum
  • Add 4 bytes of checksum in previous step, at the end of RIPEMD160 hash of step 4 (This is 25-byte bitcoin address)
  • Convert the result from byte string to base58 string

Our private key and address for testnet (but don’t reveal your private key)

Private key: “91izeJtyQ1DNGkiRtMGRKBEKYQTX46Ug8mGtKWpX9mDKqArsLpH”

Address: “moLoz9Ao9VTFMKp6AQaAwSVdzhfdfpCGf1”

bitcoin golang private-key blockchain

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Adding Messages On The Bitcoin Blockchain - A How-To Guide

Can I write on a Bitcoin Blockchain? Can I drop down a message? Yes, Luca of course you can! You can leave a message on a Bitcoin blockchain using a particular op code, called OP_RETURN.

Blockchain Certification | Blockchain Training Course | Blockchain Council

In all the market sectors, Blockchain technology has contributed to the redesign. The improvements that were once impossible have been pushed forward. Blockchain is one of the leading innovations with the ability to influence the various sectors...

What Is The Difference Between Blockchain And Bitcoin?

Our Blockchain Online Training will provide you to learn about Blockchain technology aspects with realty. Our Blockchain Course also includes live sessions, live Projects

What Is The Difference Between Blockchain And Bitcoin?

Get hands on experinece on block chain live industry experts with real world example at Block chain online Training. Enroll for free demo

The Evolution of The Public-Private Key Encryption in Blockchain Systems

There are many different methods to verify a user’s identification. Although the management of authentication and active sessions has come a long way, simple password authentication has not been able to provide sufficient security to support the rapid growth in data, advancements in mobile and cloud technologies, and increasing volumes of security breaches.Exposure of session data is only one example of where this authentication method can fall short