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.

291 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
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.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. DepositAmount: 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 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.Nil(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.Nil(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.Nil(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.Nil(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.Nil(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.Nil(t, err)
  111. pk, err := pkComp.Decompress()
  112. require.Nil(t, err)
  113. l1Tx := &L1Tx{
  114. UserOrigin: true,
  115. ToIdx: 3,
  116. TokenID: 5,
  117. Amount: big.NewInt(1),
  118. DepositAmount: big.NewInt(2),
  119. FromIdx: 2,
  120. FromBJJ: pk,
  121. FromEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  122. }
  123. encodedData, err := l1Tx.BytesUser()
  124. require.Nil(t, err)
  125. decodedData, err := L1UserTxFromBytes(encodedData)
  126. require.Nil(t, err)
  127. assert.Equal(t, l1Tx, decodedData)
  128. encodedData2, err := decodedData.BytesUser()
  129. require.Nil(t, err)
  130. assert.Equal(t, encodedData, encodedData2)
  131. // expect error if length!=68
  132. _, err = L1UserTxFromBytes(encodedData[:66])
  133. require.NotNil(t, err)
  134. _, err = L1UserTxFromBytes([]byte{})
  135. require.NotNil(t, err)
  136. _, err = L1UserTxFromBytes(nil)
  137. require.NotNil(t, err)
  138. }
  139. func TestL1TxByteParsersCompatibility(t *testing.T) {
  140. // Data from compatibility test
  141. var pkComp babyjub.PublicKeyComp
  142. pkCompB, err := hex.DecodeString("0dd02deb2c81068e7a0f7e327df80b4ab79ee1f41a7def613e73a20c32eece5a")
  143. require.Nil(t, err)
  144. pkCompL := SwapEndianness(pkCompB)
  145. err = pkComp.UnmarshalText([]byte(hex.EncodeToString(pkCompL)))
  146. require.Nil(t, err)
  147. pk, err := pkComp.Decompress()
  148. require.Nil(t, err)
  149. depositAmount := new(big.Int)
  150. depositAmount.SetString("100000000000000000000", 10)
  151. l1Tx := &L1Tx{
  152. ToIdx: 87865485,
  153. TokenID: 2098076,
  154. Amount: big.NewInt(2400000000000000000),
  155. DepositAmount: depositAmount,
  156. FromIdx: Idx(29767899),
  157. FromBJJ: pk,
  158. FromEthAddr: ethCommon.HexToAddress("0x85dab5b9e2e361d0c208d77be90efcc0439b0a53"),
  159. UserOrigin: true,
  160. }
  161. expected, err := utils.HexDecode("85dab5b9e2e361d0c208d77be90efcc0439b0a530dd02deb2c81068e7a0f7e327df80b4ab79ee1f41a7def613e73a20c32eece5a000001c638db8be880f00020039c0000053cb88d")
  162. require.Nil(t, err)
  163. encodedData, err := l1Tx.BytesUser()
  164. require.Nil(t, err)
  165. assert.Equal(t, expected, encodedData)
  166. }
  167. func TestL1CoordinatorTxByteParsers(t *testing.T) {
  168. hermezAddress := ethCommon.HexToAddress("0xD6C850aeBFDC46D7F4c207e445cC0d6B0919BDBe")
  169. chainID := big.NewInt(1337)
  170. chainIDBytes := ethCommon.LeftPadBytes(chainID.Bytes(), 2)
  171. privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
  172. require.Nil(t, err)
  173. publicKey := privateKey.Public()
  174. publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
  175. if !ok {
  176. log.Fatal("error casting public key to ECDSA")
  177. }
  178. publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
  179. pubKey, err := crypto.UnmarshalPubkey(publicKeyBytes)
  180. require.Nil(t, err)
  181. fromEthAddr := crypto.PubkeyToAddress(*pubKey)
  182. var pkComp babyjub.PublicKeyComp
  183. pkCompL := []byte("56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c")
  184. err = pkComp.UnmarshalText(pkCompL)
  185. require.Nil(t, err)
  186. pk, err := pkComp.Decompress()
  187. require.Nil(t, err)
  188. bytesMessage1 := []byte("\x19Ethereum Signed Message:\n120")
  189. bytesMessage2 := []byte("I authorize this babyjubjub key for hermez rollup account creation")
  190. babyjub := pk.Compress()
  191. babyjubB := SwapEndianness(babyjub[:])
  192. var data []byte
  193. data = append(data, bytesMessage1...)
  194. data = append(data, bytesMessage2...)
  195. data = append(data, babyjubB[:]...)
  196. data = append(data, chainIDBytes...)
  197. data = append(data, hermezAddress.Bytes()...)
  198. hash := crypto.Keccak256Hash(data)
  199. signature, err := crypto.Sign(hash.Bytes(), privateKey)
  200. require.Nil(t, err)
  201. // Ethereum adds 27 to v
  202. v := int(signature[64])
  203. signature[64] = byte(v + 27)
  204. l1Tx := &L1Tx{
  205. TokenID: 231,
  206. FromBJJ: pk,
  207. FromEthAddr: fromEthAddr,
  208. Amount: big.NewInt(0),
  209. DepositAmount: big.NewInt(0),
  210. }
  211. bytesCoordinatorL1, err := l1Tx.BytesCoordinatorTx(signature)
  212. require.Nil(t, err)
  213. l1txDecoded, err := L1CoordinatorTxFromBytes(bytesCoordinatorL1, chainID, hermezAddress)
  214. require.Nil(t, err)
  215. assert.Equal(t, l1Tx, l1txDecoded)
  216. bytesCoordinatorL12, err := l1txDecoded.BytesCoordinatorTx(signature)
  217. require.Nil(t, err)
  218. assert.Equal(t, bytesCoordinatorL1, bytesCoordinatorL12)
  219. // expect error if length!=68
  220. _, err = L1CoordinatorTxFromBytes(bytesCoordinatorL1[:66], chainID, hermezAddress)
  221. require.NotNil(t, err)
  222. _, err = L1CoordinatorTxFromBytes([]byte{}, chainID, hermezAddress)
  223. require.NotNil(t, err)
  224. _, err = L1CoordinatorTxFromBytes(nil, chainID, hermezAddress)
  225. require.NotNil(t, err)
  226. }
  227. func TestL1CoordinatorTxByteParsersCompatibility(t *testing.T) {
  228. // Data from compatibility test
  229. var signature []byte
  230. r, err := hex.DecodeString("da71e5eb097e115405d84d1e7b464009b434b32c014a2df502d1f065ced8bc3b")
  231. require.Nil(t, err)
  232. s, err := hex.DecodeString("186d7122ff7f654cfed3156719774898d573900c86599a885a706dbdffe5ea8c")
  233. require.Nil(t, err)
  234. v, err := hex.DecodeString("1b")
  235. require.Nil(t, err)
  236. signature = append(signature, r[:]...)
  237. signature = append(signature, s[:]...)
  238. signature = append(signature, v[:]...)
  239. var pkComp babyjub.PublicKeyComp
  240. pkCompB, err := hex.DecodeString("a2c2807ee39c3b3378738cff85a46a9465bb8fcf44ea597c33da9719be7c259c")
  241. require.Nil(t, err)
  242. pkCompL := SwapEndianness(pkCompB)
  243. err = pkComp.UnmarshalText([]byte(hex.EncodeToString(pkCompL)))
  244. require.Nil(t, err)
  245. // Data from the compatibility test
  246. require.Nil(t, err)
  247. pk, err := pkComp.Decompress()
  248. require.Nil(t, err)
  249. l1Tx := &L1Tx{
  250. TokenID: 231,
  251. FromBJJ: pk,
  252. }
  253. encodeData, err := l1Tx.BytesCoordinatorTx(signature)
  254. require.Nil(t, err)
  255. expected, err := utils.HexDecode("1b186d7122ff7f654cfed3156719774898d573900c86599a885a706dbdffe5ea8cda71e5eb097e115405d84d1e7b464009b434b32c014a2df502d1f065ced8bc3ba2c2807ee39c3b3378738cff85a46a9465bb8fcf44ea597c33da9719be7c259c000000e7")
  256. require.Nil(t, err)
  257. assert.Equal(t, expected, encodeData)
  258. }