diff --git a/node-registrar/client/account.go b/node-registrar/client/account.go index 808fc52..8ffd862 100644 --- a/node-registrar/client/account.go +++ b/node-registrar/client/account.go @@ -38,6 +38,7 @@ func (c *RegistrarClient) UpdateAccount(opts ...UpdateAccountOpts) (err error) { type accountCfg struct { relays []string rmbEncKey string + publicKey string } type ( @@ -58,6 +59,12 @@ func UpdateAccountWithRMBEncKey(rmbEncKey string) UpdateAccountOpts { } } +func UpdateAccountWithPublicKey(publicKey string) UpdateAccountOpts { + return func(n *accountCfg) { + n.publicKey = publicKey + } +} + // EnsureAccount ensures that an account is created with specific seed/mnemonic. func (c *RegistrarClient) EnsureAccount(relays []string, rmbEncKey string) (account Account, err error) { return c.ensureAccount(relays, rmbEncKey) @@ -276,6 +283,7 @@ func parseUpdateAccountOpts(opts []UpdateAccountOpts) map[string]any { cfg := accountCfg{ rmbEncKey: "", relays: []string{}, + publicKey: "", } for _, opt := range opts { @@ -292,5 +300,9 @@ func parseUpdateAccountOpts(opts []UpdateAccountOpts) map[string]any { data["rmb_enc_key"] = cfg.rmbEncKey } + if len(cfg.publicKey) != 0 { + data["public_key"] = cfg.publicKey + } + return data } diff --git a/node-registrar/pkg/db/accounts.go b/node-registrar/pkg/db/accounts.go index c78dba7..1545307 100644 --- a/node-registrar/pkg/db/accounts.go +++ b/node-registrar/pkg/db/accounts.go @@ -17,11 +17,19 @@ func (db *Database) CreateAccount(account *Account) error { } // UpdateAccount updates an account's relays and RMB encryption key -func (db *Database) UpdateAccount(twinID uint64, relays pq.StringArray, rmbEncKey string) error { - result := db.gormDB.Model(&Account{}).Where("twin_id = ?", twinID).Updates(map[string]interface{}{ - "relays": relays, - "rmb_enc_key": rmbEncKey, - }) +func (db *Database) UpdateAccount(twinID uint64, relays pq.StringArray, rmbEncKey string, publicKey string) error { + update := map[string]interface{}{} + if len(relays) != 0 { + update["relays"] = relays + } + if rmbEncKey != "" { + update["rmb_enc_key"] = rmbEncKey + } + if publicKey != "" { + update["public_key"] = publicKey + } + + result := db.gormDB.Model(&Account{}).Where("twin_id = ?", twinID).Updates(update) if result.Error != nil { return result.Error } diff --git a/node-registrar/pkg/server/handlers.go b/node-registrar/pkg/server/handlers.go index d4cc2e2..f49daea 100644 --- a/node-registrar/pkg/server/handlers.go +++ b/node-registrar/pkg/server/handlers.go @@ -582,6 +582,7 @@ func verifySignature(publicKey, chalange, signature []byte) (bool, error) { type UpdateAccountRequest struct { Relays pq.StringArray `json:"relays"` RMBEncKey string `json:"rmb_enc_key"` + PubKey string `json:"public_key,omitempty"` } // updateAccountHandler updates an account's relays and RMB encryption key @@ -616,7 +617,12 @@ func (s *Server) updateAccountHandler(c *gin.Context) { return } - err = s.db.UpdateAccount(twinID, req.Relays, req.RMBEncKey) + if req.PubKey != "" && isValidPublicKey(req.PubKey) { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "invalid Public Key format"}) + return + } + + err = s.db.UpdateAccount(twinID, req.Relays, req.RMBEncKey, req.PubKey) if err != nil { if errors.Is(err, db.ErrRecordNotFound) { c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": "account not found"}) @@ -625,7 +631,6 @@ func (s *Server) updateAccountHandler(c *gin.Context) { c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "failed to update account"}) return } - c.JSON(http.StatusOK, gin.H{"message": "account updated successfully"}) }