-
Notifications
You must be signed in to change notification settings - Fork 1
/
BtcPrivateKey.cpp
83 lines (71 loc) · 2.07 KB
/
BtcPrivateKey.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) 2015 Sebastian Geisler
// Distributed under the MIT software license, see the accompanying
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
#include <stdexcept>
#include "Crypto.h"
#include "Conversions.h"
#include "BtcPublicKey.h"
#include "BtcPrivateKey.h"
#include "Constants.h"
BtcPrivateKey::BtcPrivateKey() : ByteArray()
{
ByteArray pk = Crypto::newPrivateKey();
this->insert(this->begin(), pk.begin(), pk.end());
this->compressed = true;
}
BtcPrivateKey::BtcPrivateKey(const ByteArray &key) : ByteArray()
{
if (key.size() == EC_PRIVATE_KEY_LENGTH) {
this->insert(this->begin(), key.begin(), key.end());
this->compressed = false;
return;
}
else if (key.size() == EC_PRIVATE_KEY_LENGTH + 1) {
if (key.back() == 1) {
this->insert(this->begin(), key.begin(), key.begin() + EC_PRIVATE_KEY_LENGTH);
this->compressed = true;
return;
}
}
throw std::runtime_error("Wrong key size!");
}
BtcPrivateKey::BtcPrivateKey(const std::string &wifKey)
{
ByteArray key = Conversions::fromBase58Check(wifKey, 0x80);
if (key.size() == EC_PRIVATE_KEY_LENGTH)
{
this->insert(this->begin(), key.begin(), key.end());
this->compressed = false;
return;
}
else if (key.size() == EC_PRIVATE_KEY_LENGTH + 1)
{
if (key.back() == 1)
{
this->insert(this->begin(), key.begin(), key.begin() + EC_PRIVATE_KEY_LENGTH);
this->compressed = true;
return;
}
}
throw std::runtime_error("Wrong key size!");
}
BtcPublicKey BtcPrivateKey::getPublicKey() const
{
if (compressed) {
return Crypto::privKeyToCompressedPubKey(*this);
}
else {
return Crypto::privKeyToPubKey(*this);
}
}
std::string BtcPrivateKey::getWIF() const
{
if(compressed)
return (*this + 0x01).toBase58Check(0x80);
else
return this->toBase58Check(0x80);
}
ByteArray BtcPrivateKey::sign(const ByteArray &hash) const
{
return Crypto::sign(*this, hash);
}