构造交易
import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/common/hexutil"
)
const (
GAS_LIMIT = 21000
GAS_PRICE = 500000000000
)
tx := types.NewTransaction(nonce, common.HexToAddress(toAddress), value, GAS_LIMIT, big.NewInt(GAS_PRICE), nil)
签名交易
其中 StringToPrivateKey
是获取私钥的方法,在下边会有介绍
func SignTransaction(tx *types.Transaction, privateKeyStr string) (string, error) {
privateKey, err := StringToPrivateKey(privateKeyStr)
if err != nil {
return "", err
}
signTx, err := types.SignTx(tx, types.NewEIP155Signer(big.NewInt(4)), privateKey)
//signTx, err := types.SignTx(tx, types.HomesteadSigner{}, privateKey)
if err != nil {
return "", nil
}
b, err := rlp.EncodeToBytes(signTx)
if err != nil {
return "", err
}
return hex.EncodeToString(b), nil
}
其中签名有两种算法
- types.NewEIP155Signer(big.NewInt(chainId))
- types.HomesteadSigner{}
第二种不需要提供 chainId
但是据说不稳定,types.NewEIP155Signer(big.NewInt(4)
4 是 rinkeby 测试网络,1是主网
私钥的获取 *ecdsa.PrivateKey
- 从明文的私钥字符串转换成该类型
func StringToPrivateKey(privateKeyStr string) (*ecdsa.PrivateKey, error) {
privateKeyByte, err := hexutil.Decode(privateKeyStr)
if err != nil {
return nil, err
}
privateKey, err := crypto.ToECDSA(privateKeyByte)
if err != nil {
return nil, err
}
return privateKey, nil
}
- keystore + password 解析出私钥
func KeystoreToPrivateKey(keystoreContent []byte, password string) (*ecdsa.PrivateKey, error) {
unlockedKey, err := keystore.DecryptKey(keystoreContent, password)
if err != nil {
return nil, err
}
return unlockedKey.PrivateKey, nil
}
有疑问加站长微信联系(非本文作者)