BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface 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);
38  digest = fc::sha256::hash(digest);
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
std::string to_base58(const char *d, size_t s)
Definition: base58.cpp:612
T as(uint32_t max_depth) const
Definition: variant.hpp:336
private_key_secret get_secret() const
Definition: api.cpp:56
fc::optional< fc::ecc::private_key > wif_to_key(const std::string &wif_key)
static sha256 hash(const char *d, uint32_t dlen)
Definition: sha256.cpp:41
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
std::string key_to_wif(const fc::sha256 &private_secret)
fc::sha256 digest(const T &value)
Definition: digest.hpp:9
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
std::vector< char > from_base58(const std::string &base58_str)
Definition: base58.cpp:622
an elliptic curve private key.
Definition: elliptic.hpp:89