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.

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