Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
sysio::wallet::detail::soft_wallet_impl Class Reference
Collaboration diagram for sysio::wallet::detail::soft_wallet_impl:

Public Member Functions

 soft_wallet_impl (soft_wallet &s, const wallet_data &initial_data)
 
virtual ~soft_wallet_impl ()
 
void encrypt_keys ()
 
bool copy_wallet_file (string destination_filename)
 
bool is_locked () const
 
string get_wallet_filename () const
 
std::optional< private_key_typetry_get_private_key (const public_key_type &id) const
 
std::optional< signature_typetry_sign_digest (const digest_type digest, const public_key_type public_key)
 
private_key_type get_private_key (const public_key_type &id) const
 
bool import_key (string wif_key)
 
bool remove_key (string key)
 
string create_key (string key_type)
 
bool load_wallet_file (string wallet_filename="")
 
void save_wallet_file (string wallet_filename="")
 

Public Attributes

soft_walletself
 
string _wallet_filename
 
wallet_data _wallet
 
map< public_key_type, private_key_type_keys
 
fc::sha512 _checksum
 
const string _wallet_filename_extension = ".wallet"
 
const string _default_key_type = "K1"
 

Detailed Description

Definition at line 41 of file wallet.cpp.

Constructor & Destructor Documentation

◆ soft_wallet_impl()

sysio::wallet::detail::soft_wallet_impl::soft_wallet_impl ( soft_wallet & s,
const wallet_data & initial_data )
inline

Definition at line 58 of file wallet.cpp.

59 : self( s )
60 {
61 }
char * s

◆ ~soft_wallet_impl()

virtual sysio::wallet::detail::soft_wallet_impl::~soft_wallet_impl ( )
inlinevirtual

Definition at line 63 of file wallet.cpp.

64 {}

Member Function Documentation

◆ copy_wallet_file()

bool sysio::wallet::detail::soft_wallet_impl::copy_wallet_file ( string destination_filename)
inline

Definition at line 78 of file wallet.cpp.

79 {
80 fc::path src_path = get_wallet_filename();
81 if( !fc::exists( src_path ) )
82 return false;
83 fc::path dest_path = destination_filename + _wallet_filename_extension;
84 int suffix = 0;
85 while( fc::exists(dest_path) )
86 {
87 ++suffix;
88 dest_path = destination_filename + "-" + std::to_string( suffix ) + _wallet_filename_extension;
89 }
90 wlog( "backing up wallet ${src} to ${dest}",
91 ("src", src_path)
92 ("dest", dest_path) );
93
94 fc::path dest_parent = fc::absolute(dest_path).parent_path();
95 try
96 {
97 enable_umask_protection();
98 if( !fc::exists( dest_parent ) )
99 fc::create_directories( dest_parent );
100 fc::copy( src_path, dest_path );
101 disable_umask_protection();
102 }
103 catch(...)
104 {
105 disable_umask_protection();
106 throw;
107 }
108 return true;
109 }
wraps boost::filesystem::path to provide platform independent path manipulation.
fc::path parent_path() const
#define wlog(FORMAT,...)
Definition logger.hpp:124
bool exists(const path &p)
void create_directories(const path &p)
void copy(const path &from, const path &to)
path absolute(const path &p)
Here is the call graph for this function:

◆ create_key()

string sysio::wallet::detail::soft_wallet_impl::create_key ( string key_type)
inline

Definition at line 172 of file wallet.cpp.

173 {
174 if(key_type.empty())
176
177 private_key_type priv_key;
178 if(key_type == "K1")
180 else if(key_type == "R1")
182 else
183 SYS_THROW(chain::unsupported_key_type_exception, "Key type \"${kt}\" not supported by software wallet", ("kt", key_type));
184
185 import_key(priv_key.to_string());
186 return priv_key.get_public_key().to_string();
187 }
#define SYS_THROW(exc_type, FORMAT,...)
std::string to_string(const fc::yield_function_t &yield=fc::yield_function_t()) const
static private_key generate()
public_key get_public_key() const
std::string to_string(const fc::yield_function_t &yield=fc::yield_function_t()) const
struct @108 key_type
Here is the call graph for this function:

◆ encrypt_keys()

void sysio::wallet::detail::soft_wallet_impl::encrypt_keys ( )
inline

Definition at line 66 of file wallet.cpp.

67 {
68 if( !is_locked() )
69 {
70 plain_keys data;
71 data.keys = _keys;
72 data.checksum = _checksum;
73 auto plain_txt = fc::raw::pack(data);
74 _wallet.cipher_keys = fc::aes_encrypt( data.checksum, plain_txt );
75 }
76 }
map< public_key_type, private_key_type > _keys
Definition wallet.cpp:250
void pack(Stream &s, const std::deque< T > &value)
Definition raw.hpp:531
unsigned aes_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext)
Definition aes.cpp:159
vector< char > cipher_keys
Definition wallet.hpp:18
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_private_key()

private_key_type sysio::wallet::detail::soft_wallet_impl::get_private_key ( const public_key_type & id) const
inline

Definition at line 133 of file wallet.cpp.

134 {
135 auto has_key = try_get_private_key( id );
136 SYS_ASSERT( has_key, chain::key_nonexistent_exception, "Key doesn't exist!" );
137 return *has_key;
138 }
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
Definition exceptions.hpp:7
std::optional< private_key_type > try_get_private_key(const public_key_type &id) const
Definition wallet.cpp:118
Here is the call graph for this function:

◆ get_wallet_filename()

string sysio::wallet::detail::soft_wallet_impl::get_wallet_filename ( ) const
inline

Definition at line 116 of file wallet.cpp.

Here is the caller graph for this function:

◆ import_key()

bool sysio::wallet::detail::soft_wallet_impl::import_key ( string wif_key)
inline

Definition at line 145 of file wallet.cpp.

146 {
147 private_key_type priv(wif_key);
148 sysio::chain::public_key_type wif_pub_key = priv.get_public_key();
149
150 auto itr = _keys.find(wif_pub_key);
151 if( itr == _keys.end() ) {
152 _keys[wif_pub_key] = priv;
153 return true;
154 }
155 SYS_THROW( chain::key_exist_exception, "Key already in wallet" );
156 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_locked()

bool sysio::wallet::detail::soft_wallet_impl::is_locked ( ) const
inline

Definition at line 111 of file wallet.cpp.

112 {
113 return _checksum == fc::sha512();
114 }
Here is the caller graph for this function:

◆ load_wallet_file()

bool sysio::wallet::detail::soft_wallet_impl::load_wallet_file ( string wallet_filename = "")
inline

Definition at line 189 of file wallet.cpp.

190 {
191 // TODO: Merge imported wallet with existing wallet,
192 // instead of replacing it
193 if( wallet_filename == "" )
194 wallet_filename = _wallet_filename;
195
196 if( ! fc::exists( wallet_filename ) )
197 return false;
198
199 _wallet = fc::json::from_file( wallet_filename ).as< wallet_data >();
200
201 return true;
202 }
static variant from_file(const fc::path &p, const parse_type ptype=parse_type::legacy_parser, const uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition json.cpp:797
T as() const
Definition variant.hpp:327
Here is the call graph for this function:

◆ remove_key()

bool sysio::wallet::detail::soft_wallet_impl::remove_key ( string key)
inline

Definition at line 161 of file wallet.cpp.

162 {
163 public_key_type pub(key);
164 auto itr = _keys.find(pub);
165 if( itr != _keys.end() ) {
166 _keys.erase(pub);
167 return true;
168 }
169 SYS_THROW( chain::key_nonexistent_exception, "Key not in wallet" );
170 }
bool pub

◆ save_wallet_file()

void sysio::wallet::detail::soft_wallet_impl::save_wallet_file ( string wallet_filename = "")
inline

Definition at line 204 of file wallet.cpp.

205 {
206 //
207 // Serialize in memory, then save to disk
208 //
209 // This approach lessens the risk of a partially written wallet
210 // if exceptions are thrown in serialization
211 //
212
213 encrypt_keys();
214
215 if( wallet_filename == "" )
216 wallet_filename = _wallet_filename;
217
218 wlog( "saving wallet to file ${fn}", ("fn", wallet_filename) );
219
220 string data = fc::json::to_pretty_string( _wallet );
221 try
222 {
223 enable_umask_protection();
224 //
225 // Parentheses on the following declaration fails to compile,
226 // due to the Most Vexing Parse. Thanks, C++
227 //
228 // http://en.wikipedia.org/wiki/Most_vexing_parse
229 //
230 ofstream outfile{ wallet_filename };
231 if (!outfile) {
232 elog("Unable to open file: ${fn}", ("fn", wallet_filename));
233 SYS_THROW(wallet_exception, "Unable to open file: ${fn}", ("fn", wallet_filename));
234 }
235 outfile.write( data.c_str(), data.length() );
236 outfile.flush();
237 outfile.close();
238 disable_umask_protection();
239 }
240 catch(...)
241 {
242 disable_umask_protection();
243 throw;
244 }
245 }
static string to_pretty_string(const variant &v, const yield_function_t &yield, const output_formatting format=output_formatting::stringify_large_ints_and_doubles)
Definition json.cpp:775
#define elog(FORMAT,...)
Definition logger.hpp:130
Here is the call graph for this function:

◆ try_get_private_key()

std::optional< private_key_type > sysio::wallet::detail::soft_wallet_impl::try_get_private_key ( const public_key_type & id) const
inline

Definition at line 118 of file wallet.cpp.

119 {
120 auto it = _keys.find(id);
121 if( it != _keys.end() )
122 return it->second;
123 return std::optional<private_key_type>();
124 }
Here is the caller graph for this function:

◆ try_sign_digest()

std::optional< signature_type > sysio::wallet::detail::soft_wallet_impl::try_sign_digest ( const digest_type digest,
const public_key_type public_key )
inline

Definition at line 126 of file wallet.cpp.

126 {
127 auto it = _keys.find(public_key);
128 if( it == _keys.end() )
129 return std::optional<signature_type>();
130 return it->second.sign(digest);
131 }
fc::sha256 digest(const T &value)
Definition digest.hpp:9
Here is the call graph for this function:

Member Data Documentation

◆ _checksum

fc::sha512 sysio::wallet::detail::soft_wallet_impl::_checksum

Definition at line 251 of file wallet.cpp.

◆ _default_key_type

const string sysio::wallet::detail::soft_wallet_impl::_default_key_type = "K1"

Definition at line 257 of file wallet.cpp.

◆ _keys

map<public_key_type,private_key_type> sysio::wallet::detail::soft_wallet_impl::_keys

Definition at line 250 of file wallet.cpp.

◆ _wallet

wallet_data sysio::wallet::detail::soft_wallet_impl::_wallet

Definition at line 248 of file wallet.cpp.

◆ _wallet_filename

string sysio::wallet::detail::soft_wallet_impl::_wallet_filename

Definition at line 247 of file wallet.cpp.

◆ _wallet_filename_extension

const string sysio::wallet::detail::soft_wallet_impl::_wallet_filename_extension = ".wallet"

Definition at line 256 of file wallet.cpp.

◆ self

soft_wallet& sysio::wallet::detail::soft_wallet_impl::self

Definition at line 57 of file wallet.cpp.


The documentation for this class was generated from the following file: