blockchain

package
v0.0.0-...-ce5cf71 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2021 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateHash

func CalculateHash(data string) []byte

CalculateHash calculates the Hash of a generic string Value

func PrintPublicAddress

func PrintPublicAddress(k string) (string, error)

PrintPublicAddress prints the public key value from a given key.

Types

type Block

type Block struct {
	Id uuid.UUID

	Transactions TransactionList
	Hash         []byte
	PreviousHash []byte
	// contains filtered or unexported fields
}

Block to be added to an existing BlockChain.

func NewBlock

func NewBlock(t TransactionList) Block

NewBlock instantiates a new Block which can then be added to an existing BlockChain.

func (Block) CheckTransactions

func (b Block) CheckTransactions() bool

CheckTransactions in the Block. Calls VerifyTransactions underneath.

func (*Block) MineBlock

func (b *Block) MineBlock(d int32)

MineBlock recalculates the Hash to establish proof of work.

func (Block) ReCalculateHash

func (b Block) ReCalculateHash() []byte

ReCalculateHash is used for under the Verify function. Used for checking stored vs actual hashes.

func (Block) String

func (b Block) String() string

String outputs a Block in JSON string format.

type BlockChain

type BlockChain struct {
	Chain               []Block
	Difficulty          int32
	PendingTransactions TransactionList
	MiningReward        float32
}

BlockChain object to be instantiated and to have Blocks appended to its Genesis Block

func NewBlockChain

func NewBlockChain(d int32, mr float32) *BlockChain

NewBlockChain instantiates a new BlockChain to be used. Can append existing Blocks to it.

func (*BlockChain) GetAllBalances

func (bc *BlockChain) GetAllBalances() string

GetAllBalances in the BlockChain from Genesis.

func (*BlockChain) GetBalance

func (bc *BlockChain) GetBalance(address []byte) float32

GetBalance retrieves the address' current balance from Genesis Block -> last Block

func (BlockChain) GetLatestBlock

func (bc BlockChain) GetLatestBlock() Block

GetLatestBlock returns the final Block in the BlockChain

func (*BlockChain) MineTransactions

func (bc *BlockChain) MineTransactions(address []byte)

MineTransactions mines current pending Transactions on the BlockChain

func (*BlockChain) PushTransactions

func (bc *BlockChain) PushTransactions(tx ...Transaction) error

PushTransactions to the BlockChain, use MineTransactions to process them.

func (BlockChain) String

func (bc BlockChain) String() string

String outputs the BlockChain and its underlying Blocks in JSON string format.

func (BlockChain) Verify

func (bc BlockChain) Verify() bool

Verify is used to verify the integrity of the BlockChain. From the last Block to Genesis, the PreviousHash field should be linked to the proper prior Block's Hash field. Similar to a singly linked list.

type Key

type Key struct {
	Name string
	Key  *ecdsa.PrivateKey
}

Key object to handle BlockChain internals.

func LoadKey

func LoadKey(n string) (*Key, error)

LoadKey loads the Key from a file path to memory.

func NewKey

func NewKey(n string) (*Key, error)

NewKey will create a new ECDSA key pair under Key. If one already exists, it will return the current pair. Working directory is current directory.

func (*Key) Delete

func (k *Key) Delete() error

Delete the Key in the working directory.

type Transaction

type Transaction struct {
	FromAddress []byte
	ToAddress   []byte
	Amount      float32
	Signature   []byte
}

Transaction type to be used within a Block inside the BlockChain

func NewTransaction

func NewTransaction(f, t []byte, a float32) Transaction

NewTransaction instantiates a new transaction to be mined in the BlockChain

func ReadTransactionFromJSON

func ReadTransactionFromJSON() (*Transaction, error)

ReadTransactionFromJSON reads a Transaction from the working directory in transaciotn.json and loads it into memory.

func (Transaction) SaveTransactionToJSON

func (t Transaction) SaveTransactionToJSON() error

SaveTransactionToJSON saves a Transaction to the working directory named transaction.json. Only 1 Transaction can be saved.

func (*Transaction) SignTransaction

func (t *Transaction) SignTransaction(k *Key) error

SignTransaction signs a Transaction with a Key

func (Transaction) String

func (t Transaction) String() string

String outputs a Transaction in JSON string format.

func (*Transaction) VerifyTransaction

func (t *Transaction) VerifyTransaction() bool

VerifyTransaction checks the signature, calculated hash and public key to see if they match.

type TransactionList

type TransactionList []Transaction

TransactionList used to process Transactions in a BlockChain

func (TransactionList) String

func (tx TransactionList) String() string

String outputs a TransactionList in JSON string format.

Jump to

Keyboard shortcuts

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