common

package
v1.0.1-beta Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2021 License: MIT Imports: 15 Imported by: 11

Documentation

Index

Constants

View Source
const (
	EmptyString       = ""
	ZeroByte          = byte(0x00)
	DateOutputFormat  = "2006-01-02T15:04:05.999999"
	BigIntSize        = 32 // bytes
	CheckSumLen       = 4  // bytes
	AESKeySize        = 32 // bytes
	Int32Size         = 4  // bytes
	Uint32Size        = 4  // bytes
	Uint64Size        = 8  // bytes
	HashSize          = 32 // bytes
	MaxHashStringSize = HashSize * 2
	Base58Version     = 0
)

for common

View Source
const (
	PrivateKeySize   = 32  // bytes
	PublicKeySize    = 32  // bytes
	BLSPublicKeySize = 128 // bytes
	BriPublicKeySize = 33  // bytes
)
View Source
const (
	TxNormalType             = "n"   // normal tx
	TxRewardType             = "s"   // reward tx
	TxReturnStakingType      = "rs"  // return-staking tx
	TxConversionType         = "cv"  // Convert 1 - 2 normal tx
	TxTokenConversionType    = "tcv" // Convert 1 - 2 token tx
	TxCustomTokenPrivacyType = "tp"  // token  tx with supporting privacy
)

For all Transaction information

View Source
const (
	BlsConsensus    = "bls"
	BridgeConsensus = "dsa"
	IncKeyType      = "inc"
)
View Source
const (
	BurningAddress  = "15pABFiJVeh9D5uiQEhQX4SVibGGbdAVipQxBdxkmDqAJaoG1EdFKHBrNfs"
	BurningAddress2 = "12RxahVABnAVCGP3LGwCn8jkQxgw7z1x14wztHzn455TTVpi1wBq9YGwkRMQg3J4e657AbAnCvYCJSdA9czBUNuCKwGSRQt55Xwz8WA"
)
View Source
const PRVIDStr = "0000000000000000000000000000000000000000000000000000000000000004"
View Source
const (
	SalaryVerFixHash = 1
)

Variables

View Source
var (
	PRVCoinID           = Hash{4}
	ConfidentialAssetID = Hash{5}
	MaxShardNumber      = 8 //programmatically config based on networkID
	AddressVersion      = 1
)
View Source
var (
	MaxTxSize = uint64(100) // unit KB = 100KB
)

Functions

func AddPaddingBigInt

func AddPaddingBigInt(numInt *big.Int, fixedSize int) []byte

AddPaddingBigInt adds padding to big int to it is fixed size.

func B2ImN

func B2ImN(bytes []byte) *big.Int

B2ImN is Bytes to Int mod N, with N is Secp256k1 curve order

func BytesToInt

func BytesToInt(bytesArr []byte) int

BytesToInt reverts an integer number from 2-byte array.

func BytesToUint32

func BytesToUint32(b []byte) (uint32, error)

BytesToUint32 converts big endian 4-byte array to uint32 number.

func FromHex

func FromHex(s string) []byte

FromHex returns the bytes represented by the hexadecimal string s. s may be prefixed with "0x".

func GetShardIDFromLastByte

func GetShardIDFromLastByte(b byte) byte

GetShardIDFromLastByte returns the shardID from the last byte b of a public key. The shardID is calculated by taking the remainder of b % MaxShardNumber.

func Has0xPrefix

func Has0xPrefix(str string) bool

Has0xPrefix validates str begins with '0x' or '0X'.

func Hash4Bls

func Hash4Bls(data []byte) []byte

Hash4Bls is Hash function for calculate block hash this is different from hash function for calculate transaction hash

func HashB

func HashB(b []byte) []byte

HashB is the same as SHA256.

func Hex2Bytes

func Hex2Bytes(str string) []byte

Hex2Bytes returns the bytes represented by the hexadecimal string str.

func IntToBytes

func IntToBytes(n int) []byte

IntToBytes converts an integer number to 2-byte array in big endian.

func RandBigIntMaxRange

func RandBigIntMaxRange(max *big.Int) (*big.Int, error)

RandBigIntMaxRange generates a random big.Int whose value is less than a given max value.

func RandBytes

func RandBytes(l int) []byte

RandBytes generates a random l-byte long slice.

func RandChars

func RandChars(l int) string

RandChars returns a random l-character long string.

func RandInt

func RandInt() int

RandInt returns a random int number using math/rand

func RandInt64

func RandInt64() int64

RandInt64 returns a random int64 number using math/rand

func RandIntInterval

func RandIntInterval(L, R int) int

RandIntInterval returns a random int in range [L; R]

func RandUint64

func RandUint64() uint64

func SHA256

func SHA256(b []byte) []byte

SHA256 calculates SHA256-256 hashing of input b and returns the result in bytes array.

func SliceExists

func SliceExists(slice interface{}, item interface{}) (bool, error)

SliceExists checks if an item exists in a slice.

func Uint32ToBytes

func Uint32ToBytes(value uint32) []byte

Uint32ToBytes converts uint32 number to big endian 4-byte array.

Types

type AES

type AES struct {
	Key []byte
}

AES consists of the symmetric key used in the aes encryption scheme.

func (*AES) Decrypt

func (aesObj *AES) Decrypt(ciphertext []byte) ([]byte, error)

Decrypt decrypts a cipher text.

func (*AES) Encrypt

func (aesObj *AES) Encrypt(plaintext []byte) ([]byte, error)

Encrypt encrypts a message. The encryption operation uses the CRT mode.

type Hash

type Hash [HashSize]byte

Hash is a 32-byte long array wrapping a hashed value.

func HashH

func HashH(b []byte) Hash

HashH calculates SHA3-256 hashing of input b and returns the result in Hash.

func Keccak256

func Keccak256(data ...[]byte) Hash

Keccak256 returns Keccak256 hash as a Hash object for storing and comparing.

func Keccak256Hash

func Keccak256Hash(data ...[]byte) (h Hash)

Keccak256Hash calculates and returns the Keccak256 hash of the input data, converting it to an internal Hash data structure.

func (*Hash) Cmp

func (hashObj *Hash) Cmp(target *Hash) (int, error)

Cmp compare two hashes, if - hash = target: return 0. - hash > target: return 1. - hash < target: return -1.

func (*Hash) Decode

func (hashObj *Hash) Decode(dst *Hash, src string) error

Decode decodes the byte-reversed hexadecimal string encoding of a Hash to a destination.

func (*Hash) GetBytes

func (hashObj *Hash) GetBytes() []byte

GetBytes returns bytes array of hashObj

func (*Hash) IsEqual

func (hashObj *Hash) IsEqual(target *Hash) bool

IsEqual returns true if target is the same as hashObj.

func (Hash) MarshalText

func (hashObj Hash) MarshalText() ([]byte, error)

MarshalText converts hashObj string to bytes array.

func (Hash) NewHash

func (hashObj Hash) NewHash(newHash []byte) (*Hash, error)

NewHash receives a bytes array and returns a corresponding object Hash

func (Hash) NewHashFromStr

func (hashObj Hash) NewHashFromStr(hash string) (*Hash, error)

NewHashFromStr creates a Hash from a hash string. The string should be the hexadecimal string of a byte-reversed hash, but any missing characters result in zero padding at the end of the Hash.

func (*Hash) SetBytes

func (hashObj *Hash) SetBytes(newHash []byte) error

SetBytes sets the bytes array which represent the hash.

func (Hash) String

func (hashObj Hash) String() string

String returns the Hash as the hexadecimal string of the byte-reversed hash.

func (*Hash) UnmarshalJSON

func (hashObj *Hash) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshal json data to hashObj.

func (Hash) UnmarshalText

func (hashObj Hash) UnmarshalText(text []byte) error

UnmarshalText reverts bytes array to hashObj.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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