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.

51 lines
1.5 KiB

4 years ago
  1. package common
  2. import (
  3. "time"
  4. ethCommon "github.com/ethereum/go-ethereum/common"
  5. ethCrypto "github.com/ethereum/go-ethereum/crypto"
  6. "github.com/hermeznetwork/tracerr"
  7. "github.com/iden3/go-iden3-crypto/babyjub"
  8. )
  9. // AccountCreationAuth authorizations sent by users to the L2DB, to be used for account creations when necessary
  10. type AccountCreationAuth struct {
  11. EthAddr ethCommon.Address `meddler:"eth_addr"`
  12. BJJ *babyjub.PublicKey `meddler:"bjj"`
  13. Signature []byte `meddler:"signature"`
  14. Timestamp time.Time `meddler:"timestamp,utctime"`
  15. }
  16. // HashToSign builds the hash to be signed using BJJ pub key and the constant message
  17. func (a *AccountCreationAuth) HashToSign() ([]byte, error) {
  18. // Calculate message to be signed
  19. const msg = "I authorize this babyjubjub key for hermez rollup account creation"
  20. comp, err := a.BJJ.Compress().MarshalText()
  21. if err != nil {
  22. return nil, tracerr.Wrap(err)
  23. }
  24. // Hash message (msg || compressed-bjj)
  25. return ethCrypto.Keccak256Hash([]byte(msg), comp).Bytes(), nil
  26. }
  27. // VerifySignature ensures that the Signature is done with the specified EthAddr
  28. func (a *AccountCreationAuth) VerifySignature() bool {
  29. // Calculate hash to be signed
  30. msg, err := a.HashToSign()
  31. if err != nil {
  32. return false
  33. }
  34. // Get public key from Signature
  35. pubKBytes, err := ethCrypto.Ecrecover(msg, a.Signature)
  36. if err != nil {
  37. return false
  38. }
  39. pubK, err := ethCrypto.UnmarshalPubkey(pubKBytes)
  40. if err != nil {
  41. return false
  42. }
  43. // Get addr from pubK
  44. addr := ethCrypto.PubkeyToAddress(*pubK)
  45. return addr == a.EthAddr
  46. }