-
Notifications
You must be signed in to change notification settings - Fork 650
/
ledger.go
96 lines (81 loc) · 2.33 KB
/
ledger.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package ledger
import (
"fmt"
ledger "github.com/ava-labs/ledger-avalanche/go"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/crypto/keychain"
"github.com/ava-labs/avalanchego/version"
)
const rootPath = "m/44'/9000'/0'"
var _ keychain.Ledger = (*Ledger)(nil)
// Ledger is a wrapper around the low-level Ledger Device interface that
// provides Avalanche-specific access.
type Ledger struct {
device *ledger.LedgerAvalanche
}
func New() (keychain.Ledger, error) {
device, err := ledger.FindLedgerAvalancheApp()
return &Ledger{
device: device,
}, err
}
func addressPath(index uint32) string {
return fmt.Sprintf("%s/0/%d", rootPath, index)
}
func (l *Ledger) Address(hrp string, addressIndex uint32) (ids.ShortID, error) {
_, hash, err := l.device.GetPubKey(addressPath(addressIndex), true, hrp, "")
if err != nil {
return ids.ShortEmpty, err
}
return ids.ToShortID(hash)
}
func (l *Ledger) Addresses(addressIndices []uint32) ([]ids.ShortID, error) {
addresses := make([]ids.ShortID, len(addressIndices))
for i, v := range addressIndices {
_, hash, err := l.device.GetPubKey(addressPath(v), false, "", "")
if err != nil {
return nil, err
}
copy(addresses[i][:], hash)
}
return addresses, nil
}
func convertToSigningPaths(input []uint32) []string {
output := make([]string, len(input))
for i, v := range input {
output[i] = fmt.Sprintf("0/%d", v)
}
return output
}
func (l *Ledger) SignHash(hash []byte, addressIndices []uint32) ([][]byte, error) {
strIndices := convertToSigningPaths(addressIndices)
response, err := l.device.SignHash(rootPath, strIndices, hash)
if err != nil {
return nil, fmt.Errorf("%w: unable to sign hash", err)
}
responses := make([][]byte, len(addressIndices))
for i, index := range strIndices {
sig, ok := response.Signature[index]
if !ok {
return nil, fmt.Errorf("missing signature %s", index)
}
responses[i] = sig
}
return responses, nil
}
func (l *Ledger) Version() (*version.Semantic, error) {
resp, err := l.device.GetVersion()
if err != nil {
return nil, err
}
return &version.Semantic{
Major: int(resp.Major),
Minor: int(resp.Minor),
Patch: int(resp.Patch),
}, nil
}
func (l *Ledger) Disconnect() error {
return l.device.Close()
}