-
Notifications
You must be signed in to change notification settings - Fork 0
/
forwarder.go
170 lines (138 loc) · 4.56 KB
/
forwarder.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package rockside
import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
gethSigner "github.com/ethereum/go-ethereum/signer/core"
)
type Forwarder endpoint
type RelayExecuteTxMessage struct {
Signer string `json:"signer"`
To string `json:"to"`
Data string `json:"data"`
Nonce string `json:"nonce"`
}
type RelayExecuteTxRequest struct {
Speed string `json:"speed"`
GasPriceLimit string `json:"gas_price_limit"`
Message RelayExecuteTxMessage `json:"message"`
Signature string `json:"signature"`
Gas string `json:"gas"`
}
type RelayTxResponse struct {
TransactionHash string `json:"transaction_hash"`
TrackingID string `json:"tracking_id"`
}
func (e *Forwarder) Create(owner string) (ContractCreationResponse, error) {
req := struct {
Owner string `json:"owner"`
}{owner}
var result ContractCreationResponse
path := fmt.Sprintf("ethereum/%s/forwarders", e.client.network)
if _, err := e.client.post(path, req, &result); err != nil {
return result, err
}
return result, nil
}
func (e *Forwarder) Get() ([]string, error) {
var result []string
path := fmt.Sprintf("ethereum/%s/forwarders", e.client.network)
if _, err := e.client.get(path, nil, &result); err != nil {
return result, err
}
return result, nil
}
func (e *Forwarder) GetRelayParams(forwarderAddress string, account string, channels ...string) (paramsResponse, error) {
channel := "0"
if len(channels) > 0 {
channel = channels[0]
}
var result paramsResponse
path := fmt.Sprintf("ethereum/%s/forwarders/%s/relayParams", e.client.network, forwarderAddress)
req := struct {
Account string `json:"account"`
ChannelID string `json:"channel_id"`
}{Account: account, ChannelID: channel}
_, err := e.client.post(path, req, &result)
if err != nil {
return result, err
}
return result, nil
}
func (e *Forwarder) Relay(forwarderAddress string, request RelayExecuteTxRequest) (RelayTxResponse, error) {
var result RelayTxResponse
if request.Speed == "" {
request.Speed = "standard"
}
path := fmt.Sprintf("ethereum/%s/forwarders/%s", e.client.network, forwarderAddress)
if _, err := e.client.post(path, request, &result); err != nil {
return result, err
}
return result, nil
}
func (e *Forwarder) SignTxParams(privateKeyStr, forwarder, signer, destination, data, nonce string) (string, error) {
privateKey, err := crypto.HexToECDSA(privateKeyStr)
if err != nil {
return "", err
}
if nonce == "" {
paramsResponse, err := e.GetRelayParams(forwarder, signer)
if err != nil {
return "", err
}
nonce = paramsResponse.Nonce
}
nonceBig, isValidNonce := new(big.Int).SetString(nonce, 10)
if !isValidNonce {
return "", fmt.Errorf("nonce is not a valid number [%s]", nonce)
}
network := e.client.CurrentNetwork()
argsHash, err := GetHash(common.HexToAddress(signer), common.HexToAddress(destination), common.FromHex(data), nonceBig, common.HexToAddress(forwarder), network.ChainID())
if err != nil {
return "", err
}
signedHash, err := crypto.Sign(argsHash, privateKey)
if err != nil {
return "", err
}
return hexutil.Encode(signedHash), nil
}
func GetHash(signer, destination common.Address, data []byte, nonce *big.Int, forwarder common.Address, chainID *big.Int) ([]byte, error) {
EIP712DomainType := []gethSigner.Type{
{Name: "verifyingContract", Type: "address"},
{Name: "chainId", Type: "uint256"},
}
txMessageType := []gethSigner.Type{
{Name: "signer", Type: "address"},
{Name: "to", Type: "address"},
{Name: "data", Type: "bytes"},
{Name: "nonce", Type: "uint256"},
}
types := gethSigner.Types{
"TxMessage": txMessageType,
"EIP712Domain": EIP712DomainType,
}
domainData := gethSigner.TypedDataDomain{
VerifyingContract: forwarder.String(),
ChainId: math.NewHexOrDecimal256(chainID.Int64()),
}
messageData := gethSigner.TypedDataMessage{
"signer": signer.String(),
"to": destination.String(),
"data": data,
"nonce": nonce.String(),
}
signerData := gethSigner.TypedData{
Types: types,
PrimaryType: "TxMessage",
Domain: domainData,
Message: messageData,
}
typedDataHash, _ := signerData.HashStruct(signerData.PrimaryType, signerData.Message)
domainSeparator, _ := signerData.HashStruct("EIP712Domain", signerData.Domain.Map())
rawData := []byte(fmt.Sprintf("\x19\x01%s%s", string(domainSeparator), string(typedDataHash)))
return crypto.Keccak256(rawData), nil
}