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.

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