hermez-node/test/til/txs.go
test/til/txs.goRaw
1package til
2
3import (
4 "crypto/ecdsa"
5 "fmt"
6 "math/big"
7 "strconv"
8 "strings"
9 "time"
10
11 ethCommon "github.com/ethereum/go-ethereum/common"
12 ethCrypto "github.com/ethereum/go-ethereum/crypto"
13 "github.com/hermeznetwork/hermez-node/common"
14 "github.com/hermeznetwork/hermez-node/log"
15 "github.com/iden3/go-iden3-crypto/babyjub"
16)
17
18func newBatchData(batchNum int) common.BatchData {
19 return common.BatchData{
20 L1CoordinatorTxs: []common.L1Tx{},
21 L2Txs: []common.L2Tx{},
22 Batch: common.Batch{
23 BatchNum: common.BatchNum(batchNum),
24 StateRoot: big.NewInt(0), ExitRoot: big.NewInt(0)},
25 }
26}
27
28// Context contains the data of the test
29type Context struct {
30 Instructions []instruction
31 userNames []string
32 Users map[string]*User
33 lastRegisteredTokenID common.TokenID
34 l1CreatedAccounts map[string]*Account
35
36 // rollupConstMaxL1UserTx Maximum L1-user transactions allowed to be queued in a batch
37 rollupConstMaxL1UserTx int
38
39 idx int
40 currBlock common.BlockData
41 currBatch common.BatchData
42 currBatchNum int
43 queues [][]L1Tx
44 toForgeNum int
45 openToForge int
46 currBatchTest struct {
47 l1CoordinatorTxs []L1Tx
48 l2Txs []L2Tx
49 }
50 blockNum int64
51}
52
53// NewContext returns a new Context
54func NewContext(rollupConstMaxL1UserTx int) *Context {
55 currBatchNum := 1 // The protocol defines the first batchNum to be 1
56 return &Context{
57 Users: make(map[string]*User),
58 l1CreatedAccounts: make(map[string]*Account),
59 lastRegisteredTokenID: 0,
60
61 rollupConstMaxL1UserTx: rollupConstMaxL1UserTx,
62 idx: common.UserThreshold,
63 // We use some placeholder values for StateRoot and ExitTree
64 // because these values will never be nil
65 currBatch: newBatchData(currBatchNum),
66 currBatchNum: currBatchNum,
67 // start with 2 queues, one for toForge, and the other for openToForge
68 queues: make([][]L1Tx, 2),
69 toForgeNum: 0,
70 openToForge: 1,
71 //nolint:gomnd
72 blockNum: 2, // rollup genesis blockNum
73 }
74}
75
76// Account contains the data related to the account for a specific TokenID of a User
77type Account struct {
78 Idx common.Idx
79 Nonce common.Nonce
80}
81
82// User contains the data related to a testing user
83type User struct {
84 BJJ *babyjub.PrivateKey
85 Addr ethCommon.Address
86 Accounts map[common.TokenID]*Account
87}
88
89// L1Tx is the data structure used internally for transaction test generation,
90// which contains a common.L1Tx data plus some intermediate data for the
91// transaction generation.
92type L1Tx struct {
93 lineNum int
94 fromIdxName string
95 toIdxName string
96
97 L1Tx common.L1Tx
98}
99
100// L2Tx is the data structure used internally for transaction test generation,
101// which contains a common.L2Tx data plus some intermediate data for the
102// transaction generation.
103type L2Tx struct {
104 lineNum int
105 fromIdxName string
106 toIdxName string
107 tokenID common.TokenID
108 L2Tx common.L2Tx
109}
110
111// GenerateBlocks returns an array of BlockData for a given set. It uses the
112// users (keys & nonces) of the Context.
113func (tc *Context) GenerateBlocks(set string) ([]common.BlockData, error) {
114 parser := newParser(strings.NewReader(set))
115 parsedSet, err := parser.parse()
116 if err != nil {
117 return nil, err
118 }
119 if parsedSet.typ != setTypeBlockchain {
120 return nil, fmt.Errorf("Expected set type: %s, found: %s", setTypeBlockchain, parsedSet.typ)
121 }
122
123 tc.Instructions = parsedSet.instructions
124 tc.userNames = parsedSet.users
125
126 tc.generateKeys(tc.userNames)
127
128 var blocks []common.BlockData
129 for _, inst := range parsedSet.instructions {
130 switch inst.typ {
131 case txTypeCreateAccountDepositCoordinator: // tx source: L1CoordinatorTx
132 if err := tc.checkIfTokenIsRegistered(inst); err != nil {
133 log.Error(err)
134 return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
135 }
136 tx := common.L1Tx{
137 FromEthAddr: tc.Users[inst.from].Addr,
138 FromBJJ: tc.Users[inst.from].BJJ.Public(),
139 TokenID: inst.tokenID,
140 Amount: big.NewInt(0),
141 LoadAmount: big.NewInt(int64(inst.loadAmount)),
142 Type: common.TxTypeCreateAccountDeposit, // as txTypeCreateAccountDepositCoordinator is not valid oustide Til package
143 }
144 testTx := L1Tx{
145 lineNum: inst.lineNum,
146 fromIdxName: inst.from,
147 L1Tx: tx,
148 }
149
150 tc.currBatchTest.l1CoordinatorTxs = append(tc.currBatchTest.l1CoordinatorTxs, testTx)
151 case common.TxTypeCreateAccountDeposit, common.TxTypeCreateAccountDepositTransfer: // tx source: L1UserTx
152 if err := tc.checkIfTokenIsRegistered(inst); err != nil {
153 log.Error(err)
154 return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
155 }
156 tx := common.L1Tx{
157 FromEthAddr: tc.Users[inst.from].Addr,
158 FromBJJ: tc.Users[inst.from].BJJ.Public(),
159 TokenID: inst.tokenID,
160 Amount: big.NewInt(0),
161 LoadAmount: big.NewInt(int64(inst.loadAmount)),
162 Type: inst.typ,
163 }
164 if inst.typ == common.TxTypeCreateAccountDepositTransfer {
165 tx.Amount = big.NewInt(int64(inst.amount))
166 }
167 testTx := L1Tx{
168 lineNum: inst.lineNum,
169 fromIdxName: inst.from,
170 toIdxName: inst.to,
171 L1Tx: tx,
172 }
173 if err := tc.addToL1Queue(testTx); err != nil {
174 return nil, err
175 }
176 case common.TxTypeDeposit, common.TxTypeDepositTransfer: // tx source: L1UserTx
177 if err := tc.checkIfTokenIsRegistered(inst); err != nil {
178 log.Error(err)
179 return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
180 }
181 if err := tc.checkIfAccountExists(inst.from, inst); err != nil {
182 log.Error(err)
183 return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
184 }
185 tx := common.L1Tx{
186 TokenID: inst.tokenID,
187 Amount: big.NewInt(0),
188 LoadAmount: big.NewInt(int64(inst.loadAmount)),
189 Type: inst.typ,
190 }
191 if inst.typ == common.TxTypeDepositTransfer {
192 tx.Amount = big.NewInt(int64(inst.amount))
193 }
194 testTx := L1Tx{
195 lineNum: inst.lineNum,
196 fromIdxName: inst.from,
197 toIdxName: inst.to,
198 L1Tx: tx,
199 }
200 if err := tc.addToL1Queue(testTx); err != nil {
201 return nil, err
202 }
203 case common.TxTypeTransfer: // L2Tx
204 if err := tc.checkIfTokenIsRegistered(inst); err != nil {
205 log.Error(err)
206 return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
207 }
208 tx := common.L2Tx{
209 Amount: big.NewInt(int64(inst.amount)),
210 Fee: common.FeeSelector(inst.fee),
211 Type: common.TxTypeTransfer,
212 }
213 tx.BatchNum = common.BatchNum(tc.currBatchNum) // when converted to PoolL2Tx BatchNum parameter is lost
214 testTx := L2Tx{
215 lineNum: inst.lineNum,
216 fromIdxName: inst.from,
217 toIdxName: inst.to,
218 tokenID: inst.tokenID,
219 L2Tx: tx,
220 }
221 tc.currBatchTest.l2Txs = append(tc.currBatchTest.l2Txs, testTx)
222 case common.TxTypeExit: // tx source: L2Tx
223 if err := tc.checkIfTokenIsRegistered(inst); err != nil {
224 log.Error(err)
225 return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
226 }
227 tx := common.L2Tx{
228 ToIdx: common.Idx(1), // as is an Exit
229 Amount: big.NewInt(int64(inst.amount)),
230 Type: common.TxTypeExit,
231 }
232 tx.BatchNum = common.BatchNum(tc.currBatchNum) // when converted to PoolL2Tx BatchNum parameter is lost
233 testTx := L2Tx{
234 lineNum: inst.lineNum,
235 fromIdxName: inst.from,
236 toIdxName: inst.to,
237 tokenID: inst.tokenID,
238 L2Tx: tx,
239 }
240 tc.currBatchTest.l2Txs = append(tc.currBatchTest.l2Txs, testTx)
241 case common.TxTypeForceExit: // tx source: L1UserTx
242 if err := tc.checkIfTokenIsRegistered(inst); err != nil {
243 log.Error(err)
244 return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
245 }
246 tx := common.L1Tx{
247 ToIdx: common.Idx(1), // as is an Exit
248 TokenID: inst.tokenID,
249 Amount: big.NewInt(int64(inst.amount)),
250 LoadAmount: big.NewInt(0),
251 Type: common.TxTypeExit,
252 }
253 testTx := L1Tx{
254 lineNum: inst.lineNum,
255 fromIdxName: inst.from,
256 toIdxName: inst.to,
257 L1Tx: tx,
258 }
259 if err := tc.addToL1Queue(testTx); err != nil {
260 return nil, err
261 }
262 case typeNewBatch:
263 if err = tc.calculateIdxForL1Txs(true, tc.currBatchTest.l1CoordinatorTxs); err != nil {
264 return nil, err
265 }
266 if err = tc.setIdxs(); err != nil {
267 log.Error(err)
268 return nil, err
269 }
270 case typeNewBatchL1:
271 // for each L1UserTx of the queues[ToForgeNum], calculate the Idx
272 if err = tc.calculateIdxForL1Txs(false, tc.queues[tc.toForgeNum]); err != nil {
273 return nil, err
274 }
275 if err = tc.calculateIdxForL1Txs(true, tc.currBatchTest.l1CoordinatorTxs); err != nil {
276 return nil, err
277 }
278 tc.currBatch.L1Batch = true
279 if err = tc.setIdxs(); err != nil {
280 log.Error(err)
281 return nil, err
282 }
283 // advance batch
284 tc.toForgeNum++
285 if tc.toForgeNum == tc.openToForge {
286 tc.openToForge++
287 newQueue := []L1Tx{}
288 tc.queues = append(tc.queues, newQueue)
289 }
290 case typeNewBlock:
291 tc.currBlock.Block = common.Block{
292 EthBlockNum: tc.blockNum,
293 }
294 blocks = append(blocks, tc.currBlock)
295 tc.blockNum++
296 tc.currBlock = common.BlockData{}
297 case typeAddToken:
298 newToken := common.Token{
299 EthAddr: ethCommon.BigToAddress(big.NewInt(int64(inst.tokenID * 100))), //nolint:gomnd
300 // Name: fmt.Sprintf("Token %d", inst.tokenID),
301 // Symbol: fmt.Sprintf("TK%d", inst.tokenID),
302 // Decimals: 18,
303 TokenID: inst.tokenID,
304 EthBlockNum: tc.blockNum,
305 }
306 if inst.tokenID != tc.lastRegisteredTokenID+1 {
307 return nil, fmt.Errorf("Line %d: AddToken TokenID should be sequential, expected TokenID: %d, defined TokenID: %d", inst.lineNum, tc.lastRegisteredTokenID+1, inst.tokenID)
308 }
309 tc.lastRegisteredTokenID++
310 tc.currBlock.AddedTokens = append(tc.currBlock.AddedTokens, newToken)
311 default:
312 return nil, fmt.Errorf("Line %d: Unexpected type: %s", inst.lineNum, inst.typ)
313 }
314 }
315
316 return blocks, nil
317}
318
319// calculateIdxsForL1Txs calculates new Idx for new created accounts. If
320// 'isCoordinatorTxs==true', adds the tx to tc.currBatch.L1CoordinatorTxs.
321func (tc *Context) calculateIdxForL1Txs(isCoordinatorTxs bool, txs []L1Tx) error {
322 // for each batch.L1CoordinatorTxs of the queues[ToForgeNum], calculate the Idx
323 for i := 0; i < len(txs); i++ {
324 tx := txs[i]
325 if tx.L1Tx.Type == common.TxTypeCreateAccountDeposit || tx.L1Tx.Type == common.TxTypeCreateAccountDepositTransfer {
326 if tc.Users[tx.fromIdxName].Accounts[tx.L1Tx.TokenID] != nil { // if account already exists, return error
327 return fmt.Errorf("Can not create same account twice (same User & same TokenID) (this is a design property of Til)")
328 }
329 tc.Users[tx.fromIdxName].Accounts[tx.L1Tx.TokenID] = &Account{
330 Idx: common.Idx(tc.idx),
331 Nonce: common.Nonce(0),
332 }
333 tc.l1CreatedAccounts[idxTokenIDToString(tx.fromIdxName, tx.L1Tx.TokenID)] = tc.Users[tx.fromIdxName].Accounts[tx.L1Tx.TokenID]
334 tc.idx++
335 }
336 if isCoordinatorTxs {
337 tc.currBatch.L1CoordinatorTxs = append(tc.currBatch.L1CoordinatorTxs, tx.L1Tx)
338 }
339 }
340 return nil
341}
342
343// setIdxs sets the Idxs to the transactions of the tc.currBatch
344func (tc *Context) setIdxs() error {
345 // once Idxs are calculated, update transactions to use the new Idxs
346 for i := 0; i < len(tc.currBatchTest.l2Txs); i++ {
347 testTx := &tc.currBatchTest.l2Txs[i]
348
349 if tc.Users[testTx.fromIdxName].Accounts[testTx.tokenID] == nil {
350 return fmt.Errorf("Line %d: %s from User %s for TokenID %d while account not created yet", testTx.lineNum, testTx.L2Tx.Type, testTx.fromIdxName, testTx.tokenID)
351 }
352 if testTx.L2Tx.Type == common.TxTypeTransfer {
353 if _, ok := tc.l1CreatedAccounts[idxTokenIDToString(testTx.toIdxName, testTx.tokenID)]; !ok {
354 return fmt.Errorf("Line %d: Can not create Transfer for a non existing account. Batch %d, ToIdx name: %s, TokenID: %d", testTx.lineNum, tc.currBatchNum, testTx.toIdxName, testTx.tokenID)
355 }
356 }
357 tc.Users[testTx.fromIdxName].Accounts[testTx.tokenID].Nonce++
358 testTx.L2Tx.Nonce = tc.Users[testTx.fromIdxName].Accounts[testTx.tokenID].Nonce
359
360 // set real Idx
361 testTx.L2Tx.FromIdx = tc.Users[testTx.fromIdxName].Accounts[testTx.tokenID].Idx
362 if testTx.L2Tx.Type == common.TxTypeTransfer {
363 testTx.L2Tx.ToIdx = tc.Users[testTx.toIdxName].Accounts[testTx.tokenID].Idx
364 }
365 // in case Type==Exit, ToIdx=1, already set at the
366 // GenerateBlocks main switch inside TxTypeExit case
367
368 nTx, err := common.NewL2Tx(&testTx.L2Tx)
369 if err != nil {
370 return fmt.Errorf("Line %d: %s", testTx.lineNum, err.Error())
371 }
372 testTx.L2Tx = *nTx
373
374 tc.currBatch.L2Txs = append(tc.currBatch.L2Txs, testTx.L2Tx)
375 }
376
377 tc.currBatch.Batch.LastIdx = int64(tc.idx - 1) // `-1` because tc.idx is the next available idx
378 tc.currBlock.Batches = append(tc.currBlock.Batches, tc.currBatch)
379 tc.currBatchNum++
380 tc.currBatch = newBatchData(tc.currBatchNum)
381 tc.currBatchTest.l1CoordinatorTxs = nil
382 tc.currBatchTest.l2Txs = nil
383 return nil
384}
385
386// addToL1Queue adds the L1Tx into the queue that is open and has space
387func (tc *Context) addToL1Queue(tx L1Tx) error {
388 if len(tc.queues[tc.openToForge]) >= tc.rollupConstMaxL1UserTx {
389 // if current OpenToForge queue reached its Max, move into a
390 // new queue
391 tc.openToForge++
392 newQueue := []L1Tx{}
393 tc.queues = append(tc.queues, newQueue)
394 }
395 // Fill L1UserTx specific parameters
396 tx.L1Tx.UserOrigin = true
397 toForgeL1TxsNum := int64(tc.openToForge)
398 tx.L1Tx.ToForgeL1TxsNum = &toForgeL1TxsNum
399 tx.L1Tx.EthBlockNum = tc.blockNum
400 tx.L1Tx.Position = len(tc.queues[tc.openToForge])
401
402 // When an L1UserTx is generated, all idxs must be available (except when idx == 0 or idx == 1)
403 if tx.L1Tx.Type != common.TxTypeCreateAccountDeposit && tx.L1Tx.Type != common.TxTypeCreateAccountDepositTransfer {
404 tx.L1Tx.FromIdx = tc.Users[tx.fromIdxName].Accounts[tx.L1Tx.TokenID].Idx
405 }
406 tx.L1Tx.FromEthAddr = tc.Users[tx.fromIdxName].Addr
407 tx.L1Tx.FromBJJ = tc.Users[tx.fromIdxName].BJJ.Public()
408 if tx.toIdxName == "" {
409 tx.L1Tx.ToIdx = common.Idx(0)
410 } else {
411 account, ok := tc.Users[tx.toIdxName].Accounts[tx.L1Tx.TokenID]
412 if !ok {
413 return fmt.Errorf("Line %d: Transfer to User: %s, for TokenID: %d, "+
414 "while account not created yet", tx.lineNum, tx.toIdxName, tx.L1Tx.TokenID)
415 }
416 tx.L1Tx.ToIdx = account.Idx
417 }
418 if tx.L1Tx.Type == common.TxTypeExit {
419 tx.L1Tx.ToIdx = common.Idx(1)
420 }
421 nTx, err := common.NewL1Tx(&tx.L1Tx)
422 if err != nil {
423 return fmt.Errorf("Line %d: %s", tx.lineNum, err.Error())
424 }
425 tx.L1Tx = *nTx
426
427 tc.queues[tc.openToForge] = append(tc.queues[tc.openToForge], tx)
428 tc.currBlock.L1UserTxs = append(tc.currBlock.L1UserTxs, tx.L1Tx)
429
430 return nil
431}
432
433func (tc *Context) checkIfAccountExists(tf string, inst instruction) error {
434 if tc.Users[tf].Accounts[inst.tokenID] == nil {
435 return fmt.Errorf("%s at User: %s, for TokenID: %d, while account not created yet", inst.typ, tf, inst.tokenID)
436 }
437 return nil
438}
439func (tc *Context) checkIfTokenIsRegistered(inst instruction) error {
440 if inst.tokenID > tc.lastRegisteredTokenID {
441 return fmt.Errorf("Can not process %s: TokenID %d not registered, last registered TokenID: %d", inst.typ, inst.tokenID, tc.lastRegisteredTokenID)
442 }
443 return nil
444}
445
446// GeneratePoolL2Txs returns an array of common.PoolL2Tx from a given set. It
447// uses the users (keys) of the Context.
448func (tc *Context) GeneratePoolL2Txs(set string) ([]common.PoolL2Tx, error) {
449 parser := newParser(strings.NewReader(set))
450 parsedSet, err := parser.parse()
451 if err != nil {
452 return nil, err
453 }
454 if parsedSet.typ != setTypePoolL2 {
455 return nil, fmt.Errorf("Expected set type: %s, found: %s", setTypePoolL2, parsedSet.typ)
456 }
457
458 tc.Instructions = parsedSet.instructions
459 tc.userNames = parsedSet.users
460
461 tc.generateKeys(tc.userNames)
462
463 txs := []common.PoolL2Tx{}
464 for _, inst := range tc.Instructions {
465 switch inst.typ {
466 case common.TxTypeTransfer, common.TxTypeTransferToEthAddr, common.TxTypeTransferToBJJ:
467 if err := tc.checkIfAccountExists(inst.from, inst); err != nil {
468 log.Error(err)
469 return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
470 }
471 if inst.typ == common.TxTypeTransfer {
472 // if TxTypeTransfer, need to exist the ToIdx account
473 if err := tc.checkIfAccountExists(inst.to, inst); err != nil {
474 log.Error(err)
475 return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
476 }
477 }
478 tc.Users[inst.from].Accounts[inst.tokenID].Nonce++
479 // if account of receiver does not exist, don't use
480 // ToIdx, and use only ToEthAddr & ToBJJ
481 tx := common.PoolL2Tx{
482 FromIdx: tc.Users[inst.from].Accounts[inst.tokenID].Idx,
483 TokenID: inst.tokenID,
484 Amount: big.NewInt(int64(inst.amount)),
485 Fee: common.FeeSelector(inst.fee),
486 Nonce: tc.Users[inst.from].Accounts[inst.tokenID].Nonce,
487 State: common.PoolL2TxStatePending,
488 Timestamp: time.Now(),
489 RqToEthAddr: common.EmptyAddr,
490 RqToBJJ: nil,
491 Type: inst.typ,
492 }
493 if tx.Type == common.TxTypeTransfer {
494 tx.ToIdx = tc.Users[inst.to].Accounts[inst.tokenID].Idx
495 tx.ToEthAddr = tc.Users[inst.to].Addr
496 tx.ToBJJ = tc.Users[inst.to].BJJ.Public()
497 } else if tx.Type == common.TxTypeTransferToEthAddr {
498 tx.ToIdx = common.Idx(0)
499 tx.ToEthAddr = tc.Users[inst.to].Addr
500 } else if tx.Type == common.TxTypeTransferToBJJ {
501 tx.ToIdx = common.Idx(0)
502 tx.ToEthAddr = common.FFAddr
503 tx.ToBJJ = tc.Users[inst.to].BJJ.Public()
504 }
505 nTx, err := common.NewPoolL2Tx(&tx)
506 if err != nil {
507 return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
508 }
509 tx = *nTx
510 // perform signature and set it to tx.Signature
511 toSign, err := tx.HashToSign()
512 if err != nil {
513 return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
514 }
515 sig := tc.Users[inst.from].BJJ.SignPoseidon(toSign)
516 tx.Signature = sig.Compress()
517
518 txs = append(txs, tx)
519 case common.TxTypeExit:
520 tc.Users[inst.from].Accounts[inst.tokenID].Nonce++
521 tx := common.PoolL2Tx{
522 FromIdx: tc.Users[inst.from].Accounts[inst.tokenID].Idx,
523 ToIdx: common.Idx(1), // as is an Exit
524 TokenID: inst.tokenID,
525 Amount: big.NewInt(int64(inst.amount)),
526 Nonce: tc.Users[inst.from].Accounts[inst.tokenID].Nonce,
527 Type: common.TxTypeExit,
528 }
529 nTx, err := common.NewPoolL2Tx(&tx)
530 if err != nil {
531 return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
532 }
533 tx = *nTx
534 // perform signature and set it to tx.Signature
535 toSign, err := tx.HashToSign()
536 if err != nil {
537 return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
538 }
539 sig := tc.Users[inst.from].BJJ.SignPoseidon(toSign)
540 tx.Signature = sig.Compress()
541 txs = append(txs, tx)
542 default:
543 return nil, fmt.Errorf("Line %d: instruction type unrecognized: %s", inst.lineNum, inst.typ)
544 }
545 }
546
547 return txs, nil
548}
549
550// generateKeys generates BabyJubJub & Address keys for the given list of user
551// names in a deterministic way. This means, that for the same given
552// 'userNames' in a certain order, the keys will be always the same.
553func (tc *Context) generateKeys(userNames []string) {
554 for i := 1; i < len(userNames)+1; i++ {
555 if _, ok := tc.Users[userNames[i-1]]; ok {
556 // account already created
557 continue
558 }
559 // babyjubjub key
560 var sk babyjub.PrivateKey
561 copy(sk[:], []byte(strconv.Itoa(i))) // only for testing
562
563 // eth address
564 var key ecdsa.PrivateKey
565 key.D = big.NewInt(int64(i)) // only for testing
566 key.PublicKey.X, key.PublicKey.Y = ethCrypto.S256().ScalarBaseMult(key.D.Bytes())
567 key.Curve = ethCrypto.S256()
568 addr := ethCrypto.PubkeyToAddress(key.PublicKey)
569
570 u := User{
571 BJJ: &sk,
572 Addr: addr,
573 Accounts: make(map[common.TokenID]*Account),
574 }
575 tc.Users[userNames[i-1]] = &u
576 }
577}