mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Impl account creation auth endpoints
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user