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.

217 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. }
  101. expected, err := utils.HexDecode("85dab5b9e2e361d0c208d77be90efcc0439b0a530dd02deb2c81068e7a0f7e327df80b4ab79ee1f41a7def613e73a20c32eece5a000001c638db8be880f00020039c0000053cb88d")
  102. require.Nil(t, err)
  103. encodedData, err := l1Tx.BytesUser()
  104. require.Nil(t, err)
  105. assert.Equal(t, expected, encodedData)
  106. }
  107. func TestL1CoordinatorTxByteParsers(t *testing.T) {
  108. privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
  109. require.Nil(t, err)
  110. publicKey := privateKey.Public()
  111. publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
  112. if !ok {
  113. log.Fatal("error casting public key to ECDSA")
  114. }
  115. publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA)
  116. pubKey, err := crypto.UnmarshalPubkey(publicKeyBytes)
  117. require.Nil(t, err)
  118. fromEthAddr := crypto.PubkeyToAddress(*pubKey)
  119. var pkComp babyjub.PublicKeyComp
  120. pkCompL := []byte("56ca90f80d7c374ae7485e9bcc47d4ac399460948da6aeeb899311097925a72c")
  121. err = pkComp.UnmarshalText(pkCompL)
  122. require.Nil(t, err)
  123. pk, err := pkComp.Decompress()
  124. require.Nil(t, err)
  125. bytesMessage1 := []byte("\x19Ethereum Signed Message:\n98")
  126. bytesMessage2 := []byte("I authorize this babyjubjub key for hermez rollup account creation")
  127. babyjub := pk.Compress()
  128. babyjubB := SwapEndianness(babyjub[:])
  129. var data []byte
  130. data = append(data, bytesMessage1...)
  131. data = append(data, bytesMessage2...)
  132. data = append(data, babyjubB[:]...)
  133. hash := crypto.Keccak256Hash(data)
  134. signature, err := crypto.Sign(hash.Bytes(), privateKey)
  135. require.Nil(t, err)
  136. // Ethereum adds 27 to v
  137. v := int(signature[64])
  138. signature[64] = byte(v + 27)
  139. l1Tx := &L1Tx{
  140. TokenID: 231,
  141. FromBJJ: pk,
  142. FromEthAddr: fromEthAddr,
  143. }
  144. bytesCoordinatorL1, err := l1Tx.BytesCoordinatorTx(signature)
  145. require.Nil(t, err)
  146. l1txDecoded, err := L1CoordinatorTxFromBytes(bytesCoordinatorL1)
  147. require.Nil(t, err)
  148. assert.Equal(t, l1Tx, l1txDecoded)
  149. bytesCoordinatorL12, err := l1txDecoded.BytesCoordinatorTx(signature)
  150. require.Nil(t, err)
  151. assert.Equal(t, bytesCoordinatorL1, bytesCoordinatorL12)
  152. // expect error if length!=68
  153. _, err = L1CoordinatorTxFromBytes(bytesCoordinatorL1[:66])
  154. require.NotNil(t, err)
  155. _, err = L1CoordinatorTxFromBytes([]byte{})
  156. require.NotNil(t, err)
  157. _, err = L1CoordinatorTxFromBytes(nil)
  158. require.NotNil(t, err)
  159. }
  160. func TestL1CoordinatorTxByteParsersCompatibility(t *testing.T) {
  161. // Data from compatibility test
  162. var signature []byte
  163. r, err := hex.DecodeString("da71e5eb097e115405d84d1e7b464009b434b32c014a2df502d1f065ced8bc3b")
  164. require.Nil(t, err)
  165. s, err := hex.DecodeString("186d7122ff7f654cfed3156719774898d573900c86599a885a706dbdffe5ea8c")
  166. require.Nil(t, err)
  167. v, err := hex.DecodeString("1b")
  168. require.Nil(t, err)
  169. signature = append(signature, r[:]...)
  170. signature = append(signature, s[:]...)
  171. signature = append(signature, v[:]...)
  172. var pkComp babyjub.PublicKeyComp
  173. pkCompB, err := hex.DecodeString("a2c2807ee39c3b3378738cff85a46a9465bb8fcf44ea597c33da9719be7c259c")
  174. require.Nil(t, err)
  175. pkCompL := SwapEndianness(pkCompB)
  176. err = pkComp.UnmarshalText([]byte(hex.EncodeToString(pkCompL)))
  177. require.Nil(t, err)
  178. // Data from the compatibility test
  179. require.Nil(t, err)
  180. pk, err := pkComp.Decompress()
  181. require.Nil(t, err)
  182. l1Tx := &L1Tx{
  183. TokenID: 231,
  184. FromBJJ: pk,
  185. }
  186. encodeData, err := l1Tx.BytesCoordinatorTx(signature)
  187. require.Nil(t, err)
  188. expected, err := utils.HexDecode("1b186d7122ff7f654cfed3156719774898d573900c86599a885a706dbdffe5ea8cda71e5eb097e115405d84d1e7b464009b434b32c014a2df502d1f065ced8bc3ba2c2807ee39c3b3378738cff85a46a9465bb8fcf44ea597c33da9719be7c259c000000e7")
  189. require.Nil(t, err)
  190. assert.Equal(t, expected, encodeData)
  191. }