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.

242 lines
7.4 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. chainID := big.NewInt(1337)
  125. chainIDBytes := ethCommon.LeftPadBytes(chainID.Bytes(), 2)
  126. privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
  127. require.Nil(t, err)
  128. publicKey := privateKey.Public()
  129. publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
  130. if !ok {
  131. log.Fatal("error casting public key to ECDSA")
  132. }
  133. publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
  134. pubKey, err := crypto.UnmarshalPubkey(publicKeyBytes)
  135. require.Nil(t, err)
  136. fromEthAddr := crypto.PubkeyToAddress(*pubKey)
  137. var pkComp babyjub.PublicKeyComp
  138. pkCompL := []byte("56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c")
  139. err = pkComp.UnmarshalText(pkCompL)
  140. require.Nil(t, err)
  141. pk, err := pkComp.Decompress()
  142. require.Nil(t, err)
  143. bytesMessage1 := []byte("\x19Ethereum Signed Message:\n120")
  144. bytesMessage2 := []byte("I authorize this babyjubjub key for hermez rollup account creation")
  145. babyjub := pk.Compress()
  146. babyjubB := SwapEndianness(babyjub[:])
  147. var data []byte
  148. data = append(data, bytesMessage1...)
  149. data = append(data, bytesMessage2...)
  150. data = append(data, babyjubB[:]...)
  151. data = append(data, chainIDBytes...)
  152. data = append(data, hermezAddress.Bytes()...)
  153. hash := crypto.Keccak256Hash(data)
  154. signature, err := crypto.Sign(hash.Bytes(), privateKey)
  155. require.Nil(t, err)
  156. // Ethereum adds 27 to v
  157. v := int(signature[64])
  158. signature[64] = byte(v + 27)
  159. l1Tx := &L1Tx{
  160. TokenID: 231,
  161. FromBJJ: pk,
  162. FromEthAddr: fromEthAddr,
  163. Amount: big.NewInt(0),
  164. LoadAmount: big.NewInt(0),
  165. }
  166. bytesCoordinatorL1, err := l1Tx.BytesCoordinatorTx(signature)
  167. require.Nil(t, err)
  168. l1txDecoded, err := L1CoordinatorTxFromBytes(bytesCoordinatorL1, chainID, hermezAddress)
  169. require.Nil(t, err)
  170. assert.Equal(t, l1Tx, l1txDecoded)
  171. bytesCoordinatorL12, err := l1txDecoded.BytesCoordinatorTx(signature)
  172. require.Nil(t, err)
  173. assert.Equal(t, bytesCoordinatorL1, bytesCoordinatorL12)
  174. // expect error if length!=68
  175. _, err = L1CoordinatorTxFromBytes(bytesCoordinatorL1[:66], chainID, hermezAddress)
  176. require.NotNil(t, err)
  177. _, err = L1CoordinatorTxFromBytes([]byte{}, chainID, hermezAddress)
  178. require.NotNil(t, err)
  179. _, err = L1CoordinatorTxFromBytes(nil, chainID, hermezAddress)
  180. require.NotNil(t, err)
  181. }
  182. func TestL1CoordinatorTxByteParsersCompatibility(t *testing.T) {
  183. // Data from compatibility test
  184. var signature []byte
  185. r, err := hex.DecodeString("da71e5eb097e115405d84d1e7b464009b434b32c014a2df502d1f065ced8bc3b")
  186. require.Nil(t, err)
  187. s, err := hex.DecodeString("186d7122ff7f654cfed3156719774898d573900c86599a885a706dbdffe5ea8c")
  188. require.Nil(t, err)
  189. v, err := hex.DecodeString("1b")
  190. require.Nil(t, err)
  191. signature = append(signature, r[:]...)
  192. signature = append(signature, s[:]...)
  193. signature = append(signature, v[:]...)
  194. var pkComp babyjub.PublicKeyComp
  195. pkCompB, err := hex.DecodeString("a2c2807ee39c3b3378738cff85a46a9465bb8fcf44ea597c33da9719be7c259c")
  196. require.Nil(t, err)
  197. pkCompL := SwapEndianness(pkCompB)
  198. err = pkComp.UnmarshalText([]byte(hex.EncodeToString(pkCompL)))
  199. require.Nil(t, err)
  200. // Data from the compatibility test
  201. require.Nil(t, err)
  202. pk, err := pkComp.Decompress()
  203. require.Nil(t, err)
  204. l1Tx := &L1Tx{
  205. TokenID: 231,
  206. FromBJJ: pk,
  207. }
  208. encodeData, err := l1Tx.BytesCoordinatorTx(signature)
  209. require.Nil(t, err)
  210. expected, err := utils.HexDecode("1b186d7122ff7f654cfed3156719774898d573900c86599a885a706dbdffe5ea8cda71e5eb097e115405d84d1e7b464009b434b32c014a2df502d1f065ced8bc3ba2c2807ee39c3b3378738cff85a46a9465bb8fcf44ea597c33da9719be7c259c000000e7")
  211. require.Nil(t, err)
  212. assert.Equal(t, expected, encodeData)
  213. }