blockchain

package
v0.0.0-...-128d3dd Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

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

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

func HashSum

func HashSum(data []byte) []byte

func NewChain

func NewChain(filename, receiver string) error

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

func StringPrivate

func StringPrivate(priv *rsa.PrivateKey) string

func StringPublic

func StringPublic(pub *rsa.PublicKey) string

func ToBytes

func ToBytes(num uint64) []byte

func Verify

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

Types

type Block

type Block struct {
	Nonce        uint64
	Difficulty   uint8
	CurrHash     []byte
	PrevHash     []byte
	Transactions []Transaction
	Mapping      map[string]uint64
	Miner        string
	Signature    []byte
	TimeStamp    string
}

func DeserializeBlock

func DeserializeBlock(data string) *Block

func NewBlock

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

func (*Block) Accept

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

func (*Block) AddTransaction

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

func (*Block) IsValid

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

type BlockChain

type BlockChain struct {
	DB *sql.DB
}

func LoadChain

func LoadChain(filename string) *BlockChain

func (*BlockChain) AddBlock

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

func (*BlockChain) Balance

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

func (*BlockChain) LastHash

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

func (*BlockChain) Size

func (chain *BlockChain) Size() uint64

type Transaction

type Transaction struct {
	RandBytes []byte
	PrevBlock []byte
	Sender    string
	Receiver  string
	Value     uint64
	ToStorage uint64
	CurrHash  []byte
	Signature []byte
}

func DeserializeTX

func DeserializeTX(data string) *Transaction

func NewTransaction

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

type User

type User struct {
	PrivateKey *rsa.PrivateKey
}

func LoadUser

func LoadUser(purse string) *User

func NewUser

func NewUser() *User

func (*User) Address

func (user *User) Address() 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