-
Notifications
You must be signed in to change notification settings - Fork 2
feat: verify via tx or hash #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
smartcontracts
wants to merge
3
commits into
main
Choose a base branch
from
sc/verify-via-tx
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ package main | |
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
|
|
||
| "github.com/ethereum-optimism/op-txverify/core" | ||
|
|
@@ -61,21 +62,23 @@ func main() { | |
| Usage: "Generate and verify a transaction in one step", | ||
| Flags: []cli.Flag{ | ||
| &cli.StringFlag{ | ||
| Name: "network", | ||
| Aliases: []string{"n"}, | ||
| Usage: "Network name: ethereum, op, base (required)", | ||
| Required: true, | ||
| Name: "tx", | ||
| Aliases: []string{"t"}, | ||
| Usage: "Safe transaction hash or link (alternative to --network/--safe/--nonce)", | ||
| }, | ||
| &cli.StringFlag{ | ||
| Name: "safe", | ||
| Aliases: []string{"a"}, | ||
| Usage: "Safe address (required)", | ||
| Required: true, | ||
| Name: "network", | ||
| Aliases: []string{"n"}, | ||
| Usage: "Network name: ethereum, op, base (required unless --tx is used)", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be a separate PR, but we should support sepolia too |
||
| }, | ||
| &cli.StringFlag{ | ||
| Name: "safe", | ||
| Aliases: []string{"a"}, | ||
| Usage: "Safe address (required unless --tx is used)", | ||
| }, | ||
| &cli.Uint64Flag{ | ||
| Name: "nonce", | ||
| Usage: "Transaction nonce (required)", | ||
| Required: true, | ||
| Name: "nonce", | ||
| Usage: "Transaction nonce (required unless --tx is used)", | ||
| }, | ||
| &cli.StringFlag{ | ||
| Name: "output", | ||
|
|
@@ -96,21 +99,23 @@ func main() { | |
| Usage: "Generate a transaction JSON file", | ||
| Flags: []cli.Flag{ | ||
| &cli.StringFlag{ | ||
| Name: "network", | ||
| Aliases: []string{"n"}, | ||
| Usage: "Network name: ethereum, op, base (required)", | ||
| Required: true, | ||
| Name: "tx", | ||
| Aliases: []string{"t"}, | ||
| Usage: "Safe transaction hash or link (alternative to --network/--safe/--nonce)", | ||
| }, | ||
| &cli.StringFlag{ | ||
| Name: "safe", | ||
| Aliases: []string{"a"}, | ||
| Usage: "Safe address (required)", | ||
| Required: true, | ||
| Name: "network", | ||
| Aliases: []string{"n"}, | ||
| Usage: "Network name: ethereum, op, base (required unless --tx is used)", | ||
| }, | ||
| &cli.StringFlag{ | ||
| Name: "safe", | ||
| Aliases: []string{"a"}, | ||
| Usage: "Safe address (required unless --tx is used)", | ||
| }, | ||
| &cli.Uint64Flag{ | ||
| Name: "nonce", | ||
| Usage: "Transaction nonce (required)", | ||
| Required: true, | ||
| Name: "nonce", | ||
| Usage: "Transaction nonce (required unless --tx is used)", | ||
| }, | ||
| &cli.StringFlag{ | ||
| Name: "output", | ||
|
|
@@ -170,90 +175,41 @@ func offlineAction(c *cli.Context) error { | |
| return fmt.Errorf("failed to parse transaction: %w", err) | ||
| } | ||
|
|
||
| // Set verification options | ||
| options := core.VerifyOptions{ | ||
| Verbose: verbose, | ||
| } | ||
|
|
||
| // Verify the transaction | ||
| result, err := core.VerifyTransaction(tx, options) | ||
| if err != nil { | ||
| return fmt.Errorf("error verifying transaction: %w", err) | ||
| } | ||
|
|
||
| // Output the result in the requested format | ||
| switch outputFormat { | ||
| case "json": | ||
| output.FormatJSON(result, os.Stdout) | ||
| case "terminal": | ||
| output.FormatTerminal(result, os.Stdout) | ||
| default: | ||
| return fmt.Errorf("unknown output format: %s", outputFormat) | ||
| } | ||
|
|
||
| return nil | ||
| return verifyTransactionAndFormat(tx, verbose, outputFormat, os.Stdout) | ||
| } | ||
|
|
||
| func onlineAction(c *cli.Context) error { | ||
| txInput := c.String("tx") | ||
| network := c.String("network") | ||
| address := c.String("safe") | ||
| nonce := c.Uint64("nonce") | ||
| outputFormat := c.String("output") | ||
| verbose := c.Bool("verbose") | ||
|
|
||
| // Validate network | ||
| if network != "ethereum" && network != "op" && network != "base" { | ||
| return fmt.Errorf("invalid network: %s (must be ethereum, op, or base)", network) | ||
| } | ||
|
|
||
| // Strip the chain prefix if present | ||
| address = core.StripChainPrefix(address) | ||
|
|
||
| // Generate the transaction | ||
| tx, err := core.GenerateTransaction(network, address, nonce) | ||
| tx, metadata, err := getTxFromFlags(txInput, network, address, nonce, c.IsSet("nonce")) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Set verification options | ||
| options := core.VerifyOptions{ | ||
| Verbose: verbose, | ||
| } | ||
|
|
||
| // Verify the generated transaction | ||
| result, err := core.VerifyTransaction(*tx, options) | ||
| if err != nil { | ||
| return fmt.Errorf("error verifying transaction: %w", err) | ||
| } | ||
|
|
||
| // Output the result in the requested format | ||
| switch outputFormat { | ||
| case "json": | ||
| output.FormatJSON(result, os.Stdout) | ||
| case "terminal": | ||
| output.FormatTerminal(result, os.Stdout) | ||
| default: | ||
| return fmt.Errorf("unknown output format: %s", outputFormat) | ||
| if verbose && metadata != nil { | ||
| fmt.Fprintf(os.Stderr, "Found transaction on %s (chain %d)\n", metadata.Network, metadata.ChainID) | ||
| fmt.Fprintf(os.Stderr, "Safe: %s\n", metadata.SafeAddress) | ||
| fmt.Fprintf(os.Stderr, "Nonce: %d\n", metadata.Nonce) | ||
| } | ||
|
|
||
| return nil | ||
| return verifyTransactionAndFormat(*tx, verbose, outputFormat, os.Stdout) | ||
| } | ||
|
|
||
| func downloadAction(c *cli.Context) error { | ||
| txInput := c.String("tx") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add some tests for the new functionality? |
||
| network := c.String("network") | ||
| address := c.String("safe") | ||
| nonce := c.Uint64("nonce") | ||
| outputFile := c.String("output") | ||
|
|
||
| // Validate network | ||
| if network != "ethereum" && network != "op" && network != "base" { | ||
| return fmt.Errorf("invalid network: %s (must be ethereum, op, or base)", network) | ||
| } | ||
|
|
||
| // Generate the transaction JSON | ||
| tx, err := core.GenerateTransaction(network, address, nonce) | ||
| tx, _, err := getTxFromFlags(txInput, network, address, nonce, c.IsSet("nonce")) | ||
| if err != nil { | ||
| return fmt.Errorf("error generating transaction: %w", err) | ||
| return err | ||
| } | ||
|
|
||
| // Output the transaction JSON | ||
|
|
@@ -287,26 +243,66 @@ func qrAction(c *cli.Context) error { | |
| return fmt.Errorf("failed to parse transaction from QR code: %w", err) | ||
| } | ||
|
|
||
| // Set verification options | ||
| options := core.VerifyOptions{ | ||
| Verbose: verbose, | ||
| return verifyTransactionAndFormat(tx, verbose, outputFormat, os.Stdout) | ||
| } | ||
|
|
||
| // getTxFromFlags resolves a SafeTransaction from either a tx hash/link or network/safe/nonce flags. | ||
| func getTxFromFlags(txInput, network, address string, nonce uint64, nonceProvided bool) (*core.SafeTransaction, *core.TransactionMetadata, error) { | ||
| if txInput != "" { | ||
| // Extract transaction hash from URL or use directly | ||
| txHash, err := core.ExtractTransactionHash(txInput) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("error parsing transaction input: %w", err) | ||
| } | ||
|
|
||
| // Fetch transaction data from API | ||
| metadata, err := core.FetchTransactionByHash(txHash) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("error fetching transaction: %w", err) | ||
| } | ||
|
|
||
| return metadata.Transaction, metadata, nil | ||
| } | ||
|
|
||
| // Validate required parameters | ||
| if network == "" || address == "" || (nonce == 0 && !nonceProvided) { | ||
| return nil, nil, fmt.Errorf("either --tx or all of (--network, --safe, --nonce) must be provided") | ||
| } | ||
|
|
||
| // Validate network (keep aligned with CLI help text) | ||
| if network != "ethereum" && network != "op" && network != "base" { | ||
| return nil, nil, fmt.Errorf("invalid network: %s (must be ethereum, op, or base)", network) | ||
| } | ||
|
|
||
| // Strip the chain prefix if present | ||
| address = core.StripChainPrefix(address) | ||
|
|
||
| // Generate the transaction | ||
| tx, err := core.GenerateTransaction(network, address, nonce) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| return tx, nil, nil | ||
| } | ||
|
|
||
| // Verify the transaction | ||
| // verifyTransactionAndFormat verifies a transaction and writes it using the requested formatter. | ||
| func verifyTransactionAndFormat(tx core.SafeTransaction, verbose bool, outputFormat string, w io.Writer) error { | ||
| options := core.VerifyOptions{Verbose: verbose} | ||
| result, err := core.VerifyTransaction(tx, options) | ||
| if err != nil { | ||
| return fmt.Errorf("error verifying transaction: %w", err) | ||
| } | ||
| return writeVerificationOutput(result, outputFormat, w) | ||
| } | ||
|
|
||
| // Output the result in the requested format | ||
| // writeVerificationOutput writes a VerificationResult to the writer using the selected output format. | ||
| func writeVerificationOutput(result *core.VerificationResult, outputFormat string, w io.Writer) error { | ||
| switch outputFormat { | ||
| case "json": | ||
| output.FormatJSON(result, os.Stdout) | ||
| return output.FormatJSON(result, w) | ||
| case "terminal": | ||
| output.FormatTerminal(result, os.Stdout) | ||
| return output.FormatTerminal(result, w) | ||
| default: | ||
| return fmt.Errorf("unknown output format: %s", outputFormat) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should update the readme with some example invocations