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.

99 lines
2.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 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,
  68. hermezContractAddr ethCommon.Address) []*common.AccountCreationAuth {
  69. auths := []*common.AccountCreationAuth{}
  70. for i := 0; i < nAuths; i++ {
  71. // Generate keys
  72. ethPrivK, err := ethCrypto.GenerateKey()
  73. if err != nil {
  74. panic(err)
  75. }
  76. bjjPrivK := babyjub.NewRandPrivKey()
  77. // Generate auth
  78. auth := &common.AccountCreationAuth{
  79. EthAddr: ethCrypto.PubkeyToAddress(ethPrivK.PublicKey),
  80. BJJ: bjjPrivK.Public().Compress(),
  81. }
  82. // Sign
  83. h, err := auth.HashToSign(chainID, hermezContractAddr)
  84. if err != nil {
  85. panic(err)
  86. }
  87. signature, err := ethCrypto.Sign(h, ethPrivK)
  88. if err != nil {
  89. panic(err)
  90. }
  91. signature[64] += 27
  92. auth.Signature = signature
  93. auths = append(auths, auth)
  94. }
  95. return auths
  96. }