Add getpowreward RPC
[novacoin.git] / src / bitcoinrpc.cpp
index 42852c0..a973d37 100644 (file)
@@ -114,6 +114,19 @@ HexBits(unsigned int nBits)
     return HexStr(BEGIN(uBits.cBits), END(uBits.cBits));
 }
 
+unsigned int BitsHex(std::string HexBits)
+{
+    union {
+        int32_t nBits;
+        char cBits[4];
+    } uBits;
+
+    vector<unsigned char> vchBits = ParseHex(HexBits);
+    copy(vchBits.begin(), vchBits.begin() + 4, uBits.cBits);
+    uBits.nBits = htonl((int32_t)uBits.nBits);
+    return uBits.nBits;
+}
+
 void WalletTxToJSON(const CWalletTx& wtx, Object& entry)
 {
     int confirms = wtx.GetDepthInMainChain();
@@ -848,6 +861,28 @@ Value getbalance(const Array& params, bool fHelp)
 }
 
 
+Value getpowreward(const Array& params, bool fHelp)
+{
+    if (fHelp || params.size() > 1)
+        throw runtime_error(
+            "getpowreward [nBits]\n"
+            "Returns PoW reward for block with provided difficulty.");
+
+    if (params.size() == 0)
+        throw JSONRPCError(-200, "no bits provided");
+
+    std::string sBits = params[0].get_str();
+
+    if (sBits.length() != 8)
+        throw JSONRPCError(-201, "incorrect bits provided");
+
+    unsigned int nBits = BitsHex(sBits);
+
+    return (int)GetProofOfWorkReward(nBits);
+}
+
+
+
 Value movecmd(const Array& params, bool fHelp)
 {
     if (fHelp || params.size() < 3 || params.size() > 5)
@@ -1806,6 +1841,50 @@ Value validateaddress(const Array& params, bool fHelp)
     return ret;
 }
 
+Value validatepubkey(const Array& params, bool fHelp)
+{
+    if (fHelp || !params.size() || params.size() > 2)
+        throw runtime_error(
+            "validatepubkey <novacoinpubkey>\n"
+            "Return information about <novacoinpubkey>.");
+
+    std::vector<unsigned char> vchPubKey = ParseHex(params[0].get_str());
+    bool isValid;
+
+    if(vchPubKey.size() == 33) // Compressed key
+        isValid = (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03);
+    else if(vchPubKey.size() == 65) // Uncompressed key
+        isValid = vchPubKey[0] == 0x04;
+    else
+        isValid = false;
+
+    CBitcoinAddress address(vchPubKey);
+    isValid = isValid ? address.IsValid() : false;
+
+    Object ret;
+    ret.push_back(Pair("isvalid", isValid));
+    if (isValid)
+    {
+        // Call Hash160ToAddress() so we always return current ADDRESSVERSION
+        // version of the address:
+        string currentAddress = address.ToString();
+        ret.push_back(Pair("address", currentAddress));
+        if (pwalletMain->HaveKey(address))
+        {
+            ret.push_back(Pair("ismine", true));
+            CKey key;
+            key.SetPubKey(vchPubKey);
+            ret.push_back(Pair("iscompressed", key.IsCompressed()));
+        }
+        else
+            ret.push_back(Pair("ismine", false));
+        if (pwalletMain->mapAddressBook.count(address))
+            ret.push_back(Pair("account", pwalletMain->mapAddressBook[address]));
+    }
+    return ret;
+}
+
+
 Value getwork(const Array& params, bool fHelp)
 {
     if (fHelp || params.size() > 1)
@@ -2516,6 +2595,7 @@ static const CRPCCommand vRPCCommands[] =
     { "getblocknumber",         &getblocknumber,         true },
     { "getconnectioncount",     &getconnectioncount,     true },
     { "getdifficulty",          &getdifficulty,          true },
+    { "getpowreward",           &getpowreward,           true },
     { "getgenerate",            &getgenerate,            true },
     { "setgenerate",            &setgenerate,            true },
     { "gethashespersec",        &gethashespersec,        true },
@@ -2539,6 +2619,7 @@ static const CRPCCommand vRPCCommands[] =
     { "walletlock",             &walletlock,             true },
     { "encryptwallet",          &encryptwallet,          false },
     { "validateaddress",        &validateaddress,        true },
+    { "validatepubkey",         &validatepubkey,         true },
     { "getbalance",             &getbalance,             false },
     { "move",                   &movecmd,                false },
     { "sendfrom",               &sendfrom,               false },