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