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.

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