Skip to content

Commit

Permalink
interop-node: replace validatehexstring() with hexutil from geth (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobhjkim authored Jan 23, 2024
1 parent d067ee4 commit 38edc82
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 67 deletions.
9 changes: 5 additions & 4 deletions tools/interop-node/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"fmt"
"time"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/tendermint/tendermint/libs/log"

"github.com/settlus/chain/tools/interop-node/client"
"github.com/settlus/chain/tools/interop-node/config"
"github.com/settlus/chain/tools/interop-node/feeder"
"github.com/settlus/chain/tools/interop-node/subscriber"
"github.com/settlus/chain/tools/interop-node/types"
"github.com/settlus/chain/x/interop"
)

Expand Down Expand Up @@ -156,20 +156,21 @@ func (s *Server) OwnerOf(ctx context.Context, req *interop.OwnerOfRequest) (*int
}, err
}

// validateOwnerOfRequest validates the owner of request
func validateOwnerOfRequest(req *interop.OwnerOfRequest) bool {
if req == nil || req.ChainId == "" || req.ContractAddr == "" || req.TokenIdHex == "" || req.BlockHash == "" {
return false
}

if !types.ValidateHexString(req.ContractAddr) {
if _, err := hexutil.Decode(req.ContractAddr); err != nil {
return false
}

if !types.ValidateHexString(req.BlockHash) {
if _, err := hexutil.Decode(req.BlockHash); err != nil {
return false
}

if !types.ValidateHexString(req.TokenIdHex) {
if _, err := hexutil.Decode(req.TokenIdHex); err != nil {
return false
}

Expand Down
17 changes: 0 additions & 17 deletions tools/interop-node/types/util.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package types

import (
"strings"

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

func PadBytes(pad int, b []byte) []byte {
if len(b) == pad {
return b
Expand All @@ -19,14 +13,3 @@ func PadBytes(pad int, b []byte) []byte {
copy(padded[pad-len(b):], b)
return padded
}

func ValidateHexString(s string) bool {
s = strings.TrimPrefix(s, "0x")

if len(s)%2 == 1 {
s = "0" + s
}

_, err := hexutil.Decode(s)
return err == nil
}
47 changes: 1 addition & 46 deletions tools/interop-node/types/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,53 +46,8 @@ func Test_PadBytes(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := types.PadBytes(tt.args.pad, tt.args.b); !bytes.Equal(got, tt.want) {
t.Errorf("PadBytes() = %v, want %v", got, tt.want)
t.Errorf("PadBytes() = %v, ok %v", got, tt.want)
}
})
}
}

func Test_ValidateHexString(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "valid even hex string",
args: args{
s: "0x01",
},
want: true,
},
{
name: "valid odd hex string",
args: args{
s: "0x0",
},
want: false,
},
{
name: "invalid hex string",
args: args{
s: "0x0g",
},
want: false,
},
{
name: "invalid hex string",
args: args{
s: "x1",
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
types.ValidateHexString(tt.args.s)
})
}
}

0 comments on commit 38edc82

Please sign in to comment.