Browse Source

Initial work towards z_signmessage+z_verifymessage

z_signmessage
Duke Leto 4 years ago
parent
commit
dad4b8a706
  1. 60
      src/rpc/misc.cpp
  2. 2
      src/rpc/server.h
  3. 58
      src/wallet/rpcwallet.cpp

60
src/rpc/misc.cpp

@ -1,5 +1,6 @@
// Copyright (c) 2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Copyright (c) 2019-2020 The Hush developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
@ -779,6 +780,64 @@ UniValue createmultisig(const UniValue& params, bool fHelp, const CPubKey& mypk)
return result;
}
UniValue z_verifymessage(const UniValue& params, bool fHelp, const CPubKey& mypk)
{
if (fHelp || params.size() != 3)
throw runtime_error(
"z_verifymessage \"zaddr\" \"signature\" \"message\"\n"
"\nVerify a signed message\n"
"\nArguments:\n"
"1. \"zaddr\" (string, required) The Sapling zaddr to use for the signature.\n"
"2. \"signature\" (string, required) The signature provided by the signer in base 64 encoding (see signmessage).\n"
"3. \"message\" (string, required) The message that was signed.\n"
"\nResult:\n"
"true|false (boolean) If the signature is verified or not.\n"
"\nExamples:\n"
"\nCreate the signature\n"
+ HelpExampleCli("z_signmessage", "\"zs1...\" \"my message\"") +
"\nVerify the signature\n"
+ HelpExampleCli("z_verifymessage", "\"zs1...\" \"signature\" \"my message\"") +
"\nAs json rpc\n"
+ HelpExampleRpc("z_verifymessage", "\"zs1...\", \"signature\", \"my message\"")
);
LOCK(cs_main);
string strAddress = params[0].get_str();
string strSign = params[1].get_str();
string strMessage = params[2].get_str();
CTxDestination destination = DecodeDestination(strAddress);
if (!IsValidDestination(destination)) {
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid address");
}
uint32_t branchId = CurrentEpochBranchId(chainActive.Height(), Params().GetConsensus());
// Is it a valid zaddr in this set of consensus rules?
auto res = DecodePaymentAddress(strAddress);
if (!IsValidPaymentAddress(res, branchId)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid zaddr!");
}
bool fInvalid = false;
vector<unsigned char> vchSig = DecodeBase64(strSign.c_str(), &fInvalid);
if (fInvalid)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Malformed base64 encoding");
CHashWriter ss(SER_GETHASH, 0);
ss << strMessageMagic;
ss << strMessage;
//TODO: do the needful
//CPubKey pubkey;
//if (!pubkey.RecoverCompact(ss.GetHash(), vchSig))
// return false;
return false;
}
UniValue verifymessage(const UniValue& params, bool fHelp, const CPubKey& mypk)
{
if (fHelp || params.size() != 3)
@ -1662,6 +1721,7 @@ static const CRPCCommand commands[] =
{ "util", "z_validateaddress", &z_validateaddress, true }, /* uses wallet if enabled */
{ "util", "createmultisig", &createmultisig, true },
{ "util", "verifymessage", &verifymessage, true },
{ "util", "z_verifymessage", &z_verifymessage, true },
/* Not shown in help */
{ "hidden", "setmocktime", &setmocktime, true },

2
src/rpc/server.h

@ -497,6 +497,8 @@ extern UniValue opreturn_burn(const UniValue& params, bool fHelp, const CPubKey&
extern UniValue z_validateaddress(const UniValue& params, bool fHelp, const CPubKey& mypk); // in rpcmisc.cpp
extern UniValue z_getpaymentdisclosure(const UniValue& params, bool fHelp, const CPubKey& mypk); // in rpcdisclosure.cpp
extern UniValue z_validatepaymentdisclosure(const UniValue& params, bool fHelp, const CPubKey& mypk); // in rpcdisclosure.cpp
extern UniValue z_signmessage(const UniValue& params, bool fHelp, const CPubKey& mypk); // in rpcwallet.cpp
extern UniValue z_verifymessage(const UniValue& params, bool fHelp, const CPubKey& mypk); // in rpcwallet.cpp
extern UniValue MoMoMdata(const UniValue& params, bool fHelp, const CPubKey& mypk);
extern UniValue calc_MoM(const UniValue& params, bool fHelp, const CPubKey& mypk);

58
src/wallet/rpcwallet.cpp

@ -867,6 +867,62 @@ UniValue listaddressgroupings(const UniValue& params, bool fHelp, const CPubKey&
return jsonGroupings;
}
UniValue z_signmessage(const UniValue& params, bool fHelp, const CPubKey& mypk)
{
if (!EnsureWalletIsAvailable(fHelp))
return NullUniValue;
if (fHelp || params.size() != 2)
throw runtime_error(
"z_signmessage \"zaddr\" \"message\"\n"
"\nSign a message with the private key of a zaddr"
+ HelpRequiringPassphrase() + "\n"
"\nArguments:\n"
"1. \"zaddr\" (string, required) The Sapling shielded address to use for the private key.\n"
"2. \"message\" (string, required) The message to create a signature of.\n"
"\nResult:\n"
"\"signature\" (string) The signature of the message encoded in base 64\n"
"\nExamples:\n"
"\nCreate the signature\n"
+ HelpExampleCli("z_signmessage", "\"zs1...\" \"my message\"") +
"\nVerify the signature\n"
+ HelpExampleCli("z_verifymessage", "\"zs1...\" \"signature\" \"my message\"") +
"\nAs json rpc\n"
+ HelpExampleRpc("z_signmessage", "\"zs1...\", \"my message\"")
);
LOCK2(cs_main, pwalletMain->cs_wallet);
EnsureWalletIsUnlocked();
string strAddress = params[0].get_str();
string strMessage = params[1].get_str();
uint32_t branchId = CurrentEpochBranchId(chainActive.Height(), Params().GetConsensus());
// Is it a valid zaddr in this set of consensus rules?
auto res = DecodePaymentAddress(strAddress);
if (!IsValidPaymentAddress(res, branchId)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid zaddr!");
}
// Check that we have the spending key
if (!boost::apply_visitor(HaveSpendingKeyForPaymentAddress(pwalletMain), res)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "From address does not belong to this node, zaddr spending key not found.");
}
// TODO: get sig data, serialized, encode, return
CHashWriter ss(SER_GETHASH, 0);
// TODO: different magic?
ss << strMessageMagic;
ss << strMessage;
vector<unsigned char> vchSig;
//TODO: Actually get sig data
//if (!key.SignCompact(ss.GetHash(), vchSig))
// throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Sign failed");
return EncodeBase64(&vchSig[0], vchSig.size());
}
UniValue signmessage(const UniValue& params, bool fHelp, const CPubKey& mypk)
{
if (!EnsureWalletIsAvailable(fHelp))
@ -8158,6 +8214,8 @@ static const CRPCCommand commands[] =
{ "wallet", "setaccount", &setaccount, true },
{ "wallet", "settxfee", &settxfee, true },
{ "wallet", "signmessage", &signmessage, true },
{ "wallet", "z_signmessage", &z_signmessage, true },
{ "wallet", "z_verifymessage", &z_verifymessage, true },
{ "wallet", "walletlock", &walletlock, true },
{ "wallet", "walletpassphrasechange", &walletpassphrasechange, true },
{ "wallet", "walletpassphrase", &walletpassphrase, true },

Loading…
Cancel
Save