Skip to content

Commit

Permalink
[GRPH-3] Additional cli tests (#155)
Browse files Browse the repository at this point in the history
* Additional cli tests

* Compatible with latest fc changes

* Fixed Spacing issues
  • Loading branch information
srpatel19590 authored and bobinson committed Oct 10, 2019
1 parent e1a6e67 commit ec33f0c
Show file tree
Hide file tree
Showing 3 changed files with 529 additions and 11 deletions.
46 changes: 46 additions & 0 deletions libraries/wallet/include/graphene/wallet/wallet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,11 @@ class wallet_api
* @ingroup Transaction Builder API
*/
signed_transaction sign_builder_transaction(transaction_handle_type transaction_handle, bool broadcast = true);
/** Broadcast signed transaction
* @param tx signed transaction
* @returns the transaction ID along with the signed transaction.
*/
pair<transaction_id_type,signed_transaction> broadcast_transaction(signed_transaction tx);
/**
* @ingroup Transaction Builder API
*/
Expand Down Expand Up @@ -596,6 +601,12 @@ class wallet_api
*/
bool load_wallet_file(string wallet_filename = "");

/** Quitting from Peerplays wallet.
*
* The current wallet will be closed.
*/
void quit();

/** Saves the current wallet to the given filename.
*
* @warning This does not change the wallet filename that will be used for future
Expand Down Expand Up @@ -1513,6 +1524,37 @@ class wallet_api
*/
signed_transaction sign_transaction(signed_transaction tx, bool broadcast = false);

/** Get transaction signers.
*
* Returns information about who signed the transaction, specifically,
* the corresponding public keys of the private keys used to sign the transaction.
* @param tx the signed transaction
* @return the set of public_keys
*/
flat_set<public_key_type> get_transaction_signers(const signed_transaction &tx) const;

/** Get key references.
*
* Returns accounts related to given public keys.
* @param keys public keys to search for related accounts
* @return the set of related accounts
*/
vector<vector<account_id_type>> get_key_references(const vector<public_key_type> &keys) const;

/** Signs a transaction.
*
* Given a fully-formed transaction with or without signatures, signs
* the transaction with the owned keys and optionally broadcasts the
* transaction.
*
* @param tx the unsigned transaction
* @param broadcast true if you wish to broadcast the transaction
*
* @return the signed transaction
*/
signed_transaction add_transaction_signature( signed_transaction tx,
bool broadcast = false );

/** Returns an uninitialized object representing a given blockchain operation.
*
* This returns a default-initialized object of the given type; it can be used
Expand Down Expand Up @@ -1920,6 +1962,7 @@ FC_API( graphene::wallet::wallet_api,
(set_fees_on_builder_transaction)
(preview_builder_transaction)
(sign_builder_transaction)
(broadcast_transaction)
(propose_builder_transaction)
(propose_builder_transaction2)
(remove_builder_transaction)
Expand Down Expand Up @@ -2005,6 +2048,9 @@ FC_API( graphene::wallet::wallet_api,
(save_wallet_file)
(serialize_transaction)
(sign_transaction)
(get_transaction_signers)
(get_key_references)
(add_transaction_signature)
(get_prototype_operation)
(propose_parameter_change)
(propose_fee_change)
Expand Down
131 changes: 131 additions & 0 deletions libraries/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ class wallet_api_impl
private:
void claim_registered_account(const account_object& account)
{
bool import_keys = false;
auto it = _wallet.pending_account_registrations.find( account.name );
FC_ASSERT( it != _wallet.pending_account_registrations.end() );
for (const std::string& wif_key : it->second)
Expand All @@ -289,8 +290,13 @@ class wallet_api_impl
// possibility of migrating to a fork where the
// name is available, the user can always
// manually re-register)
} else {
import_keys = true;
}
_wallet.pending_account_registrations.erase( it );

if (import_keys)
save_wallet_file();
}

// after a witness registration succeeds, this saves the private key in the wallet permanently
Expand Down Expand Up @@ -599,6 +605,13 @@ class wallet_api_impl
fc::async([this, object]{subscribed_object_changed(object);}, "Object changed");
}

void quit()
{
ilog( "Quitting Cli Wallet ..." );

throw fc::canceled_exception();
}

bool copy_wallet_file( string destination_filename )
{
fc::path src_path = get_wallet_filename();
Expand Down Expand Up @@ -1147,6 +1160,20 @@ class wallet_api_impl

return _builder_transactions[transaction_handle] = sign_transaction(_builder_transactions[transaction_handle], broadcast);
}

pair<transaction_id_type,signed_transaction> broadcast_transaction(signed_transaction tx)
{
try {
_remote_net_broadcast->broadcast_transaction(tx);
}
catch (const fc::exception& e) {
elog("Caught exception while broadcasting tx ${id}: ${e}",
("id", tx.id().str())("e", e.to_detail_string()));
throw;
}
return std::make_pair(tx.id(),tx);
}

signed_transaction propose_builder_transaction(
transaction_handle_type handle,
time_point_sec expiration = time_point::now() + fc::minutes(1),
Expand Down Expand Up @@ -2250,6 +2277,84 @@ class wallet_api_impl
return tx;
}

flat_set<public_key_type> get_transaction_signers(const signed_transaction &tx) const
{
return tx.get_signature_keys(_chain_id);
}

vector<vector<account_id_type>> get_key_references(const vector<public_key_type> &keys) const
{
return _remote_db->get_key_references(keys);
}

/**
* Get the required public keys to sign the transaction which had been
* owned by us
*
* NOTE, if `erase_existing_sigs` set to true, the original trasaction's
* signatures will be erased
*
* @param tx The transaction to be signed
* @param erase_existing_sigs
* The transaction could have been partially signed already,
* if set to false, the corresponding public key of existing
* signatures won't be returned.
* If set to true, the existing signatures will be erased and
* all required keys returned.
*/
set<public_key_type> get_owned_required_keys( signed_transaction &tx,
bool erase_existing_sigs = true)
{
set<public_key_type> pks = _remote_db->get_potential_signatures( tx );
flat_set<public_key_type> owned_keys;
owned_keys.reserve( pks.size() );
std::copy_if( pks.begin(), pks.end(),
std::inserter( owned_keys, owned_keys.end() ),
[this]( const public_key_type &pk ) {
return _keys.find( pk ) != _keys.end();
} );

if ( erase_existing_sigs )
tx.signatures.clear();

return _remote_db->get_required_signatures( tx, owned_keys );
}

signed_transaction add_transaction_signature( signed_transaction tx,
bool broadcast )
{
set<public_key_type> approving_key_set = get_owned_required_keys(tx, false);

if ( ( ( tx.ref_block_num == 0 && tx.ref_block_prefix == 0 ) ||
tx.expiration == fc::time_point_sec() ) &&
tx.signatures.empty() )
{
auto dyn_props = get_dynamic_global_properties();
auto parameters = get_global_properties().parameters;
fc::time_point_sec now = dyn_props.time;
tx.set_reference_block( dyn_props.head_block_id );
tx.set_expiration( now + parameters.maximum_time_until_expiration );
}
for ( const public_key_type &key : approving_key_set )
tx.sign( get_private_key( key ), _chain_id );

if ( broadcast )
{
try
{
_remote_net_broadcast->broadcast_transaction( tx );
}
catch ( const fc::exception &e )
{
elog( "Caught exception while broadcasting tx ${id}: ${e}",
( "id", tx.id().str() )( "e", e.to_detail_string() ) );
FC_THROW( "Caught exception while broadcasting tx" );
}
}

return tx;
}

signed_transaction sell_asset(string seller_account,
string amount_to_sell,
string symbol_to_sell,
Expand Down Expand Up @@ -3645,6 +3750,11 @@ signed_transaction wallet_api::sign_builder_transaction(transaction_handle_type
return my->sign_builder_transaction(transaction_handle, broadcast);
}

pair<transaction_id_type,signed_transaction> wallet_api::broadcast_transaction(signed_transaction tx)
{
return my->broadcast_transaction(tx);
}

signed_transaction wallet_api::propose_builder_transaction(
transaction_handle_type handle,
time_point_sec expiration,
Expand Down Expand Up @@ -4117,6 +4227,22 @@ signed_transaction wallet_api::sign_transaction(signed_transaction tx, bool broa
return my->sign_transaction( tx, broadcast);
} FC_CAPTURE_AND_RETHROW( (tx) ) }

signed_transaction wallet_api::add_transaction_signature( signed_transaction tx,
bool broadcast )
{
return my->add_transaction_signature( tx, broadcast );
}

flat_set<public_key_type> wallet_api::get_transaction_signers(const signed_transaction &tx) const
{ try {
return my->get_transaction_signers(tx);
} FC_CAPTURE_AND_RETHROW( (tx) ) }

vector<vector<account_id_type>> wallet_api::get_key_references(const vector<public_key_type> &keys) const
{ try {
return my->get_key_references(keys);
} FC_CAPTURE_AND_RETHROW( (keys) ) }

operation wallet_api::get_prototype_operation(string operation_name)
{
return my->get_prototype_operation( operation_name );
Expand Down Expand Up @@ -4304,6 +4430,11 @@ bool wallet_api::load_wallet_file( string wallet_filename )
return my->load_wallet_file( wallet_filename );
}

void wallet_api::quit()
{
my->quit();
}

void wallet_api::save_wallet_file( string wallet_filename )
{
my->save_wallet_file( wallet_filename );
Expand Down
Loading

0 comments on commit ec33f0c

Please sign in to comment.