1 Star 0 Fork 0

coin-kit / eth-tx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
utils.go 1.80 KB
一键复制 编辑 原始数据 按行查看 历史
跃迁雷电 提交于 2019-07-07 17:30 . dd
package ethtx
import (
"crypto/ecdsa"
"errors"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
)
func DecodePrivKey(privkey string) (*ecdsa.PrivateKey, error) {
rawpk := hexutil.Bytes{}
err := rawpk.UnmarshalText([]byte(privkey))
if err != nil {
return nil, err
}
pk, err := crypto.ToECDSA(rawpk)
if err != nil {
return nil, err
}
return pk, nil
}
func DecodeTx(unsign_tx string) (*types.Transaction, error) {
// 反序列化
rawtx := hexutil.Bytes{}
err := rawtx.UnmarshalText([]byte(unsign_tx))
if err != nil {
return nil, err
}
// 分离FROM
rawtx = rawtx[20:]
// 解码交易
tx := &types.Transaction{}
err = rlp.DecodeBytes(rawtx, &tx)
if err != nil {
return nil, err
}
return tx, nil
}
func DecodeAddress(hex_address string) (common.Address, error) {
addr := common.HexToAddress(hex_address)
zero := common.Address{}
if addr.String() == zero.String() {
return common.Address{}, errors.New("eth from address format error")
}
return addr, nil
}
func DeriveTxFrom(tx *types.Transaction) (string, error) {
v, _, _ := tx.RawSignatureValues()
if v == nil {
return "", errors.New("invalid sender: nil V field]")
}
var signer types.Signer = types.FrontierSigner{}
if tx.Protected() {
signer = types.NewEIP155Signer(tx.ChainId())
}
from, err := types.Sender(signer, tx)
if err != nil {
return "", err
}
return from.String(), nil
}
func GenTrasferABI(to string, amount *big.Int) string {
parsed, _ := abi.JSON(strings.NewReader(TokenABI))
input, _ := parsed.Pack("transfer", common.HexToAddress(to), amount)
return common.Bytes2Hex(input)
}
Go
1
https://gitee.com/coin-kit/eth-tx.git
git@gitee.com:coin-kit/eth-tx.git
coin-kit
eth-tx
eth-tx
302523317217

搜索帮助