Building a Blockchain in Golang - Part 5 - Building a Basic Wallet Module

in #utopian-io5 years ago (edited)

golang.jpg

Repository

https://github.com/golang/go

What Will I Learn?

  • You will learn about Wallets in Blockchains
  • You will learn how to generate Private and Public Keys
  • You will learn about Elliptical Curve Algorithms
  • You will learn how to create a Checksum and a Public Key Hash
  • You will learn how to generate an address with Base58
  • You will learn how to save a set of encrypted data to a local file

Requirements

System Requirements:

Operating System:

  • FreeBSD 10.3 or later
  • Linux 2.6.23 or later with glibc
  • macOS 10.10 or later
  • Windows 7, Server 2008R2 or later

Required Knowledge

  • A little understanding of the Go programming language
  • Go installed on your computer
  • A text editor or IDE like Gogland (VS Code used)

Resources for Go and this Project:

Credits/Sources:

Difficulty

  • Advanced

Description

In the last tutorial, we set about building the basics of our transaction system for the blockchain. In this tutorial, we build on those concepts by creating a wallet module. This wallet module allows the application to generate new public and private key pairs through an elliptical curve algorithm. It also is able to generate addresses so that a user can send and receive tokens using a Base58 encryption algorithm. All of these local addresses can be saved in memory and on a file so that the user can easily recall their public and private key without having to see those values.

Describing Wallets with Public and Private Keys

Blockchains are Open and Public Databases and therefore, there is no place to store information about the users who use the blockchain. To identify which user owns which assets and tokens in the transactions, the Blockchain uses Private and Public key pairs. The private key is a set of randomized data which must stay secure and hidden to prevent a breach of security. If a user holds a private key then that user also holds any assets which are attached to the associated Public Key/Address. The Public key is derived from the Private Key using a cryptographic method called Ecliptic Curve Digital Signing. This algorithm allows the blockchain to generate 10^77 different wallets and this makes it almost impossible for two users to have the same key pair.

ec.png

An elliptic curve is a mathematical curve that can come in various different forms as seen in the image above. In the Blockchain, the Private Key uses data from a random number between 0 and 2^256 and this number is stored as bytes. This number along with a random point on the selected elliptic curve are used to create the Public Key. The random point acts as the starting point and then we choose a new random point on the curve again and again until we've chosen a sum total of points that equals the number that represents the Private Key. This is a process called Elliptic Curve Multiplication. The final coordinate is used to build the Public Key; with the X and Y values being converted into bytes and concatenated together.

Using Hash and Encoding Algorithms to Represent Data

Once a Public and Private Key are generated for a wallet, a set of hash and encoded representations for the Public and Private Keys can also be created to secure the blockchain and create human readable addresses. The Address is used for sending and receiving data to and from other users in the blockchain. Also, derivatives of the Public and Private Keys are used to sign, lock and verify transactions inside of the blockchain.

algo.png

The image above shows various flowcharts for converting the Public and Private Keys into necessary components for our blockchain. The Private Key is passed through the ECDSA or Ecliptic Curve Digital Signing Algorithm to create the Public Key. This Public Key can then be passed through a Sha256 256 bit hashing function and the result of this action is then also passed through a Ripemd160 160 bit hashing algorithm. This produces the main Public Key Hash which is used to create the address as well as the checksum. The Public Key Hash is passed through two Sha256 256 bit algorithms to create a new Hash and then the first 4 bytes/8 characters are stripped from that hash; these 4 bytes are the Checksum. The Checksum is combined to the Public Key Hash and the Version byte and then the resulting data is passed through a Base58 encoding algorithm to create the Address for this wallet. Unlike a hash function, the Base58 Algorithm, can be reversed to facilitate various features in the blockchain.

The Source Code for this video may be found here: https://github.com/tensor-programming/golang-blockchain/tree/part_5

Video Tutorial

Curriculum

Proof of Work Done

https://github.com/tensor-programming

Sort:  

Hi @tensor

Thank you for another fantastic video tutorial. I love this tutorial as it is very easy to understand especially with all the flowcharts you put in there.

I appreciate all the extra effort you put into the tutorial, especially in outlining the flowcharts to make learning easy for students to follow.

Keep up the great work you are doing.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

I am very happy with this tutorial compared to my last one. I chose to make it shorter and more detailed, and finding mermaidJS really changed things. Having flow charts and visual aids for algorithms is a big deal, and I don't have to resort to using MSPaint anymore.

As always thanks for moderating my contribution,

Tensor

Thank you for your review, @rosatravels! Keep up the good work!





This post has been voted on by the SteemSTEM curation team and voting trail in collaboration with @curie.

If you appreciate the work we are doing then consider voting both projects for witness by selecting stem.witness and curie!

For additional information please join us on the SteemSTEM discord and to get to know the rest of the community!

Hi, @tensor!

You just got a 7.34% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.

Hi @tensor!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @tensor!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.24
TRX 0.11
JST 0.032
BTC 62482.14
ETH 3044.68
USDT 1.00
SBD 3.76