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.

280 lines
8.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
  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. DepositAmount: big.NewInt(2),
  25. FromIdx: Idx(300),
  26. }
  27. l1Tx, err := NewL1Tx(l1Tx)
  28. assert.NoError(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. DepositAmount: big.NewInt(2),
  40. FromIdx: Idx(300),
  41. BatchNum: &batchNum,
  42. }
  43. l1Tx, err := NewL1Tx(l1Tx)
  44. assert.NoError(t, err)
  45. assert.Equal(t, "0x01000000000000cafe005800", l1Tx.TxID.String())
  46. }
  47. func TestL1TxCompressedData(t *testing.T) {
  48. tx := L1Tx{
  49. FromIdx: 2,
  50. ToIdx: 3,
  51. Amount: big.NewInt(4),
  52. TokenID: 5,
  53. }
  54. txCompressedData, err := tx.TxCompressedData()
  55. assert.NoError(t, err)
  56. // test vector value generated from javascript implementation
  57. expectedStr := "7307597389635308713748674793997299267459594577423"
  58. assert.Equal(t, expectedStr, txCompressedData.String())
  59. assert.Equal(t, "0500040000000000030000000000020000c60be60f", hex.EncodeToString(txCompressedData.Bytes()))
  60. }
  61. func TestBytesDataAvailability(t *testing.T) {
  62. tx := L1Tx{
  63. FromIdx: 2,
  64. ToIdx: 3,
  65. Amount: big.NewInt(4),
  66. TokenID: 5,
  67. }
  68. txCompressedData, err := tx.BytesDataAvailability(32)
  69. assert.NoError(t, err)
  70. assert.Equal(t, "0000000200000003000000", hex.EncodeToString(txCompressedData))
  71. tx = L1Tx{
  72. FromIdx: 2,
  73. ToIdx: 3,
  74. EffectiveAmount: big.NewInt(4),
  75. TokenID: 5,
  76. }
  77. txCompressedData, err = tx.BytesDataAvailability(32)
  78. assert.NoError(t, err)
  79. assert.Equal(t, "0000000200000003000400", hex.EncodeToString(txCompressedData))
  80. }
  81. func TestL1TxFromDataAvailability(t *testing.T) {
  82. tx := L1Tx{
  83. FromIdx: 2,
  84. ToIdx: 3,
  85. Amount: big.NewInt(4),
  86. }
  87. txCompressedData, err := tx.BytesDataAvailability(32)
  88. assert.NoError(t, err)
  89. l1tx, err := L1TxFromDataAvailability(txCompressedData, 32)
  90. require.NoError(t, err)
  91. assert.Equal(t, tx.FromIdx, l1tx.FromIdx)
  92. assert.Equal(t, tx.ToIdx, l1tx.ToIdx)
  93. tx = L1Tx{
  94. FromIdx: 2,
  95. ToIdx: 3,
  96. EffectiveAmount: big.NewInt(4),
  97. }
  98. txCompressedData, err = tx.BytesDataAvailability(32)
  99. assert.NoError(t, err)
  100. l1tx, err = L1TxFromDataAvailability(txCompressedData, 32)
  101. require.NoError(t, err)
  102. assert.Equal(t, tx.FromIdx, l1tx.FromIdx)
  103. assert.Equal(t, tx.ToIdx, l1tx.ToIdx)
  104. assert.Equal(t, tx.EffectiveAmount, l1tx.EffectiveAmount)
  105. }
  106. func TestL1userTxByteParsers(t *testing.T) {
  107. var pkComp babyjub.PublicKeyComp
  108. pkCompL := []byte("0x56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c")
  109. err := pkComp.UnmarshalText(pkCompL)
  110. require.NoError(t, err)
  111. l1Tx := &L1Tx{
  112. UserOrigin: true,
  113. ToIdx: 3,
  114. TokenID: 5,
  115. Amount: big.NewInt(1),
  116. DepositAmount: big.NewInt(2),
  117. FromIdx: 2,
  118. FromBJJ: pkComp,
  119. FromEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  120. }
  121. encodedData, err := l1Tx.BytesUser()
  122. require.NoError(t, err)
  123. decodedData, err := L1UserTxFromBytes(encodedData)
  124. require.NoError(t, err)
  125. assert.Equal(t, l1Tx, decodedData)
  126. encodedData2, err := decodedData.BytesUser()
  127. require.NoError(t, err)
  128. assert.Equal(t, encodedData, encodedData2)
  129. // expect error if length!=68
  130. _, err = L1UserTxFromBytes(encodedData[:66])
  131. require.NotNil(t, err)
  132. _, err = L1UserTxFromBytes([]byte{})
  133. require.NotNil(t, err)
  134. _, err = L1UserTxFromBytes(nil)
  135. require.NotNil(t, err)
  136. }
  137. func TestL1TxByteParsersCompatibility(t *testing.T) {
  138. // Data from compatibility test
  139. var pkComp babyjub.PublicKeyComp
  140. pkCompB, err := hex.DecodeString("0dd02deb2c81068e7a0f7e327df80b4ab79ee1f41a7def613e73a20c32eece5a")
  141. require.NoError(t, err)
  142. pkCompL := SwapEndianness(pkCompB)
  143. err = pkComp.UnmarshalText([]byte(hex.EncodeToString(pkCompL)))
  144. require.NoError(t, err)
  145. depositAmount := new(big.Int)
  146. depositAmount.SetString("100000000000000000000", 10)
  147. l1Tx := &L1Tx{
  148. ToIdx: 87865485,
  149. TokenID: 2098076,
  150. Amount: big.NewInt(2400000000000000000),
  151. DepositAmount: depositAmount,
  152. FromIdx: Idx(29767899),
  153. FromBJJ: pkComp,
  154. FromEthAddr: ethCommon.HexToAddress("0x85dab5b9e2e361d0c208d77be90efcc0439b0a53"),
  155. UserOrigin: true,
  156. }
  157. expected, err := utils.HexDecode("85dab5b9e2e361d0c208d77be90efcc0439b0a530dd02deb2c81068e7a0f7e327df80b4ab79ee1f41a7def613e73a20c32eece5a000001c638db8be880f00020039c0000053cb88d")
  158. require.NoError(t, err)
  159. encodedData, err := l1Tx.BytesUser()
  160. require.NoError(t, err)
  161. assert.Equal(t, expected, encodedData)
  162. }
  163. func TestL1CoordinatorTxByteParsers(t *testing.T) {
  164. hermezAddress := ethCommon.HexToAddress("0xD6C850aeBFDC46D7F4c207e445cC0d6B0919BDBe")
  165. chainID := big.NewInt(1337)
  166. chainIDBytes := ethCommon.LeftPadBytes(chainID.Bytes(), 2)
  167. privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
  168. require.NoError(t, err)
  169. publicKey := privateKey.Public()
  170. publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
  171. if !ok {
  172. log.Fatal("error casting public key to ECDSA")
  173. }
  174. publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
  175. pubKey, err := crypto.UnmarshalPubkey(publicKeyBytes)
  176. require.NoError(t, err)
  177. fromEthAddr := crypto.PubkeyToAddress(*pubKey)
  178. var pkComp babyjub.PublicKeyComp
  179. pkCompL := []byte("56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c")
  180. err = pkComp.UnmarshalText(pkCompL)
  181. require.NoError(t, err)
  182. bytesMessage1 := []byte("\x19Ethereum Signed Message:\n120")
  183. bytesMessage2 := []byte("I authorize this babyjubjub key for hermez rollup account creation")
  184. babyjubB := SwapEndianness(pkComp[:])
  185. var data []byte
  186. data = append(data, bytesMessage1...)
  187. data = append(data, bytesMessage2...)
  188. data = append(data, babyjubB[:]...)
  189. data = append(data, chainIDBytes...)
  190. data = append(data, hermezAddress.Bytes()...)
  191. hash := crypto.Keccak256Hash(data)
  192. signature, err := crypto.Sign(hash.Bytes(), privateKey)
  193. require.NoError(t, err)
  194. // Ethereum adds 27 to v
  195. v := int(signature[64])
  196. signature[64] = byte(v + 27)
  197. l1Tx := &L1Tx{
  198. TokenID: 231,
  199. FromBJJ: pkComp,
  200. FromEthAddr: fromEthAddr,
  201. Amount: big.NewInt(0),
  202. DepositAmount: big.NewInt(0),
  203. }
  204. bytesCoordinatorL1, err := l1Tx.BytesCoordinatorTx(signature)
  205. require.NoError(t, err)
  206. l1txDecoded, err := L1CoordinatorTxFromBytes(bytesCoordinatorL1, chainID, hermezAddress)
  207. require.NoError(t, err)
  208. assert.Equal(t, l1Tx, l1txDecoded)
  209. bytesCoordinatorL12, err := l1txDecoded.BytesCoordinatorTx(signature)
  210. require.NoError(t, err)
  211. assert.Equal(t, bytesCoordinatorL1, bytesCoordinatorL12)
  212. // expect error if length!=68
  213. _, err = L1CoordinatorTxFromBytes(bytesCoordinatorL1[:66], chainID, hermezAddress)
  214. require.NotNil(t, err)
  215. _, err = L1CoordinatorTxFromBytes([]byte{}, chainID, hermezAddress)
  216. require.NotNil(t, err)
  217. _, err = L1CoordinatorTxFromBytes(nil, chainID, hermezAddress)
  218. require.NotNil(t, err)
  219. }
  220. func TestL1CoordinatorTxByteParsersCompatibility(t *testing.T) {
  221. // Data from compatibility test
  222. var signature []byte
  223. r, err := hex.DecodeString("da71e5eb097e115405d84d1e7b464009b434b32c014a2df502d1f065ced8bc3b")
  224. require.NoError(t, err)
  225. s, err := hex.DecodeString("186d7122ff7f654cfed3156719774898d573900c86599a885a706dbdffe5ea8c")
  226. require.NoError(t, err)
  227. v, err := hex.DecodeString("1b")
  228. require.NoError(t, err)
  229. signature = append(signature, r[:]...)
  230. signature = append(signature, s[:]...)
  231. signature = append(signature, v[:]...)
  232. var pkComp babyjub.PublicKeyComp
  233. pkCompB, err := hex.DecodeString("a2c2807ee39c3b3378738cff85a46a9465bb8fcf44ea597c33da9719be7c259c")
  234. require.NoError(t, err)
  235. pkCompL := SwapEndianness(pkCompB)
  236. err = pkComp.UnmarshalText([]byte(hex.EncodeToString(pkCompL)))
  237. require.NoError(t, err)
  238. // Data from the compatibility test
  239. require.NoError(t, err)
  240. l1Tx := &L1Tx{
  241. TokenID: 231,
  242. FromBJJ: pkComp,
  243. }
  244. encodeData, err := l1Tx.BytesCoordinatorTx(signature)
  245. require.NoError(t, err)
  246. expected, err := utils.HexDecode("1b186d7122ff7f654cfed3156719774898d573900c86599a885a706dbdffe5ea8cda71e5eb097e115405d84d1e7b464009b434b32c014a2df502d1f065ced8bc3ba2c2807ee39c3b3378738cff85a46a9465bb8fcf44ea597c33da9719be7c259c000000e7")
  247. require.NoError(t, err)
  248. assert.Equal(t, expected, encodeData)
  249. }