mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 19:36:44 +01:00
Refactor account creation auth endpoints
This commit is contained in:
68
api/accountcreationauths.go
Normal file
68
api/accountcreationauths.go
Normal 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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user