Skip to content

Commit

Permalink
Add marshall and rlp methods
Browse files Browse the repository at this point in the history
  • Loading branch information
antonydenyer committed Jan 18, 2024
1 parent 8a2a672 commit c095e8a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
34 changes: 31 additions & 3 deletions eth/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ func (d Data) String() string {
return string(d)
}

func (d Data8) String() string {
return string(d)
}
func (d Data4) String() string { return string(d) }

func (d Data8) String() string { return string(d) }

func (d Data20) String() string {
return string(d)
Expand All @@ -180,6 +180,13 @@ func (d Data) Bytes() []byte {
return b
}

func (d Data4) Bytes() []byte {
b, err := hex.DecodeString(d.String()[2:])
if err != nil {
panic(err)
}
return b
}
func (d Data8) Bytes() []byte {
b, err := hex.DecodeString(d.String()[2:])
if err != nil {
Expand Down Expand Up @@ -217,6 +224,11 @@ func (d Data) Hash() Hash {
return hash(d)
}

// Hash returns the keccak256 hash of the Data4.
func (d Data4) Hash() Hash {
return hash(d)
}

// Hash returns the keccak256 hash of the Data8.
func (d Data8) Hash() Hash {
return hash(d)
Expand Down Expand Up @@ -246,6 +258,15 @@ func (d *Data) UnmarshalJSON(data []byte) error {
return nil
}

func (d *Data4) UnmarshalJSON(data []byte) error {
str, err := unmarshalHex(data, 4, "data")
if err != nil {
return err
}
*d = Data4(str)
return nil
}

func (d *Data8) UnmarshalJSON(data []byte) error {
str, err := unmarshalHex(data, 8, "data")
if err != nil {
Expand Down Expand Up @@ -334,6 +355,13 @@ func (d *Data) RLP() rlp.Value {
}
}

// RLP returns the Data4 as an RLP-encoded string.
func (d *Data4) RLP() rlp.Value {
return rlp.Value{
String: d.String(),
}
}

// RLP returns the Data8 as an RLP-encoded string.
func (d *Data8) RLP() rlp.Value {
return rlp.Value{
Expand Down
15 changes: 15 additions & 0 deletions eth/input.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eth

import (
"encoding/json"
"github.com/INFURA/go-ethlibs/rlp"
"github.com/pkg/errors"
"strings"
Expand Down Expand Up @@ -49,3 +50,17 @@ func (i Input) FunctionSelector() *Data4 {

return nil
}

func (i *Input) UnmarshalJSON(data []byte) error {
str, err := unmarshalHex(data, -1, "data")
if err != nil {
return err
}
*i = Input(str)
return nil
}

func (i Input) MarshalJSON() ([]byte, error) {
s := string(i)
return json.Marshal(&s)
}

0 comments on commit c095e8a

Please sign in to comment.