Repository navigation
Expand file tree
/
Copy pathKeys.cpp
More file actions
170 lines (145 loc) · 4.75 KB
/
Copy pathKeys.cpp
File metadata and controls
170 lines (145 loc) · 4.75 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
#include "Keys.h"
#include "WalletConsole.h"
#include "Data.h"
#include "PrivateKey.h"
#include "HexCoding.h"
#include "HDWallet.h"
#include <iostream>
#include <vector>
#include <cassert>
namespace TW::WalletConsole {
using namespace std;
Keys::Keys(ostream& out, const Coins& coins) : _out(out), _coins(coins) {
// init a random mnemonic
HDWallet newwall(128, "");
_currentMnemonic = newwall.mnemonic;
}
void privateKeyToResult(const PrivateKey& priKey, string& res_out) {
// take the key, but may need to take extension as well
res_out = hex(priKey.bytes);
if (priKey.extensionBytes.size() > 0) {
res_out += hex(priKey.extensionBytes);
res_out += hex(priKey.chainCodeBytes);
}
}
bool Keys::newKey(const string& coinid, string& res) {
// Create a new private key by creating a new HDWallet and deriving from it
// Use coin-specific derivation path, so that PK can be coin-specific (e.g. longer for Cardano)
// coin
Coin coin;
if (!_coins.findCoin(coinid, coin)) { return false; }
HDWallet newWallet(256, "");
DerivationPath derivationPath = DerivationPath(coin.derivPath);
PrivateKey key = newWallet.getKey(derivationPath);
privateKeyToResult(key, res);
return true;
}
bool Keys::pubPri(const string& coinid, const string& p, string& res) {
Coin coin;
if (!_coins.findCoin(coinid, coin)) { return false; }
Data privDat;
try {
privDat = parse_hex(p);
auto priv = PrivateKey(privDat);
auto pub = priv.getPublicKey((TWPublicKeyType)coin.pubKeyType);
res = hex(pub.bytes);
_out << "Public key created, type " << (int)coin.pubKeyType << ", length " << pub.bytes.size() << endl;
return true;
} catch (exception& ex) {
_out << "Error: " << ex.what() << endl;
return false;
}
}
bool Keys::priPub(const string& p, string& res) {
_out << "Not yet implemented! :)" << endl;
return false;
}
void Keys::setMnemonic(const vector<string>& param) {
if (param.size() < 1 + 12) {
_out << "Error: at least 12 words are needed for the mnemonic!" << endl;
return;
}
// concatenate
string mnem = "";
for (int i = 1; i < param.size(); ++i) {
if (i > 1) mnem += " ";
mnem += param[i];
}
// verify mnemonic
if (!HDWallet::isValid(mnem)) {
_out << "Not a valid mnemonic: " << mnem << endl;
return;
}
// store
_currentMnemonic = mnem;
_out << "Mnemonic set (" << param.size() - 1 << " words)." << endl;
}
bool Keys::newMnemonic(const string& param1, string& res) {
int strength = stoi(param1);
if (strength < 128 || strength > 256 || (strength % 32 != 0)) {
_out << "Error: strength must be between 128 and 256, and multiple of 32" << endl;
return false;
}
HDWallet newwall(strength, "");
if (newwall.mnemonic.length() == 0) {
_out << "Error: no mnemonic generated." << endl;
return false;
}
// store
_currentMnemonic = newwall.mnemonic;
res = _currentMnemonic;
_out << "New mnemonic set." << endl;
return false;
}
bool Keys::dumpSeed(string& res) {
if (_currentMnemonic.length() == 0) {
_out << "Error: no mnemonic set. Use setMnemonic." << endl;
return false;
}
HDWallet wallet(_currentMnemonic, "");
string seedHex = hex(wallet.seed);
res = seedHex;
return true;
}
bool Keys::dumpMnemonic(string& res) {
if (_currentMnemonic.length() == 0) {
_out << "Error: no mnemonic set. Use setMnemonic." << endl;
return false;
}
res = _currentMnemonic;
return true;
}
bool Keys::dumpDP(const string& coinid, string& res) {
Coin coin;
if (!_coins.findCoin(coinid, coin)) { return false; }
res = coin.derivPath;
return true;
}
bool Keys::priDP(const string& coinid, const string& dp, string& res) {
// coin
Coin coin;
if (!_coins.findCoin(coinid, coin)) { return false; }
// mnemo
if (_currentMnemonic.length() == 0) {
_out << "Error: no mnemonic set. Use setMnemonic." << endl;
return false;
}
// derivation path
string dp2 = dp;
if (dp2.length() == 0) {
// missing dp, use default
dp2 = coin.derivPath;
}
DerivationPath dp3(dp2);
_out << "Using derivation path \"" << dp2 << "\" for coin " << coin.name << endl;
HDWallet wallet(_currentMnemonic, "");
PrivateKey priKey = wallet.getKey(dp3);
privateKeyToResult(priKey, res);
return true;
}
} // namespace TW::WalletConsole