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.

43 lines
1.2 KiB

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 := l1Tx.Bytes(32)
  29. assert.Equal(t, expected, encodedData)
  30. decodedData, err := L1TxFromBytes(encodedData)
  31. require.Nil(t, err)
  32. encodedData2 := decodedData.Bytes(32)
  33. assert.Equal(t, encodedData, encodedData2)
  34. }