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

Words
861
Reading
4 min
Listen
Play
8y

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