core

package
v0.0.0-...-942729a Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2019 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HashPubKey

func HashPubKey(pubKey []byte) []byte

HashPubKey hashes public key

func ValidateAddress

func ValidateAddress(address string) bool

ValidateAddress check if address if valid

Types

type Block

type Block struct {
	Timestamp     int64
	Transactions  []*Transaction
	PrevBlockHash []byte
	Hash          []byte
	Nonce         int
	Height        int
}

Block represents a block in the blockchain

func DeserializeBlock

func DeserializeBlock(d []byte) *Block

DeserializeBlock deserializes a block

func NewBlock

func NewBlock(transactions []*Transaction, prevBlockHash []byte, height int) *Block

NewBlock creates and returns Block

func NewGenesisBlock

func NewGenesisBlock(coinbase *Transaction) *Block

NewGenesisBlock creates and returns genesis Block

func (*Block) HashTransactions

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

HashTransactions returns a hash of the transactions in the block

func (*Block) Serialize

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

Serialize serializes the block

type Blockchain

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

Blockchain implements interactions with a DB

func CreateBlockchain

func CreateBlockchain(address, nodeID string) *Blockchain

CreateBlockchain creates a new blockchain DB

func NewBlockchain

func NewBlockchain(nodeID string) *Blockchain

NewBlockchain creates a new Blockchain with genesis Block

func (*Blockchain) AddBlock

func (bc *Blockchain) AddBlock(block *Block)

AddBlock saves the block into the blockchain

func (*Blockchain) CloseConnection

func (bc *Blockchain) CloseConnection()

CloseConnection close db connection

func (*Blockchain) FindTransaction

func (bc *Blockchain) FindTransaction(ID []byte) (Transaction, error)

FindTransaction finds a transaction by its ID

func (*Blockchain) FindUTXO

func (bc *Blockchain) FindUTXO() map[string]TXOutputs

FindUTXO finds all unspent transaction outputs and returns transactions with spent outputs removed

func (*Blockchain) GetBestHeight

func (bc *Blockchain) GetBestHeight() int

GetBestHeight returns the height of the latest block

func (*Blockchain) GetBlock

func (bc *Blockchain) GetBlock(blockHash []byte) (Block, error)

GetBlock finds a block by its hash and returns it

func (*Blockchain) GetBlockHashes

func (bc *Blockchain) GetBlockHashes() [][]byte

GetBlockHashes returns a list of hashes of all the blocks in the chain

func (*Blockchain) Iterator

func (bc *Blockchain) Iterator() *BlockchainIterator

Iterator returns a BlockchainIterat

func (*Blockchain) MineBlock

func (bc *Blockchain) MineBlock(transactions []*Transaction) *Block

MineBlock mines a new block with the provided transactions

func (*Blockchain) SignTransaction

func (bc *Blockchain) SignTransaction(tx *Transaction, privKey ecdsa.PrivateKey)

SignTransaction signs inputs of a Transaction

func (*Blockchain) VerifyTransaction

func (bc *Blockchain) VerifyTransaction(tx *Transaction) bool

VerifyTransaction verifies transaction input signatures

type BlockchainIterator

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

BlockchainIterator is used to iterate over blockchain blocks

func (*BlockchainIterator) Next

func (i *BlockchainIterator) Next() *Block

Next returns next block starting from the tip

type MerkleNode

type MerkleNode struct {
	Left  *MerkleNode
	Right *MerkleNode
	Data  []byte
}

MerkleNode represent a Merkle tree node

func NewMerkleNode

func NewMerkleNode(left, right *MerkleNode, data []byte) *MerkleNode

NewMerkleNode creates a new Merkle tree node

type MerkleTree

type MerkleTree struct {
	RootNode *MerkleNode
}

MerkleTree represent a Merkle tree

func NewMerkleTree

func NewMerkleTree(data [][]byte) *MerkleTree

NewMerkleTree creates a new Merkle tree from a sequence of data

type ProofOfWork

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

ProofOfWork represents a proof-of-work

func NewProofOfWork

func NewProofOfWork(b *Block) *ProofOfWork

NewProofOfWork builds and returns a ProofOfWork

func (*ProofOfWork) Run

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

Run performs a proof-of-work

func (*ProofOfWork) Validate

func (pow *ProofOfWork) Validate() bool

Validate validates block's PoW

type TXInput

type TXInput struct {
	Txid      []byte
	Vout      int
	Signature []byte
	PubKey    []byte
}

TXInput represents a transaction input

func (*TXInput) UsesKey

func (in *TXInput) UsesKey(pubKeyHash []byte) bool

UsesKey checks whether the address initiated the transaction

type TXOutput

type TXOutput struct {
	Value      int
	PubKeyHash []byte
}

TXOutput represents a transaction output

func NewTXOutput

func NewTXOutput(value int, address string) *TXOutput

NewTXOutput create a new TXOutput

func (*TXOutput) IsLockedWithKey

func (out *TXOutput) IsLockedWithKey(pubKeyHash []byte) bool

IsLockedWithKey checks if the output can be used by the owner of the pubkey

func (*TXOutput) Lock

func (out *TXOutput) Lock(address []byte)

Lock signs the output

type TXOutputs

type TXOutputs struct {
	Outputs []TXOutput
}

TXOutputs collects TXOutput

func DeserializeOutputs

func DeserializeOutputs(data []byte) TXOutputs

DeserializeOutputs deserializes TXOutputs

func (TXOutputs) Serialize

func (outs TXOutputs) Serialize() []byte

Serialize serializes TXOutputs

type Transaction

type Transaction struct {
	ID   []byte
	Vin  []TXInput
	Vout []TXOutput
}

Transaction represents a Bitcoin transaction

func DeserializeTransaction

func DeserializeTransaction(data []byte) Transaction

DeserializeTransaction deserializes a transaction

func NewCoinbaseTX

func NewCoinbaseTX(to, data string) *Transaction

NewCoinbaseTX creates a new coinbase transaction

func NewUTXOTransaction

func NewUTXOTransaction(wallet *Wallet, to string, amount int, UTXOSet *UTXOSet) *Transaction

NewUTXOTransaction creates a new transaction

func (*Transaction) Hash

func (tx *Transaction) Hash() []byte

Hash returns the hash of the Transaction

func (Transaction) IsCoinbase

func (tx Transaction) IsCoinbase() bool

IsCoinbase checks whether the transaction is coinbase

func (Transaction) Serialize

func (tx Transaction) Serialize() []byte

Serialize returns a serialized Transaction

func (*Transaction) Sign

func (tx *Transaction) Sign(privKey ecdsa.PrivateKey, prevTXs map[string]Transaction)

Sign signs each input of a Transaction

func (Transaction) String

func (tx Transaction) String() string

String returns a human-readable representation of a transaction

func (*Transaction) TrimmedCopy

func (tx *Transaction) TrimmedCopy() Transaction

TrimmedCopy creates a trimmed copy of Transaction to be used in signing

func (*Transaction) Verify

func (tx *Transaction) Verify(prevTXs map[string]Transaction) bool

Verify verifies signatures of Transaction inputs

type UTXOSet

type UTXOSet struct {
	Blockchain *Blockchain
}

UTXOSet represents UTXO set

func (UTXOSet) CountTransactions

func (u UTXOSet) CountTransactions() int

CountTransactions returns the number of transactions in the UTXO set

func (UTXOSet) FindSpendableOutputs

func (u UTXOSet) FindSpendableOutputs(pubkeyHash []byte, amount int) (int, map[string][]int)

FindSpendableOutputs finds and returns unspent outputs to reference in inputs

func (UTXOSet) FindUTXO

func (u UTXOSet) FindUTXO(pubKeyHash []byte) []TXOutput

FindUTXO finds UTXO for a public key hash

func (UTXOSet) Reindex

func (u UTXOSet) Reindex()

Reindex rebuilds the UTXO set

func (UTXOSet) Update

func (u UTXOSet) Update(block *Block)

Update updates the UTXO set with transactions from the Block The Block is considered to be the tip of a blockchain

type Wallet

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

Wallet stores private and public keys

func NewWallet

func NewWallet(private ecdsa.PrivateKey, public []byte) *Wallet

NewWallet creates and returns a Wallet

func (Wallet) GetAddress

func (w Wallet) GetAddress() []byte

GetAddress returns wallet address

type Wallets

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

Wallets stores a collection of wallets

func NewWallets

func NewWallets(nodeID string) (*Wallets, error)

NewWallets creates Wallets and fills it from a file if it exists

func (*Wallets) CreateWallet

func (ws *Wallets) CreateWallet() string

CreateWallet adds a Wallet to Wallets

func (*Wallets) GetAddresses

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

GetAddresses returns an array of addresses stored in the wallet file

func (Wallets) GetWallet

func (ws Wallets) GetWallet(address string) Wallet

GetWallet returns a Wallet by its address

func (*Wallets) LoadFromFile

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

LoadFromFile loads wallets from the file

func (Wallets) SaveToFile

func (ws Wallets) SaveToFile(nodeID string)

SaveToFile saves wallets to a file

Jump to

Keyboard shortcuts

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