arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/api/accountcreationauths.go
api/accountcreationauths.goRaw
 1package api
 2
 3import (
 4	"errors"
 5	"net/http"
 6	"time"
 7
 8	ethCommon "github.com/ethereum/go-ethereum/common"
 9	"github.com/gin-gonic/gin"
10	"github.com/hermeznetwork/hermez-node/api/apitypes"
11	"github.com/hermeznetwork/hermez-node/common"
12	"github.com/iden3/go-iden3-crypto/babyjub"
13)
14
15func (a *API) postAccountCreationAuth(c *gin.Context) {
16	// Parse body
17	var apiAuth receivedAuth
18	if err := c.ShouldBindJSON(&apiAuth); err != nil {
19		retBadReq(err, c)
20		return
21	}
22	// API to common + verify signature
23	commonAuth := accountCreationAuthAPIToCommon(&apiAuth)
24	if !commonAuth.VerifySignature(a.cg.ChainID, a.hermezAddress) {
25		retBadReq(errors.New("invalid signature"), c)
26		return
27	}
28	// Insert to DB
29	if err := a.l2.AddAccountCreationAuthAPI(commonAuth); err != nil {
30		retSQLErr(err, c)
31		return
32	}
33	// Return OK
34	c.Status(http.StatusOK)
35}
36
37func (a *API) getAccountCreationAuth(c *gin.Context) {
38	// Get hezEthereumAddress
39	addr, err := parseParamHezEthAddr(c)
40	if err != nil {
41		retBadReq(err, c)
42		return
43	}
44	// Fetch auth from l2DB
45	auth, err := a.l2.GetAccountCreationAuthAPI(*addr)
46	if err != nil {
47		retSQLErr(err, c)
48		return
49	}
50	// Build successful response
51	c.JSON(http.StatusOK, auth)
52}
53
54type receivedAuth struct {
55	EthAddr   apitypes.StrHezEthAddr `json:"hezEthereumAddress" binding:"required"`
56	BJJ       apitypes.StrHezBJJ     `json:"bjj" binding:"required"`
57	Signature apitypes.EthSignature  `json:"signature" binding:"required"`
58	Timestamp time.Time              `json:"timestamp"`
59}
60
61func accountCreationAuthAPIToCommon(apiAuth *receivedAuth) *common.AccountCreationAuth {
62	return &common.AccountCreationAuth{
63		EthAddr:   ethCommon.Address(apiAuth.EthAddr),
64		BJJ:       (babyjub.PublicKeyComp)(apiAuth.BJJ),
65		Signature: []byte(apiAuth.Signature),
66		Timestamp: apiAuth.Timestamp,
67	}
68}