You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.6 KiB

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