util

package module
v0.0.0-...-5bd8607 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2022 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MicroquacksPerDuck is the number of microquacks equal to one duck.
	// A microquack is a billionth of a quack, which is a hundredth of a duck.
	MicroquacksPerDuck uint64 = 1e8
)

Variables

View Source
var (
	Reward uint64 = 1e6
)

Functions

func ArgsHaveOption

func ArgsHaveOption(long, short string) (hasOption bool, foundAt int)

ArgsHaveOption checks command line arguments for an option

func CheckSignature

func CheckSignature(signature, pubkey, message string) (bool, error)

CheckSignature checks if signature decodes to message using pubkey. This is useful in verifying identities.

func DBInit

func DBInit()

func DoubleShasumBytes

func DoubleShasumBytes(record []byte) []byte

DoubleShasumBytes returns sha256(sha256(record))

func EmojiToBytes

func EmojiToBytes(emoji string) ([addrBytesLen]byte, error)

func GetAllBalances

func GetAllBalances() map[Address]uint64

GetAllBalances returns a map of addresses to the balance in microquacks

func GetAllBalancesFloats

func GetAllBalancesFloats() map[Address]float64

GetAllBalancesFloats returns a map of addresses to the balance in ducks

func GetBalanceByAddr

func GetBalanceByAddr(addr Address) (uint64, error)

func GetTarget

func GetTarget(difficulty uint64) *big.Int

GetTarget returns a "target" which block hashes must be lower than to be valid. This is calculated such that miners will need to compute difficulty hashes on average for a valid hash.

func IsHashValid

func IsHashValid(hash string, target *big.Int) bool

IsHashValid checks if a hash is a valid block hash

func IsHashValidBytes

func IsHashValidBytes(hash []byte, target *big.Int) bool

IsHashValidBytes checks if a hash is a valid block hash

func IsValid

func IsValid(newBlock, oldBlock *Sblock, target *big.Int) error

func IsValidBase64

func IsValidBase64(s string) bool

IsValidBase64 checks if a string is valid base64

func IsValidNoCheckDB

func IsValidNoCheckDB(newBlock, oldBlock *Sblock, target *big.Int) error

func MakeKeyPair

func MakeKeyPair() (pub, priv string, err error)

MakeKeyPair creates a new public and private key pair

func MakeSignature

func MakeSignature(privkey, message string) (string, error)

MakeSignature signs a message with a private key.

func SaveKeyPair

func SaveKeyPair(pubkey, privkey, pubfile, privfile string) error

SaveKeyPair saves a key pair using the PEM format

func TextToBytes

func TextToBytes(text string) ([addrBytesLen]byte, error)

func ToJSON

func ToJSON(v interface{}) string

ToJSON is a convenience method for serializing to JSON.

func WriteBlockDB

func WriteBlockDB(blks ...*Sblock)

Types

type Address

type Address struct {
	Emoji string `json:",omitempty"`
	Text  string `json:",omitempty"`
	// contains filtered or unexported fields
}

func BytesToAddress

func BytesToAddress(b [addrBytesLen]byte) Address

func EmojiOrTextToAddress

func EmojiOrTextToAddress(addr string) (Address, error)

func EmojiToAddress

func EmojiToAddress(emoji string) (Address, error)

func KeyToAddress

func KeyToAddress(key string) Address

KeyToAddress derives a Duckcoin Address from a Duckcoin Public Key

func LoadKeysAndAddr

func LoadKeysAndAddr(pubfile, privfile string) (pub, priv string, addr Address, err error)

func TextToAddress

func TextToAddress(text string) (Address, error)

func (*Address) IsValid

func (a *Address) IsValid() error

IsValid verifies the checksum built into addresses, and checks the address format

func (*Address) UnmarshalJSON

func (a *Address) UnmarshalJSON(bytes []byte) error

type Lblock

type Lblock struct {
	// Index is the Lblock number
	Index uint64
	// Timestamp is the Unix timestamp in milliseconds of the date of creation of this Lblock
	Timestamp uint64
	// Data stores any (arbitrary) additional data <= 250 kb long.
	Data string
	// Hash stores the hash of this Lblock as computed by CalculateHash
	Hash string
	// PrevHash is the hash of the previous Lblock
	PrevHash string
	// Solution is the nonce value that makes the Hash be under some target value
	Solution uint64
	// Solver is the address of the sender. Address format: Q + version char + base64(shasum(pubkey)[:20])
	Solver Address `json:",omitempty"`
	// Sblocks contains the Sblocks part of this Lblock
	Sblocks []*Sblock
}

func (*Lblock) CalculateHash

func (b *Lblock) CalculateHash() string

CalculateHash calculates the hash of an Lblock.

func (*Lblock) CalculateHashBytes

func (b *Lblock) CalculateHashBytes() []byte

CalculateHashBytes calculates the hash of an Lblock.

func (*Lblock) Preimage

func (b *Lblock) Preimage() []byte

func (*Lblock) PreimageWOSolution

func (b *Lblock) PreimageWOSolution() []byte

type Sblock

type Sblock struct {
	// Index is the Sblock number
	Index uint64
	// Timestamp is the Unix timestamp in milliseconds of the date of creation of this Sblock
	Timestamp uint64
	// Data stores any (arbitrary) additional data <= 250 kb long.
	Data string
	// Hash stores the hash of this Sblock as computed by CalculateHash
	Hash string
	// PrevHash is the hash of the previous Sblock
	PrevHash string
	// Solution is the nonce value that makes the Hash be under some target value
	Solution uint64
	// Solver is the address of the sender. Address format: Q + version char + base64(shasum(pubkey)[:20])
	Solver Address     `json:",omitempty"`
	Tx     Transaction `json:",omitempty"`
}

An Sblock is one block in the chain of some Lnode. It optionally contains a transaction and arbitrary data.

func GetNewestSblock

func GetNewestSblock() (*Sblock, error)

func GetSblockByHash

func GetSblockByHash(hash string) (*Sblock, error)

func GetSblockByIndex

func GetSblockByIndex(i uint64) (*Sblock, error)

func (*Sblock) CalculateHash

func (b *Sblock) CalculateHash() string

CalculateHash calculates the hash of a Sblock.

func (*Sblock) CalculateHashBytes

func (b *Sblock) CalculateHashBytes() []byte

CalculateHashBytes calculates the hash of a Sblock.

func (*Sblock) Preimage

func (b *Sblock) Preimage() []byte

func (*Sblock) PreimageWOSolution

func (b *Sblock) PreimageWOSolution() []byte

PreimageWOSolution returns the data to be hashed to create the hash of an Sblock, but without the Solution field taken into account. This is useful when mining.

type Transaction

type Transaction struct {
	// Data is any (arbitrary) additional data <= 250 kb long.
	Data string `json:",omitempty"`
	// Sender is the address of the sender.
	Sender Address `json:",omitempty"`
	// Receiver is the address of the receiver.
	Receiver Address `json:",omitempty"`
	// Amount is the amount to be payed by the Sender to the Receiver. It is always a positive number.
	Amount uint64 `json:",omitempty"`
	// PubKey is the Duckcoin formatted public key of the sender
	PubKey string `json:",omitempty"`
	// Signature is a base64 encoded cryptographic signature that uses the hash of the Sblock
	// as the data encrypted by the private key to prevent replay attacks
	Signature string `json:",omitempty"`
}

A Transaction is a transfer of any amount of duckcoin from one address to another.

Jump to

Keyboard shortcuts

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