forked from cosmos/ledger-cosmos-go
-
Notifications
You must be signed in to change notification settings - Fork 4
/
user_app.go
337 lines (284 loc) · 9.67 KB
/
user_app.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*******************************************************************************
* (c) 2018 - 2022 ZondaX 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.
********************************************************************************/
/* THORChain uses same CLA (0x55) as Cosmos app, so okay to use cosmos/ledger-go for transport */
package ledger_thorchain_go
import (
"errors"
"fmt"
"math"
ledger_go "github.com/cosmos/ledger-go"
)
const (
userCLA = 0x55
userINSGetVersion = 0
userINSSignSECP256K1 = 2
userINSGetAddrSecp256k1 = 4
userMessageChunkSize = 250
)
// LedgerCosmos represents a connection to the Cosmos app in a Ledger Nano S device
type LedgerTHORChain struct {
api ledger_go.LedgerDevice
version VersionInfo
}
// FindLedgerCosmosUserApp finds a Cosmos user app running in a ledger device
func FindLedgerTHORChainUserApp() (_ *LedgerTHORChain, rerr error) {
ledgerAdmin := ledger_go.NewLedgerAdmin()
ledgerAPI, err := ledgerAdmin.Connect(0)
if err != nil {
return nil, err
}
defer func() {
if rerr != nil {
ledgerAPI.Close()
}
}()
app := &LedgerTHORChain{ledgerAPI, VersionInfo{}}
appVersion, err := app.GetVersion()
if err != nil {
if err.Error() == "[APDU_CODE_CLA_NOT_SUPPORTED] Class not supported" {
err = errors.New("are you sure the Cosmos app is open?")
}
return nil, err
}
if err := app.CheckVersion(*appVersion); err != nil {
return nil, err
}
return app, nil
}
// Close closes a connection with the THORChain user app
func (ledger *LedgerTHORChain) Close() error {
return ledger.api.Close()
}
// VersionIsSupported returns true if the App version is supported by this library
func (ledger *LedgerTHORChain) CheckVersion(ver VersionInfo) error {
version, err := ledger.GetVersion()
if err != nil {
return err
}
switch major := version.Major; major {
case 1:
return CheckVersion(ver, VersionInfo{0, 1, 5, 1})
case 2:
return CheckVersion(ver, VersionInfo{0, 2, 0, 0})
default:
return fmt.Errorf("App version %d is not supported", major)
}
}
// GetVersion returns the current version of the THORChain user app
func (ledger *LedgerTHORChain) GetVersion() (*VersionInfo, error) {
message := []byte{userCLA, userINSGetVersion, 0, 0, 0}
response, err := ledger.api.Exchange(message)
if err != nil {
return nil, err
}
if len(response) < 4 {
return nil, errors.New("invalid response")
}
ledger.version = VersionInfo{
AppMode: response[0], //Debug is 0xFF, otherwise 0x00
Major: response[1],
Minor: response[2],
Patch: response[3],
}
return &ledger.version, nil
}
// SignSECP256K1 signs a transaction using Cosmos user app. It can either use
// SIGN_MODE_LEGACY_AMINO_JSON (P2=0) or SIGN_MODE_TEXTUAL (P2=1).
// this command requires user confirmation in the device
func (ledger *LedgerTHORChain) SignSECP256K1(bip32Path []uint32, transaction []byte, p2 byte) ([]byte, error) {
switch major := ledger.version.Major; major {
case 1:
return ledger.signv1(bip32Path, transaction)
case 2:
return ledger.signv2(bip32Path, transaction, p2)
default:
return nil, fmt.Errorf("App version %d is not supported", major)
}
}
// GetPublicKeySECP256K1 retrieves the public key for the corresponding bip32 derivation path (compressed)
// this command DOES NOT require user confirmation in the device
func (ledger *LedgerTHORChain) GetPublicKeySECP256K1(bip32Path []uint32) ([]byte, error) {
pubkey, _, err := ledger.getAddressPubKeySECP256K1(bip32Path, "thor", false)
return pubkey, err
}
func validHRPByte(b byte) bool {
// https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
return b >= 33 && b <= 126
}
// GetAddressPubKeySECP256K1 returns the pubkey (compressed) and address (bech(
// this command requires user confirmation in the device
func (ledger *LedgerTHORChain) GetAddressPubKeySECP256K1(bip32Path []uint32, hrp string) (pubkey []byte, addr string, err error) {
return ledger.getAddressPubKeySECP256K1(bip32Path, hrp, true)
}
func (ledger *LedgerTHORChain) GetBip32bytes(bip32Path []uint32, hardenCount int) ([]byte, error) {
var pathBytes []byte
var err error
switch major := ledger.version.Major; major {
case 1:
pathBytes, err = GetBip32bytesv1(bip32Path, 3)
if err != nil {
return nil, err
}
case 2:
pathBytes, err = GetBip32bytesv2(bip32Path, 3)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("App version %d is not supported", major)
}
return pathBytes, nil
}
func (ledger *LedgerCosmos) signv1(bip32Path []uint32, transaction []byte) ([]byte, error) {
var packetIndex byte = 1
var packetCount = 1 + byte(math.Ceil(float64(len(transaction))/float64(userMessageChunkSize)))
var finalResponse []byte
var message []byte
for packetIndex <= packetCount {
chunk := userMessageChunkSize
if packetIndex == 1 {
pathBytes, err := ledger.GetBip32bytes(bip32Path, 3)
if err != nil {
return nil, err
}
header := []byte{userCLA, userINSSignSECP256K1, packetIndex, packetCount, byte(len(pathBytes))}
message = append(header, pathBytes...)
} else {
if len(transaction) < userMessageChunkSize {
chunk = len(transaction)
}
header := []byte{userCLA, userINSSignSECP256K1, packetIndex, packetCount, byte(chunk)}
message = append(header, transaction[:chunk]...)
}
response, err := ledger.api.Exchange(message)
if err != nil {
if err.Error() == "[APDU_CODE_BAD_KEY_HANDLE] The parameters in the data field are incorrect" {
// In this special case, we can extract additional info
errorMsg := string(response)
switch errorMsg {
case "ERROR: JSMN_ERROR_NOMEM":
return nil, errors.New("Not enough tokens were provided")
case "PARSER ERROR: JSMN_ERROR_INVAL":
return nil, errors.New("Unexpected character in JSON string")
case "PARSER ERROR: JSMN_ERROR_PART":
return nil, errors.New("The JSON string is not a complete.")
}
return nil, errors.New(errorMsg)
}
return nil, err
}
finalResponse = response
if packetIndex > 1 {
transaction = transaction[chunk:]
}
packetIndex++
}
return finalResponse, nil
}
func (ledger *LedgerCosmos) signv2(bip32Path []uint32, transaction []byte, p2 byte) ([]byte, error) {
var packetIndex byte = 1
var packetCount = 1 + byte(math.Ceil(float64(len(transaction))/float64(userMessageChunkSize)))
var finalResponse []byte
var message []byte
if p2 > 1 {
return nil, errors.New("only values of SIGN_MODE_LEGACY_AMINO (P2=0) and SIGN_MODE_TEXTUAL (P2=1) are allowed")
}
for packetIndex <= packetCount {
chunk := userMessageChunkSize
if packetIndex == 1 {
pathBytes, err := ledger.GetBip32bytes(bip32Path, 3)
if err != nil {
return nil, err
}
header := []byte{userCLA, userINSSignSECP256K1, 0, p2, byte(len(pathBytes))}
message = append(header, pathBytes...)
} else {
if len(transaction) < userMessageChunkSize {
chunk = len(transaction)
}
payloadDesc := byte(1)
if packetIndex == packetCount {
payloadDesc = byte(2)
}
header := []byte{userCLA, userINSSignSECP256K1, payloadDesc, p2, byte(chunk)}
message = append(header, transaction[:chunk]...)
}
response, err := ledger.api.Exchange(message)
if err != nil {
if err.Error() == "[APDU_CODE_BAD_KEY_HANDLE] The parameters in the data field are incorrect" {
// In this special case, we can extract additional info
errorMsg := string(response)
switch errorMsg {
case "ERROR: JSMN_ERROR_NOMEM":
return nil, errors.New("Not enough tokens were provided")
case "PARSER ERROR: JSMN_ERROR_INVAL":
return nil, errors.New("Unexpected character in JSON string")
case "PARSER ERROR: JSMN_ERROR_PART":
return nil, errors.New("The JSON string is not a complete.")
}
return nil, errors.New(errorMsg)
}
if err.Error() == "[APDU_CODE_DATA_INVALID] Referenced data reversibly blocked (invalidated)" {
errorMsg := string(response)
return nil, errors.New(errorMsg)
}
return nil, err
}
finalResponse = response
if packetIndex > 1 {
transaction = transaction[chunk:]
}
packetIndex++
}
return finalResponse, nil
}
// GetAddressPubKeySECP256K1 returns the pubkey (compressed) and address (bech(
// this command requires user confirmation in the device
func (ledger *LedgerTHORChain) getAddressPubKeySECP256K1(bip32Path []uint32, hrp string, requireConfirmation bool) (pubkey []byte, addr string, err error) {
if len(hrp) > 83 {
return nil, "", errors.New("hrp len should be <10")
}
hrpBytes := []byte(hrp)
for _, b := range hrpBytes {
if !validHRPByte(b) {
return nil, "", errors.New("all characters in the HRP must be in the [33, 126] range")
}
}
pathBytes, err := ledger.GetBip32bytes(bip32Path, 3)
if err != nil {
return nil, "", err
}
p1 := byte(0)
if requireConfirmation {
p1 = byte(1)
}
// Prepare message
header := []byte{userCLA, userINSGetAddrSecp256k1, p1, 0, 0}
message := append(header, byte(len(hrpBytes)))
message = append(message, hrpBytes...)
message = append(message, pathBytes...)
message[4] = byte(len(message) - len(header)) // update length
response, err := ledger.api.Exchange(message)
if err != nil {
return nil, "", err
}
if len(response) < 35+len(hrp) {
return nil, "", errors.New("Invalid response")
}
pubkey = response[0:33]
addr = string(response[33:])
return pubkey, addr, err
}