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.

97 lines
2.7 KiB

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