arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/test/l2db.go
test/l2db.goRaw
 1package test
 2
 3import (
 4	ethCommon "github.com/ethereum/go-ethereum/common"
 5	ethCrypto "github.com/ethereum/go-ethereum/crypto"
 6	"github.com/hermeznetwork/hermez-node/common"
 7	"github.com/iden3/go-iden3-crypto/babyjub"
 8)
 9
10// GenPoolTxs generates L2 pool txs.
11// WARNING: This tx doesn't follow the protocol (signature, txID, ...)
12// it's just to test getting/setting from/to the DB.
13func GenPoolTxs(n int, tokens []common.Token) []*common.PoolL2Tx {
14	/*
15		WARNING: this should be replaced by transaktio
16		txs := make([]*common.PoolL2Tx, 0, n)
17		privK := babyjub.NewRandPrivKey()
18		for i := 256; i < 256+n; i++ {
19			var state common.PoolL2TxState
20			//nolint:gomnd
21			if i%4 == 0 {
22				state = common.PoolL2TxStatePending
23				//nolint:gomnd
24			} else if i%4 == 1 {
25				state = common.PoolL2TxStateInvalid
26				//nolint:gomnd
27			} else if i%4 == 2 {
28				state = common.PoolL2TxStateForging
29				//nolint:gomnd
30			} else if i%4 == 3 {
31				state = common.PoolL2TxStateForged
32			}
33			fee := common.FeeSelector(i % 255) //nolint:gomnd
34			token := tokens[i%len(tokens)]
35			tx := &common.PoolL2Tx{
36				FromIdx:   common.Idx(i),
37				ToIdx:     common.Idx(i + 1),
38				ToEthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
39				ToBJJ:     privK.Public(),
40				TokenID:   token.TokenID,
41				Amount:    big.NewInt(int64(i)),
42				Fee:       fee,
43				Nonce:     common.Nonce(i),
44				State:     state,
45				Signature: privK.SignPoseidon(big.NewInt(int64(i))).Compress(),
46			}
47			var err error
48			tx, err = common.NewPoolL2Tx(tx)
49			if err != nil {
50				panic(err)
51			}
52			if i%2 == 0 { // Optional parameters: rq
53				tx.RqFromIdx = common.Idx(i)
54				tx.RqToIdx = common.Idx(i + 1)
55				tx.RqToEthAddr = ethCommon.BigToAddress(big.NewInt(int64(i)))
56				tx.RqToBJJ = privK.Public()
57				tx.RqTokenID = common.TokenID(i)
58				tx.RqAmount = big.NewInt(int64(i))
59				tx.RqFee = common.FeeSelector(i)
60				tx.RqNonce = common.Nonce(i)
61			}
62			txs = append(txs, tx)
63		}
64		return txs
65	*/
66	return nil
67}
68
69// GenAuths generates account creation authorizations
70func GenAuths(nAuths int, chainID uint16, hermezContractAddr ethCommon.Address) []*common.AccountCreationAuth {
71	auths := []*common.AccountCreationAuth{}
72	for i := 0; i < nAuths; i++ {
73		// Generate keys
74		ethPrivK, err := ethCrypto.GenerateKey()
75		if err != nil {
76			panic(err)
77		}
78		bjjPrivK := babyjub.NewRandPrivKey()
79		// Generate auth
80		auth := &common.AccountCreationAuth{
81			EthAddr: ethCrypto.PubkeyToAddress(ethPrivK.PublicKey),
82			BJJ:     bjjPrivK.Public().Compress(),
83		}
84		// Sign
85		h, err := auth.HashToSign(chainID, hermezContractAddr)
86		if err != nil {
87			panic(err)
88		}
89		signature, err := ethCrypto.Sign(h, ethPrivK)
90		if err != nil {
91			panic(err)
92		}
93		signature[64] += 27
94		auth.Signature = signature
95		auths = append(auths, auth)
96	}
97	return auths
98}