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.

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