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 ¶
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 ¶
HashString returns the hex-encoded result of Hash().
func (*Block) Mine ¶
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 ¶
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 ¶
String returns a readable version of this block, including all of its transactions.
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) 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 ¶
NewIdentity constructs a new identity. In doing so it generates a new private/public key pair.
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.