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.

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