-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow hex strings to be prefixed by 0x optionally in queries (#2028
) (#2029) * fix: allow hex strings to be prefixed by 0x optionally in queries * lint
- Loading branch information
1 parent
a3b4493
commit 84b9c54
Showing
10 changed files
with
85 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/hex" | ||
"strings" | ||
) | ||
|
||
// Decode a hex string. Hex string can be optionally prefixed with 0x. | ||
func HexDecode(input string) ([]byte, error) { | ||
return hex.DecodeString(strings.TrimPrefix(input, "0x")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHexDecode(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
want []byte | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "empty input", | ||
input: "", | ||
want: []byte{}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "valid input with 0x prefix", | ||
input: "0x68656c6c6f", | ||
want: []byte("hello"), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "valid input without 0x prefix", | ||
input: "68656c6c6f", | ||
want: []byte("hello"), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid input with odd number of characters", | ||
input: "68656c6c6", | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "invalid input with non-hex characters", | ||
input: "68656c6c6z", | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
got, err := HexDecode(test.input) | ||
if test.wantErr { | ||
assert.Error(t, err) | ||
return | ||
} | ||
|
||
assert.Equal(t, test.want, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters