Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 87 additions & 91 deletions cmd/op-txverify/main.go
Copy link

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

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"io"
"os"

"github.com/ethereum-optimism/op-txverify/core"
Expand Down Expand Up @@ -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)",
Copy link

Choose a reason for hiding this comment

The 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",
Expand All @@ -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",
Expand Down Expand Up @@ -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")
Copy link

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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
}
Loading