Merge pull request #275 from svost/patch
authorCryptoManiac <CryptoManiac@users.noreply.github.com>
Sun, 14 Feb 2016 19:17:22 +0000 (22:17 +0300)
committerCryptoManiac <CryptoManiac@users.noreply.github.com>
Sun, 14 Feb 2016 19:17:22 +0000 (22:17 +0300)
fDetachDB missed in constructor

doc/EncryptRPC.txt [new file with mode: 0644]
novacoin-qt.pro
src/bitcoinrpc.cpp
src/bitcoinrpc.h
src/main.cpp
src/makefile.bsd
src/makefile.linux-mingw
src/makefile.mingw
src/makefile.osx
src/makefile.unix
src/rpccrypt.cpp [new file with mode: 0644]

diff --git a/doc/EncryptRPC.txt b/doc/EncryptRPC.txt
new file mode 100644 (file)
index 0000000..e6ae1a0
--- /dev/null
@@ -0,0 +1,32 @@
+Encryption RPC api allows you to encrypt an arbitrary data bytes with some public key. Only owner of corresponding private key will be able to recover the original data stream.
+
+Example
+
+1. Let's generate new novacoin address first.
+
+> getnewaddress
+4NZFZZS9b8iawVA8sYtHsqNbpq57envuBz
+
+2. Then run validateaddress to dump its public key
+
+> validateaddress 4NZFZZS9b8iawVA8sYtHsqNbpq57envuBz
+{
+"isvalid" : true,
+"address" : "4NZFZZS9b8iawVA8sYtHsqNbpq57envuBz",
+"ismine" : true,
+"watchonly" : false,
+"isscript" : false,
+"pubkey" : "023ca82f71f40d18f5cf6a696367a4172b87d7b3db5f45086bdb4bd4a3e3e9bde9",
+"iscompressed" : true,
+"account" : ""
+}
+
+3. Trying to encrypt hex representation of "Hello world!" string using this public key.
+
+> encryptdata 023ca82f71f40d18f5cf6a696367a4172b87d7b3db5f45086bdb4bd4a3e3e9bde9 48656c6c6f20776f726c6421
+02a2fe5afb10c40fc64552e1b81d8b4e991838d50c6a7129bd9f466eee29a5ab9def7cc5a3b1b526d59d06178fa4471b778e80bf8f72ae34889e58a4568f8ad2a3ecc9004a
+
+4. Now we should be able to decrypt that cryptogram into original byte stream.
+
+> decryptdata 4NZFZZS9b8iawVA8sYtHsqNbpq57envuBz 02a2fe5afb10c40fc64552e1b81d8b4e991838d50c6a7129bd9f466eee29a5ab9def7cc5a3b1b526d59d06178fa4471b778e80bf8f72ae34889e58a4568f8ad2a3ecc9004a
+48656c6c6f20776f726c6421
index 1759e98..52f9959 100644 (file)
@@ -313,6 +313,7 @@ SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \
     src/qt/transactionview.cpp \
     src/qt/walletmodel.cpp \
     src/bitcoinrpc.cpp \
+    src/rpccrypt.cpp \
     src/rpcdump.cpp \
     src/rpcnet.cpp \
     src/rpcmining.cpp \
index c6b64fe..5535b9b 100644 (file)
@@ -321,6 +321,8 @@ static const CRPCCommand vRPCCommands[] =
     { "adjustmalleablepubkey",  &adjustmalleablepubkey,  false,  false},
     { "listmalleableviews",     &listmalleableviews,     false,  false},
     { "dumpmalleablekey",       &dumpmalleablekey,       false,  false},
+    { "encryptdata",            &encryptdata,            false,  false },
+    { "decryptdata",            &decryptdata,            false,  false },
     { "sendalert",              &sendalert,              false,  false},
 };
 
index abadd8b..d18e5f6 100644 (file)
@@ -212,6 +212,9 @@ extern json_spirit::Value adjustmalleablepubkey(const json_spirit::Array& params
 extern json_spirit::Value listmalleableviews(const json_spirit::Array& params, bool fHelp);
 extern json_spirit::Value dumpmalleablekey(const json_spirit::Array& params, bool fHelp);
 
+extern json_spirit::Value encryptdata(const json_spirit::Array& params, bool fHelp); // in rpccrypt.cpp
+extern json_spirit::Value decryptdata(const json_spirit::Array& params, bool fHelp);
+
 extern json_spirit::Value getrawtransaction(const json_spirit::Array& params, bool fHelp); // in rcprawtransaction.cpp
 extern json_spirit::Value listunspent(const json_spirit::Array& params, bool fHelp);
 extern json_spirit::Value createrawtransaction(const json_spirit::Array& params, bool fHelp);
index 68ae6a2..8a96379 100644 (file)
@@ -1190,7 +1190,7 @@ const CBlockIndex* GetLastBlockIndex(const CBlockIndex* pindex, bool fProofOfSta
 unsigned int GetNextTargetRequired(const CBlockIndex* pindexLast, bool fProofOfStake)
 {
     if (pindexLast == NULL)
-        return bnTargetLimit.GetCompact(); // genesis block
+        return bnProofOfWorkLimit.GetCompact(); // genesis block
 
     CBigNum bnTargetLimit = !fProofOfStake ? bnProofOfWorkLimit : GetProofOfStakeLimit(pindexLast->nHeight, pindexLast->nTime);
 
index a22ce33..7afc86d 100644 (file)
@@ -118,6 +118,7 @@ OBJS= \
     obj/stun.o \
     obj/protocol.o \
     obj/bitcoinrpc.o \
+    obj/rpccrypt.o \
     obj/rpcdump.o \
     obj/rpcnet.o \
     obj/rpcmining.o \
index e255112..aadac6b 100644 (file)
@@ -85,6 +85,7 @@ OBJS= \
     obj/stun.o \
     obj/protocol.o \
     obj/bitcoinrpc.o \
+    obj/rpccrypt.o \
     obj/rpcdump.o \
     obj/rpcnet.o \
     obj/rpcmining.o \
index e0bd3e0..368ca62 100644 (file)
@@ -75,6 +75,7 @@ OBJS= \
     obj/stun.o \
     obj/protocol.o \
     obj/bitcoinrpc.o \
+    obj/rpccrypt.o \
     obj/rpcdump.o \
     obj/rpcnet.o \
     obj/rpcmining.o \
index 96cf542..a057b9e 100644 (file)
@@ -82,6 +82,7 @@ OBJS= \
     obj/stun.o \
     obj/protocol.o \
     obj/bitcoinrpc.o \
+    obj/rpccrypt.o \
     obj/rpcdump.o \
     obj/rpcnet.o \
     obj/rpcmining.o \
index a37bf0e..2dddfa6 100644 (file)
@@ -119,6 +119,7 @@ OBJS= \
     obj/stun.o \
     obj/protocol.o \
     obj/bitcoinrpc.o \
+    obj/rpccrypt.o \
     obj/rpcdump.o \
     obj/rpcnet.o \
     obj/rpcmining.o \
diff --git a/src/rpccrypt.cpp b/src/rpccrypt.cpp
new file mode 100644 (file)
index 0000000..0569176
--- /dev/null
@@ -0,0 +1,52 @@
+// Copyright (c) 2010 Satoshi Nakamoto
+// Copyright (c) 2009-2012 The Bitcoin developers
+// Distributed under the MIT/X11 software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#include "wallet.h"
+#include "walletdb.h"
+#include "bitcoinrpc.h"
+#include "init.h"
+#include "util.h"
+#include "ntp.h"
+#include "base58.h"
+
+using namespace json_spirit;
+using namespace std;
+
+Value encryptdata(const Array& params, bool fHelp)
+{
+    if (fHelp || params.size() != 2)
+        throw runtime_error(
+            "encryptdata <public key> <hex data>\n"
+            "Encrypt octet stream with provided public key..\n");
+
+    CPubKey pubKey(ParseHex(params[0].get_str()));
+
+    vector<unsigned char> vchEncrypted;
+    pubKey.EncryptData(ParseHex(params[1].get_str()), vchEncrypted);
+
+    return HexStr(vchEncrypted);
+}
+
+Value decryptdata(const Array& params, bool fHelp)
+{
+    if (fHelp || params.size() != 2)
+        throw runtime_error(
+            "decryptdata <novacoin address> <encrypted stream>\n"
+            "Decrypt octet stream.\n");
+
+    EnsureWalletIsUnlocked();
+    CBitcoinAddress addr(params[0].get_str());
+
+    CKeyID keyID;
+    addr.GetKeyID(keyID);
+
+    CKey key;
+    pwalletMain->GetKey(keyID, key);
+
+    vector<unsigned char> vchDecrypted;
+    key.DecryptData(ParseHex(params[1].get_str()), vchDecrypted);
+
+    return HexStr(vchDecrypted);
+}