From 05df71c9c292877e7469884227fd3ba84d02908b Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Mon, 4 Apr 2016 02:42:25 +0300 Subject: [PATCH] RPC: now decryptdata/decryptmessage methods are able to understand WIF formatted private key. --- src/rpccrypt.cpp | 45 ++++++++++++++++++++++++++++++++------------- 1 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/rpccrypt.cpp b/src/rpccrypt.cpp index 69e14c0..91f0302 100644 --- a/src/rpccrypt.cpp +++ b/src/rpccrypt.cpp @@ -30,17 +30,26 @@ Value decryptdata(const Array& params, bool fHelp) { if (fHelp || params.size() != 2) throw runtime_error( - "decryptdata \n" + "decryptdata \n" "Decrypt octet stream.\n"); EnsureWalletIsUnlocked(); - CBitcoinAddress addr(params[0].get_str()); - - CKeyID keyID; - addr.GetKeyID(keyID); - CKey key; - pwalletMain->GetKey(keyID, key); + CBitcoinAddress addr(params[0].get_str()); + if (addr.IsValid()) { + CKeyID keyID; + addr.GetKeyID(keyID); + if (!pwalletMain->GetKey(keyID, key)) + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "We have no private key for this address"); + } + else { + CBitcoinSecret vchSecret; + if (!vchSecret.SetString(params[0].get_str())) + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Provided private key is inconsistent."); + bool fCompressed; + CSecret secret = vchSecret.GetSecret(fCompressed); + key.SetSecret(secret, fCompressed); + } vector vchDecrypted; key.DecryptData(ParseHex(params[1].get_str()), vchDecrypted); @@ -68,17 +77,27 @@ Value decryptmessage(const Array& params, bool fHelp) { if (fHelp || params.size() != 2) throw runtime_error( - "decryptmessage \n" + "decryptmessage \n" "Decrypt message string.\n"); EnsureWalletIsUnlocked(); - CBitcoinAddress addr(params[0].get_str()); - - CKeyID keyID; - addr.GetKeyID(keyID); CKey key; - pwalletMain->GetKey(keyID, key); + CBitcoinAddress addr(params[0].get_str()); + if (addr.IsValid()) { + CKeyID keyID; + addr.GetKeyID(keyID); + if (!pwalletMain->GetKey(keyID, key)) + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "We have no private key for this address"); + } + else { + CBitcoinSecret vchSecret; + if (!vchSecret.SetString(params[0].get_str())) + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Provided private key is inconsistent."); + bool fCompressed; + CSecret secret = vchSecret.GetSecret(fCompressed); + key.SetSecret(secret, fCompressed); + } vector vchEncrypted; if (!DecodeBase58Check(params[1].get_str(), vchEncrypted)) -- 1.7.1