├── go.mod ├── main.go ├── blockchain ├── block.go └── proof.go └── README.md /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/diop/blockchain-programmming-golang 2 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | 7 | "github.com/diop/blockchain-programmming-golang/blockchain" 8 | ) 9 | 10 | func main() { 11 | chain := blockchain.InitBlockChain() 12 | 13 | chain.AddBlock("First Block after Genesis") 14 | chain.AddBlock("Second Block after Genesis") 15 | chain.AddBlock("Third Block after Genesis") 16 | 17 | for _, block := range chain.Blocks { 18 | fmt.Printf("Previous Hash: %x\n", block.PrevHash) 19 | fmt.Printf("Data in Block: %s\n", block.Data) 20 | fmt.Printf("Hash: %x\n", block.Hash) 21 | 22 | pow := blockchain.NewProof(block) 23 | fmt.Printf("PoW: %s\n", strconv.FormatBool(pow.Validate())) 24 | fmt.Println() 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /blockchain/block.go: -------------------------------------------------------------------------------- 1 | package blockchain 2 | 3 | type BlockChain struct { 4 | Blocks []*Block 5 | } 6 | 7 | type Block struct { 8 | Hash []byte 9 | Data []byte 10 | PrevHash []byte 11 | Nonce int 12 | } 13 | 14 | func CreateBlock(data string, prevHash []byte) *Block { 15 | block := &Block{[]byte{}, []byte(data), prevHash, 0} 16 | pow := NewProof(block) 17 | nonce, hash := pow.Run() 18 | 19 | block.Hash = hash[:] 20 | block.Nonce = nonce 21 | 22 | return block 23 | } 24 | 25 | func (chain *BlockChain) AddBlock(data string) { 26 | prevBlock := chain.Blocks[len(chain.Blocks)-1] 27 | new := CreateBlock(data, prevBlock.Hash) 28 | chain.Blocks = append(chain.Blocks, new) 29 | } 30 | 31 | func Genesis() *Block { 32 | return CreateBlock("Genesis Block", []byte{}) 33 | } 34 | 35 | func InitBlockChain() *BlockChain { 36 | return &BlockChain{[]*Block{Genesis()}} 37 | } 38 | -------------------------------------------------------------------------------- /blockchain/proof.go: -------------------------------------------------------------------------------- 1 | package blockchain 2 | 3 | import ( 4 | "bytes" 5 | "crypto/sha256" 6 | "encoding/binary" 7 | "fmt" 8 | "log" 9 | "math" 10 | "math/big" 11 | ) 12 | 13 | // Take the data from the block 14 | 15 | // Create a counter (nonce) which starts at 0 16 | 17 | // create a hash of the data plus the counter 18 | 19 | // check the hash to see if it meets a set of requirements 20 | 21 | // Requirements: 22 | // The first few bytes must contains 0s 23 | 24 | const Difficulty = 12 25 | 26 | type ProofOfWork struct { 27 | Block *Block 28 | Target *big.Int 29 | } 30 | 31 | func NewProof(b *Block) *ProofOfWork { 32 | target := big.NewInt(1) 33 | target.Lsh(target, uint(256-Difficulty)) 34 | 35 | pow := &ProofOfWork{b, target} 36 | 37 | return pow 38 | } 39 | 40 | func (pow *ProofOfWork) InitData(nonce int) []byte { 41 | data := bytes.Join( 42 | [][]byte{ 43 | pow.Block.PrevHash, 44 | pow.Block.Data, 45 | ToHex(int64(nonce)), 46 | ToHex(int64(Difficulty)), 47 | }, 48 | []byte{}, 49 | ) 50 | return data 51 | } 52 | 53 | func (pow *ProofOfWork) Run() (int, []byte) { 54 | var intHash big.Int 55 | var hash [32]byte 56 | 57 | nonce := 0 58 | 59 | for nonce < math.MaxInt64 { 60 | data := pow.InitData(nonce) 61 | hash := sha256.Sum256(data) 62 | 63 | fmt.Printf("\r%x", hash) 64 | intHash.SetBytes(hash[:]) 65 | 66 | if intHash.Cmp(pow.Target) == -1 { 67 | break 68 | } else { 69 | nonce++ 70 | } 71 | } 72 | fmt.Println() 73 | 74 | return nonce, hash[:] 75 | 76 | } 77 | 78 | func (pow *ProofOfWork) Validate() bool { 79 | var intHash big.Int 80 | 81 | data := pow.InitData(pow.Block.Nonce) 82 | 83 | hash := sha256.Sum256(data) 84 | intHash.SetBytes(hash[:]) 85 | 86 | return intHash.Cmp(pow.Target) == -1 87 | } 88 | 89 | func ToHex(num int64) []byte { 90 | buff := new(bytes.Buffer) 91 | err := binary.Write(buff, binary.BigEndian, num) 92 | if err != nil { 93 | log.Panic(err) 94 | } 95 | 96 | return buff.Bytes() 97 | } 98 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Blockchain Programming with Go - Living Document 2 | A self-paced study guide for learning how to program blockchains in Go. 3 | 4 | ## Prerequisites 5 | This blockchain programming course assumes that you're already familiar with [Bitcoin](https://bitcoin.org) and know some [Go](https://golang.org/). 6 | 7 | + Bitcoin White Paper - Bitcoin: A Peer-to-Peer Electonic Cash System. 8 | * [https://bitcoin.org/bitcoin.pdf](https://bitcoin.org/bitcoin.pdf) 9 | 10 | + Bitcoin for developers - The Defacto Source of Truth 11 | * Documentation [https://bitcoin.org/en/developer-guide#block-chain](https://bitcoin.org/en/developer-guide#block-chain) 12 | 13 | + Bitcoin Lightning Engineering: 14 | * Main Site: [http://lightning.network/how-it-works/](http://lightning.network/how-it-works/) 15 | * Developers Documentation: [https://lightning.engineering/](https://lightning.engineering/) 16 | 17 | + Exposure to the Go(Golang) programming language: 18 | * To the minimum, you have attempted and finished: [A Tour of Go](https://tour.golang.org/welcome/1) 19 | * Ideally, you also have been exposed to: [Effective Go](https://golang.org/doc/effective_go.html) 20 | 21 | + Free Resources: 22 | * Code your own blockchain in less than 200 lines of Go! [Coral Health](https://medium.com/@mycoralhealth/code-your-own-blockchain-in-less-than-200-lines-of-go-e296282bcffc) 23 | * Building a Blockchain in Golang: [Youtube Series](https://youtu.be/mYlHT9bB6OE) 24 | * Bitcoin Development with Go: [https://gobitcoinbook.org/](https://gobitcoinbook.org/) 25 | * Cryptocurrency Cabal: [http://bitcoin-class.org/](http://bitcoin-class.org/) 26 | * Any videos by Todd Mcleod: [https://www.youtube.com/user/toddmcleod](https://www.youtube.com/user/toddmcleod) 27 | * Gophercises: [https://gophercises.com/](https://gophercises.com/) 28 | * Building a Blockchain in Go blog series: [https://jeiwan.cc/posts/building-blockchain-in-go-part-1/](https://jeiwan.cc/posts/building-blockchain-in-go-part-1/) 29 | * Create and Sign Bitcoin Transactions with Golang: [https://www.thepolyglotdeveloper.com/2018/03/create-sign-bitcoin-transactions-golang/](https://www.thepolyglotdeveloper.com/2018/03/create-sign-bitcoin-transactions-golang/) 30 | * Create A Bitcoin Hardware Wallet With Golang And A Raspberry Pi Zero: [https://www.thepolyglotdeveloper.com/2018/03/create-bitcoin-hardware-wallet-golang-raspberry-pi-zero/](https://www.thepolyglotdeveloper.com/2018/03/create-bitcoin-hardware-wallet-golang-raspberry-pi-zero/) 31 | * Building a Crypto Exchange (mostly in Go): [https://around25.com/blog/building-a-trading-engine-for-a-crypto-exchange/](https://around25.com/blog/building-a-trading-engine-for-a-crypto-exchange/) 32 | 33 | + Paid Resources: 34 | * Greater Commons: [Greater Commons](https://greatercommons.com/learn/golang) 35 | * Applied Go: [https://appliedgo.com/](https://appliedgo.com/) 36 | * Calhoun IO: [https://www.calhoun.io/courses](https://www.calhoun.io/courses) 37 | * Stephen Grider Udemy course: [https://www.udemy.com/go-the-complete-developers-guide/](https://www.udemy.com/go-the-complete-developers-guide/) 38 | 39 | + Find a job as a Go(Golang) Developer: 40 | * Golang Crypto: [https://golangcrypto.com/](https://golangcrypto.com/) 41 | * Crypto Jobs List: [https://cryptojobslist.com/](https://cryptojobslist.com/) 42 | * We Love Golang: [https://www.welovegolang.com/](https://www.welovegolang.com/) 43 | 44 | + Not Go(Golang) specific but still great: 45 | * Programming Blockchain: [https://programmingblockchain.com/](https://programmingblockchain.com/) 46 | * Bitcoin Dev Network: [https://bitcoindev.network/](https://bitcoindev.network/) 47 | * Justin Moon on Youtube: [https://www.youtube.com/channel/UCLp4OswuHySZZ3zrVBIoDjg/videos](https://www.youtube.com/channel/UCLp4OswuHySZZ3zrVBIoDjg/videos) 48 | * James Lopp's Bitcoin Resources list: [https://lopp.net/bitcoin.html](9https://lopp.net/bitcoin.html) 49 | * René Pickhardt: [https://www.youtube.com/user/renepickhardt](https://www.youtube.com/user/renepickhardts) 50 | 51 | + Attend the Programmable Money workshops in San Francisco, CA: 52 | * Programmable Money: [https://www.meetup.com/Programmable-Money/](https://www.meetup.com/Programmable-Money/) 53 | 54 | 55 | © Copyright 2019 Fodé Diop - MIT License --------------------------------------------------------------------------------