@@ -16,6 +16,7 @@ import (
16
16
17
17
"decred.org/dcrwallet/v4/errors"
18
18
"decred.org/dcrwallet/v4/walletseed"
19
+ "github.com/decred/dcrd/chaincfg/v3"
19
20
"github.com/decred/dcrd/hdkeychain/v3"
20
21
"golang.org/x/term"
21
22
)
@@ -366,6 +367,59 @@ func Seed(reader *bufio.Reader) (seed []byte, imported bool, err error) {
366
367
}
367
368
}
368
369
370
+ // ImportedAccounts prompts for any additional account names and xpubs to
371
+ // import at wallet creation.
372
+ func ImportedAccounts (reader * bufio.Reader , params * chaincfg.Params ) (names []string , xpubs []* hdkeychain.ExtendedKey , err error ) {
373
+ accounts := make (map [string ]struct {})
374
+ accounts ["default" ] = struct {}{}
375
+
376
+ for {
377
+ fmt .Printf ("Do you have an additional account to import from an " +
378
+ "extended public key? (enter account name, or 'no') [no]: " )
379
+ reply , err := reader .ReadString ('\n' )
380
+ if err != nil {
381
+ return nil , nil , err
382
+ }
383
+ reply = strings .TrimSpace (reply )
384
+ switch strings .ToLower (reply ) {
385
+ case "" , "n" , "no" :
386
+ return names , xpubs , nil
387
+ case "y" , "yes" :
388
+ continue
389
+ default :
390
+ }
391
+
392
+ account := reply
393
+ if _ , ok := accounts [account ]; ok {
394
+ fmt .Printf ("Account %q is already defined\n " , account )
395
+ continue
396
+ }
397
+ fmt .Printf ("Enter extended public key for account %q: " , account )
398
+ reply , err = reader .ReadString ('\n' )
399
+ if err != nil {
400
+ if errors .Is (err , io .EOF ) {
401
+ err = io .ErrUnexpectedEOF
402
+ }
403
+ return nil , nil , err
404
+ }
405
+ reply = strings .TrimSpace (reply )
406
+ xpub , err := hdkeychain .NewKeyFromString (reply , params )
407
+ if err != nil {
408
+ fmt .Printf ("Failed to decode extended key: %v\n " , err )
409
+ continue
410
+ }
411
+ if xpub .IsPrivate () {
412
+ fmt .Printf ("Extended key is a private key (not neutered)\n " )
413
+ continue
414
+ }
415
+
416
+ fmt .Printf ("Importing account %q from extended public key\n " , account )
417
+ names = append (names , account )
418
+ xpubs = append (xpubs , xpub )
419
+ accounts [account ] = struct {}{}
420
+ }
421
+ }
422
+
369
423
// collapseSpace takes a string and replaces any repeated areas of whitespace
370
424
// with a single space character.
371
425
func collapseSpace (in string ) string {
0 commit comments