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.

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