Bitcoin Core  0.18.99
P2P Digital Currency
descriptor.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <script/descriptor.h>
6 
7 #include <key_io.h>
8 #include <pubkey.h>
9 #include <script/script.h>
10 #include <script/standard.h>
11 
12 #include <span.h>
13 #include <util/bip32.h>
14 #include <util/system.h>
15 #include <util/strencodings.h>
16 
17 #include <memory>
18 #include <string>
19 #include <vector>
20 
21 namespace {
22 
24 // Checksum //
26 
27 // This section implements a checksum algorithm for descriptors with the
28 // following properties:
29 // * Mistakes in a descriptor string are measured in "symbol errors". The higher
30 // the number of symbol errors, the harder it is to detect:
31 // * An error substituting a character from 0123456789()[],'/*abcdefgh@:$%{} for
32 // another in that set always counts as 1 symbol error.
33 // * Note that hex encoded keys are covered by these characters. Xprvs and
34 // xpubs use other characters too, but already have their own checksum
35 // mechanism.
36 // * Function names like "multi()" use other characters, but mistakes in
37 // these would generally result in an unparseable descriptor.
38 // * A case error always counts as 1 symbol error.
39 // * Any other 1 character substitution error counts as 1 or 2 symbol errors.
40 // * Any 1 symbol error is always detected.
41 // * Any 2 or 3 symbol error in a descriptor of up to 49154 characters is always detected.
42 // * Any 4 symbol error in a descriptor of up to 507 characters is always detected.
43 // * Any 5 symbol error in a descriptor of up to 77 characters is always detected.
44 // * Is optimized to minimize the chance a 5 symbol error in a descriptor up to 387 characters is undetected
45 // * Random errors have a chance of 1 in 2**40 of being undetected.
46 //
47 // These properties are achieved by expanding every group of 3 (non checksum) characters into
48 // 4 GF(32) symbols, over which a cyclic code is defined.
49 
50 /*
51  * Interprets c as 8 groups of 5 bits which are the coefficients of a degree 8 polynomial over GF(32),
52  * multiplies that polynomial by x, computes its remainder modulo a generator, and adds the constant term val.
53  *
54  * This generator is G(x) = x^8 + {30}x^7 + {23}x^6 + {15}x^5 + {14}x^4 + {10}x^3 + {6}x^2 + {12}x + {9}.
55  * It is chosen to define an cyclic error detecting code which is selected by:
56  * - Starting from all BCH codes over GF(32) of degree 8 and below, which by construction guarantee detecting
57  * 3 errors in windows up to 19000 symbols.
58  * - Taking all those generators, and for degree 7 ones, extend them to degree 8 by adding all degree-1 factors.
59  * - Selecting just the set of generators that guarantee detecting 4 errors in a window of length 512.
60  * - Selecting one of those with best worst-case behavior for 5 errors in windows of length up to 512.
61  *
62  * The generator and the constants to implement it can be verified using this Sage code:
63  * B = GF(2) # Binary field
64  * BP.<b> = B[] # Polynomials over the binary field
65  * F_mod = b**5 + b**3 + 1
66  * F.<f> = GF(32, modulus=F_mod, repr='int') # GF(32) definition
67  * FP.<x> = F[] # Polynomials over GF(32)
68  * E_mod = x**3 + x + F.fetch_int(8)
69  * E.<e> = F.extension(E_mod) # Extension field definition
70  * alpha = e**2743 # Choice of an element in extension field
71  * for p in divisors(E.order() - 1): # Verify alpha has order 32767.
72  * assert((alpha**p == 1) == (p % 32767 == 0))
73  * G = lcm([(alpha**i).minpoly() for i in [1056,1057,1058]] + [x + 1])
74  * print(G) # Print out the generator
75  * for i in [1,2,4,8,16]: # Print out {1,2,4,8,16}*(G mod x^8), packed in hex integers.
76  * v = 0
77  * for coef in reversed((F.fetch_int(i)*(G % x**8)).coefficients(sparse=True)):
78  * v = v*32 + coef.integer_representation()
79  * print("0x%x" % v)
80  */
81 uint64_t PolyMod(uint64_t c, int val)
82 {
83  uint8_t c0 = c >> 35;
84  c = ((c & 0x7ffffffff) << 5) ^ val;
85  if (c0 & 1) c ^= 0xf5dee51989;
86  if (c0 & 2) c ^= 0xa9fdca3312;
87  if (c0 & 4) c ^= 0x1bab10e32d;
88  if (c0 & 8) c ^= 0x3706b1677a;
89  if (c0 & 16) c ^= 0x644d626ffd;
90  return c;
91 }
92 
93 std::string DescriptorChecksum(const Span<const char>& span)
94 {
108  static std::string INPUT_CHARSET =
109  "0123456789()[],'/*abcdefgh@:$%{}"
110  "IJKLMNOPQRSTUVWXYZ&+-.;<=>?!^_|~"
111  "ijklmnopqrstuvwxyzABCDEFGH`#\"\\ ";
112 
114  static std::string CHECKSUM_CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
115 
116  uint64_t c = 1;
117  int cls = 0;
118  int clscount = 0;
119  for (auto ch : span) {
120  auto pos = INPUT_CHARSET.find(ch);
121  if (pos == std::string::npos) return "";
122  c = PolyMod(c, pos & 31); // Emit a symbol for the position inside the group, for every character.
123  cls = cls * 3 + (pos >> 5); // Accumulate the group numbers
124  if (++clscount == 3) {
125  // Emit an extra symbol representing the group numbers, for every 3 characters.
126  c = PolyMod(c, cls);
127  cls = 0;
128  clscount = 0;
129  }
130  }
131  if (clscount > 0) c = PolyMod(c, cls);
132  for (int j = 0; j < 8; ++j) c = PolyMod(c, 0); // Shift further to determine the checksum.
133  c ^= 1; // Prevent appending zeroes from not affecting the checksum.
134 
135  std::string ret(8, ' ');
136  for (int j = 0; j < 8; ++j) ret[j] = CHECKSUM_CHARSET[(c >> (5 * (7 - j))) & 31];
137  return ret;
138 }
139 
140 std::string AddChecksum(const std::string& str) { return str + "#" + DescriptorChecksum(MakeSpan(str)); }
141 
143 // Internal representation //
145 
146 typedef std::vector<uint32_t> KeyPath;
147 
149 struct PubkeyProvider
150 {
151  virtual ~PubkeyProvider() = default;
152 
154  virtual bool GetPubKey(int pos, const SigningProvider& arg, CPubKey* key, KeyOriginInfo& info) const = 0;
155 
157  virtual bool IsRange() const = 0;
158 
160  virtual size_t GetSize() const = 0;
161 
163  virtual std::string ToString() const = 0;
164 
166  virtual bool ToPrivateString(const SigningProvider& arg, std::string& out) const = 0;
167 
169  virtual bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const = 0;
170 };
171 
172 class OriginPubkeyProvider final : public PubkeyProvider
173 {
174  KeyOriginInfo m_origin;
175  std::unique_ptr<PubkeyProvider> m_provider;
176 
177  std::string OriginString() const
178  {
179  return HexStr(std::begin(m_origin.fingerprint), std::end(m_origin.fingerprint)) + FormatHDKeypath(m_origin.path);
180  }
181 
182 public:
183  OriginPubkeyProvider(KeyOriginInfo info, std::unique_ptr<PubkeyProvider> provider) : m_origin(std::move(info)), m_provider(std::move(provider)) {}
184  bool GetPubKey(int pos, const SigningProvider& arg, CPubKey* key, KeyOriginInfo& info) const override
185  {
186  if (!m_provider->GetPubKey(pos, arg, key, info)) return false;
187  std::copy(std::begin(m_origin.fingerprint), std::end(m_origin.fingerprint), info.fingerprint);
188  info.path.insert(info.path.begin(), m_origin.path.begin(), m_origin.path.end());
189  return true;
190  }
191  bool IsRange() const override { return m_provider->IsRange(); }
192  size_t GetSize() const override { return m_provider->GetSize(); }
193  std::string ToString() const override { return "[" + OriginString() + "]" + m_provider->ToString(); }
194  bool ToPrivateString(const SigningProvider& arg, std::string& ret) const override
195  {
196  std::string sub;
197  if (!m_provider->ToPrivateString(arg, sub)) return false;
198  ret = "[" + OriginString() + "]" + std::move(sub);
199  return true;
200  }
201  bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override
202  {
203  return m_provider->GetPrivKey(pos, arg, key);
204  }
205 };
206 
208 class ConstPubkeyProvider final : public PubkeyProvider
209 {
210  CPubKey m_pubkey;
211 
212 public:
213  ConstPubkeyProvider(const CPubKey& pubkey) : m_pubkey(pubkey) {}
214  bool GetPubKey(int pos, const SigningProvider& arg, CPubKey* key, KeyOriginInfo& info) const override
215  {
216  if (key) *key = m_pubkey;
217  info.path.clear();
218  CKeyID keyid = m_pubkey.GetID();
219  std::copy(keyid.begin(), keyid.begin() + sizeof(info.fingerprint), info.fingerprint);
220  return true;
221  }
222  bool IsRange() const override { return false; }
223  size_t GetSize() const override { return m_pubkey.size(); }
224  std::string ToString() const override { return HexStr(m_pubkey.begin(), m_pubkey.end()); }
225  bool ToPrivateString(const SigningProvider& arg, std::string& ret) const override
226  {
227  CKey key;
228  if (!arg.GetKey(m_pubkey.GetID(), key)) return false;
229  ret = EncodeSecret(key);
230  return true;
231  }
232  bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override
233  {
234  return arg.GetKey(m_pubkey.GetID(), key);
235  }
236 };
237 
238 enum class DeriveType {
239  NO,
240  UNHARDENED,
241  HARDENED,
242 };
243 
245 class BIP32PubkeyProvider final : public PubkeyProvider
246 {
247  CExtPubKey m_extkey;
248  KeyPath m_path;
249  DeriveType m_derive;
250 
251  bool GetExtKey(const SigningProvider& arg, CExtKey& ret) const
252  {
253  CKey key;
254  if (!arg.GetKey(m_extkey.pubkey.GetID(), key)) return false;
255  ret.nDepth = m_extkey.nDepth;
256  std::copy(m_extkey.vchFingerprint, m_extkey.vchFingerprint + sizeof(ret.vchFingerprint), ret.vchFingerprint);
257  ret.nChild = m_extkey.nChild;
258  ret.chaincode = m_extkey.chaincode;
259  ret.key = key;
260  return true;
261  }
262 
263  bool IsHardened() const
264  {
265  if (m_derive == DeriveType::HARDENED) return true;
266  for (auto entry : m_path) {
267  if (entry >> 31) return true;
268  }
269  return false;
270  }
271 
272 public:
273  BIP32PubkeyProvider(const CExtPubKey& extkey, KeyPath path, DeriveType derive) : m_extkey(extkey), m_path(std::move(path)), m_derive(derive) {}
274  bool IsRange() const override { return m_derive != DeriveType::NO; }
275  size_t GetSize() const override { return 33; }
276  bool GetPubKey(int pos, const SigningProvider& arg, CPubKey* key, KeyOriginInfo& info) const override
277  {
278  if (key) {
279  if (IsHardened()) {
280  CKey priv_key;
281  if (!GetPrivKey(pos, arg, priv_key)) return false;
282  *key = priv_key.GetPubKey();
283  } else {
284  // TODO: optimize by caching
285  CExtPubKey extkey = m_extkey;
286  for (auto entry : m_path) {
287  extkey.Derive(extkey, entry);
288  }
289  if (m_derive == DeriveType::UNHARDENED) extkey.Derive(extkey, pos);
290  assert(m_derive != DeriveType::HARDENED);
291  *key = extkey.pubkey;
292  }
293  }
294  CKeyID keyid = m_extkey.pubkey.GetID();
295  std::copy(keyid.begin(), keyid.begin() + sizeof(info.fingerprint), info.fingerprint);
296  info.path = m_path;
297  if (m_derive == DeriveType::UNHARDENED) info.path.push_back((uint32_t)pos);
298  if (m_derive == DeriveType::HARDENED) info.path.push_back(((uint32_t)pos) | 0x80000000L);
299  return true;
300  }
301  std::string ToString() const override
302  {
303  std::string ret = EncodeExtPubKey(m_extkey) + FormatHDKeypath(m_path);
304  if (IsRange()) {
305  ret += "/*";
306  if (m_derive == DeriveType::HARDENED) ret += '\'';
307  }
308  return ret;
309  }
310  bool ToPrivateString(const SigningProvider& arg, std::string& out) const override
311  {
312  CExtKey key;
313  if (!GetExtKey(arg, key)) return false;
314  out = EncodeExtKey(key) + FormatHDKeypath(m_path);
315  if (IsRange()) {
316  out += "/*";
317  if (m_derive == DeriveType::HARDENED) out += '\'';
318  }
319  return true;
320  }
321  bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override
322  {
323  CExtKey extkey;
324  if (!GetExtKey(arg, extkey)) return false;
325  for (auto entry : m_path) {
326  extkey.Derive(extkey, entry);
327  }
328  if (m_derive == DeriveType::UNHARDENED) extkey.Derive(extkey, pos);
329  if (m_derive == DeriveType::HARDENED) extkey.Derive(extkey, pos | 0x80000000UL);
330  key = extkey.key;
331  return true;
332  }
333 };
334 
336 class DescriptorImpl : public Descriptor
337 {
339  const std::vector<std::unique_ptr<PubkeyProvider>> m_pubkey_args;
343  const std::unique_ptr<DescriptorImpl> m_subdescriptor_arg;
345  const std::string m_name;
346 
347 protected:
349  virtual std::string ToStringExtra() const { return ""; }
350 
362  virtual std::vector<CScript> MakeScripts(const std::vector<CPubKey>& pubkeys, const CScript* script, FlatSigningProvider& out) const = 0;
363 
364 public:
365  DescriptorImpl(std::vector<std::unique_ptr<PubkeyProvider>> pubkeys, std::unique_ptr<DescriptorImpl> script, const std::string& name) : m_pubkey_args(std::move(pubkeys)), m_subdescriptor_arg(std::move(script)), m_name(name) {}
366 
367  bool IsSolvable() const override
368  {
369  if (m_subdescriptor_arg) {
370  if (!m_subdescriptor_arg->IsSolvable()) return false;
371  }
372  return true;
373  }
374 
375  bool IsRange() const final
376  {
377  for (const auto& pubkey : m_pubkey_args) {
378  if (pubkey->IsRange()) return true;
379  }
380  if (m_subdescriptor_arg) {
381  if (m_subdescriptor_arg->IsRange()) return true;
382  }
383  return false;
384  }
385 
386  bool ToStringHelper(const SigningProvider* arg, std::string& out, bool priv) const
387  {
388  std::string extra = ToStringExtra();
389  size_t pos = extra.size() > 0 ? 1 : 0;
390  std::string ret = m_name + "(" + extra;
391  for (const auto& pubkey : m_pubkey_args) {
392  if (pos++) ret += ",";
393  std::string tmp;
394  if (priv) {
395  if (!pubkey->ToPrivateString(*arg, tmp)) return false;
396  } else {
397  tmp = pubkey->ToString();
398  }
399  ret += std::move(tmp);
400  }
401  if (m_subdescriptor_arg) {
402  if (pos++) ret += ",";
403  std::string tmp;
404  if (!m_subdescriptor_arg->ToStringHelper(arg, tmp, priv)) return false;
405  ret += std::move(tmp);
406  }
407  out = std::move(ret) + ")";
408  return true;
409  }
410 
411  std::string ToString() const final
412  {
413  std::string ret;
414  ToStringHelper(nullptr, ret, false);
415  return AddChecksum(ret);
416  }
417 
418  bool ToPrivateString(const SigningProvider& arg, std::string& out) const override final
419  {
420  bool ret = ToStringHelper(&arg, out, true);
421  out = AddChecksum(out);
422  return ret;
423  }
424 
425  bool ExpandHelper(int pos, const SigningProvider& arg, Span<const unsigned char>* cache_read, std::vector<CScript>& output_scripts, FlatSigningProvider& out, std::vector<unsigned char>* cache_write) const
426  {
427  std::vector<std::pair<CPubKey, KeyOriginInfo>> entries;
428  entries.reserve(m_pubkey_args.size());
429 
430  // Construct temporary data in `entries` and `subscripts`, to avoid producing output in case of failure.
431  for (const auto& p : m_pubkey_args) {
432  entries.emplace_back();
433  // If we have a cache, we don't need GetPubKey to compute the public key.
434  // Pass in nullptr to signify only origin info is desired.
435  if (!p->GetPubKey(pos, arg, cache_read ? nullptr : &entries.back().first, entries.back().second)) return false;
436  if (cache_read) {
437  // Cached expanded public key exists, use it.
438  if (cache_read->size() == 0) return false;
439  bool compressed = ((*cache_read)[0] == 0x02 || (*cache_read)[0] == 0x03) && cache_read->size() >= 33;
440  bool uncompressed = ((*cache_read)[0] == 0x04) && cache_read->size() >= 65;
441  if (!(compressed || uncompressed)) return false;
442  CPubKey pubkey(cache_read->begin(), cache_read->begin() + (compressed ? 33 : 65));
443  entries.back().first = pubkey;
444  *cache_read = cache_read->subspan(compressed ? 33 : 65);
445  }
446  if (cache_write) {
447  cache_write->insert(cache_write->end(), entries.back().first.begin(), entries.back().first.end());
448  }
449  }
450  std::vector<CScript> subscripts;
451  if (m_subdescriptor_arg) {
452  FlatSigningProvider subprovider;
453  if (!m_subdescriptor_arg->ExpandHelper(pos, arg, cache_read, subscripts, subprovider, cache_write)) return false;
454  out = Merge(out, subprovider);
455  }
456 
457  std::vector<CPubKey> pubkeys;
458  pubkeys.reserve(entries.size());
459  for (auto& entry : entries) {
460  pubkeys.push_back(entry.first);
461  out.origins.emplace(entry.first.GetID(), std::make_pair<CPubKey, KeyOriginInfo>(CPubKey(entry.first), std::move(entry.second)));
462  }
463  if (m_subdescriptor_arg) {
464  for (const auto& subscript : subscripts) {
465  out.scripts.emplace(CScriptID(subscript), subscript);
466  std::vector<CScript> addscripts = MakeScripts(pubkeys, &subscript, out);
467  for (auto& addscript : addscripts) {
468  output_scripts.push_back(std::move(addscript));
469  }
470  }
471  } else {
472  output_scripts = MakeScripts(pubkeys, nullptr, out);
473  }
474  return true;
475  }
476 
477  bool Expand(int pos, const SigningProvider& provider, std::vector<CScript>& output_scripts, FlatSigningProvider& out, std::vector<unsigned char>* cache = nullptr) const final
478  {
479  return ExpandHelper(pos, provider, nullptr, output_scripts, out, cache);
480  }
481 
482  bool ExpandFromCache(int pos, const std::vector<unsigned char>& cache, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const final
483  {
484  Span<const unsigned char> span = MakeSpan(cache);
485  return ExpandHelper(pos, DUMMY_SIGNING_PROVIDER, &span, output_scripts, out, nullptr) && span.size() == 0;
486  }
487 
488  void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const final
489  {
490  for (const auto& p : m_pubkey_args) {
491  CKey key;
492  if (!p->GetPrivKey(pos, provider, key)) continue;
493  out.keys.emplace(key.GetPubKey().GetID(), key);
494  }
495  if (m_subdescriptor_arg) {
496  FlatSigningProvider subprovider;
497  m_subdescriptor_arg->ExpandPrivate(pos, provider, subprovider);
498  out = Merge(out, subprovider);
499  }
500  }
501 };
502 
504 template<typename T>
505 std::vector<T> Singleton(T elem)
506 {
507  std::vector<T> ret;
508  ret.emplace_back(std::move(elem));
509  return ret;
510 }
511 
513 class AddressDescriptor final : public DescriptorImpl
514 {
515  const CTxDestination m_destination;
516 protected:
517  std::string ToStringExtra() const override { return EncodeDestination(m_destination); }
518  std::vector<CScript> MakeScripts(const std::vector<CPubKey>&, const CScript*, FlatSigningProvider&) const override { return Singleton(GetScriptForDestination(m_destination)); }
519 public:
520  AddressDescriptor(CTxDestination destination) : DescriptorImpl({}, {}, "addr"), m_destination(std::move(destination)) {}
521  bool IsSolvable() const final { return false; }
522 };
523 
525 class RawDescriptor final : public DescriptorImpl
526 {
527  const CScript m_script;
528 protected:
529  std::string ToStringExtra() const override { return HexStr(m_script.begin(), m_script.end()); }
530  std::vector<CScript> MakeScripts(const std::vector<CPubKey>&, const CScript*, FlatSigningProvider&) const override { return Singleton(m_script); }
531 public:
532  RawDescriptor(CScript script) : DescriptorImpl({}, {}, "raw"), m_script(std::move(script)) {}
533  bool IsSolvable() const final { return false; }
534 };
535 
537 class PKDescriptor final : public DescriptorImpl
538 {
539 protected:
540  std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, const CScript*, FlatSigningProvider&) const override { return Singleton(GetScriptForRawPubKey(keys[0])); }
541 public:
542  PKDescriptor(std::unique_ptr<PubkeyProvider> prov) : DescriptorImpl(Singleton(std::move(prov)), {}, "pk") {}
543 };
544 
546 class PKHDescriptor final : public DescriptorImpl
547 {
548 protected:
549  std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, const CScript*, FlatSigningProvider& out) const override
550  {
551  CKeyID id = keys[0].GetID();
552  out.pubkeys.emplace(id, keys[0]);
553  return Singleton(GetScriptForDestination(PKHash(id)));
554  }
555 public:
556  PKHDescriptor(std::unique_ptr<PubkeyProvider> prov) : DescriptorImpl(Singleton(std::move(prov)), {}, "pkh") {}
557 };
558 
560 class WPKHDescriptor final : public DescriptorImpl
561 {
562 protected:
563  std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, const CScript*, FlatSigningProvider& out) const override
564  {
565  CKeyID id = keys[0].GetID();
566  out.pubkeys.emplace(id, keys[0]);
567  return Singleton(GetScriptForDestination(WitnessV0KeyHash(id)));
568  }
569 public:
570  WPKHDescriptor(std::unique_ptr<PubkeyProvider> prov) : DescriptorImpl(Singleton(std::move(prov)), {}, "wpkh") {}
571 };
572 
574 class ComboDescriptor final : public DescriptorImpl
575 {
576 protected:
577  std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, const CScript*, FlatSigningProvider& out) const override
578  {
579  std::vector<CScript> ret;
580  CKeyID id = keys[0].GetID();
581  out.pubkeys.emplace(id, keys[0]);
582  ret.emplace_back(GetScriptForRawPubKey(keys[0])); // P2PK
583  ret.emplace_back(GetScriptForDestination(PKHash(id))); // P2PKH
584  if (keys[0].IsCompressed()) {
586  out.scripts.emplace(CScriptID(p2wpkh), p2wpkh);
587  ret.emplace_back(p2wpkh);
588  ret.emplace_back(GetScriptForDestination(ScriptHash(p2wpkh))); // P2SH-P2WPKH
589  }
590  return ret;
591  }
592 public:
593  ComboDescriptor(std::unique_ptr<PubkeyProvider> prov) : DescriptorImpl(Singleton(std::move(prov)), {}, "combo") {}
594 };
595 
597 class MultisigDescriptor final : public DescriptorImpl
598 {
599  const int m_threshold;
600 protected:
601  std::string ToStringExtra() const override { return strprintf("%i", m_threshold); }
602  std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, const CScript*, FlatSigningProvider&) const override { return Singleton(GetScriptForMultisig(m_threshold, keys)); }
603 public:
604  MultisigDescriptor(int threshold, std::vector<std::unique_ptr<PubkeyProvider>> providers) : DescriptorImpl(std::move(providers), {}, "multi"), m_threshold(threshold) {}
605 };
606 
608 class SHDescriptor final : public DescriptorImpl
609 {
610 protected:
611  std::vector<CScript> MakeScripts(const std::vector<CPubKey>&, const CScript* script, FlatSigningProvider&) const override { return Singleton(GetScriptForDestination(ScriptHash(*script))); }
612 public:
613  SHDescriptor(std::unique_ptr<DescriptorImpl> desc) : DescriptorImpl({}, std::move(desc), "sh") {}
614 };
615 
617 class WSHDescriptor final : public DescriptorImpl
618 {
619 protected:
620  std::vector<CScript> MakeScripts(const std::vector<CPubKey>&, const CScript* script, FlatSigningProvider&) const override { return Singleton(GetScriptForDestination(WitnessV0ScriptHash(*script))); }
621 public:
622  WSHDescriptor(std::unique_ptr<DescriptorImpl> desc) : DescriptorImpl({}, std::move(desc), "wsh") {}
623 };
624 
626 // Parser //
628 
629 enum class ParseScriptContext {
630  TOP,
631  P2SH,
632  P2WSH,
633 };
634 
636 bool Const(const std::string& str, Span<const char>& sp)
637 {
638  if ((size_t)sp.size() >= str.size() && std::equal(str.begin(), str.end(), sp.begin())) {
639  sp = sp.subspan(str.size());
640  return true;
641  }
642  return false;
643 }
644 
646 bool Func(const std::string& str, Span<const char>& sp)
647 {
648  if ((size_t)sp.size() >= str.size() + 2 && sp[str.size()] == '(' && sp[sp.size() - 1] == ')' && std::equal(str.begin(), str.end(), sp.begin())) {
649  sp = sp.subspan(str.size() + 1, sp.size() - str.size() - 2);
650  return true;
651  }
652  return false;
653 }
654 
657 {
658  int level = 0;
659  auto it = sp.begin();
660  while (it != sp.end()) {
661  if (*it == '(') {
662  ++level;
663  } else if (level && *it == ')') {
664  --level;
665  } else if (level == 0 && (*it == ')' || *it == ',')) {
666  break;
667  }
668  ++it;
669  }
670  Span<const char> ret = sp.first(it - sp.begin());
671  sp = sp.subspan(it - sp.begin());
672  return ret;
673 }
674 
676 std::vector<Span<const char>> Split(const Span<const char>& sp, char sep)
677 {
678  std::vector<Span<const char>> ret;
679  auto it = sp.begin();
680  auto start = it;
681  while (it != sp.end()) {
682  if (*it == sep) {
683  ret.emplace_back(start, it);
684  start = it + 1;
685  }
686  ++it;
687  }
688  ret.emplace_back(start, it);
689  return ret;
690 }
691 
693 NODISCARD bool ParseKeyPath(const std::vector<Span<const char>>& split, KeyPath& out, std::string& error)
694 {
695  for (size_t i = 1; i < split.size(); ++i) {
696  Span<const char> elem = split[i];
697  bool hardened = false;
698  if (elem.size() > 0 && (elem[elem.size() - 1] == '\'' || elem[elem.size() - 1] == 'h')) {
699  elem = elem.first(elem.size() - 1);
700  hardened = true;
701  }
702  uint32_t p;
703  if (!ParseUInt32(std::string(elem.begin(), elem.end()), &p)) {
704  error = strprintf("Key path value '%s' is not a valid uint32", std::string(elem.begin(), elem.end()).c_str());
705  return false;
706  } else if (p > 0x7FFFFFFFUL) {
707  error = strprintf("Key path value %u is out of range", p);
708  return false;
709  }
710  out.push_back(p | (((uint32_t)hardened) << 31));
711  }
712  return true;
713 }
714 
716 std::unique_ptr<PubkeyProvider> ParsePubkeyInner(const Span<const char>& sp, bool permit_uncompressed, FlatSigningProvider& out, std::string& error)
717 {
718  auto split = Split(sp, '/');
719  std::string str(split[0].begin(), split[0].end());
720  if (str.size() == 0) {
721  error = "No key provided";
722  return nullptr;
723  }
724  if (split.size() == 1) {
725  if (IsHex(str)) {
726  std::vector<unsigned char> data = ParseHex(str);
727  CPubKey pubkey(data);
728  if (pubkey.IsFullyValid()) {
729  if (permit_uncompressed || pubkey.IsCompressed()) {
730  return MakeUnique<ConstPubkeyProvider>(pubkey);
731  } else {
732  error = "Uncompressed keys are not allowed";
733  return nullptr;
734  }
735  }
736  error = strprintf("Pubkey '%s' is invalid", str);
737  return nullptr;
738  }
739  CKey key = DecodeSecret(str);
740  if (key.IsValid()) {
741  if (permit_uncompressed || key.IsCompressed()) {
742  CPubKey pubkey = key.GetPubKey();
743  out.keys.emplace(pubkey.GetID(), key);
744  return MakeUnique<ConstPubkeyProvider>(pubkey);
745  } else {
746  error = "Uncompressed keys are not allowed";
747  return nullptr;
748  }
749  }
750  }
751  CExtKey extkey = DecodeExtKey(str);
752  CExtPubKey extpubkey = DecodeExtPubKey(str);
753  if (!extkey.key.IsValid() && !extpubkey.pubkey.IsValid()) {
754  error = strprintf("key '%s' is not valid", str);
755  return nullptr;
756  }
757  KeyPath path;
758  DeriveType type = DeriveType::NO;
759  if (split.back() == MakeSpan("*").first(1)) {
760  split.pop_back();
761  type = DeriveType::UNHARDENED;
762  } else if (split.back() == MakeSpan("*'").first(2) || split.back() == MakeSpan("*h").first(2)) {
763  split.pop_back();
764  type = DeriveType::HARDENED;
765  }
766  if (!ParseKeyPath(split, path, error)) return nullptr;
767  if (extkey.key.IsValid()) {
768  extpubkey = extkey.Neuter();
769  out.keys.emplace(extpubkey.pubkey.GetID(), extkey.key);
770  }
771  return MakeUnique<BIP32PubkeyProvider>(extpubkey, std::move(path), type);
772 }
773 
775 std::unique_ptr<PubkeyProvider> ParsePubkey(const Span<const char>& sp, bool permit_uncompressed, FlatSigningProvider& out, std::string& error)
776 {
777  auto origin_split = Split(sp, ']');
778  if (origin_split.size() > 2) {
779  error = "Multiple ']' characters found for a single pubkey";
780  return nullptr;
781  }
782  if (origin_split.size() == 1) return ParsePubkeyInner(origin_split[0], permit_uncompressed, out, error);
783  if (origin_split[0].size() < 1 || origin_split[0][0] != '[') {
784  error = strprintf("Key origin start '[ character expected but not found, got '%c' instead", origin_split[0][0]);
785  return nullptr;
786  }
787  auto slash_split = Split(origin_split[0].subspan(1), '/');
788  if (slash_split[0].size() != 8) {
789  error = strprintf("Fingerprint is not 4 bytes (%u characters instead of 8 characters)", slash_split[0].size());
790  return nullptr;
791  }
792  std::string fpr_hex = std::string(slash_split[0].begin(), slash_split[0].end());
793  if (!IsHex(fpr_hex)) {
794  error = strprintf("Fingerprint '%s' is not hex", fpr_hex);
795  return nullptr;
796  }
797  auto fpr_bytes = ParseHex(fpr_hex);
798  KeyOriginInfo info;
799  static_assert(sizeof(info.fingerprint) == 4, "Fingerprint must be 4 bytes");
800  assert(fpr_bytes.size() == 4);
801  std::copy(fpr_bytes.begin(), fpr_bytes.end(), info.fingerprint);
802  if (!ParseKeyPath(slash_split, info.path, error)) return nullptr;
803  auto provider = ParsePubkeyInner(origin_split[1], permit_uncompressed, out, error);
804  if (!provider) return nullptr;
805  return MakeUnique<OriginPubkeyProvider>(std::move(info), std::move(provider));
806 }
807 
809 std::unique_ptr<DescriptorImpl> ParseScript(Span<const char>& sp, ParseScriptContext ctx, FlatSigningProvider& out, std::string& error)
810 {
811  auto expr = Expr(sp);
812  if (Func("pk", expr)) {
813  auto pubkey = ParsePubkey(expr, ctx != ParseScriptContext::P2WSH, out, error);
814  if (!pubkey) return nullptr;
815  return MakeUnique<PKDescriptor>(std::move(pubkey));
816  }
817  if (Func("pkh", expr)) {
818  auto pubkey = ParsePubkey(expr, ctx != ParseScriptContext::P2WSH, out, error);
819  if (!pubkey) return nullptr;
820  return MakeUnique<PKHDescriptor>(std::move(pubkey));
821  }
822  if (ctx == ParseScriptContext::TOP && Func("combo", expr)) {
823  auto pubkey = ParsePubkey(expr, true, out, error);
824  if (!pubkey) return nullptr;
825  return MakeUnique<ComboDescriptor>(std::move(pubkey));
826  } else if (ctx != ParseScriptContext::TOP && Func("combo", expr)) {
827  error = "Cannot have combo in non-top level";
828  return nullptr;
829  }
830  if (Func("multi", expr)) {
831  auto threshold = Expr(expr);
832  uint32_t thres;
833  std::vector<std::unique_ptr<PubkeyProvider>> providers;
834  if (!ParseUInt32(std::string(threshold.begin(), threshold.end()), &thres)) {
835  error = strprintf("Multi threshold '%s' is not valid", std::string(threshold.begin(), threshold.end()).c_str());
836  return nullptr;
837  }
838  size_t script_size = 0;
839  while (expr.size()) {
840  if (!Const(",", expr)) {
841  error = strprintf("Multi: expected ',', got '%c'", expr[0]);
842  return nullptr;
843  }
844  auto arg = Expr(expr);
845  auto pk = ParsePubkey(arg, ctx != ParseScriptContext::P2WSH, out, error);
846  if (!pk) return nullptr;
847  script_size += pk->GetSize() + 1;
848  providers.emplace_back(std::move(pk));
849  }
850  if (providers.size() < 1 || providers.size() > 16) {
851  error = strprintf("Cannot have %u keys in multisig; must have between 1 and 16 keys, inclusive", providers.size());
852  return nullptr;
853  } else if (thres < 1) {
854  error = strprintf("Multisig threshold cannot be %d, must be at least 1", thres);
855  return nullptr;
856  } else if (thres > providers.size()) {
857  error = strprintf("Multisig threshold cannot be larger than the number of keys; threshold is %d but only %u keys specified", thres, providers.size());
858  return nullptr;
859  }
860  if (ctx == ParseScriptContext::TOP) {
861  if (providers.size() > 3) {
862  error = strprintf("Cannot have %u pubkeys in bare multisig; only at most 3 pubkeys", providers.size());
863  return nullptr;
864  }
865  }
866  if (ctx == ParseScriptContext::P2SH) {
867  if (script_size + 3 > 520) {
868  error = strprintf("P2SH script is too large, %d bytes is larger than 520 bytes", script_size + 3);
869  return nullptr;
870  }
871  }
872  return MakeUnique<MultisigDescriptor>(thres, std::move(providers));
873  }
874  if (ctx != ParseScriptContext::P2WSH && Func("wpkh", expr)) {
875  auto pubkey = ParsePubkey(expr, false, out, error);
876  if (!pubkey) return nullptr;
877  return MakeUnique<WPKHDescriptor>(std::move(pubkey));
878  } else if (ctx == ParseScriptContext::P2WSH && Func("wpkh", expr)) {
879  error = "Cannot have wpkh within wsh";
880  return nullptr;
881  }
882  if (ctx == ParseScriptContext::TOP && Func("sh", expr)) {
883  auto desc = ParseScript(expr, ParseScriptContext::P2SH, out, error);
884  if (!desc || expr.size()) return nullptr;
885  return MakeUnique<SHDescriptor>(std::move(desc));
886  } else if (ctx != ParseScriptContext::TOP && Func("sh", expr)) {
887  error = "Cannot have sh in non-top level";
888  return nullptr;
889  }
890  if (ctx != ParseScriptContext::P2WSH && Func("wsh", expr)) {
891  auto desc = ParseScript(expr, ParseScriptContext::P2WSH, out, error);
892  if (!desc || expr.size()) return nullptr;
893  return MakeUnique<WSHDescriptor>(std::move(desc));
894  } else if (ctx == ParseScriptContext::P2WSH && Func("wsh", expr)) {
895  error = "Cannot have wsh within wsh";
896  return nullptr;
897  }
898  if (ctx == ParseScriptContext::TOP && Func("addr", expr)) {
899  CTxDestination dest = DecodeDestination(std::string(expr.begin(), expr.end()));
900  if (!IsValidDestination(dest)) {
901  error = "Address is not valid";
902  return nullptr;
903  }
904  return MakeUnique<AddressDescriptor>(std::move(dest));
905  }
906  if (ctx == ParseScriptContext::TOP && Func("raw", expr)) {
907  std::string str(expr.begin(), expr.end());
908  if (!IsHex(str)) {
909  error = "Raw script is not hex";
910  return nullptr;
911  }
912  auto bytes = ParseHex(str);
913  return MakeUnique<RawDescriptor>(CScript(bytes.begin(), bytes.end()));
914  }
915  if (ctx == ParseScriptContext::P2SH) {
916  error = "A function is needed within P2SH";
917  return nullptr;
918  } else if (ctx == ParseScriptContext::P2WSH) {
919  error = "A function is needed within P2WSH";
920  return nullptr;
921  }
922  error = strprintf("%s is not a valid descriptor function", std::string(expr.begin(), expr.end()));
923  return nullptr;
924 }
925 
926 std::unique_ptr<PubkeyProvider> InferPubkey(const CPubKey& pubkey, ParseScriptContext, const SigningProvider& provider)
927 {
928  std::unique_ptr<PubkeyProvider> key_provider = MakeUnique<ConstPubkeyProvider>(pubkey);
929  KeyOriginInfo info;
930  if (provider.GetKeyOrigin(pubkey.GetID(), info)) {
931  return MakeUnique<OriginPubkeyProvider>(std::move(info), std::move(key_provider));
932  }
933  return key_provider;
934 }
935 
936 std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptContext ctx, const SigningProvider& provider)
937 {
938  std::vector<std::vector<unsigned char>> data;
939  txnouttype txntype = Solver(script, data);
940 
941  if (txntype == TX_PUBKEY) {
942  CPubKey pubkey(data[0].begin(), data[0].end());
943  if (pubkey.IsValid()) {
944  return MakeUnique<PKDescriptor>(InferPubkey(pubkey, ctx, provider));
945  }
946  }
947  if (txntype == TX_PUBKEYHASH) {
948  uint160 hash(data[0]);
949  CKeyID keyid(hash);
950  CPubKey pubkey;
951  if (provider.GetPubKey(keyid, pubkey)) {
952  return MakeUnique<PKHDescriptor>(InferPubkey(pubkey, ctx, provider));
953  }
954  }
955  if (txntype == TX_WITNESS_V0_KEYHASH && ctx != ParseScriptContext::P2WSH) {
956  uint160 hash(data[0]);
957  CKeyID keyid(hash);
958  CPubKey pubkey;
959  if (provider.GetPubKey(keyid, pubkey)) {
960  return MakeUnique<WPKHDescriptor>(InferPubkey(pubkey, ctx, provider));
961  }
962  }
963  if (txntype == TX_MULTISIG) {
964  std::vector<std::unique_ptr<PubkeyProvider>> providers;
965  for (size_t i = 1; i + 1 < data.size(); ++i) {
966  CPubKey pubkey(data[i].begin(), data[i].end());
967  providers.push_back(InferPubkey(pubkey, ctx, provider));
968  }
969  return MakeUnique<MultisigDescriptor>((int)data[0][0], std::move(providers));
970  }
971  if (txntype == TX_SCRIPTHASH && ctx == ParseScriptContext::TOP) {
972  uint160 hash(data[0]);
973  CScriptID scriptid(hash);
974  CScript subscript;
975  if (provider.GetCScript(scriptid, subscript)) {
976  auto sub = InferScript(subscript, ParseScriptContext::P2SH, provider);
977  if (sub) return MakeUnique<SHDescriptor>(std::move(sub));
978  }
979  }
980  if (txntype == TX_WITNESS_V0_SCRIPTHASH && ctx != ParseScriptContext::P2WSH) {
981  CScriptID scriptid;
982  CRIPEMD160().Write(data[0].data(), data[0].size()).Finalize(scriptid.begin());
983  CScript subscript;
984  if (provider.GetCScript(scriptid, subscript)) {
985  auto sub = InferScript(subscript, ParseScriptContext::P2WSH, provider);
986  if (sub) return MakeUnique<WSHDescriptor>(std::move(sub));
987  }
988  }
989 
990  CTxDestination dest;
991  if (ExtractDestination(script, dest)) {
992  if (GetScriptForDestination(dest) == script) {
993  return MakeUnique<AddressDescriptor>(std::move(dest));
994  }
995  }
996 
997  return MakeUnique<RawDescriptor>(script);
998 }
999 
1000 
1001 } // namespace
1002 
1004 bool CheckChecksum(Span<const char>& sp, bool require_checksum, std::string& error, std::string* out_checksum = nullptr)
1005 {
1006  auto check_split = Split(sp, '#');
1007  if (check_split.size() > 2) {
1008  error = "Multiple '#' symbols";
1009  return false;
1010  }
1011  if (check_split.size() == 1 && require_checksum){
1012  error = "Missing checksum";
1013  return false;
1014  }
1015  if (check_split.size() == 2) {
1016  if (check_split[1].size() != 8) {
1017  error = strprintf("Expected 8 character checksum, not %u characters", check_split[1].size());
1018  return false;
1019  }
1020  }
1021  auto checksum = DescriptorChecksum(check_split[0]);
1022  if (checksum.empty()) {
1023  error = "Invalid characters in payload";
1024  return false;
1025  }
1026  if (check_split.size() == 2) {
1027  if (!std::equal(checksum.begin(), checksum.end(), check_split[1].begin())) {
1028  error = strprintf("Provided checksum '%s' does not match computed checksum '%s'", std::string(check_split[1].begin(), check_split[1].end()), checksum);
1029  return false;
1030  }
1031  }
1032  if (out_checksum) *out_checksum = std::move(checksum);
1033  sp = check_split[0];
1034  return true;
1035 }
1036 
1037 std::unique_ptr<Descriptor> Parse(const std::string& descriptor, FlatSigningProvider& out, std::string& error, bool require_checksum)
1038 {
1039  Span<const char> sp(descriptor.data(), descriptor.size());
1040  if (!CheckChecksum(sp, require_checksum, error)) return nullptr;
1041  auto ret = ParseScript(sp, ParseScriptContext::TOP, out, error);
1042  if (sp.size() == 0 && ret) return std::unique_ptr<Descriptor>(std::move(ret));
1043  return nullptr;
1044 }
1045 
1046 std::string GetDescriptorChecksum(const std::string& descriptor)
1047 {
1048  std::string ret;
1049  std::string error;
1050  Span<const char> sp(descriptor.data(), descriptor.size());
1051  if (!CheckChecksum(sp, false, error, &ret)) return "";
1052  return ret;
1053 }
1054 
1055 std::unique_ptr<Descriptor> InferDescriptor(const CScript& script, const SigningProvider& provider)
1056 {
1057  return InferScript(script, ParseScriptContext::TOP, provider);
1058 }
Top-level scriptPubKey.
constexpr std::ptrdiff_t size() const noexcept
Definition: span.h:30
bool CheckChecksum(Span< const char > &sp, bool require_checksum, std::string &error, std::string *out_checksum=nullptr)
Check a descriptor checksum, and update desc to be the checksum-less part.
unsigned char fingerprint[4]
First 32 bits of the Hash160 of the public key at the root of the path.
Definition: keyorigin.h:14
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:156
constexpr C * end() const noexcept
Definition: span.h:29
CKey key
Definition: key.h:149
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:293
DeriveType
Definition: descriptor.cpp:238
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1067
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:184
std::map< CKeyID, CKey > keys
bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
CExtKey DecodeExtKey(const std::string &str)
Definition: key_io.cpp:187
FlatSigningProvider Merge(const FlatSigningProvider &a, const FlatSigningProvider &b)
std::string GetDescriptorChecksum(const std::string &descriptor)
Get the checksum for a descriptor.
Definition: key.h:144
std::vector< unsigned char > ParseHex(const char *psz)
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:325
unsigned char vchFingerprint[4]
Definition: key.h:146
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:297
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > origins
std::string FormatHDKeypath(const std::vector< uint32_t > &path)
Definition: bip32.cpp:53
txnouttype Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
Definition: standard.cpp:91
constexpr Span< A > MakeSpan(A(&a)[N])
Create a span to a container exposing data() and size().
Definition: span.h:55
unsigned char * begin()
Definition: uint256.h:55
CExtPubKey DecodeExtPubKey(const std::string &str)
Definition: key_io.cpp:164
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:155
std::unique_ptr< Descriptor > Parse(const std::string &descriptor, FlatSigningProvider &out, std::string &error, bool require_checksum)
Parse a descriptor string.
constexpr Span< C > subspan(std::ptrdiff_t offset) const noexcept
Definition: span.h:33
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid()) ...
Definition: pubkey.cpp:206
std::map< CScriptID, CScript > scripts
#define NODISCARD
Definition: attributes.h:18
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
const char * name
Definition: rest.cpp:39
unsigned char nDepth
Definition: key.h:145
const SigningProvider & DUMMY_SIGNING_PROVIDER
static secp256k1_context * ctx
Definition: tests.c:46
bool IsValid() const
Definition: pubkey.h:171
An encapsulated public key.
Definition: pubkey.h:30
bool IsHex(const std::string &str)
std::map< CKeyID, CPubKey > pubkeys
unsigned int nChild
Definition: key.h:147
ParseScriptContext
Definition: descriptor.cpp:629
constexpr Span< C > first(std::ptrdiff_t count) const noexcept
Definition: span.h:35
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:96
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:289
ChainCode chaincode
Definition: key.h:148
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
virtual bool GetKey(const CKeyID &address, CKey &key) const
bool IsSolvable(const SigningProvider &provider, const CScript &script)
Definition: sign.cpp:432
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:247
P2SH redeemScript.
constexpr C * begin() const noexcept
Definition: span.h:28
txnouttype
Definition: standard.h:56
CTxDestination DecodeDestination(const std::string &str)
Definition: key_io.cpp:216
bool Derive(CExtPubKey &out, unsigned int nChild) const
Definition: pubkey.cpp:266
std::string EncodeExtPubKey(const CExtPubKey &key)
Definition: key_io.cpp:177
std::unique_ptr< Descriptor > InferDescriptor(const CScript &script, const SigningProvider &provider)
Find a descriptor for the specified script, using information from provider where possible...
An interface to be implemented by keystores that support signing.
CExtPubKey Neuter() const
Definition: key.cpp:312
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
Definition: sign.cpp:52
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
std::string HexStr(const T itbegin, const T itend)
Definition: strencodings.h:125
160-bit opaque blob.
Definition: uint256.h:110
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:22
std::string EncodeDestination(const CTxDestination &dest)
Definition: key_io.cpp:211
CPubKey pubkey
Definition: pubkey.h:211
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:302
An encapsulated private key.
Definition: key.h:27
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:17
CKey DecodeSecret(const std::string &str)
Definition: key_io.cpp:133
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:273
boost::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:139
CScript ParseScript(const std::string &s)
Definition: core_read.cpp:23
auto it
Definition: validation.cpp:366
std::string EncodeSecret(const CKey &key)
Definition: key_io.cpp:151
std::vector< uint32_t > path
Definition: keyorigin.h:15
bool error(const char *fmt, const Args &... args)
Definition: system.h:47
std::string EncodeExtKey(const CExtKey &key)
Definition: key_io.cpp:200
Interface for parsed descriptor objects.
Definition: descriptor.h:30
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:93
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:180