Skip to content

Commit db568a6

Browse files
committed
accounts, console, internal: support trezor hardware wallet
1 parent 17ce0a3 commit db568a6

38 files changed

Lines changed: 17063 additions & 38 deletions

‎accounts/accounts.go‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,26 @@ type Backend interface {
147147
Subscribe(sink chan<- WalletEvent) event.Subscription
148148
}
149149

150+
// WalletEventType represents the different event types that can be fired by
151+
// the wallet subscription subsystem.
152+
type WalletEventType int
153+
154+
const (
155+
// WalletArrived is fired when a new wallet is detected either via USB or via
156+
// a filesystem event in the keystore.
157+
WalletArrived WalletEventType = iota
158+
159+
// WalletOpened is fired when a wallet is successfully opened with the purpose
160+
// of starting any background processes such as automatic key derivation.
161+
WalletOpened
162+
163+
// WalletDropped
164+
WalletDropped
165+
)
166+
150167
// WalletEvent is an event fired by an account backend when a wallet arrival or
151168
// departure is detected.
152169
type WalletEvent struct {
153-
Wallet Wallet // Wallet instance arrived or departed
154-
Arrive bool // Whether the wallet was added or removed
170+
Wallet Wallet // Wallet instance arrived or departed
171+
Kind WalletEventType // Event type that happened in the system
155172
}

‎accounts/hd.go‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,17 @@ import (
2727
// DefaultRootDerivationPath is the root path to which custom derivation endpoints
2828
// are appended. As such, the first account will be at m/44'/60'/0'/0, the second
2929
// at m/44'/60'/0'/1, etc.
30-
var DefaultRootDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0}
30+
var DefaultRootDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}
3131

3232
// DefaultBaseDerivationPath is the base path from which custom derivation endpoints
3333
// are incremented. As such, the first account will be at m/44'/60'/0'/0, the second
3434
// at m/44'/60'/0'/1, etc.
35-
var DefaultBaseDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}
35+
var DefaultBaseDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0}
36+
37+
// DefaultLedgerBaseDerivationPath is the base path from which custom derivation endpoints
38+
// are incremented. As such, the first account will be at m/44'/60'/0'/0, the second
39+
// at m/44'/60'/0'/1, etc.
40+
var DefaultLedgerBaseDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}
3641

3742
// DerivationPath represents the computer friendly version of a hierarchical
3843
// deterministic wallet account derivaion path.

‎accounts/keystore/keystore.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,14 @@ func (ks *KeyStore) refreshWallets() {
143143
for _, account := range accs {
144144
// Drop wallets while they were in front of the next account
145145
for len(ks.wallets) > 0 && ks.wallets[0].URL().Cmp(account.URL) < 0 {
146-
events = append(events, accounts.WalletEvent{Wallet: ks.wallets[0], Arrive: false})
146+
events = append(events, accounts.WalletEvent{Wallet: ks.wallets[0], Kind: accounts.WalletDropped})
147147
ks.wallets = ks.wallets[1:]
148148
}
149149
// If there are no more wallets or the account is before the next, wrap new wallet
150150
if len(ks.wallets) == 0 || ks.wallets[0].URL().Cmp(account.URL) > 0 {
151151
wallet := &keystoreWallet{account: account, keystore: ks}
152152

153-
events = append(events, accounts.WalletEvent{Wallet: wallet, Arrive: true})
153+
events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletArrived})
154154
wallets = append(wallets, wallet)
155155
continue
156156
}
@@ -163,7 +163,7 @@ func (ks *KeyStore) refreshWallets() {
163163
}
164164
// Drop any leftover wallets and set the new batch
165165
for _, wallet := range ks.wallets {
166-
events = append(events, accounts.WalletEvent{Wallet: wallet, Arrive: false})
166+
events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletDropped})
167167
}
168168
ks.wallets = wallets
169169
ks.mu.Unlock()

‎accounts/keystore/keystore_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ func TestWalletNotifications(t *testing.T) {
296296
}
297297
select {
298298
case event := <-updates:
299-
if !event.Arrive {
300-
t.Errorf("departure event on account creation")
299+
if event.Kind != accounts.WalletArrived {
300+
t.Errorf("non-arrival event on account creation")
301301
}
302302
if event.Wallet.Accounts()[0] != account {
303303
t.Errorf("account mismatch on created wallet: have %v, want %v", event.Wallet.Accounts()[0], account)
@@ -319,8 +319,8 @@ func TestWalletNotifications(t *testing.T) {
319319
}
320320
select {
321321
case event := <-updates:
322-
if event.Arrive {
323-
t.Errorf("arrival event on account deletion")
322+
if event.Kind != accounts.WalletDropped {
323+
t.Errorf("non-drop event on account deletion")
324324
}
325325
if event.Wallet.Accounts()[0] != account {
326326
t.Errorf("account mismatch on deleted wallet: have %v, want %v", event.Wallet.Accounts()[0], account)

‎accounts/manager.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ func (am *Manager) update() {
9696
case event := <-am.updates:
9797
// Wallet event arrived, update local cache
9898
am.lock.Lock()
99-
if event.Arrive {
99+
switch event.Kind {
100+
case WalletArrived:
100101
am.wallets = merge(am.wallets, event.Wallet)
101-
} else {
102+
case WalletDropped:
102103
am.wallets = drop(am.wallets, event.Wallet)
103104
}
104105
am.lock.Unlock()

0 commit comments

Comments
 (0)