Impl account creation auth endpoints

This commit is contained in:
Arnau B
2020-10-21 16:40:31 +02:00
parent 721e5e8bf0
commit ede6c33a18
10 changed files with 271 additions and 26 deletions

View File

@@ -4,6 +4,7 @@ import (
"time"
ethCommon "github.com/ethereum/go-ethereum/common"
ethCrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/iden3/go-iden3-crypto/babyjub"
)
@@ -14,3 +15,36 @@ type AccountCreationAuth struct {
Signature []byte `meddler:"signature"`
Timestamp time.Time `meddler:"timestamp,utctime"`
}
// HashToSign builds the hash to be signed using BJJ pub key and the constant message
func (a *AccountCreationAuth) HashToSign() ([]byte, error) {
// Calculate message to be signed
const msg = "I authorize this babyjubjub key for hermez rollup account creation"
comp, err := a.BJJ.Compress().MarshalText()
if err != nil {
return nil, err
}
// Hash message (msg || compressed-bjj)
return ethCrypto.Keccak256Hash([]byte(msg), comp).Bytes(), nil
}
// VerifySignature ensures that the Signature is done with the specified EthAddr
func (a *AccountCreationAuth) VerifySignature() bool {
// Calculate hash to be signed
msg, err := a.HashToSign()
if err != nil {
return false
}
// Get public key from Signature
pubKBytes, err := ethCrypto.Ecrecover(msg, a.Signature)
if err != nil {
return false
}
pubK, err := ethCrypto.UnmarshalPubkey(pubKBytes)
if err != nil {
return false
}
// Get addr from pubK
addr := ethCrypto.PubkeyToAddress(*pubK)
return addr == a.EthAddr
}