-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from INFURA/TXL-22-include-byte-signature-in-…
…data-payload Add input type to allow function selector decoding
- Loading branch information
Showing
8 changed files
with
126 additions
and
16 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
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,51 @@ | ||
package eth | ||
|
||
import ( | ||
"github.com/INFURA/go-ethlibs/rlp" | ||
"github.com/pkg/errors" | ||
"strings" | ||
) | ||
|
||
type Input Data | ||
|
||
func NewInput(value string) (*Input, error) { | ||
if !strings.HasPrefix(value, "0x") { | ||
return nil, errors.Errorf("invalid input: %s", value) | ||
} | ||
|
||
a := Input(value) | ||
return &a, nil | ||
} | ||
|
||
func MustInput(value string) *Input { | ||
a, err := NewInput(value) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return a | ||
} | ||
|
||
func (i Input) String() string { | ||
return string(i) | ||
} | ||
|
||
func (i Input) Bytes() []byte { | ||
return Data(i).Bytes() | ||
} | ||
|
||
// RLP returns the Input as an RLP-encoded string, note Input can never be null | ||
func (i Input) RLP() rlp.Value { | ||
return rlp.Value{ | ||
String: strings.ToLower(i.String()), | ||
} | ||
} | ||
|
||
func (i Input) FunctionSelector() *Data4 { | ||
if len(i) >= 10 { | ||
b := Data4(i[:10]) | ||
return &b | ||
} | ||
|
||
return nil | ||
} |
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,33 @@ | ||
package eth | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestInput_FunctionSelector(t *testing.T) { | ||
t.Run("empty input is nil", func(t *testing.T) { | ||
input, err := NewInput("0x") | ||
require.NoError(t, err) | ||
require.Nil(t, input.FunctionSelector()) | ||
}) | ||
|
||
t.Run("short input is nil", func(t *testing.T) { | ||
input, err := NewInput("0x1234") | ||
require.NoError(t, err) | ||
require.Nil(t, input.FunctionSelector()) | ||
}) | ||
|
||
t.Run("exact input returns selector", func(t *testing.T) { | ||
input, err := NewInput("0x2fbbe334") | ||
require.NoError(t, err) | ||
require.Equal(t, input.FunctionSelector(), MustData4("0x2fbbe334")) | ||
}) | ||
|
||
t.Run("input with arg returns selector", func(t *testing.T) { | ||
input, err := NewInput("0xa41368620000000000000000000000000000000000000000000000000000000000000020") | ||
require.NoError(t, err) | ||
require.Equal(t, input.FunctionSelector(), MustData4("0xa4136862")) | ||
}) | ||
|
||
} |
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