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.

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