Skip to content

Add support for updating public key #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions node-registrar/client/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (c *RegistrarClient) UpdateAccount(opts ...UpdateAccountOpts) (err error) {
type accountCfg struct {
relays []string
rmbEncKey string
publicKey string
}

type (
Expand All @@ -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)
Expand Down Expand Up @@ -276,6 +283,7 @@ func parseUpdateAccountOpts(opts []UpdateAccountOpts) map[string]any {
cfg := accountCfg{
rmbEncKey: "",
relays: []string{},
publicKey: "",
}

for _, opt := range opts {
Expand All @@ -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
}
18 changes: 13 additions & 5 deletions node-registrar/pkg/db/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 7 additions & 2 deletions node-registrar/pkg/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"})
Expand All @@ -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"})
}

Expand Down
Loading