From cc683e3da9a4fc7785487aff7596b42f5c75ca63 Mon Sep 17 00:00:00 2001 From: robinsdan <115981357+robinsdan@users.noreply.github.com> Date: Wed, 10 Jan 2024 11:22:59 +0800 Subject: [PATCH] expose pub key to address (#75) --- contract/contract.go | 12 ++++++------ contract/provider.go | 9 ++++----- wallet/key.go | 6 +++--- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/contract/contract.go b/contract/contract.go index de1119b..8cb9ec8 100644 --- a/contract/contract.go +++ b/contract/contract.go @@ -159,7 +159,7 @@ type Txn struct { value *big.Int nonce uint64 gasLimit uint64 - gasPrice uint64 + GasPrice uint64 Data []byte hash web3.Hash } @@ -182,7 +182,7 @@ func (t *Txn) EstimateGas() (uint64, error) { To: t.to, Data: t.Data, Value: t.value, - GasPrice: t.gasPrice, + GasPrice: t.GasPrice, } return t.provider.Eth().EstimateGas(msg) } @@ -227,8 +227,8 @@ func (self *SignedTx) SendTransaction(signer *Signer) *web3.Receipt { func (t *Txn) ToTransaction() (*web3.Transaction, error) { var err error // estimate gas price - if t.gasPrice == 0 { - t.gasPrice, err = t.provider.Eth().GasPrice() + if t.GasPrice == 0 { + t.GasPrice, err = t.provider.Eth().GasPrice() if err != nil { return nil, err } @@ -254,7 +254,7 @@ func (t *Txn) ToTransaction() (*web3.Transaction, error) { txn := &web3.Transaction{ From: t.from, Input: t.Data, - GasPrice: t.gasPrice, + GasPrice: t.GasPrice, Gas: t.gasLimit, Value: t.value, Nonce: t.nonce, @@ -282,7 +282,7 @@ func (t *Txn) Do() error { // SetGasPrice sets the gas price of the transaction func (t *Txn) SetGasPrice(gasPrice uint64) *Txn { - t.gasPrice = gasPrice + t.GasPrice = gasPrice return t } diff --git a/contract/provider.go b/contract/provider.go index 8a462ad..521ff4e 100644 --- a/contract/provider.go +++ b/contract/provider.go @@ -7,10 +7,9 @@ import ( "strings" "time" - "github.com/laizy/web3/executor/remotedb" - "github.com/laizy/web3" "github.com/laizy/web3/executor" + "github.com/laizy/web3/executor/remotedb" "github.com/laizy/web3/jsonrpc" "github.com/laizy/web3/utils" "github.com/laizy/web3/wallet" @@ -97,7 +96,7 @@ func (self *Signer) WaitTx(hs web3.Hash) *web3.Receipt { } } -func (self *Signer) TransferEther(to web3.Address, value *big.Int, msg string) *web3.Transaction { +func (self *Signer) TransferEther(to web3.Address, value *big.Int, msg string) *Txn { txn := &Txn{ from: self.Address(), to: &to, @@ -105,8 +104,8 @@ func (self *Signer) TransferEther(to web3.Address, value *big.Int, msg string) * Data: []byte(msg), value: value, } - tx := txn.MustToTransaction() - return self.SignTx(tx) + + return txn } func (e *Signer) GetNonce(blockNumber web3.BlockNumber) (uint64, error) { diff --git a/wallet/key.go b/wallet/key.go index b1487d8..fb5ee82 100644 --- a/wallet/key.go +++ b/wallet/key.go @@ -61,11 +61,11 @@ func newKey(priv *ecdsa.PrivateKey) *Key { return &Key{ priv: priv, pub: &priv.PublicKey, - addr: pubKeyToAddress(&priv.PublicKey), + addr: PubKeyToAddress(&priv.PublicKey), } } -func pubKeyToAddress(pub *ecdsa.PublicKey) (addr web3.Address) { +func PubKeyToAddress(pub *ecdsa.PublicKey) (addr web3.Address) { b := keccak256(elliptic.Marshal(S256, pub.X, pub.Y)[1:]) copy(addr[:], b[12:]) return @@ -89,7 +89,7 @@ func Ecrecover(hash, signature []byte) (web3.Address, error) { if err != nil { return web3.Address{}, err } - return pubKeyToAddress(pub), nil + return PubKeyToAddress(pub), nil } func RecoverPubkey(signature, hash []byte) (*ecdsa.PublicKey, error) {