Refactor account creation auth endpoints

This commit is contained in:
Arnau B
2020-10-29 19:13:14 +01:00
parent 8c6f5f2950
commit 8c9d13ffa5
9 changed files with 316 additions and 165 deletions

View File

@@ -0,0 +1,68 @@
package api
import (
"errors"
"net/http"
"time"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/gin-gonic/gin"
"github.com/hermeznetwork/hermez-node/apitypes"
"github.com/hermeznetwork/hermez-node/common"
"github.com/iden3/go-iden3-crypto/babyjub"
)
func postAccountCreationAuth(c *gin.Context) {
// Parse body
var apiAuth receivedAuth
if err := c.ShouldBindJSON(&apiAuth); err != nil {
retBadReq(err, c)
return
}
// API to common + verify signature
commonAuth := accountCreationAuthAPIToCommon(&apiAuth)
if !commonAuth.VerifySignature() {
retBadReq(errors.New("invalid signature"), c)
return
}
// Insert to DB
if err := l2.AddAccountCreationAuth(commonAuth); err != nil {
retSQLErr(err, c)
return
}
// Return OK
c.Status(http.StatusOK)
}
func getAccountCreationAuth(c *gin.Context) {
// Get hezEthereumAddress
addr, err := parseParamHezEthAddr(c)
if err != nil {
retBadReq(err, c)
return
}
// Fetch auth from l2DB
auth, err := l2.GetAccountCreationAuthAPI(*addr)
if err != nil {
retSQLErr(err, c)
return
}
// Build succesfull response
c.JSON(http.StatusOK, auth)
}
type receivedAuth struct {
EthAddr apitypes.StrHezEthAddr `json:"hezEthereumAddress" binding:"required"`
BJJ apitypes.StrHezBJJ `json:"bjj" binding:"required"`
Signature apitypes.StrEthSignature `json:"signature" binding:"required"`
Timestamp time.Time `json:"timestamp"`
}
func accountCreationAuthAPIToCommon(apiAuth *receivedAuth) *common.AccountCreationAuth {
return &common.AccountCreationAuth{
EthAddr: ethCommon.Address(apiAuth.EthAddr),
BJJ: (*babyjub.PublicKey)(&apiAuth.BJJ),
Signature: []byte(apiAuth.Signature),
Timestamp: apiAuth.Timestamp,
}
}