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.

69 lines
1.8 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 common
  2. import (
  3. "math/big"
  4. "testing"
  5. ethCommon "github.com/ethereum/go-ethereum/common"
  6. "github.com/iden3/go-iden3-crypto/babyjub"
  7. "github.com/iden3/go-iden3-crypto/utils"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestNewL1Tx(t *testing.T) {
  12. l1Tx := &L1Tx{
  13. ToForgeL1TxsNum: int64(123456),
  14. Position: 71,
  15. ToIdx: 301,
  16. TokenID: 5,
  17. Amount: big.NewInt(1),
  18. LoadAmount: big.NewInt(2),
  19. FromIdx: 300,
  20. }
  21. l1Tx, err := NewL1Tx(l1Tx)
  22. assert.Nil(t, err)
  23. assert.Equal(t, "0x01000000000001e240004700", l1Tx.TxID.String())
  24. }
  25. func TestL1TxByteParsers(t *testing.T) {
  26. var pkComp babyjub.PublicKeyComp
  27. err := pkComp.UnmarshalText([]byte("0x56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c"))
  28. require.Nil(t, err)
  29. pk, err := pkComp.Decompress()
  30. require.Nil(t, err)
  31. l1Tx := &L1Tx{
  32. ToIdx: 3,
  33. TokenID: 5,
  34. Amount: big.NewInt(1),
  35. LoadAmount: big.NewInt(2),
  36. FromIdx: 2,
  37. FromBJJ: pk,
  38. FromEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  39. }
  40. expected, err := utils.HexDecode("c58d29fa6e86e4fae04ddced660d45bcf3cb237056ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c0000000000020002000100000005000000000003")
  41. require.Nil(t, err)
  42. encodedData, err := l1Tx.Bytes(32)
  43. require.Nil(t, err)
  44. assert.Equal(t, expected, encodedData)
  45. decodedData, err := L1TxFromBytes(encodedData)
  46. require.Nil(t, err)
  47. assert.Equal(t, l1Tx, decodedData)
  48. encodedData2, err := decodedData.Bytes(32)
  49. require.Nil(t, err)
  50. assert.Equal(t, encodedData, encodedData2)
  51. // expect error if length!=68
  52. _, err = L1TxFromBytes(encodedData[:66])
  53. require.NotNil(t, err)
  54. _, err = L1TxFromBytes([]byte{})
  55. require.NotNil(t, err)
  56. _, err = L1TxFromBytes(nil)
  57. require.NotNil(t, err)
  58. }