Skip to content

Commit 47e6f9f

Browse files
holimankjx98
authored andcommitted
cmd: implement abidump (ethereum#19958)
* abidump: implement abi dump command * cmd/abidump: add license
1 parent 03aa0b8 commit 47e6f9f

File tree

3 files changed

+79
-5
lines changed

3 files changed

+79
-5
lines changed

cmd/abidump/main.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2019 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package main
18+
19+
import (
20+
"encoding/hex"
21+
"flag"
22+
"fmt"
23+
"os"
24+
"strings"
25+
26+
"github.com/ethereum/go-ethereum/signer/core"
27+
"github.com/ethereum/go-ethereum/signer/fourbyte"
28+
)
29+
30+
func init() {
31+
flag.Usage = func() {
32+
fmt.Fprintln(os.Stderr, "Usage:", os.Args[0], "<hexdata>")
33+
flag.PrintDefaults()
34+
fmt.Fprintln(os.Stderr, `
35+
Parses the given ABI data and tries to interpret it from the fourbyte database.`)
36+
}
37+
}
38+
39+
func parse(data []byte) {
40+
db, err := fourbyte.New()
41+
if err != nil {
42+
die(err)
43+
}
44+
messages := core.ValidationMessages{}
45+
db.ValidateCallData(nil, data, &messages)
46+
for _, m := range messages.Messages {
47+
fmt.Printf("%v: %v\n", m.Typ, m.Message)
48+
}
49+
}
50+
51+
// Example
52+
// ./abidump a9059cbb000000000000000000000000ea0e2dc7d65a50e77fc7e84bff3fd2a9e781ff5c0000000000000000000000000000000000000000000000015af1d78b58c40000
53+
func main() {
54+
flag.Parse()
55+
56+
switch {
57+
case flag.NArg() == 1:
58+
hexdata := flag.Arg(0)
59+
data, err := hex.DecodeString(strings.TrimPrefix(hexdata, "0x"))
60+
if err != nil {
61+
die(err)
62+
}
63+
parse(data)
64+
default:
65+
fmt.Fprintln(os.Stderr, "Error: one argument needed")
66+
flag.Usage()
67+
os.Exit(2)
68+
}
69+
}
70+
71+
func die(args ...interface{}) {
72+
fmt.Fprintln(os.Stderr, args...)
73+
os.Exit(1)
74+
}

signer/fourbyte/abi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func parseCallData(calldata []byte, abidata string) (*decodedCallData, error) {
137137
}
138138
values, err := method.Inputs.UnpackValues(argdata)
139139
if err != nil {
140-
return nil, err
140+
return nil, fmt.Errorf("signature %q matches, but arguments mismatch: %v", method.String(), err)
141141
}
142142
// Everything valid, assemble the call infos for the signer
143143
decoded := decodedCallData{signature: method.Sig(), name: method.RawName}

signer/fourbyte/validation.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ func (db *Database) ValidateTransaction(selector *string, tx *core.SendTxArgs) (
7474
messages.Crit("Transaction recipient is the zero address")
7575
}
7676
// Semantic fields validated, try to make heads or tails of the call data
77-
db.validateCallData(selector, data, messages)
77+
db.ValidateCallData(selector, data, messages)
7878
return messages, nil
7979
}
8080

81-
// validateCallData checks if the ABI call-data + method selector (if given) can
81+
// ValidateCallData checks if the ABI call-data + method selector (if given) can
8282
// be parsed and seems to match.
83-
func (db *Database) validateCallData(selector *string, data []byte, messages *core.ValidationMessages) {
83+
func (db *Database) ValidateCallData(selector *string, data []byte, messages *core.ValidationMessages) {
8484
// If the data is empty, we have a plain value transfer, nothing more to do
8585
if len(data) == 0 {
8686
return
@@ -110,7 +110,7 @@ func (db *Database) validateCallData(selector *string, data []byte, messages *co
110110
return
111111
}
112112
if info, err := verifySelector(embedded, data); err != nil {
113-
messages.Warn(fmt.Sprintf("Transaction contains data, but provided ABI signature could not be varified: %v", err))
113+
messages.Warn(fmt.Sprintf("Transaction contains data, but provided ABI signature could not be verified: %v", err))
114114
} else {
115115
messages.Info(info.String())
116116
}

0 commit comments

Comments
 (0)