-
Notifications
You must be signed in to change notification settings - Fork 46
/
contracts_suave.go
342 lines (280 loc) · 9.24 KB
/
contracts_suave.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package vm
import (
"bytes"
"context"
"crypto/rand"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/suave/consolelog"
suave "github.com/ethereum/go-ethereum/suave/core"
"github.com/flashbots/go-boost-utils/bls"
)
var (
confStorePrecompileStoreMeter = metrics.NewRegisteredMeter("suave/confstore/store", nil)
confStorePrecompileRetrieveMeter = metrics.NewRegisteredMeter("suave/confstore/retrieve", nil)
)
var (
isConfidentialAddress = common.HexToAddress("0x42010000")
)
/* General utility precompiles */
func (b *suaveRuntime) confidentialInputs() ([]byte, error) {
return b.contextGet("confidentialInputs")
}
func (b *suaveRuntime) randomBytes(numBytes uint8) ([]byte, error) {
buf := make([]byte, numBytes)
_, err := rand.Read(buf)
if err != nil {
return nil, err
}
return buf, nil
}
/* Confidential store precompiles */
func (b *suaveRuntime) confidentialStore(dataId types.DataId, key string, data []byte) error {
if b.suaveContext.Backend.ConfidentialStore == nil {
return fmt.Errorf("confidential store is not enabled")
}
record, err := b.suaveContext.Backend.ConfidentialStore.FetchRecordByID(dataId)
if err != nil {
return suave.ErrRecordNotFound
}
log.Debug("confStore", "dataId", dataId, "key", key)
caller, err := checkIsPrecompileCallAllowed(b.suaveContext, confidentialStoreAddr, record)
if err != nil {
return err
}
if metrics.Enabled {
confStorePrecompileStoreMeter.Mark(int64(len(data)))
}
_, err = b.suaveContext.Backend.ConfidentialStore.Store(dataId, caller, key, data)
if err != nil {
return err
}
return nil
}
func (b *suaveRuntime) confidentialRetrieve(dataId types.DataId, key string) ([]byte, error) {
if b.suaveContext.Backend.ConfidentialStore == nil {
return nil, fmt.Errorf("confidential store is not enabled")
}
record, err := b.suaveContext.Backend.ConfidentialStore.FetchRecordByID(dataId)
if err != nil {
return nil, suave.ErrRecordNotFound
}
caller, err := checkIsPrecompileCallAllowed(b.suaveContext, confidentialRetrieveAddr, record)
if err != nil {
return nil, err
}
data, err := b.suaveContext.Backend.ConfidentialStore.Retrieve(dataId, caller, key)
if err != nil {
return []byte(err.Error()), err
}
if metrics.Enabled {
confStorePrecompileRetrieveMeter.Mark(int64(len(data)))
}
return data, nil
}
/* Data Record precompiles */
func (b *suaveRuntime) newDataRecord(decryptionCondition uint64, allowedPeekers []common.Address, allowedStores []common.Address, RecordType string) (types.DataRecord, error) {
if b.suaveContext.Backend.ConfidentialStore == nil {
return types.DataRecord{}, fmt.Errorf("confidential store is not enabled")
}
record, err := b.suaveContext.Backend.ConfidentialStore.InitRecord(types.DataRecord{
Salt: suave.RandomDataRecordId(),
DecryptionCondition: decryptionCondition,
AllowedPeekers: allowedPeekers,
AllowedStores: allowedStores,
Version: RecordType, // TODO : make generic
})
if err != nil {
return types.DataRecord{}, err
}
return record, nil
}
func (b *suaveRuntime) fetchDataRecords(targetBlock uint64, namespace string) ([]types.DataRecord, error) {
if b.suaveContext.Backend.ConfidentialStore == nil {
return nil, fmt.Errorf("confidential store is not enabled")
}
records1 := b.suaveContext.Backend.ConfidentialStore.FetchRecordsByProtocolAndBlock(targetBlock, namespace)
records := make([]types.DataRecord, 0, len(records1))
for _, record := range records1 {
records = append(records, record.ToInnerRecord())
}
return records, nil
}
func (s *suaveRuntime) signMessage(digest []byte, cryptoType types.CryptoSignature, signingKey string) ([]byte, error) {
if !strings.HasPrefix(signingKey, "0x") {
// we need to prefix with 0x if not present because the 'hexutil.Decode' fails to decode if there is no '0x' prefix
signingKey = "0x" + signingKey
}
signingKeyBuf, err := hexutil.Decode(signingKey)
if err != nil {
return nil, fmt.Errorf("key not formatted properly: %w", err)
}
if cryptoType == types.CryptoSignature_SECP256 {
key, err := crypto.ToECDSA(signingKeyBuf)
if err != nil {
return nil, fmt.Errorf("key not formatted properly: %w", err)
}
signature, err := crypto.Sign(digest, key)
if err != nil {
return nil, fmt.Errorf("failed to sign message: %v", err)
}
return signature, nil
} else if cryptoType == types.CryptoSignature_BLS {
suaveEthBlockSigningKey, err := bls.SecretKeyFromBytes(signingKeyBuf)
if err != nil {
fmt.Println("_B!", err)
return nil, fmt.Errorf("failed to sign message: %v", err)
}
signature := bls.Sign(suaveEthBlockSigningKey, digest).Bytes()
return signature[:], nil
}
return nil, fmt.Errorf("unsupported crypto type")
}
func mustParseAbi(data string) abi.ABI {
inoutAbi, err := abi.JSON(strings.NewReader(data))
if err != nil {
panic(err.Error())
}
return inoutAbi
}
func mustParseMethodAbi(data string, method string) abi.Method {
inoutAbi := mustParseAbi(data)
return inoutAbi.Methods[method]
}
type suaveRuntime struct {
suaveContext *SuaveContext
}
var _ SuaveRuntime = &suaveRuntime{}
type consoleLogPrecompile struct {
}
func (c *consoleLogPrecompile) RequiredGas(input []byte) uint64 {
return 0
}
func (c *consoleLogPrecompile) Run(input []byte) ([]byte, error) {
if err := consolelog.Print(input); err != nil {
log.Error("failed to console2 print", "err", err)
}
return nil, nil
}
var contextCookieKeyPrefix = "__cookie_"
func (s *suaveRuntime) doHTTPRequest(request types.HttpRequest) ([]byte, error) {
if request.Method != "GET" && request.Method != "POST" {
return nil, fmt.Errorf("only GET and POST methods are supported")
}
if request.Url == "" {
return nil, fmt.Errorf("url is empty")
}
var body io.Reader
if request.Body != nil {
body = bytes.NewReader(request.Body)
}
url, err := s.resolveURL(request.Url)
if err != nil {
return nil, err
}
req, err := http.NewRequest(request.Method, url, body)
if err != nil {
return nil, err
}
// add any cookies stored in the context
for key, val := range s.suaveContext.Context {
if strings.HasPrefix(key, contextCookieKeyPrefix) {
req.Header.Add("Cookie", string(val))
}
}
for _, header := range request.Headers {
indx := strings.Index(header, ":")
if indx == -1 {
return nil, fmt.Errorf("incorrect header format '%s', no ':' present", header)
}
req.Header.Add(header[:indx], header[indx+1:])
}
if request.WithFlashbotsSignature {
// hash the body and sign it with the kettle signing key
hashedBody := crypto.Keccak256Hash(request.Body).Hex()
sig, err := crypto.Sign(accounts.TextHash([]byte(hashedBody)), s.suaveContext.Backend.EthBundleSigningKey)
if err != nil {
return nil, err
}
signature := crypto.PubkeyToAddress(s.suaveContext.Backend.EthBundleSigningKey.PublicKey).Hex() + ":" + hexutil.Encode(sig)
req.Header.Add("X-Flashbots-Signature", signature)
}
client := &http.Client{
Timeout: 5 * time.Second, // TODO: test
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode > 299 {
return nil, fmt.Errorf("http error: %s: %v", resp.Status, data)
}
// parse the LB cookies (AWSALB, AWSALBCORS) and set them in the context
for _, cookie := range resp.Cookies() {
if cookie.Name == "AWSALB" || cookie.Name == "AWSALBCORS" {
s.suaveContext.Context[contextCookieKeyPrefix+cookie.Name] = []byte(cookie.String())
}
}
return data, nil
}
func (s *suaveRuntime) resolveURL(urlOrServiceName string) (string, error) {
var allowed bool
// resolve dns if possible
if endpoint, ok := s.suaveContext.Backend.ServiceAliasRegistry[urlOrServiceName]; ok {
return endpoint, nil
}
// decode the url and check if the domain is allowed
parsedURL, err := url.Parse(urlOrServiceName)
if err != nil {
return "", err
}
// check if the domain is allowed
for _, allowedDomain := range s.suaveContext.Backend.ExternalWhitelist {
if allowedDomain == "*" || allowedDomain == parsedURL.Hostname() {
allowed = true
break
}
}
if !allowed {
return "", fmt.Errorf("domain %s is not allowed", parsedURL.Hostname())
}
return urlOrServiceName, nil
}
func (s *suaveRuntime) newBuilder() (string, error) {
return s.suaveContext.Backend.ConfidentialEthBackend.NewSession(context.Background())
}
func (s *suaveRuntime) simulateTransaction(session string, txnBytes []byte) (types.SimulateTransactionResult, error) {
txn := new(types.Transaction)
if err := txn.UnmarshalBinary(txnBytes); err != nil {
return types.SimulateTransactionResult{}, err
}
result, err := s.suaveContext.Backend.ConfidentialEthBackend.AddTransaction(context.Background(), session, txn)
if err != nil {
return types.SimulateTransactionResult{}, err
}
return *result, nil
}
func (s *suaveRuntime) contextGet(key string) ([]byte, error) {
val, ok := s.suaveContext.Context[key]
if !ok {
return nil, fmt.Errorf("value not found")
}
return val, nil
}