forked from dcb9/janus
-
Notifications
You must be signed in to change notification settings - Fork 28
/
web3_sha3.go
37 lines (30 loc) · 947 Bytes
/
web3_sha3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package transformer
import (
"encoding/json"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/labstack/echo"
"github.com/qtumproject/janus/pkg/eth"
)
type Web3Sha3 struct{}
func (p *Web3Sha3) Method() string {
return "web3_sha3"
}
func (p *Web3Sha3) Request(rawreq *eth.JSONRPCRequest, c echo.Context) (interface{}, eth.JSONRPCError) {
var err error
var req eth.Web3Sha3Request
if err = json.Unmarshal(rawreq.Params, &req); err != nil {
// TODO: Correct error code?
return nil, eth.NewInvalidParamsError(err.Error())
}
message := req.Message
var decoded []byte
// zero length should return "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
if len(message) != 0 {
decoded, err = hexutil.Decode(string(message))
if err != nil {
return nil, eth.NewCallbackError("Failed to decode")
}
}
return hexutil.Encode(crypto.Keccak256(decoded)), nil
}