forked from cosmos/ledger-cosmos-go
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuser_app.go
More file actions
290 lines (238 loc) · 7.85 KB
/
user_app.go
File metadata and controls
290 lines (238 loc) · 7.85 KB
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
/*******************************************************************************
* (c) 2018 ZondaX GmbH
*
* 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 ledger_gitopia_go
import (
"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 LedgerCosmos struct {
appName string
api *ledger_go.Ledger
version VersionInfo
}
// FindLedgerCosmosUserApp finds a Cosmos user app running in a ledger device
func FindLedgerCosmosUserApp() (*LedgerCosmos, error) {
ledgerAPI, err := ledger_go.FindLedger()
if err != nil {
return nil, err
}
app := LedgerCosmos{"",ledgerAPI, VersionInfo{}}
err = app.LoadAppName()
if err != nil {
defer ledgerAPI.Close()
if err.Error() == "[APDU_CODE_CLA_NOT_SUPPORTED] Class not supported" {
return nil, fmt.Errorf("are you sure the Gitopia or Cosmos app is open?")
}
return nil, err
}
err = app.LoadVersion()
if err != nil {
defer ledgerAPI.Close()
if err.Error() == "[APDU_CODE_CLA_NOT_SUPPORTED] Class not supported" {
return nil, fmt.Errorf("are you sure the Gitopia or Cosmos app is open?")
}
return nil, err
}
err = app.CheckVersion()
if err != nil {
defer ledgerAPI.Close()
return nil, err
}
return &app, err
}
// Close closes a connection with the Cosmos user app
func (ledger *LedgerCosmos) Close() error {
return ledger.api.Close()
}
// VersionIsSupported returns true if the App version is supported by this library
func (ledger *LedgerCosmos) CheckVersion() error {
// ver := ledger.version
// appName := ledger.appName
// if appName == "Gitopia" {
// return CheckVersion(ver, VersionInfo{0, 0, 1, 0})
// } else if appName == "Cosmos" {
// return CheckVersion(ver, VersionInfo{0, 2, 1, 0})
// }
// return fmt.Errorf("App version is not supported")
return nil
}
// GetVersion returns the current version of the Cosmos user app
func (ledger *LedgerCosmos) LoadVersion() error {
message := []byte{userCLA, userINSGetVersion, 0, 0, 0}
response, err := ledger.api.Exchange(message)
if err != nil {
return err
}
if len(response) < 4 {
return fmt.Errorf("invalid response")
}
ledger.version = VersionInfo{
AppMode: response[0],
Major: response[1],
Minor: response[2],
Patch: response[3],
}
return nil
}
func (ledger *LedgerCosmos) LoadAppName() error {
message := []byte{0xb0, 0x01, 0, 0, 0}
response, err := ledger.api.Exchange(message)
if err != nil {
return err
}
if response[0] != 1 {
return fmt.Errorf("response format ID not recognized")
}
idx := 2
appNameLen := int(response[1])
appName := string(response[idx : idx+appNameLen])
ledger.appName = appName
return nil
}
// SignSECP256K1 signs a transaction using Cosmos user app
// this command requires user confirmation in the device
func (ledger *LedgerCosmos) SignSECP256K1(bip32Path []uint32, transaction []byte) ([]byte, error) {
return ledger.sign(bip32Path, transaction)
}
// GetPublicKeySECP256K1 retrieves the public key for the corresponding bip32 derivation path (compressed)
// this command DOES NOT require user confirmation in the device
func (ledger *LedgerCosmos) GetPublicKeySECP256K1(bip32Path []uint32) ([]byte, error) {
pubkey, _, err := ledger.getAddressPubKeySECP256K1(bip32Path, "gitopia", 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 *LedgerCosmos) GetAddressPubKeySECP256K1(bip32Path []uint32, hrp string) (pubkey []byte, addr string, err error) {
return ledger.getAddressPubKeySECP256K1(bip32Path, hrp, true)
}
func (ledger *LedgerCosmos) GetBip32bytes(bip32Path []uint32, hardenCount int) ([]byte, error) {
var pathBytes []byte
var err error
// check
if (ledger.appName == "Gitopia") {
pathBytes, err = GetBip32bytesv2(bip32Path, 3)
if err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("App version is not supported")
}
return pathBytes, nil
}
func (ledger *LedgerCosmos) sign(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, 0, 0, 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, 0, 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, fmt.Errorf("Not enough tokens were provided")
case "PARSER ERROR: JSMN_ERROR_INVAL":
return nil, fmt.Errorf("Unexpected character in JSON string")
case "PARSER ERROR: JSMN_ERROR_PART":
return nil, fmt.Errorf("The JSON string is not a complete.")
}
return nil, fmt.Errorf(errorMsg)
}
if err.Error() == "[APDU_CODE_DATA_INVALID] Referenced data reversibly blocked (invalidated)" {
errorMsg := string(response)
return nil, fmt.Errorf(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 *LedgerCosmos) getAddressPubKeySECP256K1(bip32Path []uint32, hrp string, requireConfirmation bool) (pubkey []byte, addr string, err error) {
if len(hrp) > 83 {
return nil, "", fmt.Errorf("hrp len should be <10")
}
hrpBytes := []byte(hrp)
for _, b := range hrpBytes {
if !validHRPByte(b) {
return nil, "", fmt.Errorf("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, "", fmt.Errorf("Invalid response")
}
pubkey = response[0:33]
addr = string(response[33:])
return pubkey, addr, err
}