Add unit tests for wallet keypair generation:

* Allow `passphrase` to be a seed encoded in any of three formats or a
    literal passphrase.
  * Recognize the absence of `passphrase` as requesting a random seed.

Extract walletPropose() and keypairForSignature() as separately factored
functions (from doWalletPropose() and transactionSign() respectively) to
facilitate unit testing.
This commit is contained in:
Josh Juran
2015-02-02 16:09:37 -08:00
committed by Tom Ritchford
parent 3ec88b3665
commit 436ded68b7
9 changed files with 347 additions and 23 deletions

View File

@@ -0,0 +1,55 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012-2015 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <ripple/rpc/impl/KeypairForSignature.h>
namespace ripple {
namespace RPC {
KeyPair keypairForSignature (Json::Value const& params, Json::Value& error)
{
if (! params.isMember ("secret"))
{
error = RPC::missing_field_error ("secret");
return KeyPair();
}
RippleAddress seed;
if (! seed.setSeedGeneric (params["secret"].asString ()))
{
error = RPC::make_error (rpcBAD_SEED,
RPC::invalid_field_message ("secret"));
return KeyPair();
}
KeyPair result;
RippleAddress generator = RippleAddress::createGeneratorPublic (seed);
result.secretKey.setAccountPrivate (generator, seed, 0);
result.publicKey.setAccountPublic (generator, 0);
return result;
}
} // RPC
} // ripple

View File

@@ -0,0 +1,40 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2015 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef RIPPLE_RPC_SIGNATUREKEYPAIR_H_INCLUDED
#define RIPPLE_RPC_SIGNATUREKEYPAIR_H_INCLUDED
#include <ripple/json/json_reader.h>
#include <ripple/protocol/RippleAddress.h>
namespace ripple {
namespace RPC {
struct KeyPair
{
RippleAddress secretKey;
RippleAddress publicKey;
};
KeyPair keypairForSignature (Json::Value const& params, Json::Value& error);
} // RPC
} // ripple
#endif

View File

@@ -22,6 +22,7 @@
#include <ripple/basics/StringUtilities.h>
#include <ripple/json/json_reader.h>
#include <ripple/protocol/TxFlags.h>
#include <ripple/rpc/impl/KeypairForSignature.h>
#include <ripple/rpc/impl/TransactionSign.h>
#include <beast/unit_test/suite.h>
@@ -335,18 +336,16 @@ transactionSign (
WriteLog (lsDEBUG, RPCHandler) << "transactionSign: " << params;
if (! params.isMember ("secret"))
return RPC::missing_field_error ("secret");
KeyPair const keypair = keypairForSignature (params, jvResult);
if (contains_error (jvResult))
{
return jvResult;
}
if (! params.isMember ("tx_json"))
return RPC::missing_field_error ("tx_json");
RippleAddress naSeed;
if (! naSeed.setSeedGeneric (params["secret"].asString ()))
return RPC::make_error (rpcBAD_SEED,
RPC::invalid_field_message ("secret"));
Json::Value& tx_json (params ["tx_json"]);
if (! tx_json.isObject ())
@@ -426,20 +425,13 @@ transactionSign (
return rpcError (rpcSRC_ACT_NOT_FOUND);
}
RippleAddress secret = RippleAddress::createSeedGeneric (
params["secret"].asString ());
RippleAddress masterGenerator = RippleAddress::createGeneratorPublic (
secret);
RippleAddress masterAccountPublic = RippleAddress::createAccountPublic (
masterGenerator, 0);
if (verify)
{
WriteLog (lsTRACE, RPCHandler) <<
"verify: " << masterAccountPublic.humanAccountID () <<
"verify: " << keypair.publicKey.humanAccountID() <<
" : " << raSrcAddressID.humanAccountID ();
auto const secretAccountID = masterAccountPublic.getAccountID();
auto const secretAccountID = keypair.publicKey.getAccountID();
if (raSrcAddressID.getAccountID () == secretAccountID)
{
if (ledgerFacade.accountMasterDisabled ())
@@ -462,7 +454,7 @@ transactionSign (
std::unique_ptr<STObject> sopTrans = std::move(parsed.object);
sopTrans->setFieldVL (
sfSigningPubKey,
masterAccountPublic.getAccountPublic ());
keypair.publicKey.getAccountPublic());
STTx::pointer stpTrans;
@@ -489,10 +481,8 @@ transactionSign (
// FIXME: For performance, transactions should not be signed in this code
// path.
RippleAddress naAccountPrivate = RippleAddress::createAccountPrivate (
masterGenerator, secret, 0);
stpTrans->sign (naAccountPrivate);
stpTrans->sign (keypair.secretKey);
Transaction::pointer tpTrans;