blockchain

package
v0.0.0-...-d4ca3cc Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2022 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	GENESIS_BLOCK  = "GENESIS-BLOCK"
	STORAGE_VALUE  = 100
	GENESIS_REWARD = 100
	STORAGE_CHAIN  = "STORAGE-CHAIN"
)
View Source
const (
	RAND_BYTES     = 32
	START_PERCENT  = 10
	STORAGE_REWARD = 1
)
View Source
const (
	CREATE_TABLE = `
	CREATE TABLE BlockChain (
		Id INTEGER PRIMARY KEY AUTOINCREMENT,
		Hash VARCHAR(44) UNIQUE,
		Block TEXT
	);
	`
)
View Source
const (
	DEBUG = true
)
View Source
const (
	DIFFICULTY = 20
)
View Source
const (
	KEY_SIZE = 512
)
View Source
const (
	TXS_LIMIT = 2 // Defines the maximum number of transactions in one block.
)

Variables

This section is empty.

Functions

func Base64Decode

func Base64Decode(data string) []byte

func Base64Encode

func Base64Encode(data []byte) string

func GeneratePrivate

func GeneratePrivate(bits uint) *rsa.PrivateKey

func GenerateRandomBytes

func GenerateRandomBytes(max uint) []byte

Returns a slice of pseudo-random bytes //

func HashSum

func HashSum(data []byte) []byte

func NewChain

func NewChain(filename, receiver string) error

Creation of blockchain //

func ParsePrivate

func ParsePrivate(privData string) *rsa.PrivateKey

func ParsePublic

func ParsePublic(pubData string) *rsa.PublicKey

func ProofOfWork

func ProofOfWork(blockHash []byte, difficulty uint8, ch chan bool) uint64

func SerializeBlock

func SerializeBlock(block *Block) string

func SerializeTX

func SerializeTX(tx *Transaction) string

func Sign

func Sign(priv *rsa.PrivateKey, data []byte) []byte

Signs the data based on the private key //

func StringPrivate

func StringPrivate(priv *rsa.PrivateKey) string

func StringPublic

func StringPublic(pub *rsa.PublicKey) string

Translates the public key into a set of bytes, then applies base64 encoding to translate to a string //

func ToBytes

func ToBytes(num uint64) []byte

Converts a number to a set of bytes //

func Verify

func Verify(pub *rsa.PublicKey, data, sign []byte) error

Uses the public key to verify signed data with initial //

Types

type Block

type Block struct {
	Nonce        uint64            // Confirmation result
	Difficulty   uint8             // Block difficulty
	CurrHash     []byte            // Hash of current block
	PrevHash     []byte            // Hash of previous block
	Transactions []Transaction     // User Transaction
	Mapping      map[string]uint64 // User Balance
	Miner        string            // User who mined block
	Signature    []byte            // Miner signature pointing to CurrHash
	TimeStamp    string            // TimeStamp of block creation
}

func DeserializeBlock

func DeserializeBlock(data string) *Block

func NewBlock

func NewBlock(miner string, prevHash []byte) *Block

Creates sample of block //

func (*Block) Accept

func (block *Block) Accept(chain *BlockChain, user *User, ch chan bool) error

After placing all transactions in a block, it must be confirmed //

func (*Block) AddTransaction

func (block *Block) AddTransaction(chain *BlockChain, tx *Transaction) error

Adding a transaction to the block //

func (*Block) IsValid

func (block *Block) IsValid(chain *BlockChain, size uint64) bool

type BlockChain

type BlockChain struct {
	DB *sql.DB // Pointer to the database to add to and from which takes blocks
}

func LoadChain

func LoadChain(filename string) *BlockChain

Load function to use the already created blockchain//

func (*BlockChain) AddBlock

func (chain *BlockChain) AddBlock(block *Block)

Adding new block in DB //

func (*BlockChain) Balance

func (chain *BlockChain) Balance(address string, size uint64) uint64

User balance //

func (*BlockChain) LastHash

func (chain *BlockChain) LastHash() []byte

func (*BlockChain) Size

func (chain *BlockChain) Size() uint64

Returns the number of blocks in the local database //

type Transaction

type Transaction struct {
	RandBytes []byte // Random bytes
	PrevBlock []byte // Hash of last block
	Sender    string // Sender's name
	Receiver  string // Receiver's name
	Value     uint64 // The amount of money transferred to the receiver
	ToStorage uint64 // The amount of money transferred to the storage
	CurrHash  []byte // Current transaction hash
	Signature []byte // Sender's signature
}

func DeserializeTX

func DeserializeTX(data string) *Transaction

func NewTransaction

func NewTransaction(user *User, lasthash []byte, to string, value uint64) *Transaction

After creating a block, user transactions need to be entered into it //

type User

type User struct {
	PrivateKey *rsa.PrivateKey // Private Key of User
}

func LoadUser

func LoadUser(purse string) *User

func NewUser

func NewUser() *User

func (*User) Address

func (user *User) Address() string

Converts the public key to a string //

func (*User) Private

func (user *User) Private() *rsa.PrivateKey

func (*User) Public

func (user *User) Public() *rsa.PublicKey

func (*User) Purse

func (user *User) Purse() string

Jump to

Keyboard shortcuts

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