|
8 | 8 | set -e
|
9 | 9 | [[ $# -eq 1 ]] || seth --fail-usage "$0"
|
10 | 10 |
|
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') |
13 | 22 |
|
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[@]} |
17 | 27 |
|
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 |
20 | 49 |
|
| 50 | +# Remove leading and trailing quotes from JSON, then decode |
| 51 | +echo "" |
| 52 | +sig="${sig//\"}" |
21 | 53 | seth --calldata-decode $sig $1
|
0 commit comments