forked from NethermindEth/starknet.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
554 lines (526 loc) · 15 KB
/
account.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package caigo
import (
"context"
"errors"
"fmt"
"math/big"
"github.com/dontpanicdao/caigo/gateway"
"github.com/dontpanicdao/caigo/rpcv01"
"github.com/dontpanicdao/caigo/rpcv02"
"github.com/dontpanicdao/caigo/types"
)
var (
ErrUnsupportedAccount = errors.New("unsupported account implementation")
MAX_FEE, _ = big.NewInt(0).SetString("0x20000000000", 0)
)
const (
TRANSACTION_PREFIX = "invoke"
EXECUTE_SELECTOR = "__execute__"
CONTRACT_ADDRESS_PREFIX = "STARKNET_CONTRACT_ADDRESS"
)
type account interface {
Sign(msgHash *big.Int) (*big.Int, *big.Int, error)
TransactionHash(calls []types.FunctionCall, details types.ExecuteDetails) (*big.Int, error)
Call(ctx context.Context, call types.FunctionCall) ([]string, error)
Nonce(ctx context.Context) (*big.Int, error)
EstimateFee(ctx context.Context, calls []types.FunctionCall, details types.ExecuteDetails) (*types.FeeEstimate, error)
Execute(ctx context.Context, calls []types.FunctionCall, details types.ExecuteDetails) (*types.AddInvokeTransactionOutput, error)
}
var _ account = &Account{}
type AccountPlugin interface {
PluginCall(calls []types.FunctionCall) (types.FunctionCall, error)
}
type ProviderType string
const (
ProviderRPCv01 ProviderType = "rpcv01"
ProviderRPCv02 ProviderType = "rpcv02"
ProviderGateway ProviderType = "gateway"
)
type Account struct {
rpcv01 *rpcv01.Provider
rpcv02 *rpcv02.Provider
sequencer *gateway.GatewayProvider
provider ProviderType
chainId string
AccountAddress string
private *big.Int
version uint64
plugin AccountPlugin
}
type AccountOption struct {
AccountPlugin AccountPlugin
version uint64
}
type AccountOptionFunc func(string, string) (AccountOption, error)
func AccountVersion0(string, string) (AccountOption, error) {
return AccountOption{
version: uint64(0),
}, nil
}
func AccountVersion1(string, string) (AccountOption, error) {
return AccountOption{
version: uint64(1),
}, nil
}
func newAccount(private, address string, options ...AccountOptionFunc) (*Account, error) {
var accountPlugin AccountPlugin
version := uint64(0)
for _, o := range options {
opt, err := o(private, address)
if err != nil {
return nil, err
}
if opt.version != 0 {
version = opt.version
}
if opt.AccountPlugin != nil {
if accountPlugin != nil {
return nil, errors.New("multiple plugins not supported")
}
accountPlugin = opt.AccountPlugin
}
}
priv := types.SNValToBN(private)
return &Account{
AccountAddress: address,
private: priv,
version: version,
plugin: accountPlugin,
}, nil
}
func setAccountProvider(account *Account, provider interface{}) error {
switch p := provider.(type) {
case *rpcv01.Provider:
chainID, err := p.ChainID(context.Background())
if err != nil {
return err
}
account.chainId = chainID
account.provider = ProviderRPCv01
account.rpcv01 = p
return nil
case *rpcv02.Provider:
chainID, err := p.ChainID(context.Background())
if err != nil {
return err
}
account.chainId = chainID
account.provider = ProviderRPCv02
account.rpcv02 = p
return nil
}
return errors.New("unsupported provider")
}
func NewRPCAccount[Provider *rpcv01.Provider | *rpcv02.Provider](private, address string, provider Provider, options ...AccountOptionFunc) (*Account, error) {
account, err := newAccount(private, address, options...)
if err != nil {
return nil, err
}
err = setAccountProvider(account, provider)
return account, err
}
func NewGatewayAccount(private, address string, provider *gateway.GatewayProvider, options ...AccountOptionFunc) (*Account, error) {
account, err := newAccount(private, address, options...)
if err != nil {
return nil, err
}
chainID, err := provider.ChainID(context.Background())
if err != nil {
return nil, err
}
account.chainId = chainID
account.provider = ProviderGateway
account.sequencer = provider
return account, nil
}
func (account *Account) Call(ctx context.Context, call types.FunctionCall) ([]string, error) {
switch account.provider {
case ProviderRPCv01:
if account.rpcv01 == nil {
return nil, ErrUnsupportedAccount
}
return account.rpcv01.Call(ctx, call, rpcv01.WithBlockTag("latest"))
case ProviderRPCv02:
if account.rpcv02 == nil {
return nil, ErrUnsupportedAccount
}
return account.rpcv02.Call(ctx, call, rpcv02.WithBlockTag("latest"))
case ProviderGateway:
if account.sequencer == nil {
return nil, ErrUnsupportedAccount
}
return account.sequencer.Call(ctx, call, "latest")
}
return nil, ErrUnsupportedAccount
}
func (account *Account) Sign(msgHash *big.Int) (*big.Int, *big.Int, error) {
return Curve.Sign(msgHash, account.private)
}
func (account *Account) TransactionHash(calls []types.FunctionCall, details types.ExecuteDetails) (*big.Int, error) {
var callArray []*big.Int
switch {
case account.version == 0:
callArray = fmtV0Calldata(details.Nonce, calls)
case account.version == 1:
callArray = fmtCalldata(calls)
default:
return nil, fmt.Errorf("version %d unsupported", account.version)
}
cdHash, err := Curve.ComputeHashOnElements(callArray)
if err != nil {
return nil, err
}
var multiHashData []*big.Int
switch {
case account.version == 0:
multiHashData = []*big.Int{
types.UTF8StrToBig(TRANSACTION_PREFIX),
big.NewInt(int64(account.version)),
types.SNValToBN(account.AccountAddress),
types.GetSelectorFromName(EXECUTE_SELECTOR),
cdHash,
details.MaxFee,
types.UTF8StrToBig(account.chainId),
}
case account.version == 1:
multiHashData = []*big.Int{
types.UTF8StrToBig(TRANSACTION_PREFIX),
big.NewInt(int64(account.version)),
types.SNValToBN(account.AccountAddress),
big.NewInt(0),
cdHash,
details.MaxFee,
types.UTF8StrToBig(account.chainId),
details.Nonce,
}
default:
return nil, fmt.Errorf("version %d unsupported", account.version)
}
return Curve.ComputeHashOnElements(multiHashData)
}
func (account *Account) estimateFeeHash(calls []types.FunctionCall, details types.ExecuteDetails, version *big.Int) (*big.Int, error) {
var callArray []*big.Int
switch {
case account.version == 0:
callArray = fmtV0Calldata(details.Nonce, calls)
case account.version == 1:
callArray = fmtCalldata(calls)
default:
return nil, fmt.Errorf("version %d unsupported", account.version)
}
cdHash, err := Curve.ComputeHashOnElements(callArray)
if err != nil {
return nil, err
}
var multiHashData []*big.Int
switch {
case account.version == 0:
multiHashData = []*big.Int{
types.UTF8StrToBig(TRANSACTION_PREFIX),
version,
types.SNValToBN(account.AccountAddress),
types.GetSelectorFromName(EXECUTE_SELECTOR),
cdHash,
details.MaxFee,
types.UTF8StrToBig(account.chainId),
}
case account.version == 1:
multiHashData = []*big.Int{
types.UTF8StrToBig(TRANSACTION_PREFIX),
version,
types.SNValToBN(account.AccountAddress),
big.NewInt(0),
cdHash,
details.MaxFee,
types.UTF8StrToBig(account.chainId),
details.Nonce,
}
default:
return nil, fmt.Errorf("version %d unsupported", account.version)
}
return Curve.ComputeHashOnElements(multiHashData)
}
func (account *Account) Nonce(ctx context.Context) (*big.Int, error) {
switch account.version {
case 0:
switch account.provider {
case ProviderRPCv01:
nonce, err := account.rpcv01.Call(
ctx,
types.FunctionCall{
ContractAddress: types.HexToHash(account.AccountAddress),
EntryPointSelector: "get_nonce",
Calldata: []string{},
},
rpcv01.WithBlockTag("latest"),
)
if err != nil {
return nil, err
}
if len(nonce) == 0 {
return nil, errors.New("nonce error")
}
n, ok := big.NewInt(0).SetString(nonce[0], 0)
if !ok {
return nil, errors.New("nonce error")
}
return n, nil
case ProviderRPCv02:
nonce, err := account.rpcv02.Call(
ctx,
types.FunctionCall{
ContractAddress: types.HexToHash(account.AccountAddress),
EntryPointSelector: "get_nonce",
Calldata: []string{},
},
rpcv02.WithBlockTag("latest"),
)
if err != nil {
return nil, err
}
if len(nonce) == 0 {
return nil, errors.New("nonce error")
}
n, ok := big.NewInt(0).SetString(nonce[0], 0)
if !ok {
return nil, errors.New("nonce error")
}
return n, nil
case ProviderGateway:
return account.sequencer.AccountNonce(ctx, types.HexToHash(account.AccountAddress))
}
case 1:
switch account.provider {
case ProviderRPCv01:
nonce, err := account.rpcv01.Nonce(
ctx,
types.HexToHash(account.AccountAddress),
)
if err != nil {
return nil, err
}
n, ok := big.NewInt(0).SetString(*nonce, 0)
if !ok {
return nil, errors.New("nonce error")
}
return n, nil
case ProviderRPCv02:
nonce, err := account.rpcv02.Nonce(
ctx,
rpcv02.WithBlockTag("latest"),
types.HexToHash(account.AccountAddress),
)
if err != nil {
return nil, err
}
n, ok := big.NewInt(0).SetString(*nonce, 0)
if !ok {
return nil, errors.New("nonce error")
}
return n, nil
case ProviderGateway:
return account.sequencer.Nonce(ctx, account.AccountAddress, "latest")
}
}
return nil, fmt.Errorf("version %d unsupported", account.version)
}
func (account *Account) prepFunctionInvoke(ctx context.Context, messageType string, calls []types.FunctionCall, details types.ExecuteDetails) (*types.FunctionInvoke, error) {
if messageType != "invoke" && messageType != "estimate" {
return nil, errors.New("unsupported message type")
}
nonce := details.Nonce
var err error
if details.Nonce == nil {
nonce, err = account.Nonce(ctx)
if err != nil {
return nil, err
}
}
maxFee := MAX_FEE
if details.MaxFee != nil {
maxFee = details.MaxFee
}
if account.plugin != nil {
call, err := account.plugin.PluginCall(calls)
if err != nil {
return nil, err
}
calls = append([]types.FunctionCall{call}, calls...)
}
// version, _ := big.NewInt(0).SetString("0x100000000000000000000000000000000", 0)
version, _ := big.NewInt(0).SetString("0x0", 0)
var txHash *big.Int
switch messageType {
case "invoke":
version = big.NewInt(int64(account.version))
txHash, err = account.TransactionHash(
calls,
types.ExecuteDetails{
Nonce: nonce,
MaxFee: maxFee,
},
)
if err != nil {
return nil, err
}
case "estimate":
if account.version == 1 {
// version, _ = big.NewInt(0).SetString("0x100000000000000000000000000000001", 0)
version, _ = big.NewInt(0).SetString("0x1", 0)
}
txHash, err = account.estimateFeeHash(
calls,
types.ExecuteDetails{
Nonce: nonce,
MaxFee: maxFee,
},
version,
)
if err != nil {
return nil, err
}
}
s1, s2, err := account.Sign(txHash)
if err != nil {
return nil, err
}
switch account.version {
case 0:
calldata := fmtV0CalldataStrings(nonce, calls)
return &types.FunctionInvoke{
MaxFee: maxFee,
Version: version,
Signature: types.Signature{s1, s2},
FunctionCall: types.FunctionCall{
ContractAddress: types.HexToHash(account.AccountAddress),
EntryPointSelector: EXECUTE_SELECTOR,
Calldata: calldata,
},
}, nil
case 1:
calldata := fmtCalldataStrings(calls)
return &types.FunctionInvoke{
MaxFee: maxFee,
Version: version,
Signature: types.Signature{s1, s2},
FunctionCall: types.FunctionCall{
ContractAddress: types.HexToHash(account.AccountAddress),
Calldata: calldata,
},
Nonce: nonce,
}, nil
}
return nil, ErrUnsupportedAccount
}
func (account *Account) EstimateFee(ctx context.Context, calls []types.FunctionCall, details types.ExecuteDetails) (*types.FeeEstimate, error) {
call, err := account.prepFunctionInvoke(ctx, "estimate", calls, details)
if err != nil {
return nil, err
}
switch account.provider {
case ProviderRPCv01:
return account.rpcv01.EstimateFee(ctx, *call, rpcv01.WithBlockTag("latest"))
case ProviderRPCv02:
signature := []string{}
for _, v := range call.Signature {
signature = append(signature, fmt.Sprintf("0x%s", v.Text(16)))
}
switch account.version {
case 0:
return account.rpcv02.EstimateFee(ctx, rpcv02.BroadcastedInvokeV0Transaction{
BroadcastedTxnCommonProperties: rpcv02.BroadcastedTxnCommonProperties{
MaxFee: call.MaxFee,
Version: rpcv02.TransactionV0,
Signature: signature,
Type: "INVOKE",
},
FunctionCall: call.FunctionCall,
}, rpcv02.WithBlockTag("latest"))
case 1:
return account.rpcv02.EstimateFee(ctx, rpcv02.BroadcastedInvokeV1Transaction{
BroadcastedTxnCommonProperties: rpcv02.BroadcastedTxnCommonProperties{
MaxFee: call.MaxFee,
Version: rpcv02.TransactionV1,
Signature: signature,
Nonce: call.Nonce,
Type: "INVOKE",
},
Calldata: call.FunctionCall.Calldata,
SenderAddress: types.HexToHash(account.AccountAddress),
}, rpcv02.WithBlockTag("latest"))
}
case ProviderGateway:
return account.sequencer.EstimateFee(ctx, *call, "latest")
}
return nil, ErrUnsupportedAccount
}
func (account *Account) Execute(ctx context.Context, calls []types.FunctionCall, details types.ExecuteDetails) (*types.AddInvokeTransactionOutput, error) {
maxFee := details.MaxFee
if maxFee == nil {
estimate, err := account.EstimateFee(ctx, calls, details)
if err != nil {
return nil, err
}
fmt.Printf("fee %+v\n", estimate)
v, ok := big.NewInt(0).SetString(string(estimate.OverallFee), 0)
if !ok {
return nil, errors.New("could not match OverallFee to big.Int")
}
maxFee = v.Mul(v, big.NewInt(2))
}
details.MaxFee = maxFee
call, err := account.prepFunctionInvoke(ctx, "invoke", calls, details)
if err != nil {
return nil, err
}
switch account.provider {
case ProviderRPCv01:
signature := []string{}
for _, k := range call.Signature {
signature = append(signature, fmt.Sprintf("0x%s", k.Text(16)))
}
return account.rpcv01.AddInvokeTransaction(
context.Background(),
call.FunctionCall,
signature,
fmt.Sprintf("0x%s", maxFee.Text(16)),
fmt.Sprintf("0x%d", account.version),
call.Nonce,
)
case ProviderRPCv02:
signature := []string{}
for _, v := range call.Signature {
signature = append(signature, fmt.Sprintf("0x%s", v.Text(16)))
}
switch account.version {
case 0:
return account.rpcv02.AddInvokeTransaction(ctx, rpcv02.BroadcastedInvokeV0Transaction{
BroadcastedTxnCommonProperties: rpcv02.BroadcastedTxnCommonProperties{
MaxFee: call.MaxFee,
Version: rpcv02.TransactionV0,
Signature: signature,
Type: "INVOKE",
},
FunctionCall: call.FunctionCall,
})
case 1:
return account.rpcv02.AddInvokeTransaction(ctx, rpcv02.BroadcastedInvokeV1Transaction{
BroadcastedTxnCommonProperties: rpcv02.BroadcastedTxnCommonProperties{
MaxFee: call.MaxFee,
Version: rpcv02.TransactionV1,
Signature: signature,
Nonce: call.Nonce,
Type: "INVOKE",
},
SenderAddress: types.HexToHash(account.AccountAddress),
Calldata: call.FunctionCall.Calldata,
})
}
case ProviderGateway:
return account.sequencer.Invoke(
context.Background(),
*call,
)
}
return nil, ErrUnsupportedAccount
}