Skip to content

Commit f45ee07

Browse files
committed
seth: allow id to be specified for 4byte decoding
1 parent c96ab9a commit f45ee07

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/seth/libexec/seth/seth

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ while [[ $1 ]]; do
181181
--async) export SETH_ASYNC=yes;;
182182
--follow) export SETH_FOLLOW=yes;;
183183
-j|--json) export SETH_JSON=yes;;
184+
--id) shift; export SETH_4BYTE_ID=$1;;
184185

185186
*) printf "${0##*/}: internal error: %q\\n" "$1"; exit 1
186187
esac; shift

src/seth/libexec/seth/seth-4byte-decode

+39-7
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,46 @@
88
set -e
99
[[ $# -eq 1 ]] || seth --fail-usage "$0"
1010

11-
sigs=$(curl -s "https://www.4byte.directory/api/v1/signatures/?hex_signature=${1:0:10}" | jq '.results[] | .text_signature' )
12-
PS3="Select a function signature by number: "
11+
if [[ -z $SETH_4BYTE_ID ]]; then
12+
# Function signature ID not specified, prompt user to choose a signature
13+
sigs=$(curl -s "https://www.4byte.directory/api/v1/signatures/?hex_signature=${1:0:10}" | jq '.results[] | .text_signature' )
14+
PS3="Select a function signature by number: "
15+
select sig in $sigs; do
16+
break
17+
done
18+
else
19+
# Function signature ID was specified, so parse results without user input
20+
results=$(curl -s "https://www.4byte.directory/api/v1/signatures/?hex_signature=${1:0:10}" | jq .results[] )
21+
sigs=$(echo $results | jq '.text_signature')
1322

14-
select sig in $sigs; do
15-
break
16-
done
23+
# sigs is a string so split it into an array
24+
sigarray=()
25+
for sig in $sigs; do sigarray+=($sig); done
26+
length=${#sigarray[@]}
1727

18-
sig="${sig//\"}" # remove leading and trailing quotes from JSON
19-
echo ""
28+
# parse the provided ID
29+
if [[ $SETH_4BYTE_ID = 'earliest' ]]; then
30+
# first one added to 4byte is the last in the array
31+
sig=${sigarray[$length-1]}
32+
elif [[ $SETH_4BYTE_ID = 'latest' ]]; then
33+
# last one added to 4byte is the first in the array
34+
sig=${sigarray[0]}
35+
else
36+
# specific ID number provided (if using this option, you may be better off with `seth --calldata-decode`)
37+
query=". | select(.id==$SETH_4BYTE_ID) | .text_signature"
38+
sig=$(echo $results | jq "$query")
39+
fi
40+
fi
41+
42+
# Exit if no sig found
43+
if [[ -z $sig ]]; then
44+
echo >&2 "seth 4byte-decode: no signature found"
45+
exit 1
46+
elif [[ $SETH_VERBOSE ]]; then
47+
echo "signature: $sig"
48+
fi
2049

50+
# Remove leading and trailing quotes from JSON, then decode
51+
echo ""
52+
sig="${sig//\"}"
2153
seth --calldata-decode $sig $1

0 commit comments

Comments
 (0)