Skip to content

Commit ef374cd

Browse files
authored
Merge pull request bitshares#507 from btcinshares/sign_memo_api
Proposal: expose sign/read memo API to wallet
2 parents db14a50 + 4d8e6b6 commit ef374cd

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

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

+18
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,22 @@ class wallet_api
786786
transaction_id_type get_transaction_id( const signed_transaction& trx )const { return trx.id(); }
787787

788788

789+
/** Sign a memo message.
790+
*
791+
* @param from the name or id of signing account; or a public key.
792+
* @param to the name or id of receiving account; or a public key.
793+
* @param memo text to sign.
794+
*/
795+
memo_data sign_memo(string from, string to, string memo);
796+
797+
/** Read a memo.
798+
*
799+
* @param memo JSON-enconded memo.
800+
* @returns string with decrypted message..
801+
*/
802+
string read_memo(const memo_data& memo);
803+
804+
789805
/** These methods are used for stealth transfers */
790806
///@{
791807
/**
@@ -1718,6 +1734,8 @@ FC_API( graphene::wallet::wallet_api,
17181734
(flood_network)
17191735
(network_add_nodes)
17201736
(network_get_connected_peers)
1737+
(sign_memo)
1738+
(read_memo)
17211739
(set_key_label)
17221740
(get_key_label)
17231741
(get_public_key)

libraries/wallet/wallet.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,56 @@ class wallet_api_impl
18771877
return tx;
18781878
}
18791879

1880+
memo_data sign_memo(string from, string to, string memo)
1881+
{
1882+
FC_ASSERT( !self.is_locked() );
1883+
1884+
memo_data md = memo_data();
1885+
1886+
// get account memo key, if that fails, try a pubkey
1887+
try {
1888+
account_object from_account = get_account(from);
1889+
md.from = from_account.options.memo_key;
1890+
} catch (const fc::exception& e) {
1891+
md.from = self.get_public_key( from );
1892+
}
1893+
// same as above, for destination key
1894+
try {
1895+
account_object to_account = get_account(to);
1896+
md.to = to_account.options.memo_key;
1897+
} catch (const fc::exception& e) {
1898+
md.to = self.get_public_key( to );
1899+
}
1900+
1901+
md.set_message(get_private_key(md.from), md.to, memo);
1902+
return md;
1903+
}
1904+
1905+
string read_memo(const memo_data& md)
1906+
{
1907+
FC_ASSERT(!is_locked());
1908+
std::string clear_text;
1909+
1910+
const memo_data *memo = &md;
1911+
1912+
try {
1913+
FC_ASSERT(_keys.count(memo->to) || _keys.count(memo->from), "Memo is encrypted to a key ${to} or ${from} not in this wallet.", ("to", memo->to)("from",memo->from));
1914+
if( _keys.count(memo->to) ) {
1915+
auto my_key = wif_to_key(_keys.at(memo->to));
1916+
FC_ASSERT(my_key, "Unable to recover private key to decrypt memo. Wallet may be corrupted.");
1917+
clear_text = memo->get_message(*my_key, memo->from);
1918+
} else {
1919+
auto my_key = wif_to_key(_keys.at(memo->from));
1920+
FC_ASSERT(my_key, "Unable to recover private key to decrypt memo. Wallet may be corrupted.");
1921+
clear_text = memo->get_message(*my_key, memo->to);
1922+
}
1923+
} catch (const fc::exception& e) {
1924+
elog("Error when decrypting memo: ${e}", ("e", e.to_detail_string()));
1925+
}
1926+
1927+
return clear_text;
1928+
}
1929+
18801930
signed_transaction sell_asset(string seller_account,
18811931
string amount_to_sell,
18821932
string symbol_to_sell,
@@ -3783,6 +3833,18 @@ signed_transaction wallet_api::cancel_order(object_id_type order_id, bool broadc
37833833
return my->cancel_order(order_id, broadcast);
37843834
}
37853835

3836+
memo_data wallet_api::sign_memo(string from, string to, string memo)
3837+
{
3838+
FC_ASSERT(!is_locked());
3839+
return my->sign_memo(from, to, memo);
3840+
}
3841+
3842+
string wallet_api::read_memo(const memo_data& memo)
3843+
{
3844+
FC_ASSERT(!is_locked());
3845+
return my->read_memo(memo);
3846+
}
3847+
37863848
string wallet_api::get_key_label( public_key_type key )const
37873849
{
37883850
auto key_itr = my->_wallet.labeled_keys.get<by_key>().find(key);

0 commit comments

Comments
 (0)