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.

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