diff --git a/libraries/wallet/include/graphene/wallet/wallet.hpp b/libraries/wallet/include/graphene/wallet/wallet.hpp index 4887f35b..43d0de01 100644 --- a/libraries/wallet/include/graphene/wallet/wallet.hpp +++ b/libraries/wallet/include/graphene/wallet/wallet.hpp @@ -62,6 +62,11 @@ struct brain_key_info public_key_type pub_key; }; +enum authority_type +{ + owner, + active +}; /** * Contains the confirmation receipt the sender must give the receiver and @@ -740,6 +745,41 @@ class wallet_api uint32_t referrer_percent, bool broadcast = false); + /** Updates account public keys + * + * @param name the name of the existing account + * @param old_owner the owner key for the named account to be replaced + * @param new_owner the owner key for the named account to be set as new + * @param old_active the active key for the named account to be replaced + * @param new_active the active key for the named account to be set as new + * @param broadcast true to broadcast the transaction on the network + * @returns the signed transaction updating account public keys + */ + signed_transaction update_account_keys(string name, + public_key_type old_owner, + public_key_type new_owner, + public_key_type old_active, + public_key_type new_active, + bool broadcast = false); + + /** + * This method updates the key of an authority for an exisiting account. + * Warning: You can create impossible authorities using this method. The method + * will fail if you create an impossible owner authority, but will allow impossible + * active and posting authorities. + * + * @param account_name The name of the account whose authority you wish to update + * @param type The authority type. e.g. owner or active + * @param key The public key to add to the authority + * @param weight The weight the key should have in the authority. A weight of 0 indicates the removal of the key. + * @param broadcast true if you wish to broadcast the transaction. + */ + signed_transaction update_account_auth_key(string account_name, + authority_type type, + public_key_type key, + weight_type weight, + bool broadcast); + /** * Upgrades an account to prime status. * This makes the account holder a 'lifetime member'. @@ -2433,6 +2473,8 @@ FC_REFLECT( graphene::wallet::brain_key_info, (pub_key) ) +FC_REFLECT_ENUM( graphene::wallet::authority_type, (owner)(active) ) + FC_REFLECT( graphene::wallet::exported_account_keys, (account_name)(encrypted_private_keys)(public_keys) ) FC_REFLECT( graphene::wallet::exported_keys, (password_checksum)(account_keys) ) @@ -2496,6 +2538,8 @@ FC_API( graphene::wallet::wallet_api, (derive_owner_keys_from_brain_key) (get_private_key_from_password) (register_account) + (update_account_keys) + (update_account_auth_key) (upgrade_account) (create_account_with_brain_key) (sell_asset) diff --git a/libraries/wallet/wallet.cpp b/libraries/wallet/wallet.cpp index 74bf9bd4..b1983ce4 100644 --- a/libraries/wallet/wallet.cpp +++ b/libraries/wallet/wallet.cpp @@ -1335,6 +1335,102 @@ public: } FC_CAPTURE_AND_RETHROW( (name)(owner)(active)(registrar_account)(referrer_account)(referrer_percent)(broadcast) ) } + signed_transaction update_account_keys(string name, + public_key_type old_owner, + public_key_type new_owner, + public_key_type old_active, + public_key_type new_active, + bool broadcast) + { try { + FC_ASSERT( !self.is_locked() ); + account_object account_obj = get_account(name); + + authority owner = account_obj.owner; + owner.key_auths[new_owner] = owner.key_auths[old_owner]; + owner.key_auths.erase(old_owner); + + authority active = account_obj.active; + active.key_auths[new_active] = active.key_auths[old_active]; + active.key_auths.erase(old_active); + + signed_transaction tx; + account_update_operation op; + + op.account = account_obj.get_id(); + op.owner = owner; + op.active = active; + + ilog("account_update_operation: ${op}", ("op", op)); + + tx.operations = {op}; + set_operation_fees( tx, _remote_db->get_global_properties().parameters.current_fees ); + tx.validate(); + + return sign_transaction( tx, broadcast ); + } FC_CAPTURE_AND_RETHROW( (name) ) } + + + signed_transaction update_account_auth_key(string account_name, + authority_type type, + public_key_type key, + weight_type weight, + bool broadcast) + { + FC_ASSERT( !is_locked() ); + account_object account_obj = get_account(account_name); + + account_update_operation op; + op.account = account_obj.get_id(); + + authority new_auth; + + switch( type ) + { + case( owner ): + new_auth = account_obj.owner; + break; + case( active ): + new_auth = account_obj.active; + break; + } + + if( weight == 0 ) // Remove the key + { + new_auth.key_auths.erase( key ); + } + else + { + new_auth.add_authority( key, weight ); + } + + if( new_auth.is_impossible() ) + { + if ( type == owner ) + { + FC_ASSERT( false, "Owner authority change would render account irrecoverable." ); + } + + wlog( "Authority is now impossible." ); + } + + switch( type ) + { + case( owner ): + op.owner = new_auth; + break; + case( active ): + op.active = new_auth; + break; + } + + signed_transaction tx; + tx.operations.push_back(op); + set_operation_fees( tx, _remote_db->get_global_properties().parameters.current_fees ); + tx.validate(); + + return sign_transaction( tx, broadcast ); + } + signed_transaction upgrade_account(string name, bool broadcast) { try { FC_ASSERT( !self.is_locked() ); @@ -5536,6 +5632,25 @@ map wallet_api::dump_private_keys() return my->_keys; } +signed_transaction wallet_api::update_account_keys(string name, + public_key_type old_owner, + public_key_type new_owner, + public_key_type old_active, + public_key_type new_active, + bool broadcast ) +{ + return my->update_account_keys(name, old_owner, new_owner, old_active, new_active, broadcast); +} + +signed_transaction wallet_api::update_account_auth_key(string account_name, + authority_type type, + public_key_type key, + weight_type weight, + bool broadcast) +{ + return my->update_account_auth_key(account_name, type, key, weight, broadcast); +} + signed_transaction wallet_api::upgrade_account( string name, bool broadcast ) { return my->upgrade_account(name,broadcast);