-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
127 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
 | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters