-
Notifications
You must be signed in to change notification settings - Fork 19
/
hd_key_ring.go
80 lines (70 loc) · 2.06 KB
/
hd_key_ring.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package wallet
import (
"sort"
)
type HDKeyRing struct {
keys map[string]HDKeyPair
nextIndex uint32
}
func NewHDKeyRing() *HDKeyRing {
return &HDKeyRing{
keys: map[string]HDKeyPair{},
nextIndex: 1,
}
}
func LoadHDKeyRing(keyPairs []HDKeyPair) *HDKeyRing {
keyRing := NewHDKeyRing()
for _, keyPair := range keyPairs {
keyRing.Upsert(keyPair)
}
return keyRing
}
func (r *HDKeyRing) FindPair(pubKey string) (HDKeyPair, bool) {
keyPair, ok := r.keys[pubKey]
return keyPair, ok
}
func (r *HDKeyRing) Upsert(keyPair HDKeyPair) {
r.keys[keyPair.PublicKey()] = keyPair
if r.nextIndex <= keyPair.Index() {
r.nextIndex = keyPair.Index() + 1
}
}
// ListPublicKeys returns the list of public keys sorted by key index.
func (r *HDKeyRing) ListPublicKeys() []HDPublicKey {
sortedKeyPairs := r.ListKeyPairs()
pubKeys := make([]HDPublicKey, len(r.keys))
for i, keyPair := range sortedKeyPairs {
pubKeys[i] = keyPair.ToPublicKey()
}
return pubKeys
}
func (r *HDKeyRing) NextIndex() uint32 {
return r.nextIndex
}
// ListKeyPairs returns the list of key pairs sorted by key index.
func (r *HDKeyRing) ListKeyPairs() []HDKeyPair {
keysList := make([]HDKeyPair, len(r.keys))
i := 0
for _, key := range r.keys {
keysList[i] = key
i++
}
sort.SliceStable(keysList, func(i, j int) bool {
return keysList[i].Index() < keysList[j].Index()
})
return keysList
}