Skip to content

Commit

Permalink
chore: In gnoNativeService.userAccounts, use bech32 for the key (#171)
Browse files Browse the repository at this point in the history
`gnoNativeService.userAccounts` is a map used to track all the user
accounts which have been selected, including their password. Currently,
the map key is either the [account name or the bech32
address](https://github.com/gnolang/gnonative/blob/44852235c93c78ff30680e16ec0665dddca987d8/service/api.go#L235).
We want to rely on `gnoNativeService.userAccounts` where we only know
the account address (not the name). For example, to sign a transaction
with any key that has been selected, known only by its address. This PR
standardizes the key of `gnoNativeService.userAccounts` to only be the
bech32 address:

* In `SelectAccount`, store the account info in
`gnoNativeService.userAccounts` using the bech32. (This is backwards
compatible, even if the application calls `SelectAccount` to select by
name. No changes needed to the app.)
* In `DeleteAccount`, first fetch the key from the Keybase so that we
know its address. When checking if the deleted account was the active
account, use the bech32. (This is backwards compatible, even if the
application calls `DeleteAccount` to delete by name.)

Signed-off-by: Jeff Thompson <[email protected]>
  • Loading branch information
jefft0 authored Aug 28, 2024
1 parent 4485223 commit efc2071
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions service/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,12 @@ func (s *gnoNativeService) SelectAccount(ctx context.Context, req *connect.Reque
return nil, err
}

bech32 := crypto.AddressToBech32(key.GetAddress())
s.lock.Lock()
account, ok := s.userAccounts[req.Msg.NameOrBech32]
account, ok := s.userAccounts[bech32]
if !ok {
account = &userAccount{}
s.userAccounts[req.Msg.NameOrBech32] = account
s.userAccounts[bech32] = account
}
account.keyInfo = key
s.activeAccount = account
Expand Down Expand Up @@ -343,14 +344,19 @@ func (s *gnoNativeService) QueryAccount(ctx context.Context, req *connect.Reques
}

func (s *gnoNativeService) DeleteAccount(ctx context.Context, req *connect.Request[api_gen.DeleteAccountRequest]) (*connect.Response[api_gen.DeleteAccountResponse], error) {
// Get the key from the Keybase so that we know its address
key, err := s.getSigner().Keybase.GetByNameOrAddress(req.Msg.NameOrBech32)
if err != nil {
return nil, getGrpcError(err)
}
if err := s.getSigner().Keybase.Delete(req.Msg.NameOrBech32, req.Msg.Password, req.Msg.SkipPassword); err != nil {
return nil, getGrpcError(err)
}

bech32 := crypto.AddressToBech32(key.GetAddress())
s.lock.Lock()
delete(s.userAccounts, req.Msg.NameOrBech32)
if s.activeAccount != nil &&
(s.activeAccount.keyInfo.GetName() == req.Msg.NameOrBech32 || crypto.AddressToBech32(s.activeAccount.keyInfo.GetAddress()) == req.Msg.NameOrBech32) {
delete(s.userAccounts, bech32)
if s.activeAccount != nil && crypto.AddressToBech32(s.activeAccount.keyInfo.GetAddress()) == bech32 {
// The deleted account was the active account.
s.activeAccount = nil
}
Expand Down
2 changes: 1 addition & 1 deletion service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type gnoNativeService struct {
useGnokeyMobile bool
gnokeyMobileClient gnokey_mobile_goconnect.GnokeyMobileServiceClient

// Map of key name to userAccount.
// Map of key bech32 to userAccount.
userAccounts map[string]*userAccount
// The active account in userAccounts, or nil if none
activeAccount *userAccount
Expand Down

0 comments on commit efc2071

Please sign in to comment.