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.

118 lines
3.5 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. toIdx := new(Idx)
  12. *toIdx = 300
  13. poolL2Tx := &PoolL2Tx{
  14. FromIdx: 87654,
  15. ToIdx: toIdx,
  16. Amount: big.NewInt(4),
  17. TokenID: 5,
  18. Nonce: 144,
  19. }
  20. poolL2Tx, err := NewPoolL2Tx(poolL2Tx)
  21. assert.Nil(t, err)
  22. assert.Equal(t, "0x020000000156660000000090", poolL2Tx.TxID.String())
  23. }
  24. func TestTxCompressedData(t *testing.T) {
  25. var sk babyjub.PrivateKey
  26. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  27. assert.Nil(t, err)
  28. toIdx := new(Idx)
  29. *toIdx = 3
  30. tx := PoolL2Tx{
  31. FromIdx: 2,
  32. ToIdx: toIdx,
  33. Amount: big.NewInt(4),
  34. TokenID: 5,
  35. Nonce: 6,
  36. ToBJJ: sk.Public(),
  37. }
  38. txCompressedData, err := tx.TxCompressedData()
  39. assert.Nil(t, err)
  40. // test vector value generated from javascript implementation
  41. expectedStr := "1766847064778421992193717128424891165872736891548909569553540449389241871"
  42. assert.Equal(t, expectedStr, txCompressedData.String())
  43. expected, ok := new(big.Int).SetString(expectedStr, 10)
  44. assert.True(t, ok)
  45. assert.Equal(t, expected.Bytes(), txCompressedData.Bytes())
  46. assert.Equal(t, "10000000000060000000500040000000000030000000000020001c60be60f", hex.EncodeToString(txCompressedData.Bytes())[1:])
  47. *toIdx = 8
  48. tx = PoolL2Tx{
  49. FromIdx: 7,
  50. ToIdx: toIdx,
  51. Amount: big.NewInt(9),
  52. TokenID: 10,
  53. Nonce: 11,
  54. Fee: 12,
  55. ToBJJ: sk.Public(),
  56. }
  57. txCompressedData, err = tx.TxCompressedDataV2()
  58. assert.Nil(t, err)
  59. // test vector value generated from javascript implementation
  60. expectedStr = "6571340879233176732837827812956721483162819083004853354503"
  61. assert.Equal(t, expectedStr, txCompressedData.String())
  62. expected, ok = new(big.Int).SetString(expectedStr, 10)
  63. assert.True(t, ok)
  64. assert.Equal(t, expected.Bytes(), txCompressedData.Bytes())
  65. assert.Equal(t, "10c000000000b0000000a0009000000000008000000000007", hex.EncodeToString(txCompressedData.Bytes())[1:])
  66. }
  67. func TestHashToSign(t *testing.T) {
  68. var sk babyjub.PrivateKey
  69. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  70. assert.Nil(t, err)
  71. ethAddr := ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370")
  72. toIdx := new(Idx)
  73. *toIdx = 3
  74. tx := PoolL2Tx{
  75. FromIdx: 2,
  76. ToIdx: toIdx,
  77. Amount: big.NewInt(4),
  78. TokenID: 5,
  79. Nonce: 6,
  80. ToBJJ: sk.Public(),
  81. RqToEthAddr: &ethAddr,
  82. RqToBJJ: sk.Public(),
  83. }
  84. toSign, err := tx.HashToSign()
  85. assert.Nil(t, err)
  86. assert.Equal(t, "14526446928649310956370997581245770629723313742905751117262272426489782809503", toSign.String())
  87. }
  88. func TestVerifyTxSignature(t *testing.T) {
  89. var sk babyjub.PrivateKey
  90. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  91. assert.Nil(t, err)
  92. ethAddr := ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370")
  93. toIdx := new(Idx)
  94. *toIdx = 3
  95. tx := PoolL2Tx{
  96. FromIdx: 2,
  97. ToIdx: toIdx,
  98. Amount: big.NewInt(4),
  99. TokenID: 5,
  100. Nonce: 6,
  101. ToBJJ: sk.Public(),
  102. RqToEthAddr: &ethAddr,
  103. RqToBJJ: sk.Public(),
  104. }
  105. toSign, err := tx.HashToSign()
  106. assert.Nil(t, err)
  107. assert.Equal(t, "14526446928649310956370997581245770629723313742905751117262272426489782809503", toSign.String())
  108. sig := sk.SignPoseidon(toSign)
  109. tx.Signature = sig
  110. assert.True(t, tx.VerifySignature(sk.Public()))
  111. }