You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.8 KiB

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