-
Notifications
You must be signed in to change notification settings - Fork 80
/
keystores.go
155 lines (142 loc) · 5.06 KB
/
keystores.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2018 Shift Devices AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package keystore
import (
"github.com/btcsuite/btcutil/hdkeychain"
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/coin"
coinpkg "github.com/digitalbitbox/bitbox-wallet-app/backend/coins/coin"
"github.com/digitalbitbox/bitbox-wallet-app/backend/signing"
"github.com/digitalbitbox/bitbox-wallet-app/util/errp"
)
// Keystores models a collection of keystores that can be passed to accounts to perform signing
// operations.
type Keystores struct {
keystores []Keystore
}
// NewKeystores returns a collection of the given keystores.
func NewKeystores(keystores ...Keystore) *Keystores {
return &Keystores{
keystores: keystores,
}
}
// Count returns the number of keystores in the collection.
func (keystores *Keystores) Count() int {
return len(keystores.keystores)
}
// Add adds the given keystore to the collection of keystores.
func (keystores *Keystores) Add(keystore Keystore) error {
for _, element := range keystores.keystores {
if element == keystore {
return errp.New("The collection already contains the given keystore.")
}
}
keystores.keystores = append(keystores.keystores, keystore)
return nil
}
// Remove removes the given keystore from the collection of keystores.
func (keystores *Keystores) Remove(keystore Keystore) error {
for index, element := range keystores.keystores {
if element == keystore {
indexOfLastElement := len(keystores.keystores) - 1
keystores.keystores[index] = keystores.keystores[indexOfLastElement]
keystores.keystores[indexOfLastElement] = nil // Prevent memory leak
keystores.keystores = keystores.keystores[:indexOfLastElement]
return nil
}
}
return errp.New("The collection does not contain the given keystore.")
}
// CanVerifyAddresses returns whether any of the keystores can verify an address.
func (keystores *Keystores) CanVerifyAddresses(
configuration *signing.Configuration, coin coin.Coin) (bool, bool, error) {
for _, keystore := range keystores.keystores {
canVerifyAddress, optional, err := keystore.CanVerifyAddress(configuration, coin)
if err != nil {
return false, false, err
}
if canVerifyAddress {
return true, optional, nil
}
}
return false, false, nil
}
// VerifyAddress outputs the address for the given coin with the given configuration on all
// keystores that have a secure output.
func (keystores *Keystores) VerifyAddress(
configuration *signing.Configuration,
coin coin.Coin,
) error {
found := false
for _, keystore := range keystores.keystores {
canVerifyAddress, _, err := keystore.CanVerifyAddress(configuration, coin)
if err != nil {
return err
}
if canVerifyAddress {
if err := keystore.VerifyAddress(configuration, coin); err != nil {
return err
}
found = true
}
}
if !found {
return errp.New("There is currently no keystore to securely output the address.")
}
return nil
}
// CanVerifyExtendedPublicKeys returns the indices of the keystores that support secure verification
func (keystores *Keystores) CanVerifyExtendedPublicKeys() []int {
canVerifyExtendedPublicKey := []int{}
for index, keystore := range keystores.keystores {
if keystore.CanVerifyExtendedPublicKey() {
canVerifyExtendedPublicKey = append(canVerifyExtendedPublicKey, index)
}
}
return canVerifyExtendedPublicKey
}
// SignTransaction signs the given proposed transaction on all keystores. Returns ErrSigningAborted
// if the user aborts.
func (keystores *Keystores) SignTransaction(proposedTransaction interface{}) error {
for _, keystore := range keystores.keystores {
if err := keystore.SignTransaction(proposedTransaction); err != nil {
return err
}
}
return nil
}
// Configuration returns the configuration at the given path with the given signing threshold.
func (keystores *Keystores) Configuration(
coin coinpkg.Coin,
scriptType signing.ScriptType,
absoluteKeypath signing.AbsoluteKeypath,
signingThreshold int,
) (*signing.Configuration, error) {
extendedPublicKeys := make([]*hdkeychain.ExtendedKey, len(keystores.keystores))
for index, keystore := range keystores.keystores {
if keystore.CosignerIndex() != index {
return nil, errp.New("The keystores are in the wrong order.")
}
extendedPublicKey, err := keystore.ExtendedPublicKey(coin, absoluteKeypath)
if err != nil {
return nil, err
}
extendedPublicKeys[index] = extendedPublicKey
}
return signing.NewConfiguration(
scriptType, absoluteKeypath, extendedPublicKeys, "", signingThreshold), nil
}
// Keystores returns all keystores.
func (keystores *Keystores) Keystores() []Keystore {
return keystores.keystores
}