@@ -917,6 +917,53 @@ static UniValue ProcessImport(CWallet * const pwallet, const UniValue& data, con
917917 pwallet->SetAddressBook (dest, label, " receive" );
918918 }
919919
920+ // Import public keys
921+ for (size_t i = 0 ; i < pubKeys.size (); ++i) {
922+ const std::string& pubkey = pubKeys[i].get_str ();
923+
924+ if (!IsHex (pubkey)) {
925+ throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Pubkey must be a hex string" );
926+ }
927+
928+ std::vector<unsigned char > vData (ParseHex (pubkey));
929+ CPubKey pubKey (vData.begin (), vData.end ());
930+
931+ if (!pubKey.IsFullyValid ()) {
932+ throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Pubkey is not a valid public key" );
933+ }
934+
935+ CTxDestination pubkey_dest = pubKey.GetID ();
936+ CScript pubKeyScript = GetScriptForDestination (pubkey_dest);
937+
938+ if (::IsMine (*pwallet, pubKeyScript) == ISMINE_SPENDABLE) {
939+ throw JSONRPCError (RPC_WALLET_ERROR, " The wallet already contains the private key for this address or script" );
940+ }
941+
942+ pwallet->MarkDirty ();
943+
944+ if (!pwallet->AddWatchOnly (pubKeyScript, timestamp)) {
945+ throw JSONRPCError (RPC_WALLET_ERROR, " Error adding address to wallet" );
946+ }
947+
948+ // add to address book or update label
949+ if (IsValidDestination (pubkey_dest)) {
950+ pwallet->SetAddressBook (pubkey_dest, label, " receive" );
951+ }
952+
953+ // TODO Is this necessary?
954+ CScript scriptRawPubKey = GetScriptForRawPubKey (pubKey);
955+
956+ if (::IsMine (*pwallet, scriptRawPubKey) == ISMINE_SPENDABLE) {
957+ throw JSONRPCError (RPC_WALLET_ERROR, " The wallet already contains the private key for this address or script" );
958+ }
959+
960+ pwallet->MarkDirty ();
961+
962+ if (!pwallet->AddWatchOnly (scriptRawPubKey, timestamp)) {
963+ throw JSONRPCError (RPC_WALLET_ERROR, " Error adding address to wallet" );
964+ }
965+ }
966+
920967 // Import private keys.
921968 if (keys.size ()) {
922969 for (size_t i = 0 ; i < keys.size (); i++) {
0 commit comments