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.

107 lines
3.3 KiB

  1. package common
  2. import (
  3. "encoding/hex"
  4. "math/big"
  5. "testing"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/iden3/go-iden3-crypto/babyjub"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestNewPoolL2Tx(t *testing.T) {
  11. poolL2Tx := &PoolL2Tx{
  12. FromIdx: 87654,
  13. ToIdx: 300,
  14. Amount: big.NewInt(4),
  15. TokenID: 5,
  16. Nonce: 144,
  17. }
  18. poolL2Tx, err := NewPoolL2Tx(poolL2Tx)
  19. assert.Nil(t, err)
  20. assert.Equal(t, "0x020000000156660000000090", poolL2Tx.TxID.String())
  21. }
  22. func TestTxCompressedData(t *testing.T) {
  23. var sk babyjub.PrivateKey
  24. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  25. assert.Nil(t, err)
  26. tx := PoolL2Tx{
  27. FromIdx: 2,
  28. ToIdx: 3,
  29. Amount: big.NewInt(4),
  30. TokenID: 5,
  31. Nonce: 6,
  32. ToBJJ: sk.Public(),
  33. }
  34. txCompressedData, err := tx.TxCompressedData()
  35. assert.Nil(t, err)
  36. // test vector value generated from javascript implementation
  37. expectedStr := "1766847064778421992193717128424891165872736891548909569553540449389241871"
  38. assert.Equal(t, expectedStr, txCompressedData.String())
  39. expected, ok := new(big.Int).SetString(expectedStr, 10)
  40. assert.True(t, ok)
  41. assert.Equal(t, expected.Bytes(), txCompressedData.Bytes())
  42. assert.Equal(t, "10000000000060000000500040000000000030000000000020001c60be60f", hex.EncodeToString(txCompressedData.Bytes())[1:])
  43. tx = PoolL2Tx{
  44. RqFromIdx: 7,
  45. RqToIdx: 8,
  46. RqAmount: big.NewInt(9),
  47. RqTokenID: 10,
  48. RqNonce: 11,
  49. RqFee: 12,
  50. RqToBJJ: sk.Public(),
  51. }
  52. txCompressedData, err = tx.RqTxCompressedDataV2()
  53. assert.Nil(t, err)
  54. // test vector value generated from javascript implementation
  55. expectedStr = "6571340879233176732837827812956721483162819083004853354503"
  56. assert.Equal(t, expectedStr, txCompressedData.String())
  57. expected, ok = new(big.Int).SetString(expectedStr, 10)
  58. assert.True(t, ok)
  59. assert.Equal(t, expected.Bytes(), txCompressedData.Bytes())
  60. assert.Equal(t, "10c000000000b0000000a0009000000000008000000000007", hex.EncodeToString(txCompressedData.Bytes())[1:])
  61. }
  62. func TestHashToSign(t *testing.T) {
  63. var sk babyjub.PrivateKey
  64. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  65. assert.Nil(t, err)
  66. tx := PoolL2Tx{
  67. FromIdx: 2,
  68. ToIdx: 3,
  69. Amount: big.NewInt(4),
  70. TokenID: 5,
  71. Nonce: 6,
  72. ToBJJ: sk.Public(),
  73. RqToEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  74. RqToBJJ: sk.Public(),
  75. }
  76. toSign, err := tx.HashToSign()
  77. assert.Nil(t, err)
  78. assert.Equal(t, "13412877307445712067533842795279849753265998687662992184595695642580679868064", toSign.String())
  79. }
  80. func TestVerifyTxSignature(t *testing.T) {
  81. var sk babyjub.PrivateKey
  82. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  83. assert.Nil(t, err)
  84. tx := PoolL2Tx{
  85. FromIdx: 2,
  86. ToIdx: 3,
  87. Amount: big.NewInt(4),
  88. TokenID: 5,
  89. Nonce: 6,
  90. ToBJJ: sk.Public(),
  91. RqToEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  92. RqToBJJ: sk.Public(),
  93. }
  94. toSign, err := tx.HashToSign()
  95. assert.Nil(t, err)
  96. assert.Equal(t, "13412877307445712067533842795279849753265998687662992184595695642580679868064", toSign.String())
  97. sig := sk.SignPoseidon(toSign)
  98. tx.Signature = sig
  99. assert.True(t, tx.VerifySignature(sk.Public()))
  100. }