core

package
v0.0.0-...-0764be2 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2021 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UTXOBucket = "chainstate"
)

Variables

This section is empty.

Functions

func Base58Decode

func Base58Decode(input []byte) []byte

Base58Decode decodes Base58-encoded data

func Base58Encode

func Base58Encode(input []byte) []byte

Base58Encode encodes a byte array to Base58

func BlockFile

func BlockFile(h int) string

BlockFile takes in a block height and returns a string with that heights filename

func ChainExists

func ChainExists() bool

ChainExists checks if there is already a chain

func CheckValidAddress

func CheckValidAddress(address []byte) bool

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

func CreateChecksum(payload []byte) []byte

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

func FormatB(hash []byte) []byte

FormatB formats a block hash, and joins it with the letter 'b'. Used when inserting a new block in the db

func FormatC

func FormatC(txID []byte) []byte

func GobEncode

func GobEncode(i interface{}) ([]byte, error)

func HashPublicKey

func HashPublicKey(pubKey []byte) ([]byte, error)

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

func ReformatKey(key []byte) []byte

func ReverseBytes

func ReverseBytes(data []byte)

ReverseBytes reverses a byte array

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

func DecodeBlock(data []byte) (Block, error)

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

func ReadBlockFromFile(height int) (Block, error)

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

func (b Block) EncodeBlock() ([]byte, error)

EncodeBlock encodes a block to a byte slice, this allows the block to be saved to file.

func (Block) GenerateHash

func (b Block) GenerateHash() ([]byte, error)

GenerateHash generates a new sha512 hash for a block Eventually replace this when implementing proof

func (Block) SaveToFile

func (b Block) SaveToFile() error

SaveToFile saves a block to file. If the block height is 518 then the file will be 518.dat

type Blockchain

type Blockchain struct {
	//Tip []byte
	DB *bolt.DB // DB is a pointer to an open boltDB connection
}

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

func CreateOutput(address string, amount int) Output

CreateOutput creates an output for an address, with an amount, and then locks the output to that address

func (Output) CanBeUnlocked

func (out Output) CanBeUnlocked(address []byte) bool

CanBeUnlocked checks if an address is the one who locked the output.

func (*Output) Lock

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

Lock is responsible for locking an output to an address. It gets the public key hash by decoding the address, and then removing the version and checksum from the hash.

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 (u UTXO) FindSpendableOutputs(address []byte, amount int) (int, map[string][]int, error)

func (UTXO) FindTransaction

func (u UTXO) FindTransaction(txID []byte, blockHeight int) (Transaction, error)

func (UTXO) FindUTXOs

func (u UTXO) FindUTXOs() (map[string]*UTXOutputs, error)

func (UTXO) Reindex

func (u UTXO) Reindex() error

func (UTXO) SignTransaction

func (u UTXO) SignTransaction(tx Transaction, private ecdsa.PrivateKey) error

func (UTXO) Update

func (u UTXO) Update(block Block) 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

func CreateWallet() (Wallet, error)

CreateWallet creates a single wallet, consisting of a public and private key.

func (Wallet) GetAddress

func (w Wallet) GetAddress() ([]byte, error)

GetAddress gets a Base58Encoded address. This address is the user's Blemflarck address.

type Wallets

type Wallets struct {
	Wallets map[string]Wallet
}

func DecodeWallets

func DecodeWallets(data []byte) (Wallets, error)

func ReadWalletsFromFile

func ReadWalletsFromFile() (Wallets, error)

func (Wallets) EncodeWallets

func (ws Wallets) EncodeWallets() ([]byte, error)

func (Wallets) SaveToFile

func (ws Wallets) SaveToFile() error

Jump to

Keyboard shortcuts

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