Skip to content

Commit

Permalink
Merge pull request #6 from zeuslawyer/zp-patches
Browse files Browse the repository at this point in the history
Zp patches
  • Loading branch information
zeuslawyer authored Jun 21, 2024
2 parents 20aa3cb + fec0743 commit dd60aa9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
branches: [main]
# Run tests on pushes to the development branch
push:
branches: [develop]
branches: [main, develop]

jobs:
test:
Expand Down
12 changes: 4 additions & 8 deletions encdec/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/hex"
"fmt"
"math/big"
"strconv"
"strings"

"github.com/ethereum/go-ethereum/accounts/abi"
Expand All @@ -21,10 +20,10 @@ func DecodeHexToString(hex string) string {
}

/*
* Decodes `hex` to a Big Int64. `hex` must be prexifed with 0x.
* Decodes `hex` to a Big Int. `hex` must be prexifed with 0x.
*/
func DecodeHexToBigInt(hex string) *big.Int {
if hex == "0x" || len(hex) == 0 {
if hex == "0x" || hex == "" {
panic(fmt.Sprintf("%q provided as --hex input", hex))
}

Expand All @@ -33,11 +32,8 @@ func DecodeHexToBigInt(hex string) *big.Int {
}

hexWithoutPrefix := hex[2:]
num, err := strconv.ParseInt(hexWithoutPrefix, 16, 64)
if err != nil {
panic(err)
}
return new(big.Int).SetInt64(num)
bi, _ := new(big.Int).SetString(hexWithoutPrefix, 16)
return bi
}

/*
Expand Down
31 changes: 26 additions & 5 deletions encdec/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ package encdec
import (
"math/big"
"reflect"
"strconv"
"strings"
"testing"

"github.com/ethereum/go-ethereum/common"
)

func TestDecodeHexToBigInt(t *testing.T) {
wantedBigInt, ok := new(big.Int).SetString("139000000000000000000", 10)
if !ok {
t.Fatalf("Cannot set big.Int from string %q", "139000000000000000000")
}

tests := []struct {
name string
inputHex string
Expand Down Expand Up @@ -39,9 +43,14 @@ func TestDecodeHexToBigInt(t *testing.T) {
},
{
name: "NegativeNum",
inputHex: "0x" + strconv.FormatInt(-1981, 16), // "0x-7bd"
inputHex: "0x-7bd", // "0x" + strconv.FormatInt(-1981, 16), // "0x-7bd"
want: new(big.Int).SetInt64(-1981),
},
{
name: "BigInt",
inputHex: "0x078903338be34c0000",
want: wantedBigInt,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down Expand Up @@ -106,12 +115,24 @@ func TestDecodeHexToString(t *testing.T) {
}

func TestAbiDecode(t *testing.T) {
bigNegativeNum, ok := new(big.Int).SetString("-139000000000000000000", 10)
if !ok {
t.Fatalf("Cannot set big.Int from string %q", "-139000000000000000000")

}
tests := []struct {
name string
inputHex string
dataTypes string
want []any
}{
// see https://adibas03.github.io/online-ethereum-abi-encoder-decoder/#/decode to get decoded scalar values
{
name: "HappyPath#1-single negative int",
inputHex: "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff843",
dataTypes: "int",
want: []any{big.NewInt(-1981)},
},
{
name: "HappyPath#1-0x prefix",
inputHex: "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000a676f2d686578746f6f6c00000000000000000000000000000000000000000000",
Expand All @@ -120,9 +141,9 @@ func TestAbiDecode(t *testing.T) {
},
{
name: "HappyPath#2-multiple scalar types",
inputHex: "0x000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000166f4b60000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000d68617070792074657374696e6700000000000000000000000000000000000000",
dataTypes: "uint, uint256, string",
want: []any{big.NewInt(10), big.NewInt(23524534), "happy testing"},
inputHex: "0x000000000000000000000000000000000000000000000000000000000000000afffffffffffffffffffffffffffffffffffffffffffffff876fccc741cb400000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000d68617070792074657374696e6700000000000000000000000000000000000000",
dataTypes: "uint, int256, string",
want: []any{big.NewInt(10), bigNegativeNum, "happy testing"},
},
{
name: "HappyPath#3-Prefix Gets Added",
Expand Down
4 changes: 4 additions & 0 deletions encdec/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func dataTypesToAbiArgs(dataTypes string) abi.Arguments {
fmt.Printf("...type %q converted to uint256\n", _typeName)
_typeName = "uint256"
}
if _typeName == "int" {
fmt.Printf("...type %q converted to int256\n", _typeName)
_typeName = "int256"
}
abiType, err := abi.NewType(_typeName, "", nil)
if err != nil {
panic(fmt.Sprintf("Error creating Abi.Type for %s: %v", _typeName, err))
Expand Down

0 comments on commit dd60aa9

Please sign in to comment.