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.

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