blockchain

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2022 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const Difficulty = 12

Difficulty of mining a new block

Variables

This section is empty.

Functions

func Checksum added in v1.2.2

func Checksum(payload []byte) []byte

Checksum generates a checksum of the public key hash

func DBExists added in v1.2.1

func DBExists(dbPath string) bool

Check if blockchain database exists

func NewKeyPair added in v1.2.2

func NewKeyPair() (ecdsa.PrivateKey, []byte)

NewKeyPair generates a public and private key pair

func PublicKeyHash added in v1.2.2

func PublicKeyHash(pubKey []byte) []byte

PublicKeyHash generates a public key hash

func ValidateAddress added in v1.2.2

func ValidateAddress(address string) bool

ValidateAddress checks if the address is valid

Types

type Block

type Block struct {
	Hash         []byte
	Transactions []*Transaction
	PrevHash     []byte
	Nonce        int
}

func CreateBlock

func CreateBlock(txs []*Transaction, prevHash []byte) *Block

CreateBlock creates a new block using the data and the previous block's hash

func Deserialize

func Deserialize(data []byte) *Block

Deserializes the data of a block

func Genesis

func Genesis(tx *Transaction) *Block

Genesis creates the first block in the chain

func (*Block) HashTransactions added in v1.2.1

func (b *Block) HashTransactions() []byte

HashTransactions returns a hash of the transactions in the block

func (*Block) Serialize

func (b *Block) Serialize() []byte

Serialize the block into bytes

type BlockChain

type BlockChain struct {
	LastHash []byte
	Database *badger.DB
}

BlockChain structure

func ContinueBlockChain added in v1.2.1

func ContinueBlockChain(dbPath string) *BlockChain

Continue a blockchain by pulling the last hash

func InitBlockChain

func InitBlockChain(dbPath string, address string) *BlockChain

InitBlockChain creates a new blockchain with a genesis block

func (*BlockChain) AddBlock

func (chain *BlockChain) AddBlock(transactions []*Transaction)

AddBlock is a helper function that adds a new block to the chain using the previous block's hash

func (*BlockChain) FindSpendableOutputs added in v1.2.1

func (chain *BlockChain) FindSpendableOutputs(address string, amount int) (int, map[string][]int)

Find transaction outputs for a given address that can be used as inputs for a new transaction

func (*BlockChain) FindUTXO added in v1.2.1

func (chain *BlockChain) FindUTXO(address string) []TXOutput

Find all unspent transaction outputs for a given address

func (*BlockChain) FindUnspentTransactions added in v1.2.1

func (chain *BlockChain) FindUnspentTransactions(address string) []Transaction

Find all transactions for a given address that is unspent

func (*BlockChain) Iterator

func (chain *BlockChain) Iterator() *BlockChainIterator

Iterator returns a BlockChainIterator that can be used to iterate over the blockchain.

type BlockChainIterator

type BlockChainIterator struct {
	CurrentHash []byte
	Database    *badger.DB
}

An iterator for iterating the blockchain in database

func (*BlockChainIterator) Next

func (iter *BlockChainIterator) Next() *Block

Next returns the next block in the blockchain

type ProofOfWork

type ProofOfWork struct {
	Block  *Block
	Target *big.Int
}

Proof of Work structure

func NewProof

func NewProof(b *Block) *ProofOfWork

Create a new ProofOfWork

func (*ProofOfWork) InitData

func (pow *ProofOfWork) InitData(nonce int) []byte

Prepare data to run the proof of work algorithm

func (*ProofOfWork) Run

func (pow *ProofOfWork) Run() (int, []byte)

Run the proof of work algorithm to find a number that satisfies the target

func (*ProofOfWork) Validate

func (pow *ProofOfWork) Validate() bool

Validate the nonce and the hash of the block

type TXInput added in v1.2.1

type TXInput struct {
	ID  []byte
	Out int
	Sig string
}

A transaction input

func (*TXInput) CanUnlock added in v1.2.1

func (in *TXInput) CanUnlock(data string) bool

Check if the transaction input can be unlocked with the given data

type TXOutput added in v1.2.1

type TXOutput struct {
	Value  int
	PubKey string
}

A transaction output

func (*TXOutput) CanBeUnlocked added in v1.2.1

func (out *TXOutput) CanBeUnlocked(data string) bool

Check if the transaction output can be unlocked with the given data

type Transaction added in v1.2.1

type Transaction struct {
	ID      []byte
	Inputs  []TXInput
	Outputs []TXOutput
}

A blockchain transaction

func CoinbaseTx added in v1.2.1

func CoinbaseTx(to, data string) *Transaction

Make a transaction to the given address

func NewTransaction added in v1.2.1

func NewTransaction(from string, to string, amount int, chain *BlockChain) (*Transaction, error)

func (*Transaction) IsCoinbase added in v1.2.1

func (tx *Transaction) IsCoinbase() bool

Check if the transaction is a Coinbase transaction

func (*Transaction) SetID added in v1.2.1

func (tx *Transaction) SetID()

Set the ID of the transaction

type Wallet added in v1.2.2

type Wallet struct {
	PrivateKey ecdsa.PrivateKey
	PublicKey  []byte
}

Wallet represents a wallet in the blockchain

func MakeWallet added in v1.2.2

func MakeWallet() *Wallet

MakeWallet creates a new wallet

func (Wallet) Address added in v1.2.2

func (w Wallet) Address() []byte

Address gets the address of the wallet

type Wallets added in v1.2.2

type Wallets struct {
	Wallets map[string]*Wallet
}

Wallets represents a collection of wallets

func CreateWallets added in v1.2.2

func CreateWallets(filePath string) (*Wallets, error)

Create wallets

func (*Wallets) AddWallet added in v1.2.2

func (ws *Wallets) AddWallet() string

Add a wallet to the collection

func (*Wallets) GetAllAddresses added in v1.2.2

func (ws *Wallets) GetAllAddresses() []string

Get all wallet addresses

func (*Wallets) GetWallet added in v1.2.2

func (ws *Wallets) GetWallet(address string) Wallet

Get a wallet by address

func (*Wallets) LoadFromFile added in v1.2.2

func (ws *Wallets) LoadFromFile(filePath string) error

Load the wallets from a file

func (*Wallets) Persist added in v1.2.2

func (ws *Wallets) Persist(filePath string) error

Save the wallets to a file

Jump to

Keyboard shortcuts

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