Documentation ¶
Index ¶
- Constants
- Variables
- func HashPubKey(pubKey []byte) []byte
- func ValidateAddress(address string) bool
- type Account
- type Block
- type Blockchain
- func (bc *Blockchain) Foreach(fn func(*Block) error) error
- func (bc *Blockchain) GetBestHeight() int
- func (bc *Blockchain) GetBlockByHash(hash []byte) (Block, error)
- func (bc *Blockchain) GetBlockHashes() [][]byte
- func (bc *Blockchain) GetTransactionById(id []byte) (Transaction, error)
- func (bc *Blockchain) GetUTXO() map[string]TxOutputs
- func (bc *Blockchain) Mine(txs []*Transaction) (*Block, error)
- func (bc *Blockchain) SignTx(tx *Transaction, privateKey ecdsa.PrivateKey) error
- func (bc *Blockchain) Submit(block *Block) error
- func (bc *Blockchain) T()
- func (bc *Blockchain) VerifyTx(tx *Transaction) bool
- type ProofOfWork
- type Server
- type Transaction
- func (tx *Transaction) Hash() []byte
- func (tx *Transaction) IsCoinbase() bool
- func (tx *Transaction) Serialize() []byte
- func (tx *Transaction) Sign(privateKey ecdsa.PrivateKey, prevTxs map[string]Transaction) error
- func (tx *Transaction) String() string
- func (tx *Transaction) TrimmedCopy() Transaction
- func (tx *Transaction) Verify(prevTXs map[string]Transaction) (bool, error)
- type TxInput
- type TxOutput
- type TxOutputs
- type UTXOSet
- type Wallet
Constants ¶
const ( VersionCmd = "version" AddrCmd = "addr" BlockCmd = "block" GetDataCmd = "get_data" InvCmd = "inv" GetBlocksCmd = "get_blocks" TxCmd = "tx" )
Variables ¶
var ErrNoBlock = errors.New("no more block")
Functions ¶
func ValidateAddress ¶
ValidateAddress check if address is valid.
Types ¶
type Account ¶
type Account struct { PrivateKey ecdsa.PrivateKey PublicKey []byte }
Account stores private and public keys.
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 ¶
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 ¶
HashTransactions returns a hash of the transactions in the block.
type Blockchain ¶
type Blockchain struct {
// contains filtered or unexported fields
}
Blockchain implements interactions with a DB.
func CreateBlockchain ¶
func CreateBlockchain(node, address string) (*Blockchain, error)
CreateBlockchain creates a new blockchain DB.
func NewBlockchain ¶
func NewBlockchain(node string) (*Blockchain, error)
NewBlockchain creates a new Blockchain with genesis Block.
func (*Blockchain) GetBestHeight ¶
func (bc *Blockchain) GetBestHeight() int
GetBestHeight returns the height of the latest block
func (*Blockchain) GetBlockByHash ¶
func (bc *Blockchain) GetBlockByHash(hash []byte) (Block, error)
GetBlockByHash 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) GetTransactionById ¶
func (bc *Blockchain) GetTransactionById(id []byte) (Transaction, error)
GetTransactionById get a transaction by its ID.
func (*Blockchain) GetUTXO ¶
func (bc *Blockchain) GetUTXO() map[string]TxOutputs
GetUTXO get all unspent transaction outputs and returns transactions with spent outputs removed.
func (*Blockchain) Mine ¶
func (bc *Blockchain) Mine(txs []*Transaction) (*Block, error)
Mine mines a new block with the provided transactions.
func (*Blockchain) SignTx ¶
func (bc *Blockchain) SignTx(tx *Transaction, privateKey ecdsa.PrivateKey) error
SignTx signs inputs of a Transaction
func (*Blockchain) Submit ¶
func (bc *Blockchain) Submit(block *Block) error
Submit saves the block into the blockchain.
func (*Blockchain) T ¶
func (bc *Blockchain) T()
func (*Blockchain) VerifyTx ¶
func (bc *Blockchain) VerifyTx(tx *Transaction) bool
VerifyTx verifies transaction input signatures
type ProofOfWork ¶
type ProofOfWork struct {
// contains filtered or unexported fields
}
ProofOfWork represents a proof-of-work.
func NewProofOfWork ¶
func NewProofOfWork(block *Block) *ProofOfWork
NewProofOfWork builds and returns a ProofOfWork.
func (*ProofOfWork) Validate ¶
func (pow *ProofOfWork) Validate() bool
Validate validates block's PoW.
type Server ¶
func NewServerWithBlockchain ¶
func NewServerWithBlockchain(bc *Blockchain, id, miner string) *Server
func (*Server) SendTx ¶
func (n *Server) SendTx(tx *Transaction)
type Transaction ¶
Transaction represents a Bitcoin transaction.
func DeserializeTx ¶
func DeserializeTx(v []byte) Transaction
DeserializeTx deserializes a transaction.
func NewCoinbaseTx ¶
func NewCoinbaseTx(to, data string) *Transaction
NewCoinbaseTx creates a new coinbase transaction
func NewUTXOTransaction ¶
func NewUTXOTransaction(account *Account, to string, amount int, UTXOSet *UTXOSet) (*Transaction, error)
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(privateKey ecdsa.PrivateKey, prevTxs map[string]Transaction) error
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, error)
Verify verifies signatures of Transaction inputs.
type TxOutput ¶
TxOutput represents a transaction output.
func NewTxOutput ¶
NewTxOutput create a new TXOutput.
func (*TxOutput) IsLockedWithKey ¶
IsLockedWithKey checks if the output can be used by the owner of the pubkey
type TxOutputs ¶
type TxOutputs struct {
Values []TxOutput
}
TxOutputs collects TXOutput
func DeserializeTxOutputs ¶
DeserializeTxOutputs deserializes TXOutputs
type UTXOSet ¶
type UTXOSet struct {
// contains filtered or unexported fields
}
UTXOSet represents UTXO set.
func (*UTXOSet) GetSpendableOutputs ¶
GetSpendableOutputs finds and returns unspent outputs to reference in inputs.
type Wallet ¶
Wallet stores a collection of accounts.
func (*Wallet) GetAccount ¶
GetAccount returns an Account by its address
func (*Wallet) GetAddresses ¶
GetAddresses returns an array of addresses stored in the wallet file
func (*Wallet) NewAccount ¶
NewAccount adds an Account to Wallet.