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

Commit b8eb399

Browse files
committed
types array argument option
1 parent 9bd89c3 commit b8eb399

File tree

11 files changed

+838
-45
lines changed

11 files changed

+838
-45
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@
1111
*.out
1212

1313
.DS_Store
14+
15+
dist
16+
bin
17+
build

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ all:
33

44
.PHONY: test
55
test:
6-
@go test -v `go list ./... | grep -v example` && echo "ALL PASS" || echo "FAILURE"
6+
@go test -v `go list ./... | grep -v example` $(ARGS) && echo "ALL PASS" || echo "FAILURE"
77

88
.PHONY: deps
99
deps:

README.md

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
[![License](http://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/miguelmota/go-solidity-sha3/master/LICENSE.md) [![Build Status](https://travis-ci.org/miguelmota/go-solidity-sha3.svg?branch=master)](https://travis-ci.org/miguelmota/go-solidity-sha3) [![Go Report Card](https://goreportcard.com/badge/github.com/miguelmota/go-solidity-sha3?)](https://goreportcard.com/report/github.com/miguelmota/go-solidity-sha3) [![GoDoc](https://godoc.org/github.com/miguelmota/go-solidity-sha3?status.svg)](https://godoc.org/github.com/miguelmota/go-solidity-sha3)
1414

15-
This package is the Go equivalent of `require('ethereumjs-abi').soliditySHA3` [NPM module](https://www.npmjs.com/package/ethereumjs-abi).
15+
This package is the Go equivalent of `require('ethers).utils.solidityKeccak256` [NPM module](https://www.npmjs.com/package/ethers).
1616

1717
## Install
1818

@@ -24,37 +24,78 @@ go get -u github.com/miguelmota/go-solidity-sha3
2424

2525
[Documentation on GoDoc](https://godoc.org/github.com/miguelmota/go-solidity-sha3)
2626

27-
## Usage
27+
## Getting started
2828

29-
Simple example
29+
Simple example:
3030

3131
```go
3232
package main
3333

3434
import (
35-
"encoding/hex"
36-
"fmt"
37-
"github.com/miguelmota/go-solidity-sha3"
38-
"math/big"
35+
"encoding/hex"
36+
"fmt"
37+
38+
"github.com/miguelmota/go-solidity-sha3"
39+
)
40+
41+
func main() {
42+
hash := solsha3.SoliditySHA3(
43+
// types
44+
[]string{"address", "uint256"},
45+
46+
// values
47+
"0x935F7770265D0797B621c49A5215849c333Cc3ce",
48+
"100000000000000000",
49+
)
50+
51+
fmt.Println(hex.EncodeToString(hash))
52+
}
53+
```
54+
55+
Output:
56+
57+
```bash
58+
0a3844b522d9e3a837ae56d4c57d668feb26325834bf4ba49e153d84ed7ad53d
59+
```
60+
61+
More complex example:
62+
63+
```go
64+
package main
65+
66+
import (
67+
"encoding/hex"
68+
"fmt"
69+
70+
"github.com/miguelmota/go-solidity-sha3"
3971
)
4072

4173
func main() {
42-
hash := solsha3.SoliditySHA3(
43-
solsha3.Address("0x12459c951127e0c374ff9105dda097662a027093"),
44-
solsha3.Uint256(big.NewInt(100)),
45-
solsha3.String("foo"),
46-
solsha3.Bytes32("bar"),
47-
solsha3.Bool(true),
48-
)
49-
50-
fmt.Println(hex.EncodeToString(hash))
74+
hash := solsha3.SoliditySHA3(
75+
// types
76+
[]string{"address", "bytes1", "uint8[]", "bytes32", "uint256", "address[]", "uint32"},
77+
78+
// values
79+
"0x935F7770265D0797B621c49A5215849c333Cc3ce",
80+
"0xa",
81+
[]uint8{128, 255},
82+
"0x4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45",
83+
"100000000000000000",
84+
[]string{
85+
"0x13D94859b23AF5F610aEfC2Ae5254D4D7E3F191a",
86+
"0x473029549e9d898142a169d7234c59068EDcBB33",
87+
},
88+
123456789,
89+
)
90+
91+
fmt.Println(hex.EncodeToString(hash))
5192
}
5293
```
5394

54-
Output
95+
Output:
5596

5697
```bash
57-
417a4c44724701ba79bb363151dff48909bc058a2c75a81e9cf5208ae4699369
98+
ad390a98c1c32cdb1f046f6887a4109f12290b690127e6e15da4ca210235510e
5899
```
59100

60101
## License

example/js/test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const ethers = require('ethers')
2+
3+
//console.log(ethers.utils.solidityKeccak256([ 'uint8[]' ], [ [1,2,3] ] ))
4+
//console.log(ethers.utils.solidityKeccak256([ 'bool[]' ], [ [true, false, true] ] ))
5+
//console.log(ethers.utils.solidityKeccak256([ 'int8[]' ], [ [1,2,3] ] ))
6+
7+
//console.log(ethers.utils.solidityKeccak256([ 'string[]' ], [ ['a', 'b', 'c'] ] ))
8+
9+
//console.log(ethers.utils.solidityKeccak256([ 'address[]' ], [ ['0xa', '0xb', '0xc'] ] ))
10+
11+
//console.log(ethers.utils.solidityKeccak256([ 'address' ], [ '0xa'] ))
12+
//console.log(ethers.utils.solidityKeccak256([ 'address' ], [ '0x0'] ))
13+
14+
15+
//console.log(ethers.utils.solidityKeccak256([ 'bytes32' ], [ '0x4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45' ] ))
16+
//console.log(ethers.utils.solidityKeccak256([ 'bytes1' ], [ '0x4' ] ))
17+
//console.log(ethers.utils.solidityKeccak256(['bytes32'],['0x0a00000000000000000000000000000000000000000000000000000000000000']))
18+
19+
//var msgDigest = ethers.utils.keccak256(Buffer.from('abc'));
20+
21+
//console.log(msgDigest.toString())
22+
23+
//console.log(ethers.utils.solidityKeccak256(['address', 'uint256'],['0x935F7770265D0797B621c49A5215849c333Cc3ce', '100000000000000000']))
24+
25+
/*
26+
console.log(ethers.utils.solidityKeccak256([
27+
'address', 'bytes1', 'uint8[]', 'bytes32', 'uint256', 'address[]', 'uint32' ],
28+
[
29+
'0x935F7770265D0797B621c49A5215849c333Cc3ce',
30+
'0xa',
31+
[128, 255],
32+
'0x4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45',
33+
'100000000000000000',
34+
[
35+
'0x13D94859b23AF5F610aEfC2Ae5254D4D7E3F191a',
36+
'0x473029549e9d898142a169d7234c59068EDcBB33'
37+
],
38+
123456789
39+
]
40+
))
41+
// 0xad390a98c1c32cdb1f046f6887a4109f12290b690127e6e15da4ca210235510e
42+
*/

example/legacy_usage_example.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"encoding/hex"
5+
"fmt"
6+
"math/big"
7+
8+
"github.com/miguelmota/go-solidity-sha3"
9+
)
10+
11+
func main() {
12+
hash := solsha3.SoliditySHA3(
13+
solsha3.Address("0x12459c951127e0c374ff9105dda097662a027093"),
14+
solsha3.Uint256(big.NewInt(100)),
15+
solsha3.String("foo"),
16+
solsha3.Bytes32("bar"),
17+
solsha3.Bool(true),
18+
)
19+
20+
fmt.Println(hex.EncodeToString(hash)) // 417a4c44724701ba79bb363151dff48909bc058a2c75a81e9cf5208ae4699369
21+
22+
hash2 := solsha3.SoliditySHA3WithPrefix(
23+
solsha3.String("hello"),
24+
)
25+
26+
fmt.Println(hex.EncodeToString(hash2)) // 50b2c43fd39106bafbba0da34fc430e1f91e3c96ea2acee2bc34119f92b37750
27+
}

example/simple_example.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"encoding/hex"
5+
"fmt"
6+
7+
"github.com/miguelmota/go-solidity-sha3"
8+
)
9+
10+
func main() {
11+
hash := solsha3.SoliditySHA3(
12+
// types
13+
[]string{"address", "uint256"},
14+
15+
// values
16+
"0x935F7770265D0797B621c49A5215849c333Cc3ce",
17+
"100000000000000000",
18+
)
19+
20+
fmt.Println(hex.EncodeToString(hash)) // 0a3844b522d9e3a837ae56d4c57d668feb26325834bf4ba49e153d84ed7ad53d
21+
}

example/usage_example.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@ package main
33
import (
44
"encoding/hex"
55
"fmt"
6-
"math/big"
76

87
"github.com/miguelmota/go-solidity-sha3"
98
)
109

1110
func main() {
1211
hash := solsha3.SoliditySHA3(
13-
solsha3.Address("0x12459c951127e0c374ff9105dda097662a027093"),
14-
solsha3.Uint256(big.NewInt(100)),
15-
solsha3.String("foo"),
16-
solsha3.Bytes32("bar"),
17-
solsha3.Bool(true),
18-
)
19-
20-
fmt.Println(hex.EncodeToString(hash)) // 417a4c44724701ba79bb363151dff48909bc058a2c75a81e9cf5208ae4699369
12+
// types
13+
[]string{"address", "bytes1", "uint8[]", "bytes32", "uint256", "address[]", "uint32"},
2114

22-
hash2 := solsha3.SoliditySHA3WithPrefix(
23-
solsha3.String("hello"),
15+
// values
16+
"0x935F7770265D0797B621c49A5215849c333Cc3ce",
17+
"0xa",
18+
[]uint8{128, 255},
19+
"0x4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45",
20+
"100000000000000000",
21+
[]string{
22+
"0x13D94859b23AF5F610aEfC2Ae5254D4D7E3F191a",
23+
"0x473029549e9d898142a169d7234c59068EDcBB33",
24+
},
25+
123456789,
2426
)
2527

26-
fmt.Println(hex.EncodeToString(hash2)) // 50b2c43fd39106bafbba0da34fc430e1f91e3c96ea2acee2bc34119f92b37750
28+
fmt.Println(hex.EncodeToString(hash)) // ad390a98c1c32cdb1f046f6887a4109f12290b690127e6e15da4ca210235510e
2729
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module github.com/miguelmota/go-solidity-sha3
22

33
require (
44
github.com/btcsuite/btcd v0.0.0-20190109040709-5bda5314ca95 // indirect
5+
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495
56
github.com/ethereum/go-ethereum v1.8.20
67
golang.org/x/crypto v0.0.0-20190103213133-ff983b9c42bc
78
)

0 commit comments

Comments
 (0)