@@ -10,6 +10,7 @@ import (
10
10
"time"
11
11
12
12
"github.com/btcsuite/btcd/btcutil"
13
+ "github.com/btcsuite/btcd/btcutil/hdkeychain"
13
14
"github.com/btcsuite/btcd/txscript"
14
15
"github.com/btcsuite/btcd/wire"
15
16
"github.com/btcsuite/btcwallet/waddrmgr"
@@ -498,4 +499,61 @@ func TestBalance(t *testing.T) {
498
499
)
499
500
}
500
501
502
+ // TestAccountManagerImportAccount tests that the ImportAccount method on the
503
+ // AccountManager interface works as expected.
504
+ func TestAccountManagerImportAccount (t * testing.T ) {
505
+ t .Parallel ()
506
+
507
+ // Create a new test wallet.
508
+ w , cleanup := testWallet (t )
509
+ defer cleanup ()
501
510
511
+ // We'll start by creating a new account under the BIP0084 scope.
512
+ scope := waddrmgr .KeyScopeBIP0084
513
+ addrType := waddrmgr .WitnessPubKey
514
+ masterPriv := "tprv8ZgxMBicQKsPeWwrFuNjEGTTDSY4mRLwd2KDJAPGa1AY" +
515
+ "quw38bZqNMSuB3V1Va3hqJBo9Pt8Sx7kBQer5cNMrb8SYquoWPt9" +
516
+ "Y3BZdhdtUcw"
517
+ root , err := hdkeychain .NewKeyFromString (masterPriv )
518
+ require .NoError (t , err )
519
+ acctPubKey := deriveAcctPubKey (t , root , scope , hardenedKey (0 ))
520
+
521
+ // We should be able to import the account.
522
+ props , err := w .ImportAccount (
523
+ context .Background (), scope , testAccountName , acctPubKey ,
524
+ root .ParentFingerprint (), & addrType , false ,
525
+ )
526
+ require .NoError (t , err )
527
+ require .Equal (t , testAccountName , props .AccountName )
528
+
529
+ // We should be able to get the account by its name.
530
+ _ , err = w .GetAccount (context .Background (), scope , testAccountName )
531
+ require .NoError (t , err )
532
+
533
+ // We should not be able to import an account with the same name.
534
+ _ , err = w .ImportAccount (
535
+ context .Background (), scope , testAccountName , acctPubKey ,
536
+ root .ParentFingerprint (), & addrType , false ,
537
+ )
538
+ require .Error (t , err )
539
+ require .True (
540
+ t , waddrmgr .IsError (err , waddrmgr .ErrDuplicateAccount ),
541
+ "expected ErrDuplicateAccount" ,
542
+ )
543
+
544
+ // We should be able to do a dry run of the import.
545
+ dryRunName := "dry run"
546
+ _ , err = w .ImportAccount (
547
+ context .Background (), scope , dryRunName , acctPubKey ,
548
+ root .ParentFingerprint (), & addrType , true ,
549
+ )
550
+ require .NoError (t , err )
551
+
552
+ // The account should not have been imported.
553
+ _ , err = w .GetAccount (context .Background (), scope , dryRunName )
554
+ require .Error (t , err )
555
+ require .True (
556
+ t , waddrmgr .IsError (err , waddrmgr .ErrAccountNotFound ),
557
+ "expected ErrAccountNotFound" ,
558
+ )
559
+ }
0 commit comments