@@ -36,6 +36,9 @@ import (
3636 "github.com/ethereum/go-ethereum/rlp"
3737)
3838
39+ // numberOfAccountsToDerive For hardware wallets, the number of accounts to derive
40+ const numberOfAccountsToDerive = 10
41+
3942// ExternalAPI defines the external API through which signing requests are made.
4043type ExternalAPI interface {
4144 // List available accounts
@@ -79,6 +82,9 @@ type SignerUI interface {
7982 // OnSignerStartup is invoked when the signer boots, and tells the UI info about external API location and version
8083 // information
8184 OnSignerStartup (info StartupInfo )
85+ // OnInputRequried is invoked when clef requires user input, for example master password or
86+ // pin-code for unlocking hardware wallets
87+ OnInputRequired (info UserInputRequest ) (UserInputResponse , error )
8288}
8389
8490// SignerAPI defines the actual implementation of ExternalAPI
@@ -194,6 +200,14 @@ type (
194200 StartupInfo struct {
195201 Info map [string ]interface {} `json:"info"`
196202 }
203+ UserInputRequest struct {
204+ Prompt string `json:"prompt"`
205+ Title string `json:"title"`
206+ IsPassword bool `json:"isPassword"`
207+ }
208+ UserInputResponse struct {
209+ Text string `json:"text"`
210+ }
197211)
198212
199213var ErrRequestDenied = errors .New ("Request denied" )
@@ -215,6 +229,9 @@ func NewSignerAPI(chainID int64, ksLocation string, noUSB bool, ui SignerUI, abi
215229 if len (ksLocation ) > 0 {
216230 backends = append (backends , keystore .NewKeyStore (ksLocation , n , p ))
217231 }
232+ if advancedMode {
233+ log .Info ("Clef is in advanced mode: will warn instead of reject" )
234+ }
218235 if ! noUSB {
219236 // Start a USB hub for Ledger hardware wallets
220237 if ledgerhub , err := usbwallet .NewLedgerHub (); err != nil {
@@ -231,10 +248,94 @@ func NewSignerAPI(chainID int64, ksLocation string, noUSB bool, ui SignerUI, abi
231248 log .Debug ("Trezor support enabled" )
232249 }
233250 }
234- if advancedMode {
235- log .Info ("Clef is in advanced mode: will warn instead of reject" )
251+ signer := & SignerAPI {big .NewInt (chainID ), accounts .NewManager (backends ... ), ui , NewValidator (abidb ), ! advancedMode }
252+ if ! noUSB {
253+ signer .startUSBListener ()
254+ }
255+ return signer
256+ }
257+ func (api * SignerAPI ) openTrezor (url accounts.URL ) {
258+ resp , err := api .UI .OnInputRequired (UserInputRequest {
259+ Prompt : "Pin required to open Trezor wallet\n " +
260+ "Look at the device for number positions\n \n " +
261+ "7 | 8 | 9\n " +
262+ "--+---+--\n " +
263+ "4 | 5 | 6\n " +
264+ "--+---+--\n " +
265+ "1 | 2 | 3\n \n " ,
266+ IsPassword : true ,
267+ Title : "Trezor unlock" ,
268+ })
269+ if err != nil {
270+ log .Warn ("failed getting trezor pin" , "err" , err )
271+ return
272+ }
273+ // We're using the URL instead of the pointer to the
274+ // Wallet -- perhaps it is not actually present anymore
275+ w , err := api .am .Wallet (url .String ())
276+ if err != nil {
277+ log .Warn ("wallet unavailable" , "url" , url )
278+ return
279+ }
280+ err = w .Open (resp .Text )
281+ if err != nil {
282+ log .Warn ("failed to open wallet" , "wallet" , url , "err" , err )
283+ return
236284 }
237- return & SignerAPI {big .NewInt (chainID ), accounts .NewManager (backends ... ), ui , NewValidator (abidb ), ! advancedMode }
285+
286+ }
287+
288+ // startUSBListener starts a listener for USB events, for hardware wallet interaction
289+ func (api * SignerAPI ) startUSBListener () {
290+ events := make (chan accounts.WalletEvent , 16 )
291+ am := api .am
292+ am .Subscribe (events )
293+ go func () {
294+
295+ // Open any wallets already attached
296+ for _ , wallet := range am .Wallets () {
297+ if err := wallet .Open ("" ); err != nil {
298+ log .Warn ("Failed to open wallet" , "url" , wallet .URL (), "err" , err )
299+ if err == usbwallet .ErrTrezorPINNeeded {
300+ go api .openTrezor (wallet .URL ())
301+ }
302+ }
303+ }
304+ // Listen for wallet event till termination
305+ for event := range events {
306+ switch event .Kind {
307+ case accounts .WalletArrived :
308+ if err := event .Wallet .Open ("" ); err != nil {
309+ log .Warn ("New wallet appeared, failed to open" , "url" , event .Wallet .URL (), "err" , err )
310+ if err == usbwallet .ErrTrezorPINNeeded {
311+ go api .openTrezor (event .Wallet .URL ())
312+ }
313+ }
314+ case accounts .WalletOpened :
315+ status , _ := event .Wallet .Status ()
316+ log .Info ("New wallet appeared" , "url" , event .Wallet .URL (), "status" , status )
317+
318+ derivationPath := accounts .DefaultBaseDerivationPath
319+ if event .Wallet .URL ().Scheme == "ledger" {
320+ derivationPath = accounts .DefaultLedgerBaseDerivationPath
321+ }
322+ var nextPath = derivationPath
323+ // Derive first N accounts, hardcoded for now
324+ for i := 0 ; i < numberOfAccountsToDerive ; i ++ {
325+ acc , err := event .Wallet .Derive (nextPath , true )
326+ if err != nil {
327+ log .Warn ("account derivation failed" , "error" , err )
328+ } else {
329+ log .Info ("derived account" , "address" , acc .Address )
330+ }
331+ nextPath [len (nextPath )- 1 ]++
332+ }
333+ case accounts .WalletDropped :
334+ log .Info ("Old wallet dropped" , "url" , event .Wallet .URL ())
335+ event .Wallet .Close ()
336+ }
337+ }
338+ }()
238339}
239340
240341// List returns the set of wallet this signer manages. Each wallet can contain
0 commit comments