/
configuration.go
242 lines (217 loc) · 8.43 KB
/
configuration.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// 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 signing
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"sort"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcutil/hdkeychain"
"github.com/digitalbitbox/bitbox-wallet-app/util/errp"
"github.com/digitalbitbox/bitbox-wallet-app/util/jsonp"
)
// Configuration models a signing configuration, which can be singlesig, multisig or address based.
type Configuration struct {
scriptType ScriptType // Only used in btc and ltc, dummy for eth
absoluteKeypath AbsoluteKeypath
extendedPublicKeys []*hdkeychain.ExtendedKey // Should be empty for address based watch only accounts
signingThreshold int // TODO Multisig Only
address string // For address based accounts only
}
// NewConfiguration creates a new configuration. At the moment, multisig is a predefined
// multisig-P2SH script, and is active if there are more than one xpubs. Otherwise, it's single sig
// and `scriptType` defines the type of script.
func NewConfiguration(
scriptType ScriptType,
absoluteKeypath AbsoluteKeypath,
extendedPublicKeys []*hdkeychain.ExtendedKey,
address string,
signingThreshold int,
) *Configuration {
if len(extendedPublicKeys) == 0 && len(address) == 0 {
panic("A configuration has to contain at least one extended public key or an address")
}
for _, extendedKey := range extendedPublicKeys {
if extendedKey.IsPrivate() {
panic("An extended key is private! Only extended public keys are accepted.")
}
}
return &Configuration{
scriptType: scriptType,
absoluteKeypath: absoluteKeypath,
extendedPublicKeys: extendedPublicKeys,
address: address,
signingThreshold: signingThreshold,
}
}
// NewSinglesigConfiguration creates a new singlesig configuration.
func NewSinglesigConfiguration(
scriptType ScriptType,
absoluteKeypath AbsoluteKeypath,
extendedPublicKey *hdkeychain.ExtendedKey,
) *Configuration {
return NewConfiguration(
scriptType, absoluteKeypath, []*hdkeychain.ExtendedKey{extendedPublicKey}, "", 1)
}
// NewAddressConfiguration creates a new account address configuration
func NewAddressConfiguration(
scriptType ScriptType,
absoluteKeypath AbsoluteKeypath,
address string,
) *Configuration {
return NewConfiguration(
scriptType, absoluteKeypath, []*hdkeychain.ExtendedKey{}, address, 1)
}
// ScriptType returns the configuration's keypath.
func (configuration *Configuration) ScriptType() ScriptType {
if configuration.Multisig() {
panic("scriptType is only defined for single sig")
}
return configuration.scriptType
}
// AbsoluteKeypath returns the configuration's keypath.
func (configuration *Configuration) AbsoluteKeypath() AbsoluteKeypath {
return configuration.absoluteKeypath
}
// ExtendedPublicKeys returns the configuration's extended public keys.
func (configuration *Configuration) ExtendedPublicKeys() []*hdkeychain.ExtendedKey {
return configuration.extendedPublicKeys
}
// Address returns the configuration's address
func (configuration *Configuration) Address() string {
return configuration.address
}
// IsAddressBased returns whether configuration is address based or not
func (configuration *Configuration) IsAddressBased() bool {
return configuration.address != "" && len(configuration.ExtendedPublicKeys()) == 0
}
// PublicKeys returns the configuration's public keys.
func (configuration *Configuration) PublicKeys() []*btcec.PublicKey {
publicKeys := make([]*btcec.PublicKey, configuration.NumberOfSigners())
for index, extendedPublicKey := range configuration.ExtendedPublicKeys() {
var err error
publicKeys[index], err = extendedPublicKey.ECPubKey()
if err != nil {
panic("Failed to convert an extended public key to a normal public key.")
}
}
return publicKeys
}
// SortedPublicKeys returns the configuration's public keys sorted in compressed form.
func (configuration *Configuration) SortedPublicKeys() []*btcec.PublicKey {
publicKeys := configuration.PublicKeys()
sort.Slice(publicKeys, func(i, j int) bool {
return bytes.Compare(
publicKeys[i].SerializeCompressed(),
publicKeys[j].SerializeCompressed(),
) < 0
})
return publicKeys
}
// SigningThreshold returns the signing threshold in case of a multisig config.
func (configuration *Configuration) SigningThreshold() int {
return configuration.signingThreshold
}
// NumberOfSigners returns the number of signers (1 in single sig, N in a M/N multisig).
func (configuration *Configuration) NumberOfSigners() int {
return len(configuration.extendedPublicKeys)
}
// Singlesig returns whether this is a singlesig configuration.
func (configuration *Configuration) Singlesig() bool {
return len(configuration.extendedPublicKeys) == 1
}
// Multisig returns whether this is a multisig configuration.
func (configuration *Configuration) Multisig() bool {
return len(configuration.extendedPublicKeys) > 1
}
// Derive derives a subkeypath from the configuration's base absolute keypath.
func (configuration *Configuration) Derive(relativeKeypath RelativeKeypath) (*Configuration, error) {
if relativeKeypath.Hardened() {
return nil, errp.New("A configuration can only be derived with a non-hardened relative keypath.")
}
derivedPublicKeys := make([]*hdkeychain.ExtendedKey, configuration.NumberOfSigners())
for index, extendedPublicKey := range configuration.extendedPublicKeys {
derivedPublicKey, err := relativeKeypath.Derive(extendedPublicKey)
if err != nil {
return nil, err
}
derivedPublicKeys[index] = derivedPublicKey
}
return &Configuration{
address: configuration.address,
scriptType: configuration.scriptType,
absoluteKeypath: configuration.absoluteKeypath.Append(relativeKeypath),
extendedPublicKeys: derivedPublicKeys,
signingThreshold: configuration.signingThreshold,
}, nil
}
type configurationEncoding struct {
ScriptType string `json:"scriptType"`
Keypath AbsoluteKeypath `json:"keypath"`
Threshold int `json:"threshold"`
Xpubs []string `json:"xpubs"`
Address string `json:"address"`
}
// MarshalJSON implements json.Marshaler.
func (configuration Configuration) MarshalJSON() ([]byte, error) {
length := configuration.NumberOfSigners()
xpubs := make([]string, length)
for i := 0; i < length; i++ {
xpubs[i] = configuration.extendedPublicKeys[i].String()
}
return json.Marshal(&configurationEncoding{
ScriptType: string(configuration.scriptType),
Keypath: configuration.absoluteKeypath,
Threshold: configuration.signingThreshold,
Xpubs: xpubs,
Address: configuration.address,
})
}
// UnmarshalJSON implements json.Unmarshaler.
func (configuration *Configuration) UnmarshalJSON(bytes []byte) error {
var encoding configurationEncoding
if err := json.Unmarshal(bytes, &encoding); err != nil {
return errp.Wrap(err, "Could not unmarshal a signing configuration.")
}
configuration.scriptType = ScriptType(encoding.ScriptType)
configuration.absoluteKeypath = encoding.Keypath
configuration.signingThreshold = encoding.Threshold
length := len(encoding.Xpubs)
configuration.extendedPublicKeys = make([]*hdkeychain.ExtendedKey, length)
configuration.address = encoding.Address
for i := 0; i < length; i++ {
var err error
configuration.extendedPublicKeys[i], err = hdkeychain.NewKeyFromString(encoding.Xpubs[i])
if err != nil {
return errp.Wrap(err, "Could not read an extended public key.")
}
}
return nil
}
// Hash returns a hash of the configuration.
func (configuration *Configuration) Hash() string {
hash := sha256.Sum256(jsonp.MustMarshal(configuration))
return hex.EncodeToString(hash[:])
}
// String returns a short summary of the configuration to be used in logs, etc.
func (configuration *Configuration) String() string {
if configuration.Multisig() {
return fmt.Sprintf("multisig, %d/%d",
configuration.SigningThreshold(), configuration.NumberOfSigners())
}
return fmt.Sprintf("single sig, scriptType: %s", configuration.scriptType)
}