Reuse "generate"

This commit is contained in:
UdjinM6 2019-04-05 18:59:41 +03:00
parent 0f25d020b5
commit c12388c5b5

View File

@ -1141,8 +1141,10 @@ UniValue protx(const JSONRPCRequest& request)
void bls_generate_help() void bls_generate_help()
{ {
throw std::runtime_error( throw std::runtime_error(
"bls generate\n" "bls generate ( \"secret\" )\n"
"\nReturns a BLS secret/public key pair.\n" "\nCreate a new BLS secret/public key pair or restore one from a provided BLS secret key.\n"
"\nArguments:\n"
"1. \"secret\" (string, optional) The BLS secret key.\n"
"\nResult:\n" "\nResult:\n"
"{\n" "{\n"
" \"secret\": \"xxxx\", (string) BLS secret key\n" " \"secret\": \"xxxx\", (string) BLS secret key\n"
@ -1150,49 +1152,20 @@ void bls_generate_help()
"}\n" "}\n"
"\nExamples:\n" "\nExamples:\n"
+ HelpExampleCli("bls generate", "") + HelpExampleCli("bls generate", "")
+ HelpExampleCli("bls generate", "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")
); );
} }
UniValue bls_generate(const JSONRPCRequest& request) UniValue bls_generate(const JSONRPCRequest& request)
{ {
if (request.fHelp || request.params.size() != 1) { if (request.fHelp || (request.params.size() != 1 && request.params.size() != 2)) {
bls_generate_help(); bls_generate_help();
} }
CBLSSecretKey sk; CBLSSecretKey sk;
sk.MakeNewKey(); if (request.params.size() == 1) {
sk.MakeNewKey();
UniValue ret(UniValue::VOBJ); } else if (!sk.SetHexStr(request.params[1].get_str())) {
ret.push_back(Pair("secret", sk.ToString()));
ret.push_back(Pair("public", sk.GetPublicKey().ToString()));
return ret;
}
void bls_sk2pk_help()
{
throw std::runtime_error(
"bls sk2pk \"secret\"\n"
"\nParses a BLS secret key and returns the secret/public key pair.\n"
"\nArguments:\n"
"1. \"secret\" (string, required) The BLS secret key\n"
"\nResult:\n"
"{\n"
" \"secret\": \"xxxx\", (string) BLS secret key\n"
" \"public\": \"xxxx\", (string) BLS public key\n"
"}\n"
"\nExamples:\n"
+ HelpExampleCli("bls sk2pk", "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")
);
}
UniValue bls_sk2pk(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() != 2) {
bls_sk2pk_help();
}
CBLSSecretKey sk;
if (!sk.SetHexStr(request.params[1].get_str())) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Secret key must be a valid hex string of length %d", sk.SerSize*2)); throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Secret key must be a valid hex string of length %d", sk.SerSize*2));
} }
@ -1211,8 +1184,7 @@ UniValue bls_sk2pk(const JSONRPCRequest& request)
"\nArguments:\n" "\nArguments:\n"
"1. \"command\" (string, required) The command to execute\n" "1. \"command\" (string, required) The command to execute\n"
"\nAvailable commands:\n" "\nAvailable commands:\n"
" generate - Create a BLS secret/public key pair\n" " generate - Create a new BLS secret/public key pair or restore one from a provided BLS secret key\n"
" sk2pk - Parse a BLS secret key and return the secret/public key pair\n"
); );
} }
@ -1229,8 +1201,6 @@ UniValue _bls(const JSONRPCRequest& request)
if (command == "generate") { if (command == "generate") {
return bls_generate(request); return bls_generate(request);
} else if (command == "sk2pk") {
return bls_sk2pk(request);
} else { } else {
bls_help(); bls_help();
} }