Skip to content

Commit

Permalink
expose pub key to address (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinsdan authored Jan 10, 2024
1 parent 646a553 commit cc683e3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
12 changes: 6 additions & 6 deletions contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ type Txn struct {
value *big.Int
nonce uint64
gasLimit uint64
gasPrice uint64
GasPrice uint64
Data []byte
hash web3.Hash
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down Expand Up @@ -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
}

Expand Down
9 changes: 4 additions & 5 deletions contract/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -97,16 +96,16 @@ 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,
provider: self.Client,
Data: []byte(msg),
value: value,
}
tx := txn.MustToTransaction()
return self.SignTx(tx)

return txn
}

func (e *Signer) GetNonce(blockNumber web3.BlockNumber) (uint64, error) {
Expand Down
6 changes: 3 additions & 3 deletions wallet/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit cc683e3

Please sign in to comment.