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