Examples and missed file.
author0xDEADFACE <masmfan@gmail.com>
Sun, 14 Feb 2016 18:13:06 +0000 (10:13 -0800)
committer0xDEADFACE <masmfan@gmail.com>
Sun, 14 Feb 2016 18:13:06 +0000 (10:13 -0800)
doc/EncryptRPC.txt [new file with mode: 0644]
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
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);
+}