Skip to content

Commit

Permalink
fix!: unsorted addresses in wallet listing (#721)
Browse files Browse the repository at this point in the history
Co-authored-by: b00f <[email protected]>
  • Loading branch information
amirvalhalla and b00f authored Oct 3, 2023
1 parent ce731b2 commit f2d52c9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
6 changes: 5 additions & 1 deletion wallet/addresspath/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewPathFromString(str string) (Path, error) {
if sub[0] != "m" {
return nil, ErrInvalidPath
}
path := []uint32{}
var path []uint32
for i := 1; i < len(sub); i++ {
indexStr := sub[i]
added := uint32(0)
Expand Down Expand Up @@ -55,3 +55,7 @@ func (p Path) String() string {
func (p Path) LastIndex() uint32 {
return p[len(p)-1]
}

func (p Path) AddressType() uint32 {
return p[len(p)-2] - HardenedKeyStart
}
2 changes: 1 addition & 1 deletion wallet/addresspath/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestStringToPath(t *testing.T) {
wantPath Path
wantErr error
}{
{"m", Path{}, nil},
{"m", nil, nil},
{"m/0", Path{0}, nil},
{"m/0/1", Path{0, 1}, nil},
{"m/0/1/1000000000", Path{0, 1, 1000000000}, nil},
Expand Down
39 changes: 34 additions & 5 deletions wallet/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vault

import (
"encoding/json"
"sort"
"strings"

"golang.org/x/exp/slices"
Expand Down Expand Up @@ -205,16 +206,43 @@ func (v *Vault) SetLabel(addr, label string) error {
}

func (v *Vault) AddressInfos() []AddressInfo {
importedAddrs := make([]AddressInfo, 0)
addrs := make([]AddressInfo, 0, v.AddressCount())
addrsMap := make(map[int][]AddressInfo)

for _, info := range v.Addresses {
addrs = append(addrs, info)
if strings.TrimSpace(info.Path) == "" {
importedAddrs = append(importedAddrs, info)
continue
}

path, _ := addresspath.NewPathFromString(info.Path)
addressType := path.AddressType()
addrsMap[int(addressType)] = append(addrsMap[int(addressType)], info)
}

slices.SortFunc(addrs, func(a, b AddressInfo) int {
return strings.Compare(a.Path, b.Path)
})
keys := make([]int, 0)
for key := range addrsMap {
keys = append(keys, key)
}

sort.Ints(keys)
for _, key := range keys {
addrsValue := addrsMap[key]
slices.SortFunc(addrsValue, func(a, b AddressInfo) int {
pathA, _ := addresspath.NewPathFromString(a.Path)
pathB, _ := addresspath.NewPathFromString(b.Path)

if pathA.LastIndex() < pathB.LastIndex() {
return -1
}
return 1
})

addrs = append(addrs, addrsValue...)
}

addrs = append(addrs, importedAddrs...)
return addrs
}

Expand Down Expand Up @@ -385,7 +413,8 @@ func (v *Vault) NewValidatorAddress(label string) (string, error) {
return addr, nil
}

// TODO change structure of AddressInfo to more informativelay object
// TODO change structure of AddressInfo to more informatively object

// AddressInfo like it can return bls.PublicKey instead of string.
func (v *Vault) AddressInfo(addr string) *AddressInfo {
info, ok := v.Addresses[addr]
Expand Down

0 comments on commit f2d52c9

Please sign in to comment.