blockchain

package module
v0.0.0-...-5a641bd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2018 License: Apache-2.0 Imports: 13 Imported by: 0

README

Blockchain

This repository implements a very simple Blockchain, loosely based off of this post and its sequel: https://medium.com/programmers-blockchain/create-simple-blockchain-java-tutorial-from-scratch-6eeed3cb03fa

See the test file for basic usage.

Documentation

Overview

Package blockchain implements a simple blockchain.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

type Block struct {
	// contains filtered or unexported fields
}

Block represents a single piece of data in the blockchain.

func (Block) Hash

func (b Block) Hash() []byte

Hash calculates the block's hash. It uses the previous block's hash along with this block's timestamp, nonce, and data.

func (Block) HashString

func (b Block) HashString() string

HashString returns the hex-encoded result of Hash().

func (*Block) Mine

func (b *Block) Mine() string

Mine attempts to make this block valid by searching for a nonce value that will qualify as proof-of-work. Once it succeeds, it returns the resulting hex-encoded hash.

func (*Block) SendTransaction

func (b *Block) SendTransaction(from Identity, to *ecdsa.PublicKey, data []byte) error

SendTransaction sends a transaction from the identity "from" to the public key "to". The transaction is automatically signed, returning an error if signing fails.

func (Block) String

func (b Block) String() string

String returns a readable version of this block, including all of its transactions.

func (Block) Timestamp

func (b Block) Timestamp() time.Time

Timestamp returns the block's timestamp.

func (Block) Transactions

func (b Block) Transactions() []Transaction

Data returns the block's transactions.

type Blockchain

type Blockchain struct {
	// contains filtered or unexported fields
}

Blockchain represents the blockchain.

func New

func New(difficulty int) Blockchain

New constructs a new Blockchain with the provided mining difficulty.

func (Blockchain) ForEach

func (c Blockchain) ForEach(f func(*Block))

ForEach calls f once with each block on the chain.

func (Blockchain) Len

func (c Blockchain) Len() int

Len returns the length of the blockchain.

func (Blockchain) NewBlock

func (c Blockchain) NewBlock() *Block

Add adds a new block to the chain, returning a reference to it.

func (Blockchain) Valid

func (c Blockchain) Valid() bool

Valid checks if this blockchain is valid. For a blockchain to be valid, each block must have valid proof-of-work, and each previous hash reference must match that of the previous block.

func (Blockchain) WorkProven

func (c Blockchain) WorkProven(hash string) bool

WorkProven returns true if the provided hex-encoded hash counts as valid proof-of-work.

type Identity

type Identity struct {
	// contains filtered or unexported fields
}

Identity represents a user of the blockchain. It's analogous to bitcoin's wallet in that it is used to sign messages.

func NewIdentity

func NewIdentity() (Identity, error)

NewIdentity constructs a new identity. In doing so it generates a new private/public key pair.

func (Identity) PublicKey

func (i Identity) PublicKey() *ecdsa.PublicKey

PublicKey returns the public key associated with this identity.

type Transaction

type Transaction struct {
	// contains filtered or unexported fields
}

Transaction represents a signed message on the blockchain.

func (Transaction) Data

func (t Transaction) Data() []byte

Data returns the underlying data of this transaction.

func (Transaction) Hash

func (t Transaction) Hash() []byte

Hash returns this transaction's hash, which serves as an identifier.

func (Transaction) Receiver

func (t Transaction) Receiver() string

Receiver returns a hex-encoded version of the receiver's public key.

func (Transaction) Sender

func (t Transaction) Sender() string

Sender returns a hex-encoded version of the sender's public key.

func (*Transaction) Sign

func (t *Transaction) Sign(identity Identity) error

Sign signs the transaction using the given identity. It must be equal to the sender of the message, but for security reasons we don't want to save the private key within the transaction itself.

func (*Transaction) Signed

func (t *Transaction) Signed() bool

Signed returns true if the transaction was signed and could be verified, otherwise false.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL