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.

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