Help us shape the future of Go! Take the 2026 Go Developer Survey

ecrecover

package
v0.1.103 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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) (err error) {
		rpcURL, err = flag.GetRPCURL(cmd)
		if err != nil {
			return err
		}
		return nil
	},
	Run: func(cmd *cobra.Command, args []string) {
		ctx := cmd.Context()
		var (
			signerBytes []byte
			err         error
		)

		if filePath != "" {
			var blockJSON []byte
			if filePath != "" {
				blockJSON, err = os.ReadFile(filePath)
				if err != nil {
					log.Error().Err(err).Msg("Unable to read file")
					return
				}
			} else {
				blockJSON, err = io.ReadAll(os.Stdin)
				if err != nil {
					log.Error().Err(err).Msg("Unable to read stdin")
					return
				}
			}

			var header types.Header
			if err = json.Unmarshal(blockJSON, &header); err != nil {
				log.Error().Err(err).Msg("Unable to unmarshal JSON")
				return
			}

			block := types.NewBlockWithHeader(&header)
			blockNumber = header.Number.Uint64()
			signerBytes, err = util.Ecrecover(block)

		} else if txData != "" {

			txBytes := ethcommon.FromHex(txData)
			var tx types.Transaction
			err = tx.UnmarshalBinary(txBytes)
			if err != nil {
				log.Error().Err(err).Msg("Unable to decode transaction")
				return
			}
			signerBytes, err = util.EcrecoverTx(&tx)
			if err != nil {
				log.Error().Err(err).Msg("Unable to retrieve block")
				return
			}

		} else {
			if rpcURL == "" {
				log.Error().Msg("No RPC URL provided")
				return
			}
			var rpc *ethrpc.Client
			rpc, err = ethrpc.DialContext(ctx, rpcURL)
			if err != nil {
				log.Error().Err(err).Msg("Unable to dial rpc")
				return
			}
			ec := ethclient.NewClient(rpc)
			defer ec.Close()

			var block *types.Block
			if blockNumber == 0 {
				blockNumber, err = ec.BlockNumber(ctx)
				if err != nil {
					log.Error().Err(err).Msg("Unable to retrieve latest block number")
					return
				}
				cmd.Println("Using latest block number:", blockNumber)
			}
			block, err = ec.BlockByNumber(ctx, big.NewInt(int64(blockNumber)))
			if err != nil {
				log.Error().Err(err).Msg("Unable to retrieve block")
				return
			}
			signerBytes, err = util.Ecrecover(block)
		}

		if err != nil {
			log.Error().Err(err).Msg("Unable to recover signature")
			return
		}
		cmd.Println(ethcommon.BytesToAddress(signerBytes))
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL