common

package
v0.0.0-...-0abe6a2 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2019 License: BSD-3-Clause Imports: 10 Imported by: 11

Documentation

Index

Constants

View Source
const (
	// WordBits 一个big.Word类型取值占用多少个位
	WordBits = 32 << (uint64(^big.Word(0)) >> 63)
	// WordBytes 一个big.Word类型取值占用多少个字节
	WordBytes = WordBits / 8
)
View Source
const (
	// HashLength 哈希长度
	HashLength = 32

	// Hash160Length Hash160格式的地址长度
	Hash160Length = 20
)

Variables

View Source
var (
	// Big0 大数字0
	Big0 = big.NewInt(0)
	// Big1 大数字1
	Big1 = big.NewInt(1)
	// Big32 大数字32
	Big32 = big.NewInt(32)
	// Big256 大数字256
	Big256 = big.NewInt(256)
	// Big257 大数字257
	Big257 = big.NewInt(257)
)

常用的大整数常量定义

View Source
var (
	// TT255 2的255次幂
	TT255 = BigPow(2, 255)
)

2的各种常用取幂结果

Functions

func AllZero

func AllZero(b []byte) bool

AllZero 判断字节数组内容是否全为零

func BigMax

func BigMax(x, y *big.Int) *big.Int

BigMax 返回两者之中的较大值

func BigMin

func BigMin(x, y *big.Int) *big.Int

BigMin 返回两者之中的较小值

func BigPow

func BigPow(a, b int64) *big.Int

BigPow 返回a的b次幂

func BigUint64

func BigUint64(v *big.Int) (uint64, bool)

BigUint64 将大整数转换为uint64,并判断是否溢出

func Byte

func Byte(bigint *big.Int, padlength, n int) byte

Byte big.Int以小端编码时,第n个位置的字节取值 例如: bigint '5', padlength 32, n=31 => 5

func Bytes2Hex

func Bytes2Hex(b []byte) string

Bytes2Hex 将字节数组转换为16进制的字符串表示

func Bytes2HexTrim

func Bytes2HexTrim(b []byte) string

Bytes2HexTrim 将字节数组转换为16进制的字符串表示 并且将前面多余的0去除

func Exp

func Exp(base, exponent *big.Int) *big.Int

Exp 指数函数,可以指定底数,结果被截断为256位长度

func FromHex

func FromHex(s string) []byte

FromHex 十六进制的字符串转换为字节数组

func GetData

func GetData(data []byte, start uint64, size uint64) []byte

GetData 返回从开始位置制定长度的数据 如果源数据不足,则剩余位置用零填充

func GetDataBig

func GetDataBig(data []byte, start *big.Int, size *big.Int) []byte

GetDataBig 返回从开始位置制定长度的数据 如果源数据不足,则剩余位置用零填充

func Hex2Bytes

func Hex2Bytes(str string) []byte

Hex2Bytes 十六进制字符串转换为字节数组

func HexToBytes

func HexToBytes(str string) ([]byte, error)

HexToBytes 十六进制字符串转换为字节数组

func LeftPadBytes

func LeftPadBytes(slice []byte, l int) []byte

LeftPadBytes 左填充字节数组

func PaddedBigBytes

func PaddedBigBytes(bigint *big.Int, n int) []byte

PaddedBigBytes encodes a big integer as a big-endian byte slice. The length of the slice is at least n bytes.

func ReadBits

func ReadBits(bigint *big.Int, buf []byte)

ReadBits 以大端方式将big.Int编码为字节数组

func RightPadBytes

func RightPadBytes(slice []byte, l int) []byte

RightPadBytes 右填充字节数组

func S256

func S256(x *big.Int) *big.Int

S256 interprets x as a two's complement number. x must not exceed 256 bits (the result is undefined if it does) and is not modified.

S256(0)        = 0
S256(1)        = 1
S256(2**255)   = -2**255
S256(2**256-1) = -1

func SafeAdd

func SafeAdd(x, y uint64) (uint64, bool)

SafeAdd 加法运算,返回是否溢出

func SafeMul

func SafeMul(x, y uint64) (uint64, bool)

SafeMul 乘法运算,返回是否溢出

func ToWordSize

func ToWordSize(size uint64) uint64

ToWordSize 计算制定字节长度所对应的字长度(一个字,对应32个字节,也就是256位) 如果长度不足一个字长,则补足 EVM在内存中存储时的最小单位是字长(256位),而不是字节

func U256

func U256(x *big.Int) *big.Int

U256 求补

func Zero

func Zero(value *big.Int) bool

Zero 检查数字是否为0

Types

type Address

type Address struct {
	// contains filtered or unexported fields
}

Address 封装地址结构体,并提供各种常用操作封装 这里封装的操作主要是为了提供Address<->big.Int, Address<->[]byte 之间的互相转换 并且转换的核心是使用地址对象中的Hash160元素,因为在EVM中地址固定为[20]byte,超出此范围的地址无法正确解释执行

func BigToAddress

func BigToAddress(b *big.Int) Address

BigToAddress 大数字转换为地址

func BytesToAddress

func BytesToAddress(b []byte) Address

BytesToAddress 字节向地址转换

func EmptyAddress

func EmptyAddress() Address

EmptyAddress 返回空地址

func ExecAddress

func ExecAddress(execName string) Address

ExecAddress 返回合约地址

func NewAddress

func NewAddress(txHash []byte) Address

NewAddress xHash生成EVM合约地址

func StringToAddress

func StringToAddress(s string) *Address

StringToAddress 字符串转换为地址

func (Address) Big

func (a Address) Big() *big.Int

Big 大数字

func (Address) Bytes

func (a Address) Bytes() []byte

Bytes 字节数组

func (Address) Hash

func (a Address) Hash() Hash

Hash 计算地址哈希

func (Address) String

func (a Address) String() string

String 字符串结构

func (Address) ToHash160

func (a Address) ToHash160() Hash160Address

ToHash160 返回EVM类型地址

type Hash

type Hash common.Hash

Hash 重定义哈希类型

func BigToHash

func BigToHash(b *big.Int) Hash

BigToHash 大数字转换为哈希

func BytesToHash

func BytesToHash(b []byte) Hash

BytesToHash 将[]byte直接当做哈希处理

func ToHash

func ToHash(data []byte) Hash

ToHash 将[]byte经过哈希计算后转化为哈希对象

func (Hash) Big

func (h Hash) Big() *big.Int

Big 大数字形式

func (Hash) Bytes

func (h Hash) Bytes() []byte

Bytes 二进制形式

func (Hash) Hex

func (h Hash) Hex() string

Hex 十六进制形式

func (*Hash) SetBytes

func (h *Hash) SetBytes(b []byte)

SetBytes 设置哈希中的字节值,如果字节数组长度超过哈希长度,则被截断,只保留后面的部分

func (Hash) Str

func (h Hash) Str() string

Str 字符串形式

type Hash160Address

type Hash160Address [Hash160Length]byte

Hash160Address EVM中使用的地址格式

func BytesToHash160Address

func BytesToHash160Address(b []byte) Hash160Address

BytesToHash160Address 字节向地址转换

func HexToAddress

func HexToAddress(s string) Hash160Address

HexToAddress returns Address with byte values of s. If s is larger than len(h), s will be cropped from the left.

func (Hash160Address) Hex

func (h Hash160Address) Hex() string

Hex returns an EIP55-compliant hex string representation of the address.

func (*Hash160Address) SetBytes

func (h *Hash160Address) SetBytes(b []byte)

SetBytes sets the address to the value of b. If b is larger than len(a) it will panic.

func (Hash160Address) String

func (h Hash160Address) String() string

String implements fmt.Stringer.

func (Hash160Address) ToAddress

func (h Hash160Address) ToAddress() Address

ToAddress 返回Chain33格式的地址

type Message

type Message struct {
	// contains filtered or unexported fields
}

Message 合约交易消息模型 在EVM执行器中传递此消息,由外部Tx等价构造

func NewMessage

func NewMessage(from Address, to *Address, nonce int64, amount uint64, gasLimit uint64, gasPrice uint32, data []byte, alias, abi string) *Message

NewMessage 新建消息结构

func (Message) ABI

func (m Message) ABI() string

ABI 合约ABI

func (Message) Alias

func (m Message) Alias() string

Alias 合约别名

func (Message) Data

func (m Message) Data() []byte

Data 附带数据

func (Message) From

func (m Message) From() Address

From 来源

func (Message) GasLimit

func (m Message) GasLimit() uint64

GasLimit Gas限制

func (Message) GasPrice

func (m Message) GasPrice() uint32

GasPrice Gas价格

func (Message) Nonce

func (m Message) Nonce() int64

Nonce nonce值

func (Message) To

func (m Message) To() *Address

To 目的地址

func (Message) Value

func (m Message) Value() uint64

Value 转账金额

Directories

Path Synopsis
bn256
Package bn256 implements a particular bilinear group at the 128-bit security level.
Package bn256 implements a particular bilinear group at the 128-bit security level.

Jump to

Keyboard shortcuts

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