Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Jeiwan/blockchain_go
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: part_4
Choose a base ref
...
head repository: Jeiwan/blockchain_go
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: part_5
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.

Commits on Sep 7, 2017

  1. Copy the full SHA
    70c04fa View commit details
  2. Copy the full SHA
    24b1938 View commit details
  3. Save wallet to a file

    Jeiwan committed Sep 7, 2017
    Copy the full SHA
    8d7f945 View commit details
  4. Copy the full SHA
    5a1e6f7 View commit details
  5. Copy the full SHA
    caf7174 View commit details
  6. Implement Base58Decode

    Jeiwan committed Sep 7, 2017
    Copy the full SHA
    4805ce1 View commit details

Commits on Sep 8, 2017

  1. Copy the full SHA
    5b0e4ec View commit details
  2. Copy the full SHA
    deb7e2c View commit details
  3. Update usage

    Jeiwan committed Sep 8, 2017
    Copy the full SHA
    7510598 View commit details
  4. Copy the full SHA
    2b0619e View commit details
  5. Copy the full SHA
    6b40010 View commit details
  6. Fix Transaction.SetID

    Jeiwan committed Sep 8, 2017
    Copy the full SHA
    e6eed11 View commit details
  7. Copy the full SHA
    484d0bb View commit details
  8. Copy the full SHA
    cb17762 View commit details
  9. Copy the full SHA
    92be537 View commit details
  10. Copy the full SHA
    7e8c888 View commit details

Commits on Sep 10, 2017

  1. Copy the full SHA
    2ce04f8 View commit details
  2. Copy the full SHA
    fc0c819 View commit details
  3. Fix TXInput.UnlocksOutputWith

    Jeiwan committed Sep 10, 2017
    Copy the full SHA
    843858d View commit details
  4. Clean up base58.go

    Jeiwan committed Sep 10, 2017
    Copy the full SHA
    80e320a View commit details
  5. Copy the full SHA
    9421206 View commit details
  6. Copy the full SHA
    bb70b49 View commit details
  7. Copy the full SHA
    5f9e6c0 View commit details
  8. Copy the full SHA
    7290aaa View commit details
  9. Implement ValidateAddress

    Jeiwan committed Sep 10, 2017
    Copy the full SHA
    a436da6 View commit details
  10. Copy the full SHA
    bf8c5d2 View commit details
  11. Fix some initializations

    Jeiwan committed Sep 10, 2017
    Copy the full SHA
    7b6d569 View commit details
  12. Improve the printchain command

    Jeiwan committed Sep 10, 2017
    Copy the full SHA
    c0b4d6d View commit details

Commits on Sep 12, 2017

  1. Fix the 'checksum' function

    Jeiwan committed Sep 12, 2017
    Copy the full SHA
    465b85d View commit details
  2. Fix Signature resetting

    Jeiwan committed Sep 12, 2017
    Copy the full SHA
    b6f7626 View commit details
  3. Fix PubKey resetting

    Jeiwan committed Sep 12, 2017
    Copy the full SHA
    a6394c7 View commit details
  4. Update README

    Jeiwan committed Sep 12, 2017
    Copy the full SHA
    402b298 View commit details

Commits on Sep 19, 2017

  1. Revert "Fix the 'checksum' function"

    This reverts commit 465b85d.
    Jeiwan committed Sep 19, 2017
    Copy the full SHA
    201e7a1 View commit details

Commits on Jun 26, 2021

  1. Fix URLs in README

    Jeiwan committed Jun 26, 2021
    Copy the full SHA
    f35aa3c View commit details
Showing with 803 additions and 231 deletions.
  1. +1 −0 .gitignore
  2. +5 −4 README.md
  3. +59 −0 base58.go
  4. +24 −24 block.go
  5. +155 −121 blockchain.go
  6. +34 −0 blockchain_iterator.go
  7. +23 −53 cli.go
  8. +15 −0 cli_createblockchain.go
  9. +11 −0 cli_createwallet.go
  10. +25 −0 cli_getbalance.go
  11. +18 −0 cli_listaddress.go
  12. +30 −0 cli_printchain.go
  13. +22 −0 cli_send.go
  14. +146 −29 transaction.go
  15. +18 −0 transaction_input.go
  16. +29 −0 transaction_output.go
  17. +7 −0 utils.go
  18. +87 −0 wallet.go
  19. +94 −0 wallets.go
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.db
wallet.dat
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -2,7 +2,8 @@

A blockchain implementation in Go, as described in these articles:

1. [Basic Prototype](https://jeiwan.cc/posts/building-blockchain-in-go-part-1/)
2. [Proof-of-Work](https://jeiwan.cc/posts/building-blockchain-in-go-part-2/)
2. [Persistence and CLI](https://jeiwan.cc/posts/building-blockchain-in-go-part-3/)
3. [Transactions 1](https://jeiwan.cc/posts/building-blockchain-in-go-part-4/)
1. [Basic Prototype](https://jeiwan.net/posts/building-blockchain-in-go-part-1/)
2. [Proof-of-Work](https://jeiwan.net/posts/building-blockchain-in-go-part-2/)
2. [Persistence and CLI](https://jeiwan.net/posts/building-blockchain-in-go-part-3/)
3. [Transactions 1](https://jeiwan.net/posts/building-blockchain-in-go-part-4/)
3. [Addresses](https://jeiwan.net/posts/building-blockchain-in-go-part-5/)
59 changes: 59 additions & 0 deletions base58.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"bytes"
"math/big"
)

var b58Alphabet = []byte("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")

// Base58Encode encodes a byte array to Base58
func Base58Encode(input []byte) []byte {
var result []byte

x := big.NewInt(0).SetBytes(input)

base := big.NewInt(int64(len(b58Alphabet)))
zero := big.NewInt(0)
mod := &big.Int{}

for x.Cmp(zero) != 0 {
x.DivMod(x, base, mod)
result = append(result, b58Alphabet[mod.Int64()])
}

ReverseBytes(result)
for b := range input {
if b == 0x00 {
result = append([]byte{b58Alphabet[0]}, result...)
} else {
break
}
}

return result
}

// Base58Decode decodes Base58-encoded data
func Base58Decode(input []byte) []byte {
result := big.NewInt(0)
zeroBytes := 0

for b := range input {
if b == 0x00 {
zeroBytes++
}
}

payload := input[zeroBytes:]
for _, b := range payload {
charIndex := bytes.IndexByte(b58Alphabet, b)
result.Mul(result, big.NewInt(58))
result.Add(result, big.NewInt(int64(charIndex)))
}

decoded := result.Bytes()
decoded = append(bytes.Repeat([]byte{byte(0x00)}, zeroBytes), decoded...)

return decoded
}
48 changes: 24 additions & 24 deletions block.go
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import (
"time"
)

// Block keeps block headers
// Block represents a block in the blockchain
type Block struct {
Timestamp int64
Transactions []*Transaction
@@ -17,17 +17,21 @@ type Block struct {
Nonce int
}

// Serialize serializes the block
func (b *Block) Serialize() []byte {
var result bytes.Buffer
encoder := gob.NewEncoder(&result)
// NewBlock creates and returns Block
func NewBlock(transactions []*Transaction, prevBlockHash []byte) *Block {
block := &Block{time.Now().Unix(), transactions, prevBlockHash, []byte{}, 0}
pow := NewProofOfWork(block)
nonce, hash := pow.Run()

err := encoder.Encode(b)
if err != nil {
log.Panic(err)
}
block.Hash = hash[:]
block.Nonce = nonce

return result.Bytes()
return block
}

// NewGenesisBlock creates and returns genesis Block
func NewGenesisBlock(coinbase *Transaction) *Block {
return NewBlock([]*Transaction{coinbase}, []byte{})
}

// HashTransactions returns a hash of the transactions in the block
@@ -36,28 +40,24 @@ func (b *Block) HashTransactions() []byte {
var txHash [32]byte

for _, tx := range b.Transactions {
txHashes = append(txHashes, tx.ID)
txHashes = append(txHashes, tx.Hash())
}
txHash = sha256.Sum256(bytes.Join(txHashes, []byte{}))

return txHash[:]
}

// NewBlock creates and returns Block
func NewBlock(transactions []*Transaction, prevBlockHash []byte) *Block {
block := &Block{time.Now().Unix(), transactions, prevBlockHash, []byte{}, 0}
pow := NewProofOfWork(block)
nonce, hash := pow.Run()

block.Hash = hash[:]
block.Nonce = nonce
// Serialize serializes the block
func (b *Block) Serialize() []byte {
var result bytes.Buffer
encoder := gob.NewEncoder(&result)

return block
}
err := encoder.Encode(b)
if err != nil {
log.Panic(err)
}

// NewGenesisBlock creates and returns genesis Block
func NewGenesisBlock(coinbase *Transaction) *Block {
return NewBlock([]*Transaction{coinbase}, []byte{})
return result.Bytes()
}

// DeserializeBlock deserializes a block
Loading