-
Notifications
You must be signed in to change notification settings - Fork 0
/
keytreeutil.cpp
128 lines (108 loc) · 5.05 KB
/
keytreeutil.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
////////////////////////////////////////////////////////////////////////////////
//
// keytreeutil.h
//
// Copyright (c) 2013-2014 Tim Lee
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "keytreeutil.h"
#include <sstream>
namespace KeyTreeUtil {
uchar_vector sha256Rounds(const uchar_vector& data, uint64_t rounds) {
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, &data[0], data.size());
SHA256_Final(hash, &sha256);
for (int i = 0; i < rounds - 1; i++) {
SHA256_Init(&sha256);
SHA256_Update(&sha256, hash, SHA256_DIGEST_LENGTH);
SHA256_Final(hash, &sha256);
}
uchar_vector rval(hash, SHA256_DIGEST_LENGTH);
return rval;
}
uchar_vector extKeyBase58OrHexToBytes(const std::string& extKey) {
uchar_vector extendedKey;
if (isBase58CheckValid(extKey))
extendedKey = fromBase58ExtKey(extKey);
else if (StringUtils::isHex(extKey))
extendedKey = uchar_vector(extKey);
else
throw std::runtime_error("Invalid extended key. Extended key must be in base58 or hex form.");
return extendedKey;
}
uchar_vector fromBase58ExtKey(const std::string& extKey) {
static unsigned int dummy = 0;
uchar_vector fillKey;
fromBase58Check(extKey, fillKey, dummy);
static const std::string VERSION_BYTE("04");
return uchar_vector(VERSION_BYTE+fillKey.getHex()); //append VERSION_BYTE to begining
}
TreeChains parseChainString(const std::string& chainStr, bool isPrivate) {
TreeChains treeChains;
const std::string s = StringUtils::split(chainStr)[0]; //trim trailing whitespaces
std::deque<std::string> splitChain = StringUtils::split(s, '/');
//account for root node
treeChains.push_back(IsPrivateNPathRange(true , Range(NODE_IDX_M_FLAG, NODE_IDX_M_FLAG)));
if (splitChain.back() == "") splitChain.pop_back(); // happens if chainStr has '/' at end
for (std::string& node : splitChain) {
if (node.back() == '\'') {
if (! isPrivate) throw std::runtime_error("Invalid chain "+ chainStr+ ", not private extended key.");
node = node.substr(0,node.length()-1);
if (node.front() == '(' && node.back() == ')') {
IsPrivateNPathRange range = parseRange(node, true);
treeChains.push_back(range);
} else {
uint32_t num = toPrime(std::stoi(node));
treeChains.push_back(IsPrivateNPathRange(true , Range(num, num)));
}
} else {
if (node.front() == '(' && node.back() == ')') {
IsPrivateNPathRange range = parseRange(node, false);
treeChains.push_back(range);
} else {
uint32_t num = std::stoi(node);
treeChains.push_back(IsPrivateNPathRange(false , Range(num, num)));
}
}
}
return treeChains;
}
IsPrivateNPathRange parseRange(const std::string node, bool isPrivate) {
//node must be like (123-9345)
const std::string minMaxString = node.substr(1,node.length()-2);
const std::deque<std::string> minMaxPair = StringUtils::split(minMaxString, '-');
if (minMaxPair.size() != 2)
throw std::invalid_argument("Invalid arguments.");
uint32_t min = std::stoi(minMaxPair[0]);
uint32_t max = std::stoi(minMaxPair[1]);
if (isPrivate) {
return IsPrivateNPathRange(true, Range(min, max));
} else {
return IsPrivateNPathRange(false, Range(min, max));
}
}
std::string iToString(uint32_t i) {
std::stringstream ss;
ss << (0x7fffffff & i);
if (isPrime(i)) { ss << "'"; }
return ss.str();
}
}