FormatMoney cleanup
[novacoin.git] / src / rpcwallet.cpp
index 8b3780e..ff627c1 100644 (file)
@@ -323,9 +323,23 @@ Value sendtoaddress(const Array& params, bool fHelp)
             "<amount> is a real and is rounded to the nearest " + FormatMoney(nMinimumInputValue)
             + HelpRequiringPassphrase());
 
-    CBitcoinAddress address(params[0].get_str());
-    if (!address.IsValid())
-        throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid NovaCoin address");
+    // Parse address
+    CScript scriptPubKey;
+    string strAddress = params[0].get_str();
+
+    CBitcoinAddress address(strAddress);
+    if (address.IsValid())
+        scriptPubKey.SetDestination(address.Get());
+    else
+    {
+        CMalleablePubKey mpk(strAddress);
+        if (!mpk.IsValid())
+            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid NovaCoin address");
+
+        CPubKey R, pubKeyVariant;
+        mpk.GetVariant(R, pubKeyVariant);
+        scriptPubKey.SetDestination(R, pubKeyVariant);
+    }
 
     // Amount
     int64_t nAmount = AmountFromValue(params[1]);
@@ -343,7 +357,7 @@ Value sendtoaddress(const Array& params, bool fHelp)
     if (pwalletMain->IsLocked())
         throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Error: Please enter the wallet passphrase with walletpassphrase first.");
 
-    string strError = pwalletMain->SendMoneyToDestination(address.Get(), nAmount, wtx);
+    string strError = pwalletMain->SendMoney(scriptPubKey, nAmount, wtx);
     if (strError != "")
         throw JSONRPCError(RPC_WALLET_ERROR, strError);
 
@@ -695,9 +709,25 @@ Value sendfrom(const Array& params, bool fHelp)
             + HelpRequiringPassphrase());
 
     string strAccount = AccountFromValue(params[0]);
-    CBitcoinAddress address(params[1].get_str());
-    if (!address.IsValid())
-        throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid NovaCoin address");
+
+    // Parse address
+    CScript scriptPubKey;
+    string strAddress = params[0].get_str();
+
+    CBitcoinAddress address(strAddress);
+    if (address.IsValid())
+        scriptPubKey.SetDestination(address.Get());
+    else
+    {
+        CMalleablePubKey mpk(strAddress);
+        if (!mpk.IsValid())
+            throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid NovaCoin address");
+
+        CPubKey R, pubKeyVariant;
+        mpk.GetVariant(R, pubKeyVariant);
+        scriptPubKey.SetDestination(R, pubKeyVariant);
+    }
+
     int64_t nAmount = AmountFromValue(params[2]);
 
     if (nAmount < nMinimumInputValue)
@@ -722,7 +752,7 @@ Value sendfrom(const Array& params, bool fHelp)
         throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds");
 
     // Send
-    string strError = pwalletMain->SendMoneyToDestination(address.Get(), nAmount, wtx);
+    string strError = pwalletMain->SendMoney(scriptPubKey, nAmount, wtx);
     if (strError != "")
         throw JSONRPCError(RPC_WALLET_ERROR, strError);
 
@@ -1755,7 +1785,7 @@ Value reservebalance(const Array& params, bool fHelp)
             nAmount = (nAmount / CENT) * CENT;  // round to cent
             if (nAmount < 0)
                 throw runtime_error("amount cannot be negative.\n");
-            mapArgs["-reservebalance"] = FormatMoney(nAmount).c_str();
+            mapArgs["-reservebalance"] = FormatMoney(nAmount);
         }
         else
         {
@@ -1866,7 +1896,7 @@ Value newmalleablekey(const Array& params, bool fHelp)
             "newmalleablekey\n"
             "Make a malleable public/private key pair.\n");
 
-    if (!fTestNet)
+    if (!fTestNet && GetTime() < SMALLDATA_SWITCH_TIME)
         throw runtime_error("This feature has been disabled for mainNet clients");
 
     CMalleableKeyView keyView = pwalletMain->GenerateNewMalleableKey();
@@ -1882,23 +1912,28 @@ Value newmalleablekey(const Array& params, bool fHelp)
     return result;
 }
 
-Value dumpmalleablekey(const Array& params, bool fHelp)
+Value validatemalleablepubkey(const Array& params, bool fHelp)
 {
     if (fHelp || params.size() != 1)
-        throw runtime_error (
-            "dumpmalleablekey <Key view>\n"
-            "Dump the private and public key pairs, which correspond to provided key view.\n");
-
-    CMalleableKey mKey;
-    CMalleableKeyView keyView;
-    keyView.SetString(params[0].get_str());
+        throw runtime_error(
+            "validatemalleablekey <Malleable public key data>\n"
+            "Check the validity and ownership for priovided malleable public key.\n");
 
-    if (!pwalletMain->GetMalleableKey(keyView, mKey))
-        throw runtime_error("There is no such item in the wallet");
+    CMalleablePubKey mpk;
+    bool isValid = mpk.SetString(params[0].get_str());
 
     Object result;
-    result.push_back(Pair("PrivatePair", mKey.ToString()));
-    result.push_back(Pair("PublicPair", mKey.GetMalleablePubKey().ToString()));
+    result.push_back(Pair("isvalid", isValid));
+
+    if (isValid)
+    {
+        CMalleableKeyView view;
+        bool isMine = pwalletMain->GetMalleableView(mpk, view);
+        result.push_back(Pair("ismine", isMine));
+
+        if (isMine)
+            result.push_back(Pair("KeyView", view.ToString()));
+    }
 
     return result;
 }