Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hex package #291

Merged
merged 6 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions pkg/utils/hex/hex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package hex

import (
"encoding/hex"
"errors"
"fmt"
"math/big"
"strings"
)

// EnsurePrefix adds the prefix (0x) to a given hex string.
func EnsurePrefix(str string) string {
if !strings.HasPrefix(str, "0x") {
str = "0x" + str
}
return str
}

// ToBig parses the given hex string or panics if it is invalid.
func ToBig(s string) *big.Int {
n, ok := new(big.Int).SetString(s, 16)
if !ok {
panic(fmt.Errorf(`failed to convert "%s" as hex to big.Int`, s))
jmank88 marked this conversation as resolved.
Show resolved Hide resolved
}
return n
}

// RemovePrefix removes the prefix (0x) of a given hex string.
func RemovePrefix(str string) string {
jmank88 marked this conversation as resolved.
Show resolved Hide resolved
if HasPrefix(str) {
return str[2:]
}
return str
}

// HasPrefix returns true if the string starts with 0x.
func HasPrefix(str string) bool {
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
}

// TryParse parses the given hex string to bytes,
// it can return error if the hex string is invalid.
// Follows the semantic of ethereum's FromHex.
func TryParse(s string) (b []byte, err error) {
jmank88 marked this conversation as resolved.
Show resolved Hide resolved
if !HasPrefix(s) {
err = errors.New("hex string must have 0x prefix")
} else {
s = s[2:]
if len(s)%2 == 1 {
s = "0" + s
}
b, err = hex.DecodeString(s)
}
return
}
68 changes: 68 additions & 0 deletions pkg/utils/hex/hex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package hex_test

import (
"testing"

"github.com/smartcontractkit/chainlink-common/pkg/utils/hex"
"github.com/stretchr/testify/assert"
)

func TestHasPrefix(t *testing.T) {
t.Parallel()

t.Run("has prefix", func(t *testing.T) {
t.Parallel()

r := hex.HasPrefix("0xabc")
assert.True(t, r)
})

t.Run("doesn't have prefix", func(t *testing.T) {
t.Parallel()

r := hex.HasPrefix("abc")
assert.False(t, r)
})

t.Run("has 0x suffix", func(t *testing.T) {
t.Parallel()

r := hex.HasPrefix("abc0x")
assert.False(t, r)
})

}

func TestTryParse(t *testing.T) {
t.Parallel()

t.Run("0x prefix missing", func(t *testing.T) {
t.Parallel()

_, err := hex.TryParse("abcd")
assert.Error(t, err)
})

t.Run("wrong hex characters", func(t *testing.T) {
t.Parallel()

_, err := hex.TryParse("0xabcdzzz")
assert.Error(t, err)
})

t.Run("valid hex string", func(t *testing.T) {
t.Parallel()

b, err := hex.TryParse("0x1234")
assert.NoError(t, err)
assert.Equal(t, []byte{0x12, 0x34}, b)
})

t.Run("prepend odd length with zero", func(t *testing.T) {
t.Parallel()

b, err := hex.TryParse("0x123")
assert.NoError(t, err)
assert.Equal(t, []byte{0x1, 0x23}, b)
})
}
9 changes: 0 additions & 9 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"math"
mrand "math/rand"
"strings"
"time"

"github.com/smartcontractkit/chainlink-common/pkg/services"
Expand Down Expand Up @@ -50,11 +49,3 @@ func IsZero[C comparable](val C) bool {
var zero C
return zero == val
}

// EnsureHexPrefix adds the prefix (0x) to a given hex string.
func EnsureHexPrefix(str string) string {
if !strings.HasPrefix(str, "0x") {
str = "0x" + str
}
return str
}
Loading