PPCoin: Replace RPC command 'setcheckpointkey' with option '-checkpointkey'
authorSunny King <sunnyking9999@gmail.com>
Sun, 29 Jul 2012 21:09:43 +0000 (22:09 +0100)
committerSunny King <sunnyking9999@gmail.com>
Sun, 29 Jul 2012 21:09:43 +0000 (22:09 +0100)
src/bitcoinrpc.cpp
src/checkpoints.cpp
src/checkpoints.h
src/init.cpp

index 04faea9..e650eb0 100644 (file)
@@ -2024,7 +2024,9 @@ Value getcheckpoint(const Array& params, bool fHelp)
     pindexCheckpoint = mapBlockIndex[Checkpoints::hashSyncCheckpoint];        
     result.push_back(Pair("height", pindexCheckpoint->nHeight));
     result.push_back(Pair("timestamp", DateTimeStrFormat(pindexCheckpoint->GetBlockTime()).c_str()));
-    
+    if (mapArgs.count("-checkpointkey"))
+        result.push_back(Pair("checkpointmaster", true));
+
     return result;
 }
 
@@ -2211,31 +2213,6 @@ Value sendalert(const Array& params, bool fHelp)
     return result;
 }
 
-// ppcoin: set checkpoint key
-Value setcheckpointkey(const Array& params, bool fHelp)
-{
-    if (fHelp || params.size() != 1)
-        throw runtime_error(
-            "setcheckpointkey <privatekey>\n"
-            "<privatekey> is hex string of checkpoint master private key\n");
-
-    CSyncCheckpoint checkpoint;
-    checkpoint.hashCheckpoint = hashGenesisBlock;
-    CDataStream sMsg(SER_NETWORK, PROTOCOL_VERSION);
-    sMsg << (CUnsignedSyncCheckpoint)checkpoint;
-    checkpoint.vchMsg = vector<unsigned char>(sMsg.begin(), sMsg.end());
-
-    vector<unsigned char> vchPrivKey = ParseHex(params[0].get_str());
-    CKey key;
-    key.SetPrivKey(CPrivKey(vchPrivKey.begin(), vchPrivKey.end())); // if key is not correct openssl may crash
-    if (!key.Sign(Hash(checkpoint.vchMsg.begin(), checkpoint.vchMsg.end()), checkpoint.vchSig))
-        throw runtime_error(
-            "Unable to sign checkpoint, check private key?\n");
-
-    CSyncCheckpoint::strMasterPrivKey = params[0].get_str();
-
-    return "checkpoint master key has been set.";
-}
 
 
 //
@@ -2298,7 +2275,6 @@ static const CRPCCommand vRPCCommands[] =
     { "repairwallet",           &repairwallet,           false},
     { "makekeypair",            &makekeypair,            false},
     { "sendalert",              &sendalert,              false},
-    { "setcheckpointkey",       &setcheckpointkey,       false},
 };
 
 CRPCTable::CRPCTable()
index 5bb9df6..7de5883 100644 (file)
@@ -288,6 +288,26 @@ namespace Checkpoints
             pfrom->AskFor(CInv(MSG_BLOCK, hashPendingCheckpoint));
     }
 
+    bool SetCheckpointPrivKey(std::string strPrivKey)
+    {
+        // Test signing a sync-checkpoint with genesis block
+        CSyncCheckpoint checkpoint;
+        checkpoint.hashCheckpoint = hashGenesisBlock;
+        CDataStream sMsg(SER_NETWORK, PROTOCOL_VERSION);
+        sMsg << (CUnsignedSyncCheckpoint)checkpoint;
+        checkpoint.vchMsg = std::vector<unsigned char>(sMsg.begin(), sMsg.end());
+
+        std::vector<unsigned char> vchPrivKey = ParseHex(strPrivKey);
+        CKey key;
+        key.SetPrivKey(CPrivKey(vchPrivKey.begin(), vchPrivKey.end())); // if key is not correct openssl may crash
+        if (!key.Sign(Hash(checkpoint.vchMsg.begin(), checkpoint.vchMsg.end()), checkpoint.vchSig))
+            return false;
+
+        // Test signing successful, proceed
+        CSyncCheckpoint::strMasterPrivKey = strPrivKey;
+        return true;
+    }
+
     bool SendSyncCheckpoint(uint256 hashCheckpoint)
     {
         CSyncCheckpoint checkpoint;
index be18c79..f14e610 100644 (file)
@@ -43,6 +43,7 @@ namespace Checkpoints
     bool WantedByPendingSyncCheckpoint(uint256 hashBlock);
     bool ResetSyncCheckpoint();
     void AskForPendingSyncCheckpoint(CNode* pfrom);
+    bool SetCheckpointPrivKey(std::string strPrivKey);
     bool SendSyncCheckpoint(uint256 hashCheckpoint);
 }
 
index 8635ace..62b6103 100644 (file)
@@ -10,6 +10,7 @@
 #include "init.h"
 #include "util.h"
 #include "ui_interface.h"
+#include "checkpoints.h"
 #include <boost/filesystem.hpp>
 #include <boost/filesystem/fstream.hpp>
 #include <boost/filesystem/convenience.hpp>
@@ -595,6 +596,12 @@ bool AppInit2(int argc, char* argv[])
         }
     }
 
+    if (mapArgs.count("-checkpointkey")) // ppcoin: checkpoint master priv key
+    {
+        if (!Checkpoints::SetCheckpointPrivKey(GetArg("-checkpointkey", "")))
+            ThreadSafeMessageBox(_("Unable to sign checkpoint, wrong checkpointkey?\n"), _("PPCoin"), wxOK | wxMODAL);
+    }
+
     //
     // Start the node
     //