utils

package
v0.0.0-...-43a58b6 Latest Latest
Warning

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

Go to latest
Published: May 19, 2021 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByteHasLeadingZeros

func ByteHasLeadingZeros(bytes []byte, difficulty int) bool

func BytesToHex

func BytesToHex(bytes []byte) string

func BytesToPrivateKey

func BytesToPrivateKey(priv []byte) *rsa.PrivateKey

BytesToPrivateKey bytes to private key

func BytesToPublicKey

func BytesToPublicKey(pub []byte) *rsa.PublicKey

BytesToPublicKey bytes to public key

func CalcTxFee

func CalcTxFee(txs []*model.Transaction, l *model.Ledger) (float64, error)

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 CreateUtxoFromInput

func CreateUtxoFromInput(input *model.Input) model.UTXO

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 Float64ToBytes(f float64) []byte

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

func GetBlockBytes(block *model.Block) ([]byte, error)

Get block bytes without its hash.

func GetInputBytes

func GetInputBytes(input *model.Input, withSig bool) ([]byte, error)

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

func GetLedgerDeepCopy(l *model.Ledger) *model.Ledger

Deep (well..not deep enough) copy a ledger.

func GetOutputBytes

func GetOutputBytes(output *model.Output) []byte

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 HexToBytes(str string) ([]byte, error)

func Int64ToBytes

func Int64ToBytes(i int64) []byte

func IsSameBytes

func IsSameBytes(lhs []byte, rhs []byte) bool

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 MatchDifficulty

func MatchDifficulty(block *model.Block, difficulty int) (bool, string)

func Mine

func Mine(block *model.Block, difficulty int, ctl chan commands.Command) (commands.Command, error)

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

func PublicKeyToBytes(pub *rsa.PublicKey) []byte

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 SHA256

func SHA256(msg []byte) []byte

Hash message using SHA256

func Sign

func Sign(msg []byte, sk *rsa.PrivateKey) ([]byte, error)

Sign a message's SHA256 digest with provided private key.

func Verify

func Verify(msg []byte, pk *rsa.PublicKey, signature []byte) bool

Verify the given signature matches the message.

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.

Jump to

Keyboard shortcuts

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