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.

54 lines
1.4 KiB

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 TestL1TxCodec(t *testing.T) {
  12. var pkComp babyjub.PublicKeyComp
  13. err := pkComp.UnmarshalText([]byte("0x56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c"))
  14. require.Nil(t, err)
  15. pk, err := pkComp.Decompress()
  16. require.Nil(t, err)
  17. l1Tx := L1Tx{
  18. ToIdx: 3,
  19. TokenID: 5,
  20. Amount: big.NewInt(1),
  21. LoadAmount: big.NewInt(2),
  22. FromIdx: 2,
  23. FromBJJ: pk,
  24. FromEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  25. }
  26. expected, err := utils.HexDecode("c58d29fa6e86e4fae04ddced660d45bcf3cb237056ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c00000002000200010000000500000003")
  27. require.Nil(t, err)
  28. encodedData, err := l1Tx.Bytes(32)
  29. require.Nil(t, err)
  30. assert.Equal(t, expected, encodedData)
  31. decodedData, err := L1TxFromBytes(encodedData)
  32. require.Nil(t, err)
  33. assert.Equal(t, l1Tx, *decodedData)
  34. encodedData2, err := decodedData.Bytes(32)
  35. require.Nil(t, err)
  36. assert.Equal(t, encodedData, encodedData2)
  37. // expect error if length!=68
  38. _, err = L1TxFromBytes(encodedData[:66])
  39. require.NotNil(t, err)
  40. _, err = L1TxFromBytes([]byte{})
  41. require.NotNil(t, err)
  42. _, err = L1TxFromBytes(nil)
  43. require.NotNil(t, err)
  44. }