Documentation
¶
Index ¶
- func ByteHasLeadingZeros(bytes []byte, difficulty int) bool
- func BytesToHex(bytes []byte) string
- func BytesToPrivateKey(priv []byte) *rsa.PrivateKey
- func BytesToPublicKey(pub []byte) *rsa.PublicKey
- func CalcTxFee(txs []*model.Transaction, l *model.Ledger) (float64, error)
- func CreateCoinbaseTx(totalReward float64, pk []byte, height int64) *model.Transaction
- func CreateNewBlock(txs []*model.Transaction, prevHash string, reward float64, height int64, ...) (*model.Block, commands.Command, []*model.Transaction, error)
- func CreatePendingTransaction(sk *rsa.PrivateKey, utxos map[model.UTXOLite]*model.Output, ...) (*model.Transaction, error)
- func CreateUtxoFromInput(input *model.Input) model.UTXO
- func FillTxHash(tx *model.Transaction) error
- func Float64ToBytes(f float64) []byte
- func GenerateKeyPair(bits int) (*rsa.PrivateKey, *rsa.PublicKey)
- func GetAllTxsInPool(txPool *model.TransactionPool) []*model.Transaction
- func GetBlockBytes(block *model.Block) ([]byte, error)
- func GetInputBytes(input *model.Input, withSig bool) ([]byte, error)
- func GetInputDataToSignByIndex(t *model.Transaction, index int) ([]byte, error)
- func GetLedgerDeepCopy(l *model.Ledger) *model.Ledger
- func GetOutputBytes(output *model.Output) []byte
- func GetTransactionBytes(tx *model.Transaction, withHash bool) ([]byte, error)
- func HandleTransaction(tx *model.Transaction, l *model.Ledger) error
- func HandleTransactions(txs []*model.Transaction, l *model.Ledger) ([]*model.Transaction, error)
- func HexToBytes(str string) ([]byte, error)
- func Int64ToBytes(i int64) []byte
- func IsSameBytes(lhs []byte, rhs []byte) bool
- func IsValidCoinbase(tx *model.Transaction, maxFee float64) error
- func IsValidTransaction(tx *model.Transaction, l *model.Ledger) error
- func MatchDifficulty(block *model.Block, difficulty int) (bool, string)
- func Mine(block *model.Block, difficulty int, ctl chan commands.Command) (commands.Command, error)
- func ParseKeyFile(path string, rsaLen int) *rsa.PrivateKey
- func PrivateKeyToBytes(priv *rsa.PrivateKey) []byte
- func ProcessInputsAndOutputs(tx *model.Transaction, l *model.Ledger)
- func PublicKeyToBytes(pub *rsa.PublicKey) []byte
- func ReadKeyFromPath(path string) *rsa.PrivateKey
- func SHA256(msg []byte) []byte
- func Sign(msg []byte, sk *rsa.PrivateKey) ([]byte, error)
- func Verify(msg []byte, pk *rsa.PublicKey, signature []byte) bool
- func WritePrivateKeyToFile(sk *rsa.PrivateKey, path string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ByteHasLeadingZeros ¶
func BytesToHex ¶
func BytesToPrivateKey ¶
func BytesToPrivateKey(priv []byte) *rsa.PrivateKey
BytesToPrivateKey bytes to private key
func BytesToPublicKey ¶
BytesToPublicKey bytes to public key
func CalcTxFee ¶
Calculate total transaction fee given transaction and ledger. This function will not modify ledger.
func CreateCoinbaseTx ¶
func CreateCoinbaseTx(totalReward float64, pk []byte, height int64) *model.Transaction
Create a transaction with a single output, which is the miner's public key. READONLY: *pk
func CreateNewBlock ¶
func CreateNewBlock(txs []*model.Transaction, prevHash string, reward float64, height int64, pk []byte, l *model.Ledger, difficulty int, ctl chan commands.Command) (*model.Block, commands.Command, []*model.Transaction, error)
Create a block from the provided transactions and the previous hash, miner's reward, current 1. Fill in previous hash. 2. Create coinbase transactions (Reward + Tx fee). 3. Fill in transactions provided. 4. Mine the block. Also, **input ledger must be a deep copy because it will be change permanently.**
func CreatePendingTransaction ¶
func CreatePendingTransaction(sk *rsa.PrivateKey, utxos map[model.UTXOLite]*model.Output, outputs []*model.Output) (*model.Transaction, error)
Create a pending transaction to transfer money to users with public key wallet : a pointer to a wallet struct outputs : an array of struct Output READONLY: * wallet
func FillTxHash ¶
func FillTxHash(tx *model.Transaction) error
Fill hash simply compute the SHA256 hash for the transaction raw data and set the hash.
func Float64ToBytes ¶
func GenerateKeyPair ¶
func GenerateKeyPair(bits int) (*rsa.PrivateKey, *rsa.PublicKey)
GenerateKeyPair generates a new key pair
func GetAllTxsInPool ¶
func GetAllTxsInPool(txPool *model.TransactionPool) []*model.Transaction
Return all transactions in the pool. TODO: pre-allocate all memory by size.
func GetBlockBytes ¶
Get block bytes without its hash.
func GetInputBytes ¶
GetInputBytes converts input to byte slice. With or without the signature.
func GetInputDataToSignByIndex ¶
func GetInputDataToSignByIndex(t *model.Transaction, index int) ([]byte, error)
GetInputDataToSign fetches the input from the transaction and return it in byte stream.
func GetLedgerDeepCopy ¶
Deep (well..not deep enough) copy a ledger.
func GetOutputBytes ¶
func GetTransactionBytes ¶
func GetTransactionBytes(tx *model.Transaction, withHash bool) ([]byte, error)
Concat all inputs (including signature) and outputs raw data in byte slices. withHash specifies whether TX hash should be included or not.
func HandleTransaction ¶
func HandleTransaction(tx *model.Transaction, l *model.Ledger) error
Handle transaction: 1. Validate transaction. 2. Claim every input. 3. Store every output. Return true if currently handles the transactions, false if the transaction is invalid. Note: ledger will be changed afterwards, please make a deep copy before passing in.
func HandleTransactions ¶
func HandleTransactions(txs []*model.Transaction, l *model.Ledger) ([]*model.Transaction, error)
Handle a bunch of transactions. Note that ledger will be changed directly, when passing ledger to this function, be sure to pass a deep copy. When error, this function returns all transactions that causes error. MUTABLE: * l
func HexToBytes ¶
func Int64ToBytes ¶
func IsSameBytes ¶
func IsValidCoinbase ¶
func IsValidCoinbase(tx *model.Transaction, maxFee float64) error
A valid coinbase transaction should contains 0 input and 1 output. And total reward should be smaller than transaction fee + default reward. READONLY: * tx
func IsValidTransaction ¶
func IsValidTransaction(tx *model.Transaction, l *model.Ledger) error
A transaction is valid if: 1. All inputs are UTXO. 2. Total outputs are smaller or equal to inputs. 3. Outputs are non-negative number. 4. Signatures are valid. 5. No 2 inputs claiming the same UTXO in this transaction. 6. Hash matches. This function
func Mine ¶
Mine a block, fill the nounce and hash given the current difficulty setting. difficulty - how many leading zeros Always listen for command interruption and stop mining at any time. This process will only terminate when receive signal.
func ParseKeyFile ¶
func ParseKeyFile(path string, rsaLen int) *rsa.PrivateKey
This function is called in startup time. ParseKeyFile returns key pair from the given path. This function will exit on any error because there's no need to continue if we cannot get key.
func PrivateKeyToBytes ¶
func PrivateKeyToBytes(priv *rsa.PrivateKey) []byte
PrivateKeyToBytes private key to bytes
func ProcessInputsAndOutputs ¶
func ProcessInputsAndOutputs(tx *model.Transaction, l *model.Ledger)
func PublicKeyToBytes ¶
PublicKeyToBytes public key to bytes
func ReadKeyFromPath ¶
func ReadKeyFromPath(path string) *rsa.PrivateKey
Read key from the given path. If the key is invalid, exit the execution because there is no need to continue.
func Sign ¶
func Sign(msg []byte, sk *rsa.PrivateKey) ([]byte, error)
Sign a message's SHA256 digest with provided private key.
func WritePrivateKeyToFile ¶
func WritePrivateKeyToFile(sk *rsa.PrivateKey, path string)
Write the given private key into file in bytes. Exit if fail to write
Types ¶
This section is empty.