Building a Blockchain with Go - Part 6 - Adding Digital Signatures

Words
839
Reading
4 min
8y
Listen to this post
~6 min

golang.jpg

Repository

https://github.com/golang/go

What Will I Learn?

  • You will learn how to Lock Transactions
  • You will learn about Signatures
  • You will learn how to Verify Digital Signatures
  • You will learn how to use ECDSA
  • You will learn about Serializing, Hashing and Regenerating Transactions and Addresses
  • You will learn how to use the String Method in Go

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

Thus far in this Tutorial Series, most of the core blockchain features have been added to this example blockchain. With the wallet module now fully functional, it is possible to modify the transaction system to appropriately lock, verify and sign the data. This includes working backwards from an address to a public hash key and a checksum and then using those pieces to manipulate the wallets and the transactions.

Implementing Digital Signatures

All transactions must be signed in a blockchain. In Cryptography, a Digital Signature is a concept that guarantees core behavior of the blockchain:

  • The Signed Data can't be modified while it is being transferred from one address to another.
  • The sender of a transaction is the one who created that transaction.
  • Data sent by an address can not be denied by the user who sent the data.

The digital signature is derived from the private key of an account and verified with the public key of that account. The signature itself is stored in the Transaction Inputs and can be verified through the Outputs.

tx.png

This image details a flowchart of the current format of transactions and blocks in the blockchain. In this chart, User A mines the Genesis block and obtains a reward of 100 tokens. User A then sends 10 tokens to User B in Block 1. When User A creates the initial transaction, the input in Block 1 references the Output from the Genesis Block. This Input contains the public key of User A and a signature for the entire Transaction. The Blockchain verifies that the Input matches the hash of the referenced Output from the Genesis block. This is the mechanism that allows a user to spend tokens that are allocated to them.

Verification and Recreation of Hashes

The Verification process uses this idea of recreation and redundancy. To verify an address, the blockchain decodes the address using a base58 decoder algorithm and then splits the bytes of the resulting hash into pieces. Each of those pieces are used to rebuild the checksum of the address. This original checksum can then be compared with the new checksum to verify that the address is real. The same is true for a Transaction. When a signature is made, it is derived from two numbers which are concatenated and stored in an Input. The numbers can be split and then regenerated through an ecdsaalgorithm to verify that the transaction is real. Along with these two numbers is the public key of the user who created the transaction. Public keys are made up of two coordinate numbers on an elliptic curve and they also can be split and verified using an ecdsa algorithm.

address.png

Above is an image that contains an Address, the Address's full hash and that full hash split into the Version Hash, the Pub Key Hash and the checksum. Each of these components are combined to create the address and also can be used to re-build the checksum for verification.

verify.png

Here is the code which is used to verify a transaction. The variables R and S are variables which are derived from the signature of the address. These two numbers are originally a part of an elliptic curve algorithm. The X and Y values are the two halves of the Public Key and can similarly be split into separate pieces. On the final line, a new elliptic curve is used along with the x and y values from the transaction digital signature to regenerate and rebuild the public key. This public key is then used with R, S and Hashed Transaction Data to verify the Transaction Data.

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

Video Tutorial

Curriculum

Proof of Work Done

https://github.com/tensor-programming

Building a Blockchain with Go - Part 6 - Adding Digital Signatures | Ecency