forked from NebulousLabs/nanos-app-sia
-
Notifications
You must be signed in to change notification settings - Fork 7
/
sialedger.go
501 lines (444 loc) · 11.5 KB
/
sialedger.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package main
import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"os"
"strconv"
"github.com/karalabe/hid"
"gitlab.com/NebulousLabs/Sia/types"
"lukechampine.com/flagg"
)
var DEBUG bool
type hidFramer struct {
rw io.ReadWriter
seq uint16
buf [64]byte
pos int
}
func (hf *hidFramer) Reset() {
hf.seq = 0
}
func (hf *hidFramer) Write(p []byte) (int, error) {
if DEBUG {
fmt.Println("HID <=", hex.EncodeToString(p))
}
// split into 64-byte chunks
chunk := make([]byte, 64)
binary.BigEndian.PutUint16(chunk[:2], 0x0101)
chunk[2] = 0x05
var seq uint16
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, uint16(len(p)))
buf.Write(p)
for buf.Len() > 0 {
binary.BigEndian.PutUint16(chunk[3:5], seq)
n, _ := buf.Read(chunk[5:])
if n, err := hf.rw.Write(chunk[:5+n]); err != nil {
return n, err
}
seq++
}
return len(p), nil
}
func (hf *hidFramer) Read(p []byte) (int, error) {
if hf.seq > 0 && hf.pos != 64 {
// drain buf
n := copy(p, hf.buf[hf.pos:])
hf.pos += n
return n, nil
}
// read next 64-byte packet
if n, err := hf.rw.Read(hf.buf[:]); err != nil {
return 0, err
} else if n != 64 {
panic("read less than 64 bytes from HID")
}
// parse header
channelID := binary.BigEndian.Uint16(hf.buf[:2])
commandTag := hf.buf[2]
seq := binary.BigEndian.Uint16(hf.buf[3:5])
if channelID != 0x0101 {
return 0, fmt.Errorf("bad channel ID 0x%x", channelID)
} else if commandTag != 0x05 {
return 0, fmt.Errorf("bad command tag 0x%x", commandTag)
} else if seq != hf.seq {
return 0, fmt.Errorf("bad sequence number %v (expected %v)", seq, hf.seq)
}
hf.seq++
// start filling p
n := copy(p, hf.buf[5:])
hf.pos = 5 + n
return n, nil
}
type APDU struct {
CLA byte
INS byte
P1, P2 byte
Payload []byte
}
type apduFramer struct {
hf *hidFramer
buf [2]byte // to read APDU length prefix
}
func (af *apduFramer) Exchange(apdu APDU) ([]byte, error) {
if len(apdu.Payload) > 255 {
panic("APDU payload cannot exceed 255 bytes")
}
af.hf.Reset()
data := append([]byte{
apdu.CLA,
apdu.INS,
apdu.P1, apdu.P2,
byte(len(apdu.Payload)),
}, apdu.Payload...)
if _, err := af.hf.Write(data); err != nil {
return nil, err
}
// read APDU length
if _, err := io.ReadFull(af.hf, af.buf[:]); err != nil {
return nil, err
}
// read APDU payload
respLen := binary.BigEndian.Uint16(af.buf[:2])
resp := make([]byte, respLen)
_, err := io.ReadFull(af.hf, resp)
if DEBUG {
fmt.Println("HID =>", hex.EncodeToString(resp))
}
return resp, err
}
type NanoS struct {
device *apduFramer
}
type ErrCode uint16
func (c ErrCode) Error() string {
return fmt.Sprintf("Error code 0x%x", uint16(c))
}
const codeSuccess = 0x9000
const codeUserRejected = 0x6985
const codeInvalidParam = 0x6b01
var errUserRejected = errors.New("user denied request")
var errInvalidParam = errors.New("invalid request parameters")
func (n *NanoS) Exchange(cmd byte, p1, p2 byte, data []byte) (resp []byte, err error) {
resp, err = n.device.Exchange(APDU{
CLA: 0xe0,
INS: cmd,
P1: p1,
P2: p2,
Payload: data,
})
if err != nil {
return nil, err
} else if len(resp) < 2 {
return nil, errors.New("APDU response missing status code")
}
code := binary.BigEndian.Uint16(resp[len(resp)-2:])
resp = resp[:len(resp)-2]
switch code {
case codeSuccess:
err = nil
case codeUserRejected:
err = errUserRejected
case codeInvalidParam:
err = errInvalidParam
default:
err = ErrCode(code)
}
return
}
const (
cmdGetVersion = 0x01
cmdGetPublicKey = 0x02
cmdSignHash = 0x04
cmdCalcTxnHash = 0x08
p1First = 0x00
p1More = 0x80
p2DisplayAddress = 0x00
p2DisplayPubkey = 0x01
p2DisplayHash = 0x00
p2SignHash = 0x01
)
func (n *NanoS) GetVersion() (version string, err error) {
resp, err := n.Exchange(cmdGetVersion, 0, 0, nil)
if err != nil {
return "", err
} else if len(resp) != 3 {
return "", errors.New("version has wrong length")
}
return fmt.Sprintf("v%d.%d.%d", resp[0], resp[1], resp[2]), nil
}
func (n *NanoS) GetPublicKey(index uint32) (pubkey [32]byte, err error) {
encIndex := make([]byte, 4)
binary.LittleEndian.PutUint32(encIndex, index)
resp, err := n.Exchange(cmdGetPublicKey, 0, p2DisplayPubkey, encIndex)
if err != nil {
return [32]byte{}, err
}
if copy(pubkey[:], resp) != len(pubkey) {
return [32]byte{}, errors.New("pubkey has wrong length")
}
return
}
func (n *NanoS) GetAddress(index uint32) (addr types.UnlockHash, err error) {
encIndex := make([]byte, 4)
binary.LittleEndian.PutUint32(encIndex, index)
resp, err := n.Exchange(cmdGetPublicKey, 0, p2DisplayAddress, encIndex)
if err != nil {
return types.UnlockHash{}, err
}
err = addr.LoadString(string(resp[32:]))
return
}
func (n *NanoS) SignHash(hash [32]byte, keyIndex uint32) (sig [64]byte, err error) {
encIndex := make([]byte, 4)
binary.LittleEndian.PutUint32(encIndex, keyIndex)
resp, err := n.Exchange(cmdSignHash, 0, 0, append(encIndex, hash[:]...))
if err != nil {
return [64]byte{}, err
}
if copy(sig[:], resp) != len(sig) {
return [64]byte{}, errors.New("signature has wrong length")
}
return
}
func (n *NanoS) CalcTxnHash(txn types.Transaction, sigIndex uint16) (hash [32]byte, err error) {
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, uint32(0)) // keyIndex
binary.Write(buf, binary.LittleEndian, sigIndex)
txn.MarshalSia(buf)
var resp []byte
for buf.Len() > 0 {
var p1 byte = p1More
if resp == nil {
p1 = p1First
}
resp, err = n.Exchange(cmdCalcTxnHash, p1, p2DisplayHash, buf.Next(255))
if err != nil {
return [32]byte{}, err
}
}
if copy(hash[:], resp) != len(hash) {
return [32]byte{}, errors.New("hash has wrong length")
}
return
}
func (n *NanoS) SignTxn(txn types.Transaction, sigIndex uint16, keyIndex uint32) (sig [64]byte, err error) {
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, keyIndex)
binary.Write(buf, binary.LittleEndian, sigIndex)
txn.MarshalSia(buf)
var resp []byte
for buf.Len() > 0 {
var p1 byte = p1More
if resp == nil {
p1 = p1First
}
resp, err = n.Exchange(cmdCalcTxnHash, p1, p2SignHash, buf.Next(255))
if err != nil {
return [64]byte{}, err
}
}
if copy(sig[:], resp) != len(sig) {
return [64]byte{}, errors.New("signature has wrong length")
}
return
}
func OpenNanoS() (*NanoS, error) {
const (
ledgerVendorID = 0x2c97
ledgerNanoSProductID = 0x0001
//ledgerUsageID = 0xffa0
)
// search for Nano S
devices := hid.Enumerate(ledgerVendorID, ledgerNanoSProductID)
if len(devices) == 0 {
return nil, errors.New("Nano S not detected")
} else if len(devices) > 1 {
return nil, errors.New("Unexpected error -- Is the Sia wallet app running?")
}
// open the device
device, err := devices[0].Open()
if err != nil {
return nil, err
}
// wrap raw device I/O in HID+APDU protocols
return &NanoS{
device: &apduFramer{
hf: &hidFramer{
rw: device,
},
},
}, nil
}
func parseIndex(s string) uint32 {
index, err := strconv.ParseUint(s, 10, 32)
if err != nil {
log.Fatalln("Couldn't parse index:", err)
} else if index > math.MaxUint32 {
log.Fatalf("Index too large (max %v)", math.MaxUint32)
}
return uint32(index)
}
const (
rootUsage = `Usage:
sialedger [flags] [action]
Actions:
addr generate an address
pubkey generate a pubkey
hash sign a trusted hash
txn sign a transaction
`
debugUsage = `print raw APDU exchanges`
versionUsage = `Usage:
sialedger version
Prints the version of the sialedger binary, as well as the version reported by
the Sia Ledger Nano S app (if available).
`
addrUsage = `Usage:
sialedger addr [key index]
Generates an address using the public key with the specified index.
`
pubkeyUsage = `Usage:
sialedger pubkey [key index]
Generates the public key with the specified index.
`
hashUsage = `Usage:
sialedger hash [hex-encoded hash] [key index]
Signs a 256-bit hash using the private key with the specified index. The hash
must be hex-encoded.
Only sign hashes you trust. In practice, it is very difficult
to calculate a hash in a trusted manner.
`
txnUsage = `Usage:
sialedger txn [flags] [txn.json] [sig index] [key index]
Calculates and signs the hash of a transaction using the private key with the
specified key index. The CoveredFields of the specified TransactionSignature
must set WholeTransaction = true.
`
txnHashUsage = `calculate the transaction hash, but do not sign it`
)
func main() {
log.SetFlags(0)
rootCmd := flagg.Root
rootCmd.Usage = flagg.SimpleUsage(rootCmd, rootUsage)
rootCmd.BoolVar(&DEBUG, "apdu", false, debugUsage)
versionCmd := flagg.New("version", versionUsage)
addrCmd := flagg.New("addr", addrUsage)
pubkeyCmd := flagg.New("pubkey", pubkeyUsage)
hashCmd := flagg.New("hash", hashUsage)
txnCmd := flagg.New("txn", txnUsage)
txnHash := txnCmd.Bool("sighash", false, txnHashUsage)
cmd := flagg.Parse(flagg.Tree{
Cmd: rootCmd,
Sub: []flagg.Tree{
{Cmd: versionCmd},
{Cmd: addrCmd},
{Cmd: pubkeyCmd},
{Cmd: hashCmd},
{Cmd: txnCmd},
},
})
args := cmd.Args()
var nanos *NanoS
if cmd != rootCmd && cmd != versionCmd {
var err error
nanos, err = OpenNanoS()
if err != nil {
log.Fatalln("Couldn't open device:", err)
}
}
switch cmd {
case rootCmd:
if len(args) != 0 {
rootCmd.Usage()
return
}
fallthrough
case versionCmd:
// try to get Nano S app version
var appVersion string
nanos, err := OpenNanoS()
if err != nil {
appVersion = "(could not connect to Nano S)"
} else if appVersion, err = nanos.GetVersion(); err != nil {
appVersion = "(could not read version from Nano S: " + err.Error() + ")"
}
fmt.Printf("%s v0.1.0\n", os.Args[0])
fmt.Println("Nano S app version:", appVersion)
case addrCmd:
if len(args) != 1 {
addrCmd.Usage()
return
}
addr, err := nanos.GetAddress(parseIndex(args[0]))
if err != nil {
log.Fatalln("Couldn't get address:", err)
}
fmt.Println(addr)
case pubkeyCmd:
if len(args) != 1 {
pubkeyCmd.Usage()
return
}
pubkey, err := nanos.GetPublicKey(parseIndex(args[0]))
if err != nil {
log.Fatalln("Couldn't get public key:", err)
}
pk := types.Ed25519PublicKey(pubkey)
fmt.Println(pk.String())
case hashCmd:
if len(args) != 2 {
hashCmd.Usage()
return
}
var hash [32]byte
hashBytes, err := hex.DecodeString(args[0])
if err != nil {
log.Fatalln("Couldn't read hash:", err)
} else if len(hashBytes) != 32 {
log.Fatalf("Wrong hex hash length (%v, wanted 32)", len(hashBytes))
}
copy(hash[:], hashBytes)
sig, err := nanos.SignHash(hash, parseIndex(args[1]))
if err != nil {
log.Fatalln("Couldn't get signature:", err)
}
fmt.Println(base64.StdEncoding.EncodeToString(sig[:]))
case txnCmd:
if (*txnHash && len(args) != 2) || (!*txnHash && len(args) != 3) {
txnCmd.Usage()
return
}
txnBytes, err := ioutil.ReadFile(args[0])
if err != nil {
log.Fatalln("Couldn't read transaction:", err)
}
var txn types.Transaction
if err := json.Unmarshal(txnBytes, &txn); err != nil {
log.Fatalln("Couldn't decode transaction:", err)
}
sigIndex := uint16(parseIndex(args[1]))
if *txnHash {
sighash, err := nanos.CalcTxnHash(txn, sigIndex)
if err != nil {
log.Fatalln("Couldn't get hash:", err)
}
fmt.Println(hex.EncodeToString(sighash[:]))
} else {
sig, err := nanos.SignTxn(txn, sigIndex, parseIndex(args[2]))
if err != nil {
log.Fatalln("Couldn't get signature:", err)
}
fmt.Println(base64.StdEncoding.EncodeToString(sig[:]))
}
}
}