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.

227 lines
7.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
  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 TestL1userTxByteParsers(t *testing.T) {
  48. var pkComp babyjub.PublicKeyComp
  49. pkCompL := []byte("0x56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c")
  50. err := pkComp.UnmarshalText(pkCompL)
  51. require.Nil(t, err)
  52. pk, err := pkComp.Decompress()
  53. require.Nil(t, err)
  54. l1Tx := &L1Tx{
  55. UserOrigin: true,
  56. ToIdx: 3,
  57. TokenID: 5,
  58. Amount: big.NewInt(1),
  59. LoadAmount: big.NewInt(2),
  60. FromIdx: 2,
  61. FromBJJ: pk,
  62. FromEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  63. }
  64. encodedData, err := l1Tx.BytesUser()
  65. require.Nil(t, err)
  66. decodedData, err := L1UserTxFromBytes(encodedData)
  67. require.Nil(t, err)
  68. assert.Equal(t, l1Tx, decodedData)
  69. encodedData2, err := decodedData.BytesUser()
  70. require.Nil(t, err)
  71. assert.Equal(t, encodedData, encodedData2)
  72. // expect error if length!=68
  73. _, err = L1UserTxFromBytes(encodedData[:66])
  74. require.NotNil(t, err)
  75. _, err = L1UserTxFromBytes([]byte{})
  76. require.NotNil(t, err)
  77. _, err = L1UserTxFromBytes(nil)
  78. require.NotNil(t, err)
  79. }
  80. func TestL1TxByteParsersCompatibility(t *testing.T) {
  81. // Data from compatibility test
  82. var pkComp babyjub.PublicKeyComp
  83. pkCompB, err := hex.DecodeString("0dd02deb2c81068e7a0f7e327df80b4ab79ee1f41a7def613e73a20c32eece5a")
  84. require.Nil(t, err)
  85. pkCompL := SwapEndianness(pkCompB)
  86. err = pkComp.UnmarshalText([]byte(hex.EncodeToString(pkCompL)))
  87. require.Nil(t, err)
  88. pk, err := pkComp.Decompress()
  89. require.Nil(t, err)
  90. loadAmount := new(big.Int)
  91. loadAmount.SetString("100000000000000000000", 10)
  92. l1Tx := &L1Tx{
  93. ToIdx: 87865485,
  94. TokenID: 2098076,
  95. Amount: big.NewInt(2400000000000000000),
  96. LoadAmount: loadAmount,
  97. FromIdx: Idx(29767899),
  98. FromBJJ: pk,
  99. FromEthAddr: ethCommon.HexToAddress("0x85dab5b9e2e361d0c208d77be90efcc0439b0a53"),
  100. UserOrigin: true,
  101. }
  102. expected, err := utils.HexDecode("85dab5b9e2e361d0c208d77be90efcc0439b0a530dd02deb2c81068e7a0f7e327df80b4ab79ee1f41a7def613e73a20c32eece5a000001c638db8be880f00020039c0000053cb88d")
  103. require.Nil(t, err)
  104. encodedData, err := l1Tx.BytesUser()
  105. require.Nil(t, err)
  106. assert.Equal(t, expected, encodedData)
  107. }
  108. func TestL1CoordinatorTxByteParsers(t *testing.T) {
  109. hermezAddress := ethCommon.HexToAddress("0xD6C850aeBFDC46D7F4c207e445cC0d6B0919BDBe")
  110. hermezAddressBytes := ethCommon.LeftPadBytes(hermezAddress.Bytes(), 32)
  111. chainID := big.NewInt(1337)
  112. chainIDBytes := ethCommon.LeftPadBytes(chainID.Bytes(), 2)
  113. privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
  114. require.Nil(t, err)
  115. publicKey := privateKey.Public()
  116. publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
  117. if !ok {
  118. log.Fatal("error casting public key to ECDSA")
  119. }
  120. publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
  121. pubKey, err := crypto.UnmarshalPubkey(publicKeyBytes)
  122. require.Nil(t, err)
  123. fromEthAddr := crypto.PubkeyToAddress(*pubKey)
  124. var pkComp babyjub.PublicKeyComp
  125. pkCompL := []byte("56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c")
  126. err = pkComp.UnmarshalText(pkCompL)
  127. require.Nil(t, err)
  128. pk, err := pkComp.Decompress()
  129. require.Nil(t, err)
  130. bytesMessage1 := []byte("\x19Ethereum Signed Message:\n120")
  131. bytesMessage2 := []byte("I authorize this babyjubjub key for hermez rollup account creation")
  132. babyjub := pk.Compress()
  133. babyjubB := SwapEndianness(babyjub[:])
  134. var data []byte
  135. data = append(data, bytesMessage1...)
  136. data = append(data, bytesMessage2...)
  137. data = append(data, babyjubB[:]...)
  138. data = append(data, chainIDBytes...)
  139. data = append(data, hermezAddressBytes...)
  140. hash := crypto.Keccak256Hash(data)
  141. signature, err := crypto.Sign(hash.Bytes(), privateKey)
  142. require.Nil(t, err)
  143. // Ethereum adds 27 to v
  144. v := int(signature[64])
  145. signature[64] = byte(v + 27)
  146. l1Tx := &L1Tx{
  147. TokenID: 231,
  148. FromBJJ: pk,
  149. FromEthAddr: fromEthAddr,
  150. Amount: big.NewInt(0),
  151. LoadAmount: big.NewInt(0),
  152. }
  153. bytesCoordinatorL1, err := l1Tx.BytesCoordinatorTx(signature)
  154. require.Nil(t, err)
  155. l1txDecoded, err := L1CoordinatorTxFromBytes(bytesCoordinatorL1, chainID, hermezAddress)
  156. require.Nil(t, err)
  157. assert.Equal(t, l1Tx, l1txDecoded)
  158. bytesCoordinatorL12, err := l1txDecoded.BytesCoordinatorTx(signature)
  159. require.Nil(t, err)
  160. assert.Equal(t, bytesCoordinatorL1, bytesCoordinatorL12)
  161. // expect error if length!=68
  162. _, err = L1CoordinatorTxFromBytes(bytesCoordinatorL1[:66], chainID, hermezAddress)
  163. require.NotNil(t, err)
  164. _, err = L1CoordinatorTxFromBytes([]byte{}, chainID, hermezAddress)
  165. require.NotNil(t, err)
  166. _, err = L1CoordinatorTxFromBytes(nil, chainID, hermezAddress)
  167. require.NotNil(t, err)
  168. }
  169. func TestL1CoordinatorTxByteParsersCompatibility(t *testing.T) {
  170. // Data from compatibility test
  171. var signature []byte
  172. r, err := hex.DecodeString("da71e5eb097e115405d84d1e7b464009b434b32c014a2df502d1f065ced8bc3b")
  173. require.Nil(t, err)
  174. s, err := hex.DecodeString("186d7122ff7f654cfed3156719774898d573900c86599a885a706dbdffe5ea8c")
  175. require.Nil(t, err)
  176. v, err := hex.DecodeString("1b")
  177. require.Nil(t, err)
  178. signature = append(signature, r[:]...)
  179. signature = append(signature, s[:]...)
  180. signature = append(signature, v[:]...)
  181. var pkComp babyjub.PublicKeyComp
  182. pkCompB, err := hex.DecodeString("a2c2807ee39c3b3378738cff85a46a9465bb8fcf44ea597c33da9719be7c259c")
  183. require.Nil(t, err)
  184. pkCompL := SwapEndianness(pkCompB)
  185. err = pkComp.UnmarshalText([]byte(hex.EncodeToString(pkCompL)))
  186. require.Nil(t, err)
  187. // Data from the compatibility test
  188. require.Nil(t, err)
  189. pk, err := pkComp.Decompress()
  190. require.Nil(t, err)
  191. l1Tx := &L1Tx{
  192. TokenID: 231,
  193. FromBJJ: pk,
  194. }
  195. encodeData, err := l1Tx.BytesCoordinatorTx(signature)
  196. require.Nil(t, err)
  197. expected, err := utils.HexDecode("1b186d7122ff7f654cfed3156719774898d573900c86599a885a706dbdffe5ea8cda71e5eb097e115405d84d1e7b464009b434b32c014a2df502d1f065ced8bc3ba2c2807ee39c3b3378738cff85a46a9465bb8fcf44ea597c33da9719be7c259c000000e7")
  198. require.Nil(t, err)
  199. assert.Equal(t, expected, encodeData)
  200. }