BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
key_conversion.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
25 #include <fc/crypto/base58.hpp>
26 #include <fc/variant.hpp>
27 
28 namespace graphene { namespace utilities {
29 
30 std::string key_to_wif(const fc::sha256& secret )
31 {
32  const size_t size_of_data_to_hash = sizeof(secret) + 1;
33  const size_t size_of_hash_bytes = 4;
34  char data[size_of_data_to_hash + size_of_hash_bytes];
35  data[0] = (char)0x80;
36  memcpy(&data[1], (char*)&secret, sizeof(secret));
37  fc::sha256 digest = fc::sha256::hash(data, size_of_data_to_hash);
39  memcpy(data + size_of_data_to_hash, (char*)&digest, size_of_hash_bytes);
40  return fc::to_base58(data, sizeof(data));
41 }
42 std::string key_to_wif(const fc::ecc::private_key& key)
43 {
44  return key_to_wif( key.get_secret() );
45 }
46 
47 fc::optional<fc::ecc::private_key> wif_to_key( const std::string& wif_key )
48 {
49  std::vector<char> wif_bytes;
50  try
51  {
52  wif_bytes = fc::from_base58(wif_key);
53  }
54  catch (const fc::parse_error_exception&)
55  {
57  }
58  if (wif_bytes.size() < 5)
60  std::vector<char> key_bytes(wif_bytes.begin() + 1, wif_bytes.end() - 4);
61  fc::ecc::private_key key = fc::variant( key_bytes, 1 ).as<fc::ecc::private_key>( 1 );
62  fc::sha256 check = fc::sha256::hash(wif_bytes.data(), wif_bytes.size() - 4);
63  fc::sha256 check2 = fc::sha256::hash(check);
64 
65  if( memcmp( (char*)&check, wif_bytes.data() + wif_bytes.size() - 4, 4 ) == 0 ||
66  memcmp( (char*)&check2, wif_bytes.data() + wif_bytes.size() - 4, 4 ) == 0 )
67  return key;
68 
70 }
71 
72 } } // end namespace graphene::utilities
fc::to_base58
std::string to_base58(const char *d, size_t s)
Definition: base58.cpp:612
fc::digest
fc::sha256 digest(const T &value)
Definition: digest.hpp:9
graphene::utilities::key_to_wif
std::string key_to_wif(const fc::sha256 &private_secret)
Definition: key_conversion.cpp:30
graphene::utilities::wif_to_key
fc::optional< fc::ecc::private_key > wif_to_key(const std::string &wif_key)
Definition: key_conversion.cpp:47
fc::ecc::private_key::get_secret
private_key_secret get_secret() const
Definition: elliptic_impl_priv.cpp:59
fc::from_base58
std::vector< char > from_base58(const std::string &base58_str)
Definition: base58.cpp:622
fc::sha256
Definition: sha256.hpp:10
fc::ecc::private_key
an elliptic curve private key.
Definition: elliptic.hpp:89
fc::sha256::data
char * data() const
Definition: sha256.cpp:29
fc::variant::as
T as(uint32_t max_depth) const
Definition: variant.hpp:337
base58.hpp
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
variant.hpp
fc::optional
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
key_conversion.hpp
fc::sha256::hash
static sha256 hash(const char *d, uint32_t dlen)
Definition: sha256.cpp:41
graphene
Definition: api.cpp:48