7
7
#include < assert.h>
8
8
#include < string.h>
9
9
10
+ #include " base58.h"
11
+ #include " eccryptoverify.h"
12
+ #include " keystore.h"
10
13
#include " util.h"
14
+ #include " utilstrencodings.h"
11
15
16
+ #include " libdbb/crypto.h"
12
17
13
18
14
19
// ignore osx depracation warning
15
20
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
16
21
17
22
BitPayWalletClient::BitPayWalletClient ()
18
23
{
19
-
24
+ SelectParams (CBaseChainParams::MAIN);
20
25
}
21
26
22
27
BitPayWalletClient::~BitPayWalletClient ()
@@ -30,7 +35,120 @@ CKey BitPayWalletClient::GetNewKey()
30
35
return key;
31
36
}
32
37
38
+
39
+ std::vector<std::string> BitPayWalletClient::split (const std::string& str, std::vector<int > indexes) {
40
+ std::vector<std::string> parts;
41
+ indexes.push_back (str.size ());
42
+ int i = 0 ;
43
+ while (i < indexes.size ()) {
44
+ int from = i == 0 ? 0 : indexes[i - 1 ];
45
+ parts.push_back (str.substr (from, indexes[i]-from));
46
+ i++;
47
+ };
48
+ return parts;
49
+ };
50
+
51
+ std::string BitPayWalletClient::_copayerHash (const std::string& name, const std::string& xPubKey, const std::string& requestPubKey) {
52
+ return name+" |" +xPubKey+" |" +requestPubKey;
53
+ };
54
+
55
+ std::string BitPayWalletClient::GetXPubKey ()
56
+ {
57
+ CBitcoinExtPubKey xpubkey;
58
+ xpubkey.SetKey (masterPubKey);
59
+ return xpubkey.ToString ();
60
+ }
61
+ bool BitPayWalletClient::GetCopayerHash (const std::string& name, std::string& out) {
62
+ if (!requestKey.IsValid ())
63
+ return false ;
64
+
65
+ CBitcoinExtPubKey xpubkey;
66
+ xpubkey.SetKey (masterPubKey);
67
+
68
+ std::string requestKeyHex;
69
+ if (!GetRequestPubKey (requestKeyHex))
70
+ return false ;
71
+
72
+ out = _copayerHash (name, xpubkey.ToString (), requestKeyHex);
73
+ return true ;
74
+ };
75
+
76
+ bool BitPayWalletClient::GetCopayerSignature (const std::string& stringToHash, const CKey& privKey, std::string& sigHexOut) {
77
+ uint256 hash = Hash (stringToHash.begin (), stringToHash.end ());
78
+ std::vector<unsigned char > signature;
79
+ privKey.Sign (hash, signature);
80
+
81
+ sigHexOut = HexStr (signature);
82
+ return true ;
83
+ };
84
+
85
+ void BitPayWalletClient::seed ()
86
+ {
87
+ CKeyingMaterial vSeed;
88
+ vSeed.resize (32 );
89
+
90
+ RandAddSeedPerfmon ();
91
+ do {
92
+ GetRandBytes (&vSeed[0 ], vSeed.size ());
93
+ } while (!eccrypto::Check (&vSeed[0 ]));
94
+
95
+ CExtKey masterPrivKeyRoot;
96
+ masterPrivKeyRoot.SetMaster (&vSeed[0 ], vSeed.size ()); // m
97
+ masterPrivKeyRoot.Derive (masterPrivKey, 45 ); // m/45' xpriv
98
+ masterPubKey = masterPrivKey.Neuter (); // m/45' xpub
99
+
100
+ CExtKey requestKeyChain;
101
+ masterPrivKeyRoot.Derive (requestKeyChain, 1 ); // m/1'
102
+
103
+ CExtKey requestKeyExt;
104
+ requestKeyChain.Derive (requestKeyExt, 0 );
105
+
106
+ requestKey = requestKeyExt.key ;
107
+ }
108
+
109
+ bool BitPayWalletClient::GetRequestPubKey (std::string &pubKeyOut)
110
+ {
111
+ if (!requestKey.IsValid ())
112
+ return false ;
113
+
114
+ pubKeyOut = HexStr (requestKey.GetPubKey (), false );
115
+ return true ;
116
+ }
117
+
33
118
bool BitPayWalletClient::ParseWalletInvitation (const std::string& walletInvitation, BitpayWalletInvitation& invitationOut)
34
119
{
120
+ std::vector<int > splits = {22 , 74 };
121
+ std::vector<std::string> secretSplit = split (walletInvitation, splits);
122
+
123
+ // TODO: var widBase58 = secretSplit[0].replace(/0/g, '');
124
+ std::string widBase58 = secretSplit[0 ];
125
+ std::vector<unsigned char > vch;
126
+ if (!DecodeBase58 (widBase58.c_str (), vch))
127
+ return false ;
128
+
129
+ std::string widHex = HexStr (vch, false );
130
+
131
+ splits = {8 , 12 , 16 , 20 };
132
+ std::vector<std::string> walletIdParts = split (widHex, splits);
133
+ invitationOut.walletID = walletIdParts[0 ]+" -" +walletIdParts[1 ]+" -" +walletIdParts[2 ]+" -" +walletIdParts[3 ]+" -" +walletIdParts[4 ];
134
+
135
+ std::string walletPrivKeyStr = secretSplit[1 ];
136
+ CBitcoinSecret vchSecret;
137
+ if (!vchSecret.SetString (walletPrivKeyStr))
138
+ return false ;
139
+
140
+ invitationOut.walletPrivKey = vchSecret.GetKey ();
141
+ invitationOut.network = secretSplit[2 ] == " T" ? " testnet" : " livenet" ;
35
142
return true ;
36
- }
143
+ }
144
+
145
+ std::string BitPayWalletClient::SignRequest (const std::string& method,
146
+ const std::string& url,
147
+ const std::string& args)
148
+ {
149
+ std::string message = method+" |" +url+" |" +args;
150
+ uint256 hash = Hash (message.begin (), message.end ());
151
+ std::vector<unsigned char > signature;
152
+ requestKey.Sign (hash, signature);
153
+ return HexStr (signature);
154
+ };
0 commit comments