Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.

Commit e88f18d

Browse files
committed
Add bytes methods
1 parent 94693bf commit e88f18d

File tree

14 files changed

+1861
-1440
lines changed

14 files changed

+1861
-1440
lines changed

address.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package solsha3
2+
3+
import (
4+
"encoding/hex"
5+
"reflect"
6+
7+
"github.com/ethereum/go-ethereum/common"
8+
)
9+
10+
// Address address
11+
func Address(input interface{}) []byte {
12+
switch v := input.(type) {
13+
case common.Address:
14+
return v.Bytes()[:]
15+
case string:
16+
v = removeHexPrefix(v)
17+
if v == "" || v == "0" {
18+
return []byte{0}
19+
}
20+
21+
v = evenLengthHex(v)
22+
decoded, err := hex.DecodeString(v)
23+
if err != nil {
24+
panic(err)
25+
}
26+
27+
return decoded
28+
case []byte:
29+
return v
30+
}
31+
32+
if isArray(input) {
33+
return AddressArray(input)
34+
}
35+
36+
return common.HexToAddress("").Bytes()[:]
37+
}
38+
39+
// AddressArray address
40+
func AddressArray(input interface{}) []byte {
41+
var values []byte
42+
s := reflect.ValueOf(input)
43+
for i := 0; i < s.Len(); i++ {
44+
val := s.Index(i).Interface()
45+
result := common.LeftPadBytes(Address(val), 32)
46+
values = append(values, result...)
47+
}
48+
return values
49+
}

address_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package solsha3
2+
3+
import (
4+
"encoding/hex"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/ethereum/go-ethereum/common"
9+
)
10+
11+
func TestAddress(t *testing.T) {
12+
t.Run("address", func(t *testing.T) {
13+
for i, tt := range []struct {
14+
in []byte
15+
out string
16+
}{
17+
{
18+
SoliditySHA3(
19+
[]string{"address"},
20+
"0x0",
21+
),
22+
"bc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a",
23+
},
24+
{
25+
SoliditySHA3(
26+
[]string{"address"},
27+
"0x0a",
28+
),
29+
30+
"0ef9d8f8804d174666011a394cab7901679a8944d24249fd148a6a36071151f8",
31+
},
32+
{
33+
SoliditySHA3(
34+
[]string{"address"},
35+
"0x12459c951127e0c374ff9105dda097662a027092",
36+
),
37+
"4b998b071d7bb74aee1ce2cdcc268cb0f6409b4a3387fc915617ec08415298ad",
38+
},
39+
{
40+
SoliditySHA3(
41+
[]string{"address"},
42+
common.HexToAddress("0x12459c951127e0c374ff9105dda097662a027092"),
43+
),
44+
"4b998b071d7bb74aee1ce2cdcc268cb0f6409b4a3387fc915617ec08415298ad",
45+
},
46+
{
47+
SoliditySHA3(
48+
[]string{"string[]"},
49+
[]string{"a", "b", "c"},
50+
),
51+
"4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45",
52+
},
53+
} {
54+
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
55+
got := hex.EncodeToString(tt.in)
56+
if got != tt.out {
57+
t.Errorf("want %v, got %v", tt.out, got)
58+
}
59+
})
60+
}
61+
})
62+
}

bool.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package solsha3
2+
3+
import (
4+
"reflect"
5+
6+
"github.com/ethereum/go-ethereum/common"
7+
)
8+
9+
// Bool bool
10+
func Bool(input interface{}) []byte {
11+
switch v := input.(type) {
12+
case bool:
13+
if v {
14+
return []byte{0x1}
15+
}
16+
return []byte{0x0}
17+
}
18+
19+
if isArray(input) {
20+
return BoolArray(input)
21+
}
22+
23+
return []byte{0x0}
24+
}
25+
26+
// BoolArray bool array
27+
func BoolArray(input interface{}) []byte {
28+
var values []byte
29+
s := reflect.ValueOf(input)
30+
for i := 0; i < s.Len(); i++ {
31+
val := s.Index(i).Interface()
32+
result := common.LeftPadBytes(Bool(val), 32)
33+
values = append(values, result...)
34+
}
35+
return values
36+
}

bool_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package solsha3
2+
3+
import (
4+
"encoding/hex"
5+
"fmt"
6+
"testing"
7+
)
8+
9+
func TestBool(t *testing.T) {
10+
t.Run("bool", func(t *testing.T) {
11+
for i, tt := range []struct {
12+
in []byte
13+
out string
14+
}{
15+
{
16+
SoliditySHA3(
17+
[]string{"bool"},
18+
true,
19+
),
20+
"5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2",
21+
},
22+
{
23+
SoliditySHA3(
24+
[]string{"bool"},
25+
false,
26+
),
27+
"bc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a",
28+
},
29+
{
30+
SoliditySHA3(
31+
[]string{"bool[]"},
32+
[3]bool{true, false, true},
33+
),
34+
"5c6090c0461491a2941743bda5c3658bf1ea53bbd3edcde54e16205e18b45792",
35+
},
36+
} {
37+
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
38+
got := hex.EncodeToString(tt.in)
39+
if got != tt.out {
40+
t.Errorf("want %v, got %v", tt.out, got)
41+
}
42+
})
43+
}
44+
})
45+
}

0 commit comments

Comments
 (0)