Skip to content

Commit

Permalink
working version of ecrecover
Browse files Browse the repository at this point in the history
  • Loading branch information
gatsbyz committed Jan 26, 2024
1 parent d5fbde6 commit 1b5ab1a
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 103 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Note: Do not modify this section! It is auto-generated by `cobra` using `make ge

- [polycli dumpblocks](doc/polycli_dumpblocks.md) - Export a range of blocks from a JSON-RPC endpoint.

- [polycli ecrecover](doc/polycli_ecrecover.md) - Recovers and returns the public key of the signature

- [polycli enr](doc/polycli_enr.md) - Convert between ENR and Enode format

- [polycli fork](doc/polycli_fork.md) - Take a forked block and walk up the chain to do analysis.
Expand Down
53 changes: 0 additions & 53 deletions cmd/ecrecover/cmd.go

This file was deleted.

111 changes: 63 additions & 48 deletions cmd/ecrecover/ecrecover.go
Original file line number Diff line number Diff line change
@@ -1,71 +1,86 @@
package ecrecover

import (
"context"
"crypto/ecdsa"
_ "embed"
"fmt"
"math/big"

_ "embed"

"github.com/ethereum/go-ethereum/common"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/maticnetwork/polygon-cli/util"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

func ecrecover(ctx context.Context) (common.Address, error) {
rpc, err := ethrpc.DialContext(ctx, rpcUrl)
if err != nil {
log.Error().Err(err).Msg("Unable to dial rpc")
return common.Address{}, err
}
var (
//go:embed usage.md
usage string

ec := ethclient.NewClient(rpc)
if _, err = ec.BlockNumber(ctx); err != nil {
return common.Address{}, err
}
rpcUrl string
blockNumber int
extraData string
)

block, err := ec.BlockByNumber(ctx, big.NewInt(int64(blockNumber)))
if err != nil {
log.Error().Err(err).Msg("Unable to retrieve block")
return common.Address{}, err
}
var EcRecoverCmd = &cobra.Command{
Use: "ecrecover",
Short: "Recovers and returns the public key of the signature",
Long: usage,
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
return checkFlags()
},
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
rpc, err := ethrpc.DialContext(ctx, rpcUrl)
if err != nil {
log.Error().Err(err).Msg("Unable to dial rpc")
return
}

if len(block.Transactions()) == 0 {
return common.Address{}, fmt.Errorf("no transaction to derive public key from")
}
ec := ethclient.NewClient(rpc)
if _, err = ec.BlockNumber(ctx); err != nil {
return
}

signerBytes, err := util.Ecrecover(block)
if err != nil {
log.Error().Err(err).Msg("Unable to recover signature")
return common.Address{}, err
}
signerAddress := ethcommon.BytesToAddress(signerBytes)
block, err := ec.BlockByNumber(ctx, big.NewInt(int64(blockNumber)))
if err != nil {
log.Error().Err(err).Msg("Unable to retrieve block")
return
}

chainID, err := ec.NetworkID(ctx)
if err != nil {
log.Fatal().Err(err).Msg("Failed to get network ID")
}
if len(block.Transactions()) == 0 {
log.Error().Msg("no transaction to derive public key fromk")
return
}

var publicKey *ecdsa.PublicKey
for _, tx := range block.Transactions() {
tx.Value()
if msg, err := tx.AsMessage(types.LatestSignerForChainID(chainID), nil); err == nil {
if msg.From().Hex() == signerAddress {
publicKeyECDSA, err := crypto.SigToPub(signHash(msg), msg.Signature())
if err != nil {
log.Fatal().Err(err).Msg("Failed to recover public key")
}
publicKey = publicKeyECDSA
break
}
signerBytes, err := util.Ecrecover(block)
if err != nil {
log.Error().Err(err).Msg("Unable to recover signature")
return
}
cmd.Println(ethcommon.BytesToAddress(signerBytes))
},
}

func init() {
EcRecoverCmd.PersistentFlags().StringVarP(&rpcUrl, "rpc-url", "r", "http://localhost:8545", "The RPC endpoint url")
EcRecoverCmd.PersistentFlags().IntVarP(&blockNumber, "block-number", "b", 0, "Block number to check the extra data for")
EcRecoverCmd.PersistentFlags().StringVarP(&extraData, "extra-data", "e", "", "Raw extra data")
}

func checkFlags() (err error) {
if err = util.ValidateUrl(rpcUrl); err != nil {
return
}

if blockNumber <= 0 {
return fmt.Errorf("block-number should be greater than 0")
}

// if extraData == "" {
// return fmt.Errorf("block-number should be greater than 0")
// }

return nil
}
2 changes: 2 additions & 0 deletions doc/polycli.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Polycli is a collection of tools that are meant to be useful while building, tes

- [polycli dumpblocks](polycli_dumpblocks.md) - Export a range of blocks from a JSON-RPC endpoint.

- [polycli ecrecover](polycli_ecrecover.md) - Recovers and returns the public key of the signature

- [polycli enr](polycli_enr.md) - Convert between ENR and Enode format

- [polycli fork](polycli_fork.md) - Take a forked block and walk up the chain to do analysis.
Expand Down
54 changes: 54 additions & 0 deletions doc/polycli_ecrecover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# `polycli ecrecover`

> Auto-generated documentation.
## Table of Contents

- [Description](#description)
- [Usage](#usage)
- [Flags](#flags)
- [See Also](#see-also)

## Description

Recovers and returns the public key of the signature

```bash
polycli ecrecover [flags]
```

## Usage

![GIF of `polycli monitor`](assets/monitor.gif)

If you're using the terminal UI and you'd like to be able to select text for copying, you might need to use a modifier key.

If you're experiencing missing blocks, try adjusting the `--batch-size` and `--interval` flags so that you poll for more blocks or more frequently.

## Flags

```bash
-b, --block-number int Block number to check the extra data for
-e, --extra-data string Raw extra data
-h, --help help for ecrecover
-r, --rpc-url string The RPC endpoint url (default "http://localhost:8545")
```

The command also inherits flags from parent commands.

```bash
--config string config file (default is $HOME/.polygon-cli.yaml)
--pretty-logs Should logs be in pretty format or JSON (default true)
-v, --verbosity int 0 - Silent
100 Panic
200 Fatal
300 Error
400 Warning
500 Info
600 Debug
700 Trace (default 500)
```

## See also

- [polycli](polycli.md) - A Swiss Army knife of blockchain tools.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/cenkalti/backoff/v4 v4.2.1
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/cockroachdb/pebble v0.0.0-20230928194634-aa077af62593
github.com/ethereum/go-ethereum v1.13.8
github.com/ethereum/go-ethereum v1.13.11
github.com/gizak/termui/v3 v3.1.1-0.20231111080052-b3569a6cd52d
github.com/google/gofuzz v1.2.0
github.com/hashicorp/golang-lru v1.0.2
Expand Down Expand Up @@ -73,7 +73,7 @@ require (
github.com/ethereum/c-kzg-4844 v0.4.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/getsentry/sentry-go v0.18.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ github.com/ethereum/c-kzg-4844 v0.4.0 h1:3MS1s4JtA868KpJxroZoepdV0ZKBp3u/O5HcZ7R
github.com/ethereum/c-kzg-4844 v0.4.0/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0=
github.com/ethereum/go-ethereum v1.13.8 h1:1od+thJel3tM52ZUNQwvpYOeRHlbkVFZ5S8fhi0Lgsg=
github.com/ethereum/go-ethereum v1.13.8/go.mod h1:sc48XYQxCzH3fG9BcrXCOOgQk2JfZzNAmIKnceogzsA=
github.com/ethereum/go-ethereum v1.13.11 h1:b51Dsm+rEg7anFRUMGB8hODXHvNfcRKzz9vcj8wSdUs=
github.com/ethereum/go-ethereum v1.13.11/go.mod h1:gFtlVORuUcT+UUIcJ/veCNjkuOSujCi338uSHJrYAew=
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 h1:FtmdgXiUlNeRsoNMFlKLDt+S+6hbjVMEW6RGQ7aUf7c=
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0=
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
Expand All @@ -184,6 +186,8 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2
github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
Expand Down

0 comments on commit 1b5ab1a

Please sign in to comment.