Skip to content

Commit de31b56

Browse files
authored
Merge pull request #1032 from cifer-lee/issue867
Issue867 Add CLI command to add signatures to a partially signed transaction
2 parents 5162ebe + 26ffeef commit de31b56

File tree

3 files changed

+223
-10
lines changed

3 files changed

+223
-10
lines changed

libraries/wallet/include/graphene/wallet/wallet.hpp

+15
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,20 @@ class wallet_api
16381638

16391639
order_book get_order_book( const string& base, const string& quote, unsigned limit = 50);
16401640

1641+
/** Signs a transaction.
1642+
*
1643+
* Given a fully-formed transaction with or without signatures, signs
1644+
* the transaction with the owned keys and optionally broadcasts the
1645+
* transaction.
1646+
*
1647+
* @param tx the unsigned transaction
1648+
* @param broadcast true if you wish to broadcast the transaction
1649+
*
1650+
* @return the signed transaction
1651+
*/
1652+
signed_transaction add_transaction_signature( signed_transaction tx,
1653+
bool broadcast = false );
1654+
16411655
void dbg_make_uia(string creator, string symbol);
16421656
void dbg_make_mia(string creator, string symbol);
16431657
void dbg_push_blocks( std::string src_filename, uint32_t count );
@@ -1828,6 +1842,7 @@ FC_API( graphene::wallet::wallet_api,
18281842
(save_wallet_file)
18291843
(serialize_transaction)
18301844
(sign_transaction)
1845+
(add_transaction_signature)
18311846
(get_prototype_operation)
18321847
(propose_parameter_change)
18331848
(propose_fee_change)

libraries/wallet/wallet.cpp

+76-10
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,74 @@ class wallet_api_impl
789789
return true;
790790
}
791791

792+
/**
793+
* Get the required public keys to sign the transaction which had been
794+
* owned by us
795+
*
796+
* NOTE, if `erase_existing_sigs` set to true, the original trasaction's
797+
* signatures will be erased
798+
*
799+
* @param tx The transaction to be signed
800+
* @param erase_existing_sigs
801+
* The transaction could have been partially signed already,
802+
* if set to false, the corresponding public key of existing
803+
* signatures won't be returned.
804+
* If set to true, the existing signatures will be erased and
805+
* all required keys returned.
806+
*/
807+
set<public_key_type> get_owned_required_keys( signed_transaction &tx,
808+
bool erase_existing_sigs = true)
809+
{
810+
set<public_key_type> pks = _remote_db->get_potential_signatures( tx );
811+
flat_set<public_key_type> owned_keys;
812+
owned_keys.reserve( pks.size() );
813+
std::copy_if( pks.begin(), pks.end(),
814+
std::inserter( owned_keys, owned_keys.end() ),
815+
[this]( const public_key_type &pk ) {
816+
return _keys.find( pk ) != _keys.end();
817+
} );
818+
819+
if ( erase_existing_sigs )
820+
tx.signatures.clear();
821+
822+
return _remote_db->get_required_signatures( tx, owned_keys );
823+
}
824+
825+
signed_transaction add_transaction_signature( signed_transaction tx,
826+
bool broadcast )
827+
{
828+
set<public_key_type> approving_key_set = get_owned_required_keys(tx, false);
829+
830+
if ( ( ( tx.ref_block_num == 0 && tx.ref_block_prefix == 0 ) ||
831+
tx.expiration == fc::time_point_sec() ) &&
832+
tx.signatures.empty() )
833+
{
834+
auto dyn_props = get_dynamic_global_properties();
835+
auto parameters = get_global_properties().parameters;
836+
fc::time_point_sec now = dyn_props.time;
837+
tx.set_reference_block( dyn_props.head_block_id );
838+
tx.set_expiration( now + parameters.maximum_time_until_expiration );
839+
}
840+
for ( const public_key_type &key : approving_key_set )
841+
tx.sign( get_private_key( key ), _chain_id );
842+
843+
if ( broadcast )
844+
{
845+
try
846+
{
847+
_remote_net_broadcast->broadcast_transaction( tx );
848+
}
849+
catch ( const fc::exception &e )
850+
{
851+
elog( "Caught exception while broadcasting tx ${id}: ${e}",
852+
( "id", tx.id().str() )( "e", e.to_detail_string() ) );
853+
FC_THROW( "Caught exception while broadcasting tx" );
854+
}
855+
}
856+
857+
return tx;
858+
}
859+
792860
void quit()
793861
{
794862
ilog( "Quitting Cli Wallet ..." );
@@ -976,7 +1044,6 @@ class wallet_api_impl
9761044
_builder_transactions.erase(handle);
9771045
}
9781046

979-
9801047
signed_transaction register_account(string name,
9811048
public_key_type owner,
9821049
public_key_type active,
@@ -1044,7 +1111,6 @@ class wallet_api_impl
10441111
return tx;
10451112
} FC_CAPTURE_AND_RETHROW( (name)(owner)(active)(registrar_account)(referrer_account)(referrer_percent)(broadcast) ) }
10461113

1047-
10481114
signed_transaction upgrade_account(string name, bool broadcast)
10491115
{ try {
10501116
FC_ASSERT( !self.is_locked() );
@@ -1062,7 +1128,6 @@ class wallet_api_impl
10621128
return sign_transaction( tx, broadcast );
10631129
} FC_CAPTURE_AND_RETHROW( (name) ) }
10641130

1065-
10661131
// This function generates derived keys starting with index 0 and keeps incrementing
10671132
// the index until it finds a key that isn't registered in the block chain. To be
10681133
// safer, it continues checking for a few more keys to make sure there wasn't a short gap
@@ -1952,13 +2017,8 @@ class wallet_api_impl
19522017

19532018
signed_transaction sign_transaction(signed_transaction tx, bool broadcast = false)
19542019
{
1955-
set<public_key_type> pks = _remote_db->get_potential_signatures( tx );
1956-
flat_set<public_key_type> owned_keys;
1957-
owned_keys.reserve( pks.size() );
1958-
std::copy_if( pks.begin(), pks.end(), std::inserter(owned_keys, owned_keys.end()),
1959-
[this](const public_key_type& pk){ return _keys.find(pk) != _keys.end(); } );
1960-
tx.clear_signatures();
1961-
set<public_key_type> approving_key_set = _remote_db->get_required_signatures( tx, owned_keys );
2020+
2021+
set<public_key_type> approving_key_set = get_owned_required_keys(tx);
19622022

19632023
auto dyn_props = get_dynamic_global_properties();
19642024
tx.set_reference_block( dyn_props.head_block_id );
@@ -3853,6 +3913,12 @@ dynamic_global_property_object wallet_api::get_dynamic_global_properties() const
38533913
return my->get_dynamic_global_properties();
38543914
}
38553915

3916+
signed_transaction wallet_api::add_transaction_signature( signed_transaction tx,
3917+
bool broadcast )
3918+
{
3919+
return my->add_transaction_signature( tx, broadcast );
3920+
}
3921+
38563922
string wallet_api::help()const
38573923
{
38583924
std::vector<std::string> method_names = my->method_documentation.get_method_names();

tests/cli/main.cpp

+132
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,138 @@ BOOST_FIXTURE_TEST_CASE( account_history_pagination, cli_fixture )
583583
}
584584
}
585585

586+
587+
///////////////////////
588+
// Create a multi-sig account and verify that only when all signatures are
589+
// signed, the transaction could be broadcast
590+
///////////////////////
591+
BOOST_AUTO_TEST_CASE( cli_multisig_transaction )
592+
{
593+
using namespace graphene::chain;
594+
using namespace graphene::app;
595+
std::shared_ptr<graphene::app::application> app1;
596+
try {
597+
fc::temp_directory app_dir( graphene::utilities::temp_directory_path() );
598+
599+
int server_port_number = 0;
600+
app1 = start_application(app_dir, server_port_number);
601+
602+
// connect to the server
603+
client_connection con(app1, app_dir, server_port_number);
604+
605+
BOOST_TEST_MESSAGE("Setting wallet password");
606+
con.wallet_api_ptr->set_password("supersecret");
607+
con.wallet_api_ptr->unlock("supersecret");
608+
609+
// import Nathan account
610+
BOOST_TEST_MESSAGE("Importing nathan key");
611+
std::vector<std::string> nathan_keys{"5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"};
612+
BOOST_CHECK_EQUAL(nathan_keys[0], "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3");
613+
BOOST_CHECK(con.wallet_api_ptr->import_key("nathan", nathan_keys[0]));
614+
615+
BOOST_TEST_MESSAGE("Importing nathan's balance");
616+
std::vector<signed_transaction> import_txs = con.wallet_api_ptr->import_balance("nathan", nathan_keys, true);
617+
account_object nathan_acct_before_upgrade = con.wallet_api_ptr->get_account("nathan");
618+
619+
// upgrade nathan
620+
BOOST_TEST_MESSAGE("Upgrading Nathan to LTM");
621+
signed_transaction upgrade_tx = con.wallet_api_ptr->upgrade_account("nathan", true);
622+
account_object nathan_acct_after_upgrade = con.wallet_api_ptr->get_account("nathan");
623+
624+
// verify that the upgrade was successful
625+
BOOST_CHECK_PREDICATE( std::not_equal_to<uint32_t>(), (nathan_acct_before_upgrade.membership_expiration_date.sec_since_epoch())(nathan_acct_after_upgrade.membership_expiration_date.sec_since_epoch()) );
626+
BOOST_CHECK(nathan_acct_after_upgrade.is_lifetime_member());
627+
628+
// create a new multisig account
629+
graphene::wallet::brain_key_info bki1 = con.wallet_api_ptr->suggest_brain_key();
630+
graphene::wallet::brain_key_info bki2 = con.wallet_api_ptr->suggest_brain_key();
631+
graphene::wallet::brain_key_info bki3 = con.wallet_api_ptr->suggest_brain_key();
632+
graphene::wallet::brain_key_info bki4 = con.wallet_api_ptr->suggest_brain_key();
633+
BOOST_CHECK(!bki1.brain_priv_key.empty());
634+
BOOST_CHECK(!bki2.brain_priv_key.empty());
635+
BOOST_CHECK(!bki3.brain_priv_key.empty());
636+
BOOST_CHECK(!bki4.brain_priv_key.empty());
637+
638+
signed_transaction create_multisig_acct_tx;
639+
account_create_operation account_create_op;
640+
641+
account_create_op.referrer = nathan_acct_after_upgrade.id;
642+
account_create_op.referrer_percent = nathan_acct_after_upgrade.referrer_rewards_percentage;
643+
account_create_op.registrar = nathan_acct_after_upgrade.id;
644+
account_create_op.name = "cifer.test";
645+
account_create_op.owner = authority(1, bki1.pub_key, 1);
646+
account_create_op.active = authority(2, bki2.pub_key, 1, bki3.pub_key, 1);
647+
account_create_op.options.memo_key = bki4.pub_key;
648+
account_create_op.fee = asset(1000000); // should be enough for creating account
649+
650+
create_multisig_acct_tx.operations.push_back(account_create_op);
651+
con.wallet_api_ptr->sign_transaction(create_multisig_acct_tx, true);
652+
653+
// attempt to give cifer.test some bitsahres
654+
BOOST_TEST_MESSAGE("Transferring bitshares from Nathan to cifer.test");
655+
signed_transaction transfer_tx1 = con.wallet_api_ptr->transfer("nathan", "cifer.test", "10000", "1.3.0", "Here are some BTS for your new account", true);
656+
657+
// transfer bts from cifer.test to nathan
658+
BOOST_TEST_MESSAGE("Transferring bitshares from cifer.test to nathan");
659+
auto dyn_props = app1->chain_database()->get_dynamic_global_properties();
660+
account_object cifer_test = con.wallet_api_ptr->get_account("cifer.test");
661+
662+
// construct a transfer transaction
663+
signed_transaction transfer_tx2;
664+
transfer_operation xfer_op;
665+
xfer_op.from = cifer_test.id;
666+
xfer_op.to = nathan_acct_after_upgrade.id;
667+
xfer_op.amount = asset(100000000);
668+
xfer_op.fee = asset(3000000); // should be enough for transfer
669+
transfer_tx2.operations.push_back(xfer_op);
670+
671+
// case1: sign a transaction without TaPoS and expiration fields
672+
// expect: return a transaction with TaPoS and expiration filled
673+
transfer_tx2 =
674+
con.wallet_api_ptr->add_transaction_signature( transfer_tx2, false );
675+
BOOST_CHECK( ( transfer_tx2.ref_block_num != 0 &&
676+
transfer_tx2.ref_block_prefix != 0 ) ||
677+
( transfer_tx2.expiration != fc::time_point_sec() ) );
678+
679+
// case2: broadcast without signature
680+
// expect: exception with missing active authority
681+
BOOST_CHECK_THROW(con.wallet_api_ptr->broadcast_transaction(transfer_tx2), fc::exception);
682+
683+
// case3:
684+
// import one of the private keys for this new account in the wallet file,
685+
// sign and broadcast with partial signatures
686+
//
687+
// expect: exception with missing active authority
688+
BOOST_CHECK(con.wallet_api_ptr->import_key("cifer.test", bki2.wif_priv_key));
689+
BOOST_CHECK_THROW(con.wallet_api_ptr->add_transaction_signature(transfer_tx2, true), fc::exception);
690+
691+
// case4: sign again as signature exists
692+
// expect: num of signatures not increase
693+
transfer_tx2 = con.wallet_api_ptr->add_transaction_signature(transfer_tx2, false);
694+
BOOST_CHECK_EQUAL(transfer_tx2.signatures.size(), 1);
695+
696+
// case5:
697+
// import another private key, sign and broadcast without full signatures
698+
//
699+
// expect: transaction broadcast successfully
700+
BOOST_CHECK(con.wallet_api_ptr->import_key("cifer.test", bki3.wif_priv_key));
701+
con.wallet_api_ptr->add_transaction_signature(transfer_tx2, true);
702+
auto balances = con.wallet_api_ptr->list_account_balances( "cifer.test" );
703+
for (auto b : balances) {
704+
if (b.asset_id == asset_id_type()) {
705+
BOOST_ASSERT(b == asset(900000000 - 3000000));
706+
}
707+
}
708+
709+
// wait for everything to finish up
710+
fc::usleep(fc::seconds(1));
711+
} catch( fc::exception& e ) {
712+
edump((e.to_detail_string()));
713+
throw;
714+
}
715+
app1->shutdown();
716+
}
717+
586718
graphene::wallet::plain_keys decrypt_keys( const std::string& password, const vector<char>& cipher_keys )
587719
{
588720
auto pw = fc::sha512::hash( password.c_str(), password.size() );

0 commit comments

Comments
 (0)