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.

41 lines
1.3 KiB

  1. package common
  2. import (
  3. "encoding/hex"
  4. "testing"
  5. ethCommon "github.com/ethereum/go-ethereum/common"
  6. ethCrypto "github.com/ethereum/go-ethereum/crypto"
  7. "github.com/iden3/go-iden3-crypto/babyjub"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestAccountCreationAuth(t *testing.T) {
  12. // Ethereum key
  13. ethSk, err := ethCrypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
  14. require.NoError(t, err)
  15. ethAddr := ethCrypto.PubkeyToAddress(ethSk.PublicKey)
  16. // BabyJubJub key
  17. var sk babyjub.PrivateKey
  18. _, err = hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  19. assert.NoError(t, err)
  20. chainID := uint16(0)
  21. hermezContractAddr := ethCommon.HexToAddress("0xc344E203a046Da13b0B4467EB7B3629D0C99F6E6")
  22. a := AccountCreationAuth{
  23. EthAddr: ethAddr,
  24. BJJ: sk.Public().Compress(),
  25. }
  26. msg, err := a.HashToSign(chainID, hermezContractAddr)
  27. assert.NoError(t, err)
  28. assert.Equal(t, "cb5a7e44329ff430c81fec49fb2ac6741f02d5ec96cbcb618a6991f0a9c80ffd", hex.EncodeToString(msg))
  29. // sign AccountCreationAuth with eth key
  30. sig, err := ethCrypto.Sign(msg, ethSk)
  31. assert.NoError(t, err)
  32. a.Signature = sig
  33. assert.True(t, a.VerifySignature(chainID, hermezContractAddr))
  34. }