Documentation ¶
Index ¶
- Constants
- func Base58Decode(input []byte) []byte
- func Base58Encode(input []byte) []byte
- func BlockFile(h int) string
- func ChainExists() bool
- func CheckValidAddress(address []byte) bool
- func CreateChecksum(payload []byte) []byte
- func FormatB(hash []byte) []byte
- func FormatC(txID []byte) []byte
- func GobEncode(i interface{}) ([]byte, error)
- func HashPublicKey(pubKey []byte) ([]byte, error)
- func NewKeyPair() (ecdsa.PrivateKey, []byte, error)
- func ReformatKey(key []byte) []byte
- func ReverseBytes(data []byte)
- type BCIterator
- type Block
- type Blockchain
- func (bc *Blockchain) AddBlock(TXs []Transaction) error
- func (bc Blockchain) CompareBlocks(height int32, hash []byte) (bool, error)
- func (bc *Blockchain) CreateGenesisBlock(address string) Block
- func (bc Blockchain) FindUTXOs() (map[string]*UTXOutputs, error)
- func (bc Blockchain) GetChainHeight() (int32, error)
- func (bc Blockchain) GetTailHash() ([]byte, error)
- func (bc Blockchain) NewIterator() (*BCIterator, error)
- func (bc *Blockchain) NewTransaction(from, to string, amount int) (Transaction, error)
- func (bc *Blockchain) UpdateWithNewBlock(block Block) error
- type Input
- type Output
- type Transaction
- func (tx Transaction) Hash() ([]byte, error)
- func (tx Transaction) IsCoinbase() bool
- func (tx Transaction) Serialize() ([]byte, error)
- func (tx *Transaction) Sign(private ecdsa.PrivateKey, prevTXs map[string]Transaction) error
- func (tx Transaction) TrimmedTransaction() Transaction
- func (tx Transaction) Verify(prevTXs map[string]Transaction) (bool, error)
- type UTXO
- func (u UTXO) FindReferencedOutputs(tx Transaction) (map[string]Transaction, error)
- func (u UTXO) FindSpendableOutputs(address []byte, amount int) (int, map[string][]int, error)
- func (u UTXO) FindTransaction(txID []byte, blockHeight int) (Transaction, error)
- func (u UTXO) FindUTXOs() (map[string]*UTXOutputs, error)
- func (u UTXO) Reindex() error
- func (u UTXO) SignTransaction(tx Transaction, private ecdsa.PrivateKey) error
- func (u UTXO) Update(block Block) error
- func (u UTXO) VerifyTransaction(tx Transaction) (bool, error)
- type UTXOutputs
- type Wallet
- type Wallets
Constants ¶
const (
UTXOBucket = "chainstate"
)
Variables ¶
This section is empty.
Functions ¶
func Base58Encode ¶
Base58Encode encodes a byte array to Base58
func CheckValidAddress ¶
CheckValidAddress checks if a wallet address is indeed a valid address. It does so by reversing the process used to create the address, and then checks the checksums against each other. First it base58 decodes the address. Then separates the version, pubKeyHash, and checksum. Then it checks if the checksum of the version+hash matches the address's checksum
func CreateChecksum ¶
CreateChecksum creates a checksum for a version+hash(of pub key) combo. It simply double hashes the payload, and returns the last 4 bytes.
func FormatB ¶
FormatB formats a block hash, and joins it with the letter 'b'. Used when inserting a new block in the db
func HashPublicKey ¶
HashPublicKey hashes a public key. First it runs a sha512 hashing on the key, then sends that output through a RIPEMD160 Hasher.
func NewKeyPair ¶
func NewKeyPair() (ecdsa.PrivateKey, []byte, error)
NewKeyPair generates a new ecdsa keypair. The publicKey is actually two []byte appended together. In practice, when verifying a signature with the publicKey, split them back up into x and y values
func ReformatKey ¶
Types ¶
type BCIterator ¶
type BCIterator struct { LastHash []byte // LastHash is the hash of the block the iterator will iterate over next. DB *bolt.DB // DB is a pointer to an open boltDB connection }
BCIterator is an instance of a blockchain iterator
func (*BCIterator) Next ¶
func (bci *BCIterator) Next() Block
Next iterates over a blockchain and gets each block in the chain. It starts with the top and goes top -> down
type Block ¶
type Block struct { Timestamp int64 // Timestamp is the time when the block was created. Hash []byte // Hash is sha512 64-byte hash of the block. Its unique identifier. PrevHash []byte // PrevHash is the previous blocks hash. Transactions []Transaction Height int // Height is the index of the block in the blockchain Validator []byte // Validator is the winner of the Proof of Stake lottery Winner []byte // Winner is the winner of the random file lottery }
Block is an instance of a single block.
func DecodeBlock ¶
DecodeBlock takes in an encoded block, decodes it, and then returns the block.
func NewBlock ¶
func NewBlock(PrevBlock Block, TXs []Transaction) (Block, error)
NewBlock takes the previous block, some data, and then creates a new block
func ReadBlockFromFile ¶
ReadBlocksFromFile reads a block in from a file, with the file name being its height followed by .dat. For example, 400.dat TODO: perhaps we should make height an in32 instead of int
func (Block) EncodeBlock ¶
EncodeBlock encodes a block to a byte slice, this allows the block to be saved to file.
func (Block) GenerateHash ¶
GenerateHash generates a new sha512 hash for a block Eventually replace this when implementing proof
func (Block) SaveToFile ¶
SaveToFile saves a block to file. If the block height is 518 then the file will be 518.dat
type Blockchain ¶
Blockchain is a single instance of the blockchain.
func CreateBlockchain ¶
func CreateBlockchain(address string) (*Blockchain, error)
CreateBlockchain is responsible for either creating and returning, or just returning a blockchain instance. If there are no blocks, then create a new genesis and blockchain. Otherwise just return a blockchain instance.
func (*Blockchain) AddBlock ¶
func (bc *Blockchain) AddBlock(TXs []Transaction) error
AddBlock adds a block to the blockchain. It first gets the previous block, and then creates a new block. It saves the new block to a file, and updates the block bucket, storing the file number under the key 'l'.
func (Blockchain) CompareBlocks ¶
func (bc Blockchain) CompareBlocks(height int32, hash []byte) (bool, error)
func (*Blockchain) CreateGenesisBlock ¶
func (bc *Blockchain) CreateGenesisBlock(address string) Block
CreateGenesisBlock creates the first (genesis) block of a chain.
func (Blockchain) FindUTXOs ¶
func (bc Blockchain) FindUTXOs() (map[string]*UTXOutputs, error)
eventually create this where chainstate db stores state of UTXO's
func (Blockchain) GetChainHeight ¶
func (bc Blockchain) GetChainHeight() (int32, error)
func (Blockchain) GetTailHash ¶
func (bc Blockchain) GetTailHash() ([]byte, error)
func (Blockchain) NewIterator ¶
func (bc Blockchain) NewIterator() (*BCIterator, error)
NewIterator creates a new blockchain iterator
func (*Blockchain) NewTransaction ¶
func (bc *Blockchain) NewTransaction(from, to string, amount int) (Transaction, error)
func (*Blockchain) UpdateWithNewBlock ¶
func (bc *Blockchain) UpdateWithNewBlock(block Block) error
UpdateWithNewBlock updates the blockchain with a new block. Used when receiving a block from another node.
type Input ¶
type Input struct { TransactionID []byte // TransactionID is the ID of the transaction that houses the output that this input references. OutputIndex int // OutputIndex is the index of the output on the transaction. Signature []byte // Signature stores the signature of the transaction after it gets signed. This signature can then be verified. PubKey []byte // PubKey is the full public key of the one who created this input by creating a transaction. I.e: the sender. }
Input is a single Transaction input Inputs always reference outputs, unless they are part of a coinbase transaction.
type Output ¶
type Output struct { Value int // The amount of 'coins' stored in this output. PubKeyHash []byte // The public key hash of the owner of the coins. This hash is a double sha512 hash of the owners public key. }
Output is a single output instance. Outputs exist withing transactions. They are where 'coins' are stored, and are locked with a public key hash.
func CreateOutput ¶
CreateOutput creates an output for an address, with an amount, and then locks the output to that address
func (Output) CanBeUnlocked ¶
CanBeUnlocked checks if an address is the one who locked the output.
type Transaction ¶
type Transaction struct { ID []byte Vout []Output Vin []Input // implemented since two cb tx's were ending up with duplicate hashes Timestamp int64 }
func DeserializeTransaction ¶
func DeserializeTransaction(data []byte) (Transaction, error)
func NewCoinbaseTransaction ¶
func NewCoinbaseTransaction(address string) (Transaction, error)
func (Transaction) Hash ¶
func (tx Transaction) Hash() ([]byte, error)
func (Transaction) IsCoinbase ¶
func (tx Transaction) IsCoinbase() bool
TOOD: update this as transaction gets more complicated
func (Transaction) Serialize ¶
func (tx Transaction) Serialize() ([]byte, error)
func (*Transaction) Sign ¶
func (tx *Transaction) Sign(private ecdsa.PrivateKey, prevTXs map[string]Transaction) error
Sign is responsible for the logic behind signing a tx. Signing validates that when a transaction is made, the owner of the output is the one making the transaction. It does so by creating a trimmed transaction, setting the publicKey of each input to that of the output it is referencing, and then hashing that trimmed transaction. Then the private key and trimmed id get signed together to form a two piece signature. Those are appended and that is the signature. That signature can now be verified with the public key, and the end result of the same hashedTX process.
func (Transaction) TrimmedTransaction ¶
func (tx Transaction) TrimmedTransaction() Transaction
TrimmedTransaction takes a transaction and removes the pubKey + signature from the inputs. This is in preparation for signing, as we don't need to sign the entire tx.
func (Transaction) Verify ¶
func (tx Transaction) Verify(prevTXs map[string]Transaction) (bool, error)
type UTXO ¶
type UTXO struct {
Blockchain *Blockchain
}
UTXO stands for unspent transaction outputs.
func (UTXO) FindReferencedOutputs ¶
func (u UTXO) FindReferencedOutputs(tx Transaction) (map[string]Transaction, error)
func (UTXO) FindSpendableOutputs ¶
func (UTXO) FindTransaction ¶
func (u UTXO) FindTransaction(txID []byte, blockHeight int) (Transaction, error)
func (UTXO) SignTransaction ¶
func (u UTXO) SignTransaction(tx Transaction, private ecdsa.PrivateKey) error
func (UTXO) VerifyTransaction ¶
func (u UTXO) VerifyTransaction(tx Transaction) (bool, error)
type UTXOutputs ¶
type UTXOutputs struct { Outputs []Output // A list of outputs Indexes []int // Indexes are the indexes of where the output is in the transaction BlockHeight int // BlockHeight is the height of the block that contains this TX }
func DecodeOutputs ¶
func DecodeOutputs(data []byte) (UTXOutputs, error)
func (UTXOutputs) SerializeOutputs ¶
func (uo UTXOutputs) SerializeOutputs() ([]byte, error)
type Wallet ¶
type Wallet struct { // PrivateKey is an instance of ecdsa.PrivateKey. This contains both the public and private key PrivateKey ecdsa.PrivateKey PublicKey []byte }
Wallet is an instance of a single Wallet
func CreateWallet ¶
CreateWallet creates a single wallet, consisting of a public and private key.
func (Wallet) GetAddress ¶
GetAddress gets a Base58Encoded address. This address is the user's Blemflarck address.