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.

156 lines
4.6 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
4 years ago
4 years ago
  1. package common
  2. import (
  3. "crypto/ecdsa"
  4. "encoding/hex"
  5. "log"
  6. "math/big"
  7. "testing"
  8. ethCommon "github.com/ethereum/go-ethereum/common"
  9. "github.com/ethereum/go-ethereum/crypto"
  10. "github.com/iden3/go-iden3-crypto/babyjub"
  11. "github.com/iden3/go-iden3-crypto/utils"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. )
  15. func TestNewL1Tx(t *testing.T) {
  16. toForge := new(int64)
  17. *toForge = 123456
  18. fromIdx := new(Idx)
  19. *fromIdx = 300
  20. l1Tx := &L1Tx{
  21. ToForgeL1TxsNum: toForge,
  22. Position: 71,
  23. ToIdx: 301,
  24. TokenID: 5,
  25. Amount: big.NewInt(1),
  26. LoadAmount: big.NewInt(2),
  27. FromIdx: fromIdx,
  28. }
  29. l1Tx, err := NewL1Tx(l1Tx)
  30. assert.Nil(t, err)
  31. assert.Equal(t, "0x01000000000001e240004700", l1Tx.TxID.String())
  32. }
  33. func TestL1TxByteParsers(t *testing.T) {
  34. var pkComp babyjub.PublicKeyComp
  35. err := pkComp.UnmarshalText([]byte("0x56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c"))
  36. require.Nil(t, err)
  37. pk, err := pkComp.Decompress()
  38. require.Nil(t, err)
  39. fromIdx := new(Idx)
  40. *fromIdx = 2
  41. l1Tx := &L1Tx{
  42. ToIdx: 3,
  43. TokenID: 5,
  44. Amount: big.NewInt(1),
  45. LoadAmount: big.NewInt(2),
  46. FromIdx: fromIdx,
  47. FromBJJ: pk,
  48. FromEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  49. }
  50. expected, err := utils.HexDecode("c58d29fa6e86e4fae04ddced660d45bcf3cb237056ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c0000000000020002000100000005000000000003")
  51. require.Nil(t, err)
  52. encodedData, err := l1Tx.Bytes()
  53. require.Nil(t, err)
  54. assert.Equal(t, expected, encodedData)
  55. decodedData, err := L1TxFromBytes(encodedData)
  56. require.Nil(t, err)
  57. assert.Equal(t, l1Tx, decodedData)
  58. encodedData2, err := decodedData.Bytes()
  59. require.Nil(t, err)
  60. assert.Equal(t, encodedData, encodedData2)
  61. // expect error if length!=68
  62. _, err = L1TxFromBytes(encodedData[:66])
  63. require.NotNil(t, err)
  64. _, err = L1TxFromBytes([]byte{})
  65. require.NotNil(t, err)
  66. _, err = L1TxFromBytes(nil)
  67. require.NotNil(t, err)
  68. }
  69. func TestL1CoordinatorTxByteParsers(t *testing.T) {
  70. privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
  71. require.Nil(t, err)
  72. publicKey := privateKey.Public()
  73. publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
  74. if !ok {
  75. log.Fatal("error casting public key to ECDSA")
  76. }
  77. publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
  78. pubKey, err := crypto.UnmarshalPubkey(publicKeyBytes)
  79. require.Nil(t, err)
  80. fromEthAddr := crypto.PubkeyToAddress(*pubKey)
  81. var pkComp babyjub.PublicKeyComp
  82. err = pkComp.UnmarshalText([]byte("0x56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c"))
  83. require.Nil(t, err)
  84. pk, err := pkComp.Decompress()
  85. require.Nil(t, err)
  86. bytesMessage1 := []byte("\x19Ethereum Signed Message:\n98")
  87. bytesMessage2 := []byte("I authorize this babyjubjub key for hermez rollup account creation")
  88. babyjub := pk.Compress()
  89. var data []byte
  90. data = append(data, bytesMessage1...)
  91. data = append(data, bytesMessage2...)
  92. data = append(data, babyjub[:]...)
  93. hash := crypto.Keccak256Hash(data)
  94. signature, err := crypto.Sign(hash.Bytes(), privateKey)
  95. require.Nil(t, err)
  96. // Ethereum adds 27 to v
  97. v := int(signature[64])
  98. signature[64] = byte(v + 27)
  99. l1Tx := &L1Tx{
  100. TokenID: 231,
  101. FromBJJ: pk,
  102. FromEthAddr: fromEthAddr,
  103. }
  104. bytesCoordinatorL1, err := l1Tx.BytesCoordinatorTx(signature)
  105. require.Nil(t, err)
  106. l1txDecoded, err := L1TxFromCoordinatorBytes(bytesCoordinatorL1)
  107. require.Nil(t, err)
  108. assert.Equal(t, l1Tx, l1txDecoded)
  109. }
  110. func TestL1CoordinatorTxByteParsersCompatibility(t *testing.T) {
  111. var signature []byte
  112. r, err := hex.DecodeString("da71e5eb097e115405d84d1e7b464009b434b32c014a2df502d1f065ced8bc3b")
  113. require.Nil(t, err)
  114. s, err := hex.DecodeString("186d7122ff7f654cfed3156719774898d573900c86599a885a706dbdffe5ea8c")
  115. require.Nil(t, err)
  116. v, err := hex.DecodeString("1b")
  117. require.Nil(t, err)
  118. signature = append(signature, r[:]...)
  119. signature = append(signature, s[:]...)
  120. signature = append(signature, v[:]...)
  121. var pkComp babyjub.PublicKeyComp
  122. err = pkComp.UnmarshalText([]byte("0xa2c2807ee39c3b3378738cff85a46a9465bb8fcf44ea597c33da9719be7c259c"))
  123. require.Nil(t, err)
  124. // Data from the compatibility test
  125. expected := "1b186d7122ff7f654cfed3156719774898d573900c86599a885a706dbdffe5ea8cda71e5eb097e115405d84d1e7b464009b434b32c014a2df502d1f065ced8bc3ba2c2807ee39c3b3378738cff85a46a9465bb8fcf44ea597c33da9719be7c259c000000e7"
  126. pk, err := pkComp.Decompress()
  127. require.Nil(t, err)
  128. l1Tx := &L1Tx{
  129. TokenID: 231,
  130. FromBJJ: pk,
  131. }
  132. encodeData, err := l1Tx.BytesCoordinatorTx(signature)
  133. require.Nil(t, err)
  134. assert.Equal(t, expected, hex.EncodeToString(encodeData))
  135. }