]>
Commit | Line | Data |
---|---|---|
652e1569 | 1 | // Copyright (c) 2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
72fb3d29 | 3 | // Distributed under the MIT software license, see the accompanying |
652e1569 WL |
4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | ||
6 | #include "base58.h" | |
71697f97 | 7 | #include "clientversion.h" |
652e1569 WL |
8 | #include "init.h" |
9 | #include "main.h" | |
10 | #include "net.h" | |
11 | #include "netbase.h" | |
c037531d | 12 | #include "rpcserver.h" |
14f888ca | 13 | #include "timedata.h" |
652e1569 WL |
14 | #include "util.h" |
15 | #ifdef ENABLE_WALLET | |
50c72f23 JS |
16 | #include "wallet/wallet.h" |
17 | #include "wallet/walletdb.h" | |
652e1569 WL |
18 | #endif |
19 | ||
20 | #include <stdint.h> | |
21 | ||
22 | #include <boost/assign/list_of.hpp> | |
a10a6e2a JS |
23 | |
24 | #include <univalue.h> | |
652e1569 | 25 | |
4e16a724 S |
26 | #include "zcash/Address.hpp" |
27 | ||
40a158e1 | 28 | using namespace std; |
652e1569 | 29 | |
f9de17ec WL |
30 | /** |
31 | * @note Do not add or change anything in the information returned by this | |
72fb3d29 | 32 | * method. `getinfo` exists for backwards-compatibility only. It combines |
f9de17ec WL |
33 | * information from wildly different sources in the program, which is a mess, |
34 | * and is thus planned to be deprecated eventually. | |
35 | * | |
36 | * Based on the source of the information, new information should be added to: | |
37 | * - `getblockchaininfo`, | |
38 | * - `getnetworkinfo` or | |
39 | * - `getwalletinfo` | |
40 | * | |
41 | * Or alternatively, create a specific query method for the information. | |
42 | **/ | |
945f015d | 43 | |
b487a8fe | 44 | int32_t Jumblr_depositaddradd(char *depositaddr); |
eeaaf554 | 45 | int32_t Jumblr_secretaddradd(char *secretaddr); |
da16b12e | 46 | uint64_t komodo_interestsum(); |
f595c2e0 | 47 | int32_t komodo_longestchain(); |
c75c18fc | 48 | int32_t komodo_notarized_height(uint256 *hashp,uint256 *txidp); |
e34a9d43 | 49 | uint32_t komodo_chainactive_timestamp(); |
8683bd8d | 50 | int32_t komodo_whoami(char *pubkeystr,int32_t height,uint32_t timestamp); |
eea03b7b | 51 | extern int32_t KOMODO_LASTMINED,JUMBLR_PAUSE; |
b000fa04 | 52 | extern char ASSETCHAINS_SYMBOL[]; |
0cb025c1 | 53 | int32_t notarizedtxid_height(char *dest,char *txidstr,int32_t *kmdnotarized_heightp); |
eeaaf554 | 54 | #define KOMODO_VERSION "0.1.1" |
49311c05 | 55 | uint32_t komodo_chainactive_timestamp(); |
09e3cf94 | 56 | |
d014114d | 57 | UniValue getinfo(const UniValue& params, bool fHelp) |
16bc9aaf | 58 | { |
7ea5a371 | 59 | uint256 notarized_hash,notarized_desttxid; int32_t notarized_height,longestchain,kmdnotarized_height,txid_height; |
16bc9aaf WL |
60 | if (fHelp || params.size() != 0) |
61 | throw runtime_error( | |
62 | "getinfo\n" | |
63 | "Returns an object containing various state info.\n" | |
64 | "\nResult:\n" | |
65 | "{\n" | |
66 | " \"version\": xxxxx, (numeric) the server version\n" | |
67 | " \"protocolversion\": xxxxx, (numeric) the protocol version\n" | |
68 | " \"walletversion\": xxxxx, (numeric) the wallet version\n" | |
c1652849 | 69 | " \"balance\": xxxxxxx, (numeric) the total Zcash balance of the wallet\n" |
16bc9aaf WL |
70 | " \"blocks\": xxxxxx, (numeric) the current number of blocks processed in the server\n" |
71 | " \"timeoffset\": xxxxx, (numeric) the time offset\n" | |
72 | " \"connections\": xxxxx, (numeric) the number of connections\n" | |
73 | " \"proxy\": \"host:port\", (string, optional) the proxy used by the server\n" | |
74 | " \"difficulty\": xxxxxx, (numeric) the current difficulty\n" | |
75 | " \"testnet\": true|false, (boolean) if the server is using testnet or not\n" | |
76 | " \"keypoololdest\": xxxxxx, (numeric) the timestamp (seconds since GMT epoch) of the oldest pre-generated key in the key pool\n" | |
77 | " \"keypoolsize\": xxxx, (numeric) how many new keys are pre-generated\n" | |
16bc9aaf | 78 | " \"unlocked_until\": ttt, (numeric) the timestamp in seconds since epoch (midnight Jan 1 1970 GMT) that the wallet is unlocked for transfers, or 0 if the wallet is locked\n" |
091b2116 RN |
79 | " \"paytxfee\": x.xxxx, (numeric) the transaction fee set in " + CURRENCY_UNIT + "/kB\n" |
80 | " \"relayfee\": x.xxxx, (numeric) minimum relay fee for non-free transactions in " + CURRENCY_UNIT + "/kB\n" | |
16bc9aaf WL |
81 | " \"errors\": \"...\" (string) any error messages\n" |
82 | "}\n" | |
83 | "\nExamples:\n" | |
84 | + HelpExampleCli("getinfo", "") | |
85 | + HelpExampleRpc("getinfo", "") | |
86 | ); | |
87 | ||
4401b2d7 EL |
88 | #ifdef ENABLE_WALLET |
89 | LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL); | |
90 | #else | |
91 | LOCK(cs_main); | |
92 | #endif | |
93 | ||
16bc9aaf WL |
94 | proxyType proxy; |
95 | GetProxy(NET_IPV4, proxy); | |
c75c18fc | 96 | notarized_height = komodo_notarized_height(¬arized_hash,¬arized_desttxid); |
16bc9aaf | 97 | |
38fc4b70 | 98 | UniValue obj(UniValue::VOBJ); |
faadbe17 PK |
99 | obj.push_back(Pair("version", CLIENT_VERSION)); |
100 | obj.push_back(Pair("protocolversion", PROTOCOL_VERSION)); | |
b000fa04 | 101 | obj.push_back(Pair("KMDversion", KOMODO_VERSION)); |
c75c18fc | 102 | obj.push_back(Pair("notarized", notarized_height)); |
103 | obj.push_back(Pair("notarizedhash", notarized_hash.ToString())); | |
104 | obj.push_back(Pair("notarizedtxid", notarized_desttxid.ToString())); | |
7ea5a371 | 105 | txid_height = notarizedtxid_height(ASSETCHAINS_SYMBOL[0] != 0 ? (char *)"KMD" : (char *)"BTC",(char *)notarized_desttxid.ToString().c_str(),&kmdnotarized_height); |
70d83934 | 106 | if ( txid_height > 0 ) |
107 | obj.push_back(Pair("notarizedtxid_height", txid_height)); | |
108 | else obj.push_back(Pair("notarizedtxid_height", "mempool")); | |
57dd4ada | 109 | if ( ASSETCHAINS_SYMBOL[0] != 0 ) |
110 | obj.push_back(Pair("KMDnotarized_height", kmdnotarized_height)); | |
111 | obj.push_back(Pair("notarized_confirms", txid_height < kmdnotarized_height ? (kmdnotarized_height - txid_height + 1) : 0)); | |
16bc9aaf WL |
112 | #ifdef ENABLE_WALLET |
113 | if (pwalletMain) { | |
114 | obj.push_back(Pair("walletversion", pwalletMain->GetVersion())); | |
115 | obj.push_back(Pair("balance", ValueFromAmount(pwalletMain->GetBalance()))); | |
9e1bde7e | 116 | if ( ASSETCHAINS_SYMBOL[0] == 0 ) |
b000fa04 | 117 | obj.push_back(Pair("interest", ValueFromAmount(komodo_interestsum()))); |
16bc9aaf WL |
118 | } |
119 | #endif | |
120 | obj.push_back(Pair("blocks", (int)chainActive.Height())); | |
a19b28ce | 121 | if ( (longestchain= komodo_longestchain()) != 0 && chainActive.Height() > longestchain ) |
122 | longestchain = chainActive.Height(); | |
123 | obj.push_back(Pair("longestchain", longestchain)); | |
d56e30ca | 124 | obj.push_back(Pair("timeoffset", GetTimeOffset())); |
98d166cf | 125 | if ( chainActive.Tip() != 0 ) |
49c9db6a | 126 | obj.push_back(Pair("tiptime", (int)chainActive.Tip()->nTime)); |
16bc9aaf | 127 | obj.push_back(Pair("connections", (int)vNodes.size())); |
67a79493 | 128 | obj.push_back(Pair("proxy", (proxy.IsValid() ? proxy.proxy.ToStringIPPort() : string()))); |
16bc9aaf | 129 | obj.push_back(Pair("difficulty", (double)GetDifficulty())); |
cc972107 | 130 | obj.push_back(Pair("testnet", Params().TestnetToBeDeprecatedFieldRPC())); |
16bc9aaf WL |
131 | #ifdef ENABLE_WALLET |
132 | if (pwalletMain) { | |
d56e30ca | 133 | obj.push_back(Pair("keypoololdest", pwalletMain->GetOldestKeyPoolTime())); |
16bc9aaf WL |
134 | obj.push_back(Pair("keypoolsize", (int)pwalletMain->GetKeyPoolSize())); |
135 | } | |
16bc9aaf | 136 | if (pwalletMain && pwalletMain->IsCrypted()) |
d56e30ca | 137 | obj.push_back(Pair("unlocked_until", nWalletUnlockTime)); |
c6cb21d1 | 138 | obj.push_back(Pair("paytxfee", ValueFromAmount(payTxFee.GetFeePerK()))); |
16bc9aaf | 139 | #endif |
13fc83c7 | 140 | obj.push_back(Pair("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK()))); |
16bc9aaf | 141 | obj.push_back(Pair("errors", GetWarnings("statusbar"))); |
58033467 | 142 | { |
143 | char pubkeystr[65]; int32_t notaryid; | |
e34a9d43 | 144 | if ( (notaryid= komodo_whoami(pubkeystr,(int32_t)chainActive.Tip()->nHeight,komodo_chainactive_timestamp())) >= 0 ) |
1e2a41f6 | 145 | { |
146 | obj.push_back(Pair("notaryid", notaryid)); | |
147 | obj.push_back(Pair("pubkey", pubkeystr)); | |
148 | if ( KOMODO_LASTMINED != 0 ) | |
149 | obj.push_back(Pair("lastmined", KOMODO_LASTMINED)); | |
150 | } | |
58033467 | 151 | } |
16bc9aaf WL |
152 | return obj; |
153 | } | |
154 | ||
452955f5 | 155 | #ifdef ENABLE_WALLET |
d014114d | 156 | class DescribeAddressVisitor : public boost::static_visitor<UniValue> |
452955f5 WL |
157 | { |
158 | public: | |
d014114d | 159 | UniValue operator()(const CNoDestination &dest) const { return UniValue(UniValue::VOBJ); } |
452955f5 | 160 | |
851f58f9 | 161 | UniValue operator()(const CKeyID &keyID) const { |
38fc4b70 | 162 | UniValue obj(UniValue::VOBJ); |
452955f5 | 163 | CPubKey vchPubKey; |
452955f5 | 164 | obj.push_back(Pair("isscript", false)); |
56215c77 | 165 | if (pwalletMain && pwalletMain->GetPubKey(keyID, vchPubKey)) { |
c8988460 PW |
166 | obj.push_back(Pair("pubkey", HexStr(vchPubKey))); |
167 | obj.push_back(Pair("iscompressed", vchPubKey.IsCompressed())); | |
168 | } | |
452955f5 WL |
169 | return obj; |
170 | } | |
171 | ||
851f58f9 | 172 | UniValue operator()(const CScriptID &scriptID) const { |
38fc4b70 | 173 | UniValue obj(UniValue::VOBJ); |
9c7167d1 | 174 | CScript subscript; |
452955f5 | 175 | obj.push_back(Pair("isscript", true)); |
56215c77 | 176 | if (pwalletMain && pwalletMain->GetCScript(scriptID, subscript)) { |
c8988460 PW |
177 | std::vector<CTxDestination> addresses; |
178 | txnouttype whichType; | |
179 | int nRequired; | |
180 | ExtractDestinations(subscript, whichType, addresses, nRequired); | |
181 | obj.push_back(Pair("script", GetTxnOutputType(whichType))); | |
182 | obj.push_back(Pair("hex", HexStr(subscript.begin(), subscript.end()))); | |
38fc4b70 | 183 | UniValue a(UniValue::VARR); |
c8988460 PW |
184 | BOOST_FOREACH(const CTxDestination& addr, addresses) |
185 | a.push_back(CBitcoinAddress(addr).ToString()); | |
186 | obj.push_back(Pair("addresses", a)); | |
187 | if (whichType == TX_MULTISIG) | |
188 | obj.push_back(Pair("sigsrequired", nRequired)); | |
189 | } | |
452955f5 WL |
190 | return obj; |
191 | } | |
192 | }; | |
193 | #endif | |
194 | ||
eeaaf554 | 195 | UniValue jumblr_deposit(const UniValue& params, bool fHelp) |
196 | { | |
b487a8fe | 197 | int32_t retval; UniValue result(UniValue::VOBJ); |
eeaaf554 | 198 | if (fHelp || params.size() != 1) |
199 | throw runtime_error("jumblr_deposit \"depositaddress\"\n"); | |
200 | CBitcoinAddress address(params[0].get_str()); | |
201 | bool isValid = address.IsValid(); | |
202 | if ( isValid != 0 ) | |
203 | { | |
204 | string addr = params[0].get_str(); | |
b487a8fe | 205 | if ( (retval= Jumblr_depositaddradd((char *)addr.c_str())) >= 0 ) |
95819099 | 206 | { |
190dbe6d | 207 | result.push_back(Pair("result", retval)); |
95819099 | 208 | JUMBLR_PAUSE = 0; |
209 | } | |
b487a8fe | 210 | else result.push_back(Pair("error", retval)); |
eeaaf554 | 211 | } else result.push_back(Pair("error", "invalid address")); |
212 | return(result); | |
213 | } | |
214 | ||
215 | UniValue jumblr_secret(const UniValue& params, bool fHelp) | |
216 | { | |
217 | int32_t retval; UniValue result(UniValue::VOBJ); | |
218 | if (fHelp || params.size() != 1) | |
219 | throw runtime_error("jumblr_secret \"secretaddress\"\n"); | |
220 | CBitcoinAddress address(params[0].get_str()); | |
221 | bool isValid = address.IsValid(); | |
222 | if ( isValid != 0 ) | |
223 | { | |
224 | string addr = params[0].get_str(); | |
bc035879 | 225 | retval = Jumblr_secretaddradd((char *)addr.c_str()); |
eeaaf554 | 226 | result.push_back(Pair("result", "success")); |
227 | result.push_back(Pair("num", retval)); | |
95819099 | 228 | JUMBLR_PAUSE = 0; |
eeaaf554 | 229 | } else result.push_back(Pair("error", "invalid address")); |
230 | return(result); | |
231 | } | |
232 | ||
eea03b7b | 233 | UniValue jumblr_pause(const UniValue& params, bool fHelp) |
234 | { | |
235 | int32_t retval; UniValue result(UniValue::VOBJ); | |
236 | if (fHelp ) | |
237 | throw runtime_error("jumblr_pause\n"); | |
238 | JUMBLR_PAUSE = 1; | |
239 | result.push_back(Pair("result", "paused")); | |
240 | return(result); | |
241 | } | |
242 | ||
243 | UniValue jumblr_resume(const UniValue& params, bool fHelp) | |
244 | { | |
245 | int32_t retval; UniValue result(UniValue::VOBJ); | |
246 | if (fHelp ) | |
247 | throw runtime_error("jumblr_resume\n"); | |
248 | JUMBLR_PAUSE = 0; | |
249 | result.push_back(Pair("result", "resumed")); | |
250 | return(result); | |
251 | } | |
252 | ||
d014114d | 253 | UniValue validateaddress(const UniValue& params, bool fHelp) |
452955f5 WL |
254 | { |
255 | if (fHelp || params.size() != 1) | |
256 | throw runtime_error( | |
d2c1e4a8 | 257 | "validateaddress \"zcashaddress\"\n" |
c1652849 | 258 | "\nReturn information about the given Zcash address.\n" |
452955f5 | 259 | "\nArguments:\n" |
c1652849 | 260 | "1. \"zcashaddress\" (string, required) The Zcash address to validate\n" |
452955f5 WL |
261 | "\nResult:\n" |
262 | "{\n" | |
263 | " \"isvalid\" : true|false, (boolean) If the address is valid or not. If not, this is the only property returned.\n" | |
c1652849 | 264 | " \"address\" : \"zcashaddress\", (string) The Zcash address validated\n" |
426a74ed | 265 | " \"scriptPubKey\" : \"hex\", (string) The hex encoded scriptPubKey generated by the address\n" |
452955f5 WL |
266 | " \"ismine\" : true|false, (boolean) If the address is yours or not\n" |
267 | " \"isscript\" : true|false, (boolean) If the key is a script\n" | |
268 | " \"pubkey\" : \"publickeyhex\", (string) The hex value of the raw public key\n" | |
269 | " \"iscompressed\" : true|false, (boolean) If the address is compressed\n" | |
7b782f5b | 270 | " \"account\" : \"account\" (string) DEPRECATED. The account associated with the address, \"\" is the default account\n" |
452955f5 WL |
271 | "}\n" |
272 | "\nExamples:\n" | |
273 | + HelpExampleCli("validateaddress", "\"1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc\"") | |
274 | + HelpExampleRpc("validateaddress", "\"1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc\"") | |
275 | ); | |
276 | ||
4401b2d7 EL |
277 | #ifdef ENABLE_WALLET |
278 | LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL); | |
279 | #else | |
280 | LOCK(cs_main); | |
281 | #endif | |
282 | ||
452955f5 WL |
283 | CBitcoinAddress address(params[0].get_str()); |
284 | bool isValid = address.IsValid(); | |
285 | ||
38fc4b70 | 286 | UniValue ret(UniValue::VOBJ); |
452955f5 WL |
287 | ret.push_back(Pair("isvalid", isValid)); |
288 | if (isValid) | |
289 | { | |
290 | CTxDestination dest = address.Get(); | |
291 | string currentAddress = address.ToString(); | |
292 | ret.push_back(Pair("address", currentAddress)); | |
426a74ed PT |
293 | |
294 | CScript scriptPubKey = GetScriptForDestination(dest); | |
295 | ret.push_back(Pair("scriptPubKey", HexStr(scriptPubKey.begin(), scriptPubKey.end()))); | |
296 | ||
452955f5 | 297 | #ifdef ENABLE_WALLET |
a3e192a3 J |
298 | isminetype mine = pwalletMain ? IsMine(*pwalletMain, dest) : ISMINE_NO; |
299 | ret.push_back(Pair("ismine", (mine & ISMINE_SPENDABLE) ? true : false)); | |
9c7167d1 | 300 | ret.push_back(Pair("iswatchonly", (mine & ISMINE_WATCH_ONLY) ? true: false)); |
301 | UniValue detail = boost::apply_visitor(DescribeAddressVisitor(), dest); | |
302 | ret.pushKVs(detail); | |
452955f5 WL |
303 | if (pwalletMain && pwalletMain->mapAddressBook.count(dest)) |
304 | ret.push_back(Pair("account", pwalletMain->mapAddressBook[dest].name)); | |
305 | #endif | |
306 | } | |
307 | return ret; | |
308 | } | |
309 | ||
4e16a724 | 310 | |
0d37ae3a | 311 | UniValue z_validateaddress(const UniValue& params, bool fHelp) |
4e16a724 S |
312 | { |
313 | if (fHelp || params.size() != 1) | |
314 | throw runtime_error( | |
315 | "z_validateaddress \"zaddr\"\n" | |
316 | "\nReturn information about the given z address.\n" | |
317 | "\nArguments:\n" | |
318 | "1. \"zaddr\" (string, required) The z address to validate\n" | |
319 | "\nResult:\n" | |
320 | "{\n" | |
321 | " \"isvalid\" : true|false, (boolean) If the address is valid or not. If not, this is the only property returned.\n" | |
322 | " \"address\" : \"zaddr\", (string) The z address validated\n" | |
323 | " \"ismine\" : true|false, (boolean) If the address is yours or not\n" | |
324 | " \"payingkey\" : \"hex\", (string) The hex value of the paying key, a_pk\n" | |
325 | " \"transmissionkey\" : \"hex\", (string) The hex value of the transmission key, pk_enc\n" | |
326 | ||
327 | "}\n" | |
328 | "\nExamples:\n" | |
337a99a2 JG |
329 | + HelpExampleCli("z_validateaddress", "\"zcWsmqT4X2V4jgxbgiCzyrAfRT1vi1F4sn7M5Pkh66izzw8Uk7LBGAH3DtcSMJeUb2pi3W4SQF8LMKkU2cUuVP68yAGcomL\"") |
330 | + HelpExampleRpc("z_validateaddress", "\"zcWsmqT4X2V4jgxbgiCzyrAfRT1vi1F4sn7M5Pkh66izzw8Uk7LBGAH3DtcSMJeUb2pi3W4SQF8LMKkU2cUuVP68yAGcomL\"") | |
4e16a724 S |
331 | ); |
332 | ||
333 | ||
334 | #ifdef ENABLE_WALLET | |
335 | LOCK2(cs_main, pwalletMain->cs_wallet); | |
336 | #else | |
337 | LOCK(cs_main); | |
338 | #endif | |
339 | ||
340 | bool isValid = false; | |
341 | bool isMine = false; | |
342 | std::string payingKey, transmissionKey; | |
343 | ||
344 | string strAddress = params[0].get_str(); | |
345 | try { | |
346 | CZCPaymentAddress address(strAddress); | |
347 | libzcash::PaymentAddress addr = address.Get(); | |
348 | ||
349 | #ifdef ENABLE_WALLET | |
350 | isMine = pwalletMain->HaveSpendingKey(addr); | |
351 | #endif | |
352 | payingKey = addr.a_pk.GetHex(); | |
353 | transmissionKey = addr.pk_enc.GetHex(); | |
354 | isValid = true; | |
355 | } catch (std::runtime_error e) { | |
356 | // address is invalid, nop here as isValid is false. | |
357 | } | |
358 | ||
0d37ae3a | 359 | UniValue ret(UniValue::VOBJ); |
4e16a724 S |
360 | ret.push_back(Pair("isvalid", isValid)); |
361 | if (isValid) | |
362 | { | |
363 | ret.push_back(Pair("address", strAddress)); | |
364 | ret.push_back(Pair("payingkey", payingKey)); | |
365 | ret.push_back(Pair("transmissionkey", transmissionKey)); | |
366 | #ifdef ENABLE_WALLET | |
367 | ret.push_back(Pair("ismine", isMine)); | |
368 | #endif | |
369 | } | |
370 | return ret; | |
371 | } | |
372 | ||
373 | ||
72fb3d29 MF |
374 | /** |
375 | * Used by addmultisigaddress / createmultisig: | |
376 | */ | |
d014114d | 377 | CScript _createmultisig_redeemScript(const UniValue& params) |
723a03d2 WL |
378 | { |
379 | int nRequired = params[0].get_int(); | |
d014114d | 380 | const UniValue& keys = params[1].get_array(); |
723a03d2 WL |
381 | |
382 | // Gather public keys | |
383 | if (nRequired < 1) | |
384 | throw runtime_error("a multisignature address must require at least one key to redeem"); | |
385 | if ((int)keys.size() < nRequired) | |
386 | throw runtime_error( | |
387 | strprintf("not enough keys supplied " | |
783b182c | 388 | "(got %u keys, but need at least %d to redeem)", keys.size(), nRequired)); |
e5d9d77d | 389 | if (keys.size() > 16) |
390 | throw runtime_error("Number of addresses involved in the multisignature address creation > 16\nReduce the number"); | |
723a03d2 WL |
391 | std::vector<CPubKey> pubkeys; |
392 | pubkeys.resize(keys.size()); | |
393 | for (unsigned int i = 0; i < keys.size(); i++) | |
394 | { | |
395 | const std::string& ks = keys[i].get_str(); | |
396 | #ifdef ENABLE_WALLET | |
397 | // Case 1: Bitcoin address and we have full public key: | |
398 | CBitcoinAddress address(ks); | |
399 | if (pwalletMain && address.IsValid()) | |
400 | { | |
401 | CKeyID keyID; | |
402 | if (!address.GetKeyID(keyID)) | |
403 | throw runtime_error( | |
7d9d134b | 404 | strprintf("%s does not refer to a key",ks)); |
723a03d2 WL |
405 | CPubKey vchPubKey; |
406 | if (!pwalletMain->GetPubKey(keyID, vchPubKey)) | |
407 | throw runtime_error( | |
7d9d134b | 408 | strprintf("no full public key for address %s",ks)); |
723a03d2 WL |
409 | if (!vchPubKey.IsFullyValid()) |
410 | throw runtime_error(" Invalid public key: "+ks); | |
411 | pubkeys[i] = vchPubKey; | |
412 | } | |
413 | ||
414 | // Case 2: hex public key | |
415 | else | |
416 | #endif | |
417 | if (IsHex(ks)) | |
418 | { | |
419 | CPubKey vchPubKey(ParseHex(ks)); | |
420 | if (!vchPubKey.IsFullyValid()) | |
421 | throw runtime_error(" Invalid public key: "+ks); | |
422 | pubkeys[i] = vchPubKey; | |
423 | } | |
424 | else | |
425 | { | |
426 | throw runtime_error(" Invalid public key: "+ks); | |
427 | } | |
428 | } | |
0be990ba | 429 | CScript result = GetScriptForMultisig(nRequired, pubkeys); |
787ee0c9 PT |
430 | |
431 | if (result.size() > MAX_SCRIPT_ELEMENT_SIZE) | |
432 | throw runtime_error( | |
433 | strprintf("redeemScript exceeds size limit: %d > %d", result.size(), MAX_SCRIPT_ELEMENT_SIZE)); | |
434 | ||
723a03d2 WL |
435 | return result; |
436 | } | |
437 | ||
d014114d | 438 | UniValue createmultisig(const UniValue& params, bool fHelp) |
723a03d2 WL |
439 | { |
440 | if (fHelp || params.size() < 2 || params.size() > 2) | |
441 | { | |
442 | string msg = "createmultisig nrequired [\"key\",...]\n" | |
443 | "\nCreates a multi-signature address with n signature of m keys required.\n" | |
444 | "It returns a json object with the address and redeemScript.\n" | |
445 | ||
446 | "\nArguments:\n" | |
447 | "1. nrequired (numeric, required) The number of required signatures out of the n keys or addresses.\n" | |
c1652849 | 448 | "2. \"keys\" (string, required) A json array of keys which are Zcash addresses or hex-encoded public keys\n" |
723a03d2 | 449 | " [\n" |
c1652849 | 450 | " \"key\" (string) Zcash address or hex-encoded public key\n" |
723a03d2 WL |
451 | " ,...\n" |
452 | " ]\n" | |
453 | ||
454 | "\nResult:\n" | |
455 | "{\n" | |
456 | " \"address\":\"multisigaddress\", (string) The value of the new multisig address.\n" | |
457 | " \"redeemScript\":\"script\" (string) The string value of the hex-encoded redemption script.\n" | |
458 | "}\n" | |
459 | ||
460 | "\nExamples:\n" | |
461 | "\nCreate a multisig address from 2 addresses\n" | |
70454796 | 462 | + HelpExampleCli("createmultisig", "2 \"[\\\"t16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5\\\",\\\"t171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV\\\"]\"") + |
723a03d2 | 463 | "\nAs a json rpc call\n" |
70454796 | 464 | + HelpExampleRpc("createmultisig", "2, \"[\\\"t16sSauSf5pF2UkUwvKGq4qjNRzBZYqgEL5\\\",\\\"t171sgjn4YtPu27adkKGrdDwzRTxnRkBfKV\\\"]\"") |
723a03d2 WL |
465 | ; |
466 | throw runtime_error(msg); | |
467 | } | |
468 | ||
469 | // Construct using pay-to-script-hash: | |
787ee0c9 | 470 | CScript inner = _createmultisig_redeemScript(params); |
066e2a14 | 471 | CScriptID innerID(inner); |
723a03d2 WL |
472 | CBitcoinAddress address(innerID); |
473 | ||
b47f3aea | 474 | UniValue result(UniValue::VOBJ); |
723a03d2 WL |
475 | result.push_back(Pair("address", address.ToString())); |
476 | result.push_back(Pair("redeemScript", HexStr(inner.begin(), inner.end()))); | |
477 | ||
478 | return result; | |
479 | } | |
480 | ||
d014114d | 481 | UniValue verifymessage(const UniValue& params, bool fHelp) |
c3a7f516 WL |
482 | { |
483 | if (fHelp || params.size() != 3) | |
484 | throw runtime_error( | |
d2c1e4a8 | 485 | "verifymessage \"zcashaddress\" \"signature\" \"message\"\n" |
c3a7f516 WL |
486 | "\nVerify a signed message\n" |
487 | "\nArguments:\n" | |
c1652849 | 488 | "1. \"zcashaddress\" (string, required) The Zcash address to use for the signature.\n" |
c3a7f516 WL |
489 | "2. \"signature\" (string, required) The signature provided by the signer in base 64 encoding (see signmessage).\n" |
490 | "3. \"message\" (string, required) The message that was signed.\n" | |
491 | "\nResult:\n" | |
492 | "true|false (boolean) If the signature is verified or not.\n" | |
493 | "\nExamples:\n" | |
494 | "\nUnlock the wallet for 30 seconds\n" | |
495 | + HelpExampleCli("walletpassphrase", "\"mypassphrase\" 30") + | |
496 | "\nCreate the signature\n" | |
70454796 | 497 | + HelpExampleCli("signmessage", "\"t14oHp2v54vfmdgQ3v3SNuQga8JKHTNi2a1\" \"my message\"") + |
c3a7f516 | 498 | "\nVerify the signature\n" |
70454796 | 499 | + HelpExampleCli("verifymessage", "\"t14oHp2v54vfmdgQ3v3SNuQga8JKHTNi2a1\" \"signature\" \"my message\"") + |
c3a7f516 | 500 | "\nAs json rpc\n" |
70454796 | 501 | + HelpExampleRpc("verifymessage", "\"t14oHp2v54vfmdgQ3v3SNuQga8JKHTNi2a1\", \"signature\", \"my message\"") |
c3a7f516 WL |
502 | ); |
503 | ||
4401b2d7 EL |
504 | LOCK(cs_main); |
505 | ||
c3a7f516 WL |
506 | string strAddress = params[0].get_str(); |
507 | string strSign = params[1].get_str(); | |
508 | string strMessage = params[2].get_str(); | |
509 | ||
510 | CBitcoinAddress addr(strAddress); | |
511 | if (!addr.IsValid()) | |
512 | throw JSONRPCError(RPC_TYPE_ERROR, "Invalid address"); | |
513 | ||
514 | CKeyID keyID; | |
515 | if (!addr.GetKeyID(keyID)) | |
516 | throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key"); | |
517 | ||
518 | bool fInvalid = false; | |
519 | vector<unsigned char> vchSig = DecodeBase64(strSign.c_str(), &fInvalid); | |
520 | ||
521 | if (fInvalid) | |
522 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Malformed base64 encoding"); | |
523 | ||
524 | CHashWriter ss(SER_GETHASH, 0); | |
525 | ss << strMessageMagic; | |
526 | ss << strMessage; | |
527 | ||
528 | CPubKey pubkey; | |
529 | if (!pubkey.RecoverCompact(ss.GetHash(), vchSig)) | |
530 | return false; | |
531 | ||
532 | return (pubkey.GetID() == keyID); | |
533 | } | |
a8b2ce55 | 534 | |
d014114d | 535 | UniValue setmocktime(const UniValue& params, bool fHelp) |
a8b2ce55 GA |
536 | { |
537 | if (fHelp || params.size() != 1) | |
538 | throw runtime_error( | |
539 | "setmocktime timestamp\n" | |
540 | "\nSet the local time to given timestamp (-regtest only)\n" | |
541 | "\nArguments:\n" | |
542 | "1. timestamp (integer, required) Unix seconds-since-epoch timestamp\n" | |
543 | " Pass 0 to go back to using the system time." | |
544 | ); | |
545 | ||
546 | if (!Params().MineBlocksOnDemand()) | |
547 | throw runtime_error("setmocktime for regression testing (-regtest mode) only"); | |
548 | ||
abb0e8cc GA |
549 | // cs_vNodes is locked and node send/receive times are updated |
550 | // atomically with the time change to prevent peers from being | |
551 | // disconnected because we think we haven't communicated with them | |
552 | // in a long time. | |
553 | LOCK2(cs_main, cs_vNodes); | |
4401b2d7 | 554 | |
9756b7bd | 555 | RPCTypeCheck(params, boost::assign::list_of(UniValue::VNUM)); |
a8b2ce55 GA |
556 | SetMockTime(params[0].get_int64()); |
557 | ||
abb0e8cc GA |
558 | uint64_t t = GetTime(); |
559 | BOOST_FOREACH(CNode* pnode, vNodes) { | |
560 | pnode->nLastSend = pnode->nLastRecv = t; | |
561 | } | |
562 | ||
9756b7bd | 563 | return NullUniValue; |
a8b2ce55 | 564 | } |