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.

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