peerplays_migrated/programs/light_client/Wallet.cpp

239 lines
5.8 KiB
C++
Raw Normal View History

2015-07-22 20:23:42 +00:00
#include "Wallet.hpp"
2015-07-22 20:59:22 +00:00
#include <fc/crypto/aes.hpp>
#include <fc/io/json.hpp>
2015-07-22 20:23:42 +00:00
Wallet::Wallet()
{
}
Wallet::~Wallet()
{
close();
}
bool Wallet::open( QString file_path )
{
2015-07-22 20:59:22 +00:00
fc::path p( file_path.toStdString() );
if( !fc::exists( p ) )
{
ilog( "Unable to open wallet file '${f}', it does not exist", ("f",p) );
return false;
}
_wallet_file_path = p;
return true;
}
bool Wallet::isOpen()const
{
return _wallet_file_path != fc::path();
2015-07-22 20:23:42 +00:00
}
bool Wallet::close()
{
2015-07-22 20:59:22 +00:00
if( !isOpen() ) return false;
2015-07-22 20:23:42 +00:00
save();
2015-07-22 20:59:22 +00:00
_wallet_file_path = fc::path();
2015-07-22 20:23:42 +00:00
return false;
}
bool Wallet::save()
{
2015-07-22 20:59:22 +00:00
if( !isOpen() ) return false;
/// TODO: backup existing wallet file first.
fc::json::save_to_file( _data, _wallet_file_path );
2015-07-22 20:23:42 +00:00
return false;
}
bool Wallet::saveAs( QString file_path )
{
2015-07-22 20:59:22 +00:00
if( !isOpen() ) return false;
2015-07-22 20:23:42 +00:00
return false;
}
2015-07-22 20:59:22 +00:00
bool Wallet::create( QString file_path, QString password, QString brain_key )
2015-07-22 20:23:42 +00:00
{
2015-07-22 20:59:22 +00:00
if( isOpen() ) return false;
if( password == QString() ) return false;
fc::path p( file_path.toStdString() );
if( fc::exists( p ) )
{
ilog( "Unable to create wallet file '${f}' because a file with that name already exists.", ("f",p) );
return false;
}
if( brain_key == QString() )
{
auto key = fc::ecc::private_key::generate().get_secret();
brain_key.fromStdString( fc::variant(key).as_string() );
}
auto brainkey = brain_key.toStdString();
auto pw_str = password.toStdString();
auto password_hash = fc::sha512::hash( pw_str.c_str(), pw_str.size() );
_decrypted_master_key = fc::sha512::hash( fc::ecc::private_key::generate().get_secret() );
_data.master_key_digest = fc::sha512::hash( _decrypted_master_key );
_data.encrypted_master_key = fc::aes_encrypt( password_hash, fc::raw::pack(_decrypted_master_key) );
_data.brain_key_digest = fc::sha512::hash( brainkey.c_str(), brainkey.size() );
_data.encrypted_brain_key = fc::aes_encrypt( _decrypted_master_key, fc::raw::pack( brainkey ) );
fc::json::save_to_file( _data, p );
_wallet_file_path = p;
2015-07-22 20:23:42 +00:00
return false;
}
bool Wallet::loadBrainKey( QString brain_key )
{
2015-07-22 20:59:22 +00:00
if( !isOpen() ) return false;
if( brain_key == QString() ) return false;
auto brainkey = brain_key.toStdString();
if( _data.brain_key_digest != fc::sha512::hash( brainkey.c_str(), brainkey.size() ) )
return false;
_brain_key = brain_key;
return true;
2015-07-22 20:23:42 +00:00
}
bool Wallet::purgeBrainKey()
{
2015-07-22 20:59:22 +00:00
if( !isOpen() ) return false;
_data.encrypted_brain_key.resize(0);
_brain_key = QString();
return save();
2015-07-22 20:23:42 +00:00
}
bool Wallet::hasBrainKey()const
{
2015-07-22 20:59:22 +00:00
if( !isOpen() ) return false;
return _brain_key != QString() || _data.encrypted_brain_key.size();
2015-07-22 20:23:42 +00:00
}
2015-07-22 20:59:22 +00:00
QString Wallet::getBrainKey()
2015-07-22 20:23:42 +00:00
{
2015-07-22 20:59:22 +00:00
if( !isOpen() ) return QString();
if( isLocked() ) return QString();
if( _brain_key != QString() )
return _brain_key;
auto dec_brain_key = fc::aes_decrypt( _decrypted_master_key, _data.encrypted_brain_key );
auto dec_brain_key_str = fc::raw::unpack<string>(dec_brain_key);
_brain_key.fromStdString( dec_brain_key_str );
return _brain_key;
2015-07-22 20:23:42 +00:00
}
bool Wallet::isLocked()const
{
2015-07-22 20:59:22 +00:00
if( !isOpen() ) return true;
2015-07-22 20:23:42 +00:00
return false;
}
2015-07-22 22:37:00 +00:00
bool Wallet::unlock( QString password )
2015-07-22 20:23:42 +00:00
{
2015-07-22 22:37:00 +00:00
if( !isLocked() ) return true;
auto pw_str = password.toStdString();
auto password_hash = fc::sha512::hash( pw_str.c_str(), pw_str.size() );
auto plain_txt = fc::aes_decrypt( password_hash, _data.encrypted_master_key );
_decrypted_master_key = fc::raw::unpack<fc::sha512>(plain_txt);
if( _data.master_key_digest != fc::sha512::hash(_decrypted_master_key) )
_decrypted_master_key = fc::sha512();
return !isLocked();
2015-07-22 20:23:42 +00:00
}
bool Wallet::lock()
{
2015-07-22 20:59:22 +00:00
if( !isOpen() ) return false;
_brain_key = QString();
_decrypted_master_key = fc::sha512();
return true;
2015-07-22 20:23:42 +00:00
}
bool Wallet::changePassword( QString new_password )
{
2015-07-22 20:59:22 +00:00
if( !isOpen() ) return false;
2015-07-22 22:37:00 +00:00
if( isLocked() ) return false;
auto pw_str = new_password.toStdString();
auto password_hash = fc::sha512::hash( pw_str.c_str(), pw_str.size() );
_data.encrypted_master_key = fc::aes_encrypt( password_hash, fc::raw::pack(_decrypted_master_key) );
save();
return true;
2015-07-22 20:23:42 +00:00
}
QString Wallet::getActivePrivateKey( QString owner_pub_key, uint32_t seq )
{
2015-07-22 20:59:22 +00:00
if( !isOpen() ) return QString();
2015-07-22 20:23:42 +00:00
return QString();
}
QString Wallet::getOwnerPrivateKey( uint32_t seq )
{
2015-07-22 20:59:22 +00:00
if( !isOpen() ) return QString();
2015-07-22 20:23:42 +00:00
return QString();
}
QString Wallet::getKeyLabel( QString pubkey )
{
2015-07-22 20:59:22 +00:00
if( !isOpen() ) return QString();
2015-07-22 20:23:42 +00:00
return QString();
}
QString Wallet::getPublicKey( QString label )
{
2015-07-22 20:59:22 +00:00
if( !isOpen() ) return QString();
2015-07-22 20:23:42 +00:00
return QString();
}
/** imports a public key and assigns it a label */
bool Wallet::importPublicKey( QString pubkey, QString label)
{
2015-07-22 20:59:22 +00:00
if( !isOpen() ) return false;
2015-07-22 20:23:42 +00:00
return false;
}
/**
* @param wifkey a private key in (WIF) Wallet Import Format
* @pre !isLocked()
**/
bool Wallet::importPrivateKey( QString wifkey, QString label )
{
2015-07-22 20:59:22 +00:00
if( !isOpen() ) return false;
2015-07-22 20:23:42 +00:00
return false;
}
/** removes the key, its lablel and its private key */
bool Wallet::removePublicKey( QString pubkey )
{
2015-07-22 20:59:22 +00:00
if( !isOpen() ) return false;
2015-07-22 20:23:42 +00:00
return false;
}
/** removes only the private key, keeping the public key and label */
bool Wallet::removePrivateKey( QString pubkey )
{
2015-07-22 20:59:22 +00:00
if( !isOpen() ) return false;
2015-07-22 20:23:42 +00:00
return false;
}
/**
* @pre !isLocked()
*/
vector<signature_type> Wallet::signDigest( const digest_type& d,
const set<public_key_type>& keys )const
{
vector<signature_type> result;
return result;
}
const flat_set<public_key_type>& Wallet::getAvailablePrivateKeys()const
{
return _available_private_keys;
}