Documentation
¶
Index ¶
- Constants
- Variables
- func BigPow(a, b int64) *big.Int
- func CalTxHash(signedTxHex string) (string, error)
- func Decode(input string) ([]byte, error)
- func DecodeTx(rawTx string) (string, error)
- func DecodeTxNonce(signedTxHex string) (uint64, error)
- func EIP712Hash(typedData TypedData) string
- func EIP712HashWithTypedDataHash(domainSeparatorHex, typedDataHashHex string) string
- func EIP712ParamsHash(typedData TypedData) string
- func EcRecover(signature, message string, addPrefix bool) (string, error)
- func EcRecoverPubKey(signature, message string, addPrefix bool) (*btcec.PublicKey, error)
- func FromHex(s string) []byte
- func GenerateRawTransactionWithSignature(txType int, chainId, unsignedRawTx, r, s, v string) (string, error)
- func GetAddress(pubkeyHex string) string
- func GetEthGroupAddress(prefix string, pubKey *btcec.PublicKey) string
- func GetEthGroupPubHash(pubKey *btcec.PublicKey) []byte
- func GetEthereumMessagePrefix(message string) string
- func GetNewAddress(pubKey *btcec.PublicKey) string
- func GetNewAddressBytes(pubKey *btcec.PublicKey) []byte
- func HasHexPrefix(str string) bool
- func Hex2Bytes(str string) []byte
- func IsEthHexAddress(s string) bool
- func IsHex(str string) bool
- func IsHexAddress(s string) bool
- func MessageHash(data string) string
- func NewEip1559Transaction(chainId *big.Int, nonce uint64, maxPriorityFeePerGas *big.Int, ...) *types.Transaction
- func OnlyRemovePrefix(s string) string
- func PaddedBigBytes(bigint *big.Int, n int) []byte
- func PubKeyToAddr(publicKey []byte) (string, error)
- func ReadBits(bigint *big.Int, buf []byte)
- func SignEthTypeMessage(message string, prvKey *btcec.PrivateKey, addPrefix bool) (string, error)
- func TypedDataAndHash(typedData TypedData) ([]byte, string, error)
- func U256(x *big.Int) *big.Int
- func U256Bytes(n *big.Int) []byte
- func ValidateAddress(address string) bool
- func VerifySignMsg(signature, message, address string, addPrefix bool) error
- type AccessList
- type AccessTuple
- type Address
- type DynamicFeeTx
- type EIP1559Transaction
- type Eip1559Token
- type Eip1559Transaction
- type EthTransaction
- func (tx *EthTransaction) GetSigningHash(chainId *big.Int) (string, string, error)
- func (tx *EthTransaction) Hash() (string, error)
- func (tx *EthTransaction) SignTransaction(chainId *big.Int, prvKey *btcec.PrivateKey) (string, error)
- func (tx *EthTransaction) SignedTx(chainId *big.Int, sig *SignatureData) (string, error)
- func (tx *EthTransaction) UnSignedTx(chainId *big.Int) (string, error)
- type Hash
- type NameValueType
- type SignatureData
- func NewSignatureData(msgHash []byte, publicKey string, r, s *big.Int) (*SignatureData, error)
- func SignAsRecoverable(value []byte, prvKey *btcec.PrivateKey) (*SignatureData, error)
- func SignMessage(message []byte, prvKey *btcec.PrivateKey) (*SignatureData, error)
- func SignMessageEIP1559(message []byte, prvKey *btcec.PrivateKey) (*SignatureData, error)
- type Transaction
- type Type
- type TypePriority
- type TypedData
- func (typedData *TypedData) Dependencies(primaryType string, found []string) []string
- func (typedData *TypedData) EncodeData(primaryType string, data map[string]interface{}, depth int) ([]byte, error)
- func (typedData *TypedData) EncodePrimitiveValue(encType string, encValue interface{}, depth int) ([]byte, error)
- func (typedData *TypedData) EncodeType(primaryType string) hexutil.Bytes
- func (typedData *TypedData) Format() ([]*NameValueType, error)
- func (typedData *TypedData) HashStruct(primaryType string, data TypedDataMessage) ([]byte, error)
- func (typedData *TypedData) Map() map[string]interface{}
- func (typedData *TypedData) TypeHash(primaryType string) []byte
- type TypedDataDomain
- type TypedDataMessage
- type Types
- type UnsignedTx
Constants ¶
const AddressLength = 20
const DynamicFeeTxType = 0x02
const MessagePrefixTmp = "\u0019Ethereum Signed Message:\n%d"
Variables ¶
var ( ErrEmptyString = &decError{"empty hex string"} ErrSyntax = &decError{"invalid hex string"} ErrMissingPrefix = &decError{"hex string without 0x prefix"} ErrOddLength = &decError{"hex string of odd length"} ErrUint64Range = &decError{"hex number > 64 bits"} )
Errors
var (
ErrInvalidParam = errors.New("invalid param")
)
var (
ErrInvalidSignature = errors.New("invalid signature")
)
Functions ¶
func DecodeTxNonce ¶
func EIP712Hash ¶
func EIP712ParamsHash ¶
func EcRecoverPubKey ¶
func FromHex ¶
FromHex returns the bytes represented by the hexadecimal string s. s may be prefixed with "0x".
func GenerateRawTransactionWithSignature ¶
func GenerateRawTransactionWithSignature(txType int, chainId, unsignedRawTx, r, s, v string) (string, error)
GenerateRawTransactionWithSignature Generate the transaction to be broadcast based on the unsigned transaction and the signature result
func GetAddress ¶
func GetEthGroupPubHash ¶
func GetNewAddress ¶
func GetNewAddressBytes ¶
func HasHexPrefix ¶
func IsEthHexAddress ¶
func IsHexAddress ¶
IsHexAddress verifies whether a string can represent a valid hex-encoded Ethereum address or not.
func MessageHash ¶
func NewEip1559Transaction ¶
func OnlyRemovePrefix ¶
func PaddedBigBytes ¶
PaddedBigBytes encodes a big integer as a big-endian byte slice. The length of the slice is at least n bytes.
func PubKeyToAddr ¶
func ReadBits ¶
ReadBits encodes the absolute value of bigint as big-endian bytes. Callers must ensure that buf has enough space. If buf is too short the result will be incomplete.
func SignEthTypeMessage ¶
func TypedDataAndHash ¶
TypedDataAndHash is a helper function that calculates a hash for typed data conforming to EIP-712. This hash can then be safely used to calculate a signature.
See https://eips.ethereum.org/EIPS/eip-712 for the full specification.
This gives context to the signed typed data and prevents signing of transactions.
func U256Bytes ¶
U256Bytes converts a big Int into a 256bit EVM number. This operation is destructive.
func ValidateAddress ¶
func VerifySignMsg ¶
Types ¶
type AccessList ¶
type AccessList []AccessTuple
type AccessTuple ¶
type AccessTuple struct {
Address []byte `json:"address" gencodec:"required"`
StorageKeys []Hash `json:"storageKeys" gencodec:"required"`
}
AccessTuple is the element type of an access list.
type Address ¶
type Address [20]byte
Address represents the 20 byte address of an Ethereum account.
func BigToAddress ¶
BigToAddress returns Address with byte values of b. If b is larger than len(h), b will be cropped from the left.
func BytesToAddress ¶
BytesToAddress returns Address with value b. If b is larger than len(h), b will be cropped from the left.
func HexToAddress ¶
HexToAddress returns Address with byte values of s. If s is larger than len(h), s will be cropped from the left.
type DynamicFeeTx ¶
type DynamicFeeTx struct {
ChainID *big.Int `json:"chainId"`
Nonce uint64 `json:"nonce"`
GasTipCap *big.Int `json:"gasTipCap"` // a.k.a. maxPriorityFeePerGas
GasFeeCap *big.Int `json:"gasFeeCap"` // a.k.a. maxFeePerGas
Gas uint64 `json:"gas"`
To *Address `json:"to"` // nil means contract creation
Value *big.Int `json:"value"`
Data []byte `json:"data"`
AccessList AccessList
// Signature values
V *big.Int `json:"v" gencodec:"required"`
R *big.Int `json:"r" gencodec:"required"`
S *big.Int `json:"s" gencodec:"required"`
}
DynamicFeeTx represents an EIP-1559 transaction.
func NewEthDynamicFeeTx ¶
func (*DynamicFeeTx) Hash ¶
func (tx *DynamicFeeTx) Hash() (string, error)
func (*DynamicFeeTx) SignTransaction ¶
func (tx *DynamicFeeTx) SignTransaction(prvKey *btcec.PrivateKey) (string, error)
type EIP1559Transaction ¶
type EIP1559Transaction struct {
Transaction
TxType int `json:"txType"`
MaxFeePerGas string `json:"maxFeePerGas"`
MaxPriorityFeePerGas string `json:"maxPriorityFeePerGas"`
}
type Eip1559Token ¶
type Eip1559Token struct {
EIP1559Transaction
ContractAddress string `json:"contract_address"`
Amount string `json:"amount"`
}
type Eip1559Transaction ¶
type Eip1559Transaction struct {
ChainId *big.Int `json:"chainId"`
Nonce uint64 `json:"nonce"`
GasTipCap *big.Int `json:"gasTipCap"`
GasFeeCap *big.Int `json:"gasFeeCap"`
Gas uint64 `json:"gas"`
To *common.Address `json:"to"`
Value *big.Int `json:"value"`
Data []byte `json:"data"`
AccessList types.AccessList `json:"accessList"`
}
type EthTransaction ¶
type EthTransaction struct {
Nonce *big.Int `json:"nonce"`
GasPrice *big.Int `json:"gasPrice"`
GasLimit *big.Int `json:"gas"`
To []byte `json:"to"`
Value *big.Int `json:"value"`
Data []byte `json:"data"`
// Signature values
V *big.Int `json:"v"`
R *big.Int `json:"r"`
S *big.Int `json:"s"`
}
func NewEthTransaction ¶
func NewEthTransaction(nonce, gasLimit, gasPrice, value *big.Int, to, data string) *EthTransaction
func NewTransactionFromRaw ¶
func NewTransactionFromRaw(raw string) (*EthTransaction, error)
func (*EthTransaction) GetSigningHash ¶
func (*EthTransaction) Hash ¶
func (tx *EthTransaction) Hash() (string, error)
func (*EthTransaction) SignTransaction ¶
func (tx *EthTransaction) SignTransaction(chainId *big.Int, prvKey *btcec.PrivateKey) (string, error)
func (*EthTransaction) SignedTx ¶
func (tx *EthTransaction) SignedTx(chainId *big.Int, sig *SignatureData) (string, error)
func (*EthTransaction) UnSignedTx ¶
func (tx *EthTransaction) UnSignedTx(chainId *big.Int) (string, error)
type NameValueType ¶
type NameValueType struct {
Name string `json:"name"`
Value interface{} `json:"value"`
Typ string `json:"type"`
}
NameValueType is a very simple struct with Name, Value and Type. It's meant for simple json structures used to communicate signing-info about typed data with the UI
func (*NameValueType) Pprint ¶
func (nvt *NameValueType) Pprint(depth int) string
Pprint returns a pretty-printed version of nvt
type SignatureData ¶
func NewSignatureData ¶
func SignAsRecoverable ¶
func SignAsRecoverable(value []byte, prvKey *btcec.PrivateKey) (*SignatureData, error)
func SignMessage ¶
func SignMessage(message []byte, prvKey *btcec.PrivateKey) (*SignatureData, error)
func SignMessageEIP1559 ¶
func SignMessageEIP1559(message []byte, prvKey *btcec.PrivateKey) (*SignatureData, error)
func (SignatureData) ToBytes ¶
func (sd SignatureData) ToBytes() []byte
func (*SignatureData) ToHex ¶
func (sd *SignatureData) ToHex() string
type Transaction ¶
type TypePriority ¶
type TypedData ¶
type TypedData struct {
Types Types `json:"types"`
PrimaryType string `json:"primaryType"`
Domain TypedDataDomain `json:"domain"`
Message TypedDataMessage `json:"message"`
}
func (*TypedData) Dependencies ¶
Dependencies returns an array of custom types ordered by their hierarchical reference tree
func (*TypedData) EncodeData ¶
func (typedData *TypedData) EncodeData(primaryType string, data map[string]interface{}, depth int) ([]byte, error)
EncodeData generates the following encoding: `enc(value₁) ‖ enc(value₂) ‖ … ‖ enc(valueₙ)`
each encoded member is 32-byte long
func (*TypedData) EncodePrimitiveValue ¶
func (typedData *TypedData) EncodePrimitiveValue(encType string, encValue interface{}, depth int) ([]byte, error)
EncodePrimitiveValue deals with the primitive values found while searching through the typed data
func (*TypedData) EncodeType ¶
EncodeType generates the following encoding: `name ‖ "(" ‖ member₁ ‖ "," ‖ member₂ ‖ "," ‖ … ‖ memberₙ ")"`
each member is written as `type ‖ " " ‖ name` encodings cascade down and are sorted by name
func (*TypedData) Format ¶
func (typedData *TypedData) Format() ([]*NameValueType, error)
Format returns a representation of typedData, which can be easily displayed by a user-interface without in-depth knowledge about 712 rules
func (*TypedData) HashStruct ¶
func (typedData *TypedData) HashStruct(primaryType string, data TypedDataMessage) ([]byte, error)
HashStruct generates a keccak256 hash of the encoding of the provided data
type TypedDataDomain ¶
type TypedDataDomain struct {
Name string `json:"name"`
Version string `json:"version"`
ChainId *big.Int `json:"chainId"`
VerifyingContract string `json:"verifyingContract"`
Salt string `json:"salt"`
}
func (*TypedDataDomain) Map ¶
func (domain *TypedDataDomain) Map() map[string]interface{}
Map is a helper function to generate a map version of the domain
type TypedDataMessage ¶
type TypedDataMessage = map[string]interface{}
type UnsignedTx ¶
func GenerateTxWithJSON ¶
generate tx with json param
func NewUnsignedTx ¶
func NewUnsignedTx(nonce, gasLimit, gasPrice, value, chainId *big.Int, to, data string) (*UnsignedTx, error)