From f357d9eb1c0e4a3e72328ddc8cc538267f636b94 Mon Sep 17 00:00:00 2001 From: DeckerSU Date: Mon, 24 Jun 2019 16:00:34 +0300 Subject: [PATCH] add convertpassphrase RPC method (convert Agama passphrases to WIF) --- src/wallet/rpcdump.cpp | 59 ++++++++++++++++++++++++++++++++++++++++ src/wallet/rpcwallet.cpp | 2 ++ 2 files changed, 61 insertions(+) diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 05552b50b..81ee82473 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -89,6 +89,65 @@ std::string DecodeDumpString(const std::string &str) { return ret.str(); } +UniValue convertpassphrase(const UniValue& params, bool fHelp) +{ + if (fHelp || params.size() < 1 || params.size() > 1) + throw runtime_error( + "convertpassphrase \"agamapassphrase\"\n" + "\nConverts Agama passphrase to a private key and WIF (for import with importprivkey).\n" + "\nArguments:\n" + "1. \"agamapassphrase\" (string, required) Agama passphrase\n" + "\nResult:\n" + "\"agamapassphrase\": \"agamapassphrase\", (string) Agama passphrase you entered\n" + "\"address\": \"komodoaddress\", (string) Address corresponding to your passphrase\n" + "\"pubkey\": \"publickeyhex\", (string) The hex value of the raw public key\n" + "\"privkey\": \"privatekeyhex\", (string) The hex value of the raw private key\n" + "\"wif\": \"wif\" (string) The private key in WIF format to use with 'importprivkey'\n" + "\nExamples:\n" + + HelpExampleCli("convertpassphrase", "\"agamapassphrase\"") + + HelpExampleRpc("convertpassphrase", "\"agamapassphrase\"") + ); + + bool fCompressed = true; + string strAgamaPassphrase = params[0].get_str(); + + UniValue ret(UniValue::VOBJ); + ret.push_back(Pair("agamapassphrase", strAgamaPassphrase)); + + CKey tempkey = DecodeSecret(strAgamaPassphrase); + /* first we should check if user pass wif to method, instead of passphrase */ + if (!tempkey.IsValid()) { + /* it's a passphrase, not wif */ + uint256 sha256; + CSHA256().Write((const unsigned char *)strAgamaPassphrase.c_str(), strAgamaPassphrase.length()).Finalize(sha256.begin()); + std::vector privkey(sha256.begin(), sha256.begin() + sha256.size()); + privkey.front() &= 0xf8; + privkey.back() &= 0x7f; + privkey.back() |= 0x40; + CKey key; + key.Set(privkey.begin(),privkey.end(), fCompressed); + CPubKey pubkey = key.GetPubKey(); + assert(key.VerifyPubKey(pubkey)); + CKeyID vchAddress = pubkey.GetID(); + + ret.push_back(Pair("address", EncodeDestination(vchAddress))); + ret.push_back(Pair("pubkey", HexStr(pubkey))); + ret.push_back(Pair("privkey", HexStr(privkey))); + ret.push_back(Pair("wif", EncodeSecret(key))); + } else { + /* seems it's a wif */ + CPubKey pubkey = tempkey.GetPubKey(); + assert(tempkey.VerifyPubKey(pubkey)); + CKeyID vchAddress = pubkey.GetID(); + ret.push_back(Pair("address", EncodeDestination(vchAddress))); + ret.push_back(Pair("pubkey", HexStr(pubkey))); + ret.push_back(Pair("privkey", HexStr(tempkey))); + ret.push_back(Pair("wif", strAgamaPassphrase)); + } + + return ret; +} + UniValue importprivkey(const UniValue& params, bool fHelp) { if (!EnsureWalletIsAvailable(fHelp)) diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 6a47e71a0..ce377d094 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -7977,6 +7977,7 @@ UniValue heirlist(const UniValue& params, bool fHelp) extern UniValue dumpprivkey(const UniValue& params, bool fHelp); // in rpcdump.cpp +extern UniValue convertpassphrase(const UniValue& params, bool fHelp); extern UniValue importprivkey(const UniValue& params, bool fHelp); extern UniValue importaddress(const UniValue& params, bool fHelp); extern UniValue dumpwallet(const UniValue& params, bool fHelp); @@ -8012,6 +8013,7 @@ static const CRPCCommand commands[] = { "wallet", "gettransaction", &gettransaction, false }, { "wallet", "getunconfirmedbalance", &getunconfirmedbalance, false }, { "wallet", "getwalletinfo", &getwalletinfo, false }, + { "wallet", "convertpassphrase", &convertpassphrase, true }, { "wallet", "importprivkey", &importprivkey, true }, { "wallet", "importwallet", &importwallet, true }, { "wallet", "importaddress", &importaddress, true },