Skip to content

Commit 698bd64

Browse files
committed
[Backport] key_io.h/cpp
1 parent 155a8d9 commit 698bd64

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ BITCOIN_CORE_H = \
126126
kernel.h \
127127
swifttx.h \
128128
key.h \
129+
key_io.h \
129130
keystore.h \
130131
leveldbwrapper.h \
131132
limitedmap.h \
@@ -389,6 +390,7 @@ libbitcoin_common_a_SOURCES = \
389390
hash.cpp \
390391
invalid.cpp \
391392
key.cpp \
393+
key_io.cpp \
392394
keystore.cpp \
393395
netbase.cpp \
394396
protocol.cpp \

src/key_io.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2014-2019 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include "key_io.h"
6+
7+
#include "base58.h"
8+
#include <boost/variant/apply_visitor.hpp>
9+
#include <boost/variant/static_visitor.hpp>
10+
11+
#include <assert.h>
12+
#include <string.h>
13+
#include <algorithm>
14+
15+
std::string EncodeSecret(const CKey& key)
16+
{
17+
assert(key.IsValid());
18+
std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::SECRET_KEY);
19+
data.insert(data.end(), key.begin(), key.end());
20+
if (key.IsCompressed()) {
21+
data.push_back(1);
22+
}
23+
std::string ret = EncodeBase58Check(data);
24+
memory_cleanse(data.data(), data.size());
25+
return ret;
26+
}
27+
28+
CExtKey DecodeExtKey(const std::string& str)
29+
{
30+
CExtKey key;
31+
std::vector<unsigned char> data;
32+
if (DecodeBase58Check(str, data)) {
33+
const std::vector<unsigned char>& prefix = Params().Base58Prefix(CChainParams::EXT_SECRET_KEY);
34+
if (data.size() == BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
35+
key.Decode(data.data() + prefix.size());
36+
}
37+
}
38+
return key;
39+
}
40+
41+
std::string EncodeExtKey(const CExtKey& key)
42+
{
43+
std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::EXT_SECRET_KEY);
44+
size_t size = data.size();
45+
data.resize(size + BIP32_EXTKEY_SIZE);
46+
key.Encode(data.data() + size);
47+
std::string ret = EncodeBase58Check(data);
48+
memory_cleanse(data.data(), data.size());
49+
return ret;
50+
}

src/key_io.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2018 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#ifndef PIVX_KEY_IO_H
7+
#define PIVX_KEY_IO_H
8+
9+
#include "chainparams.h"
10+
#include "key.h"
11+
#include "pubkey.h"
12+
#include "script/standard.h"
13+
14+
#include <string>
15+
16+
std::string EncodeSecret(const CKey& key);
17+
18+
CExtKey DecodeExtKey(const std::string& str);
19+
std::string EncodeExtKey(const CExtKey& extkey);
20+
21+
#endif //PIVX_KEY_IO_H

0 commit comments

Comments
 (0)