// TestFilterInsertKey ensures inserting public keys and addresses works as // expected. func TestFilterInsertKey(t *testing.T) { secret := "PtWU93QdrNBasyWA7GDJ3ycEN5aQRF69EynXJfmnyWDS4G7pzpEvN" wif, err := dcrutil.DecodeWIF(secret) if err != nil { t.Errorf("TestFilterInsertKey DecodeWIF failed: %v", err) return } f := bloom.NewFilter(2, 0, 0.001, wire.BloomUpdateAll) f.Add(wif.SerializePubKey()) f.Add(dcrutil.Hash160(wif.SerializePubKey())) want, err := hex.DecodeString("03323f6e080000000000000001") if err != nil { t.Errorf("TestFilterInsertWithTweak DecodeString failed: %v\n", err) return } got := bytes.NewBuffer(nil) err = f.MsgFilterLoad().BtcEncode(got, wire.ProtocolVersion) if err != nil { t.Errorf("TestFilterInsertWithTweak BtcDecode failed: %v\n", err) return } if !bytes.Equal(got.Bytes(), want) { t.Errorf("TestFilterInsertWithTweak failure: got %v want %v\n", got.Bytes(), want) return } }
func (s *walletServer) ImportPrivateKey(ctx context.Context, req *pb.ImportPrivateKeyRequest) ( *pb.ImportPrivateKeyResponse, error) { defer zero.Bytes(req.Passphrase) wif, err := dcrutil.DecodeWIF(req.PrivateKeyWif) if err != nil { return nil, grpc.Errorf(codes.InvalidArgument, "Invalid WIF-encoded private key: %v", err) } lock := make(chan time.Time, 1) defer func() { lock <- time.Time{} // send matters, not the value }() err = s.wallet.Unlock(req.Passphrase, lock) if err != nil { return nil, translateError(err) } // At the moment, only the special-cased import account can be used to // import keys. if req.Account != waddrmgr.ImportedAddrAccount { return nil, grpc.Errorf(codes.InvalidArgument, "Only the imported account accepts private key imports") } if req.ScanFrom < 0 { return nil, grpc.Errorf(codes.InvalidArgument, "Attempted to scan from a negative block height") } if req.ScanFrom > 0 && req.Rescan == true { return nil, grpc.Errorf(codes.InvalidArgument, "Passed a rescan height without rescan set") } chainClient := s.wallet.ChainClient() if req.Rescan && chainClient == nil { return nil, grpc.Errorf(codes.FailedPrecondition, "Cannot rescan without an associated consensus server RPC client") } _, err = s.wallet.ImportPrivateKey(wif) if err != nil { return nil, translateError(err) } if req.Rescan { s.wallet.RescanFromHeight(chainClient, req.ScanFrom) } return &pb.ImportPrivateKeyResponse{}, nil }
// newManager creates a new waddrmgr and imports the given privKey into it. func newManager(t *testing.T, privKeys []string, bs *waddrmgr.BlockStamp) *waddrmgr.Manager { dbPath := filepath.Join(os.TempDir(), "wallet.bin") os.Remove(dbPath) db, err := walletdb.Create("bdb", dbPath) if err != nil { t.Fatal(err) } namespace, err := db.Namespace(waddrmgrNamespaceKey) if err != nil { t.Fatal(err) } seed, err := hdkeychain.GenerateSeed(hdkeychain.RecommendedSeedLen) if err != nil { t.Fatal(err) } pubPassphrase := []byte("pub") privPassphrase := []byte("priv") mgr, err := waddrmgr.Create(namespace, seed, pubPassphrase, privPassphrase, &chaincfg.TestNetParams, fastScrypt) if err != nil { t.Fatal(err) } for _, key := range privKeys { wif, err := dcrutil.DecodeWIF(key) if err != nil { t.Fatal(err) } if err = mgr.Unlock(privPassphrase); err != nil { t.Fatal(err) } _, err = mgr.ImportPrivateKey(wif, bs) if err != nil { t.Fatal(err) } } return mgr }