Bitcoin Core  21.99.0
P2P Digital Currency
descriptor.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018-2020 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/spanparsing.h>
15 #include <util/system.h>
16 #include <util/strencodings.h>
17 #include <util/vector.h>
18 
19 #include <memory>
20 #include <optional>
21 #include <string>
22 #include <vector>
23 
24 namespace {
25 
27 // Checksum //
29 
30 // This section implements a checksum algorithm for descriptors with the
31 // following properties:
32 // * Mistakes in a descriptor string are measured in "symbol errors". The higher
33 // the number of symbol errors, the harder it is to detect:
34 // * An error substituting a character from 0123456789()[],'/*abcdefgh@:$%{} for
35 // another in that set always counts as 1 symbol error.
36 // * Note that hex encoded keys are covered by these characters. Xprvs and
37 // xpubs use other characters too, but already have their own checksum
38 // mechanism.
39 // * Function names like "multi()" use other characters, but mistakes in
40 // these would generally result in an unparsable descriptor.
41 // * A case error always counts as 1 symbol error.
42 // * Any other 1 character substitution error counts as 1 or 2 symbol errors.
43 // * Any 1 symbol error is always detected.
44 // * Any 2 or 3 symbol error in a descriptor of up to 49154 characters is always detected.
45 // * Any 4 symbol error in a descriptor of up to 507 characters is always detected.
46 // * Any 5 symbol error in a descriptor of up to 77 characters is always detected.
47 // * Is optimized to minimize the chance a 5 symbol error in a descriptor up to 387 characters is undetected
48 // * Random errors have a chance of 1 in 2**40 of being undetected.
49 //
50 // These properties are achieved by expanding every group of 3 (non checksum) characters into
51 // 4 GF(32) symbols, over which a cyclic code is defined.
52 
53 /*
54  * Interprets c as 8 groups of 5 bits which are the coefficients of a degree 8 polynomial over GF(32),
55  * multiplies that polynomial by x, computes its remainder modulo a generator, and adds the constant term val.
56  *
57  * 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}.
58  * It is chosen to define an cyclic error detecting code which is selected by:
59  * - Starting from all BCH codes over GF(32) of degree 8 and below, which by construction guarantee detecting
60  * 3 errors in windows up to 19000 symbols.
61  * - Taking all those generators, and for degree 7 ones, extend them to degree 8 by adding all degree-1 factors.
62  * - Selecting just the set of generators that guarantee detecting 4 errors in a window of length 512.
63  * - Selecting one of those with best worst-case behavior for 5 errors in windows of length up to 512.
64  *
65  * The generator and the constants to implement it can be verified using this Sage code:
66  * B = GF(2) # Binary field
67  * BP.<b> = B[] # Polynomials over the binary field
68  * F_mod = b**5 + b**3 + 1
69  * F.<f> = GF(32, modulus=F_mod, repr='int') # GF(32) definition
70  * FP.<x> = F[] # Polynomials over GF(32)
71  * E_mod = x**3 + x + F.fetch_int(8)
72  * E.<e> = F.extension(E_mod) # Extension field definition
73  * alpha = e**2743 # Choice of an element in extension field
74  * for p in divisors(E.order() - 1): # Verify alpha has order 32767.
75  * assert((alpha**p == 1) == (p % 32767 == 0))
76  * G = lcm([(alpha**i).minpoly() for i in [1056,1057,1058]] + [x + 1])
77  * print(G) # Print out the generator
78  * for i in [1,2,4,8,16]: # Print out {1,2,4,8,16}*(G mod x^8), packed in hex integers.
79  * v = 0
80  * for coef in reversed((F.fetch_int(i)*(G % x**8)).coefficients(sparse=True)):
81  * v = v*32 + coef.integer_representation()
82  * print("0x%x" % v)
83  */
84 uint64_t PolyMod(uint64_t c, int val)
85 {
86  uint8_t c0 = c >> 35;
87  c = ((c & 0x7ffffffff) << 5) ^ val;
88  if (c0 & 1) c ^= 0xf5dee51989;
89  if (c0 & 2) c ^= 0xa9fdca3312;
90  if (c0 & 4) c ^= 0x1bab10e32d;
91  if (c0 & 8) c ^= 0x3706b1677a;
92  if (c0 & 16) c ^= 0x644d626ffd;
93  return c;
94 }
95 
96 std::string DescriptorChecksum(const Span<const char>& span)
97 {
111  static std::string INPUT_CHARSET =
112  "0123456789()[],'/*abcdefgh@:$%{}"
113  "IJKLMNOPQRSTUVWXYZ&+-.;<=>?!^_|~"
114  "ijklmnopqrstuvwxyzABCDEFGH`#\"\\ ";
115 
117  static std::string CHECKSUM_CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
118 
119  uint64_t c = 1;
120  int cls = 0;
121  int clscount = 0;
122  for (auto ch : span) {
123  auto pos = INPUT_CHARSET.find(ch);
124  if (pos == std::string::npos) return "";
125  c = PolyMod(c, pos & 31); // Emit a symbol for the position inside the group, for every character.
126  cls = cls * 3 + (pos >> 5); // Accumulate the group numbers
127  if (++clscount == 3) {
128  // Emit an extra symbol representing the group numbers, for every 3 characters.
129  c = PolyMod(c, cls);
130  cls = 0;
131  clscount = 0;
132  }
133  }
134  if (clscount > 0) c = PolyMod(c, cls);
135  for (int j = 0; j < 8; ++j) c = PolyMod(c, 0); // Shift further to determine the checksum.
136  c ^= 1; // Prevent appending zeroes from not affecting the checksum.
137 
138  std::string ret(8, ' ');
139  for (int j = 0; j < 8; ++j) ret[j] = CHECKSUM_CHARSET[(c >> (5 * (7 - j))) & 31];
140  return ret;
141 }
142 
143 std::string AddChecksum(const std::string& str) { return str + "#" + DescriptorChecksum(str); }
144 
146 // Internal representation //
148 
149 typedef std::vector<uint32_t> KeyPath;
150 
152 struct PubkeyProvider
153 {
154 protected:
157  uint32_t m_expr_index;
158 
159 public:
160  explicit PubkeyProvider(uint32_t exp_index) : m_expr_index(exp_index) {}
161 
162  virtual ~PubkeyProvider() = default;
163 
169  virtual bool GetPubKey(int pos, const SigningProvider& arg, CPubKey& key, KeyOriginInfo& info, const DescriptorCache* read_cache = nullptr, DescriptorCache* write_cache = nullptr) = 0;
170 
172  virtual bool IsRange() const = 0;
173 
175  virtual size_t GetSize() const = 0;
176 
178  virtual std::string ToString() const = 0;
179 
181  virtual bool ToPrivateString(const SigningProvider& arg, std::string& out) const = 0;
182 
184  virtual bool ToNormalizedString(const SigningProvider& arg, std::string& out, bool priv) const = 0;
185 
187  virtual bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const = 0;
188 };
189 
190 class OriginPubkeyProvider final : public PubkeyProvider
191 {
192  KeyOriginInfo m_origin;
193  std::unique_ptr<PubkeyProvider> m_provider;
194 
195  std::string OriginString() const
196  {
197  return HexStr(m_origin.fingerprint) + FormatHDKeypath(m_origin.path);
198  }
199 
200 public:
201  OriginPubkeyProvider(uint32_t exp_index, KeyOriginInfo info, std::unique_ptr<PubkeyProvider> provider) : PubkeyProvider(exp_index), m_origin(std::move(info)), m_provider(std::move(provider)) {}
202  bool GetPubKey(int pos, const SigningProvider& arg, CPubKey& key, KeyOriginInfo& info, const DescriptorCache* read_cache = nullptr, DescriptorCache* write_cache = nullptr) override
203  {
204  if (!m_provider->GetPubKey(pos, arg, key, info, read_cache, write_cache)) return false;
205  std::copy(std::begin(m_origin.fingerprint), std::end(m_origin.fingerprint), info.fingerprint);
206  info.path.insert(info.path.begin(), m_origin.path.begin(), m_origin.path.end());
207  return true;
208  }
209  bool IsRange() const override { return m_provider->IsRange(); }
210  size_t GetSize() const override { return m_provider->GetSize(); }
211  std::string ToString() const override { return "[" + OriginString() + "]" + m_provider->ToString(); }
212  bool ToPrivateString(const SigningProvider& arg, std::string& ret) const override
213  {
214  std::string sub;
215  if (!m_provider->ToPrivateString(arg, sub)) return false;
216  ret = "[" + OriginString() + "]" + std::move(sub);
217  return true;
218  }
219  bool ToNormalizedString(const SigningProvider& arg, std::string& ret, bool priv) const override
220  {
221  std::string sub;
222  if (!m_provider->ToNormalizedString(arg, sub, priv)) return false;
223  // If m_provider is a BIP32PubkeyProvider, we may get a string formatted like a OriginPubkeyProvider
224  // In that case, we need to strip out the leading square bracket and fingerprint from the substring,
225  // and append that to our own origin string.
226  if (sub[0] == '[') {
227  sub = sub.substr(9);
228  ret = "[" + OriginString() + std::move(sub);
229  } else {
230  ret = "[" + OriginString() + "]" + std::move(sub);
231  }
232  return true;
233  }
234  bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override
235  {
236  return m_provider->GetPrivKey(pos, arg, key);
237  }
238 };
239 
241 class ConstPubkeyProvider final : public PubkeyProvider
242 {
243  CPubKey m_pubkey;
244 
245 public:
246  ConstPubkeyProvider(uint32_t exp_index, const CPubKey& pubkey) : PubkeyProvider(exp_index), m_pubkey(pubkey) {}
247  bool GetPubKey(int pos, const SigningProvider& arg, CPubKey& key, KeyOriginInfo& info, const DescriptorCache* read_cache = nullptr, DescriptorCache* write_cache = nullptr) override
248  {
249  key = m_pubkey;
250  info.path.clear();
251  CKeyID keyid = m_pubkey.GetID();
252  std::copy(keyid.begin(), keyid.begin() + sizeof(info.fingerprint), info.fingerprint);
253  return true;
254  }
255  bool IsRange() const override { return false; }
256  size_t GetSize() const override { return m_pubkey.size(); }
257  std::string ToString() const override { return HexStr(m_pubkey); }
258  bool ToPrivateString(const SigningProvider& arg, std::string& ret) const override
259  {
260  CKey key;
261  if (!arg.GetKey(m_pubkey.GetID(), key)) return false;
262  ret = EncodeSecret(key);
263  return true;
264  }
265  bool ToNormalizedString(const SigningProvider& arg, std::string& ret, bool priv) const override
266  {
267  if (priv) return ToPrivateString(arg, ret);
268  ret = ToString();
269  return true;
270  }
271  bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override
272  {
273  return arg.GetKey(m_pubkey.GetID(), key);
274  }
275 };
276 
277 enum class DeriveType {
278  NO,
279  UNHARDENED,
280  HARDENED,
281 };
282 
284 class BIP32PubkeyProvider final : public PubkeyProvider
285 {
286  // Root xpub, path, and final derivation step type being used, if any
287  CExtPubKey m_root_extkey;
288  KeyPath m_path;
289  DeriveType m_derive;
290  // Cache of the parent of the final derived pubkeys.
291  // Primarily useful for situations when no read_cache is provided
292  CExtPubKey m_cached_xpub;
293 
294  bool GetExtKey(const SigningProvider& arg, CExtKey& ret) const
295  {
296  CKey key;
297  if (!arg.GetKey(m_root_extkey.pubkey.GetID(), key)) return false;
298  ret.nDepth = m_root_extkey.nDepth;
299  std::copy(m_root_extkey.vchFingerprint, m_root_extkey.vchFingerprint + sizeof(ret.vchFingerprint), ret.vchFingerprint);
300  ret.nChild = m_root_extkey.nChild;
301  ret.chaincode = m_root_extkey.chaincode;
302  ret.key = key;
303  return true;
304  }
305 
306  // Derives the last xprv
307  bool GetDerivedExtKey(const SigningProvider& arg, CExtKey& xprv) const
308  {
309  if (!GetExtKey(arg, xprv)) return false;
310  for (auto entry : m_path) {
311  xprv.Derive(xprv, entry);
312  }
313  return true;
314  }
315 
316  bool IsHardened() const
317  {
318  if (m_derive == DeriveType::HARDENED) return true;
319  for (auto entry : m_path) {
320  if (entry >> 31) return true;
321  }
322  return false;
323  }
324 
325 public:
326  BIP32PubkeyProvider(uint32_t exp_index, const CExtPubKey& extkey, KeyPath path, DeriveType derive) : PubkeyProvider(exp_index), m_root_extkey(extkey), m_path(std::move(path)), m_derive(derive) {}
327  bool IsRange() const override { return m_derive != DeriveType::NO; }
328  size_t GetSize() const override { return 33; }
329  bool GetPubKey(int pos, const SigningProvider& arg, CPubKey& key_out, KeyOriginInfo& final_info_out, const DescriptorCache* read_cache = nullptr, DescriptorCache* write_cache = nullptr) override
330  {
331  // Info of parent of the to be derived pubkey
332  KeyOriginInfo parent_info;
333  CKeyID keyid = m_root_extkey.pubkey.GetID();
334  std::copy(keyid.begin(), keyid.begin() + sizeof(parent_info.fingerprint), parent_info.fingerprint);
335  parent_info.path = m_path;
336 
337  // Info of the derived key itself which is copied out upon successful completion
338  KeyOriginInfo final_info_out_tmp = parent_info;
339  if (m_derive == DeriveType::UNHARDENED) final_info_out_tmp.path.push_back((uint32_t)pos);
340  if (m_derive == DeriveType::HARDENED) final_info_out_tmp.path.push_back(((uint32_t)pos) | 0x80000000L);
341 
342  // Derive keys or fetch them from cache
343  CExtPubKey final_extkey = m_root_extkey;
344  CExtPubKey parent_extkey = m_root_extkey;
345  bool der = true;
346  if (read_cache) {
347  if (!read_cache->GetCachedDerivedExtPubKey(m_expr_index, pos, final_extkey)) {
348  if (m_derive == DeriveType::HARDENED) return false;
349  // Try to get the derivation parent
350  if (!read_cache->GetCachedParentExtPubKey(m_expr_index, parent_extkey)) return false;
351  final_extkey = parent_extkey;
352  if (m_derive == DeriveType::UNHARDENED) der = parent_extkey.Derive(final_extkey, pos);
353  }
354  } else if (m_cached_xpub.pubkey.IsValid() && m_derive != DeriveType::HARDENED) {
355  parent_extkey = final_extkey = m_cached_xpub;
356  if (m_derive == DeriveType::UNHARDENED) der = parent_extkey.Derive(final_extkey, pos);
357  } else if (IsHardened()) {
358  CExtKey xprv;
359  if (!GetDerivedExtKey(arg, xprv)) return false;
360  parent_extkey = xprv.Neuter();
361  if (m_derive == DeriveType::UNHARDENED) der = xprv.Derive(xprv, pos);
362  if (m_derive == DeriveType::HARDENED) der = xprv.Derive(xprv, pos | 0x80000000UL);
363  final_extkey = xprv.Neuter();
364  } else {
365  for (auto entry : m_path) {
366  der = parent_extkey.Derive(parent_extkey, entry);
367  assert(der);
368  }
369  final_extkey = parent_extkey;
370  if (m_derive == DeriveType::UNHARDENED) der = parent_extkey.Derive(final_extkey, pos);
371  assert(m_derive != DeriveType::HARDENED);
372  }
373  assert(der);
374 
375  final_info_out = final_info_out_tmp;
376  key_out = final_extkey.pubkey;
377 
378  // We rely on the consumer to check that m_derive isn't HARDENED as above
379  // But we can't have already cached something in case we read something from the cache
380  // and parent_extkey isn't actually the parent.
381  if (!m_cached_xpub.pubkey.IsValid()) m_cached_xpub = parent_extkey;
382 
383  if (write_cache) {
384  // Only cache parent if there is any unhardened derivation
385  if (m_derive != DeriveType::HARDENED) {
386  write_cache->CacheParentExtPubKey(m_expr_index, parent_extkey);
387  } else if (final_info_out.path.size() > 0) {
388  write_cache->CacheDerivedExtPubKey(m_expr_index, pos, final_extkey);
389  }
390  }
391 
392  return true;
393  }
394  std::string ToString() const override
395  {
396  std::string ret = EncodeExtPubKey(m_root_extkey) + FormatHDKeypath(m_path);
397  if (IsRange()) {
398  ret += "/*";
399  if (m_derive == DeriveType::HARDENED) ret += '\'';
400  }
401  return ret;
402  }
403  bool ToPrivateString(const SigningProvider& arg, std::string& out) const override
404  {
405  CExtKey key;
406  if (!GetExtKey(arg, key)) return false;
407  out = EncodeExtKey(key) + FormatHDKeypath(m_path);
408  if (IsRange()) {
409  out += "/*";
410  if (m_derive == DeriveType::HARDENED) out += '\'';
411  }
412  return true;
413  }
414  bool ToNormalizedString(const SigningProvider& arg, std::string& out, bool priv) const override
415  {
416  // For hardened derivation type, just return the typical string, nothing to normalize
417  if (m_derive == DeriveType::HARDENED) {
418  if (priv) return ToPrivateString(arg, out);
419  out = ToString();
420  return true;
421  }
422  // Step backwards to find the last hardened step in the path
423  int i = (int)m_path.size() - 1;
424  for (; i >= 0; --i) {
425  if (m_path.at(i) >> 31) {
426  break;
427  }
428  }
429  // Either no derivation or all unhardened derivation
430  if (i == -1) {
431  if (priv) return ToPrivateString(arg, out);
432  out = ToString();
433  return true;
434  }
435  // Derive the xpub at the last hardened step
436  CExtKey xprv;
437  if (!GetExtKey(arg, xprv)) return false;
438  KeyOriginInfo origin;
439  int k = 0;
440  for (; k <= i; ++k) {
441  // Derive
442  xprv.Derive(xprv, m_path.at(k));
443  // Add to the path
444  origin.path.push_back(m_path.at(k));
445  // First derivation element, get the fingerprint for origin
446  if (k == 0) {
447  std::copy(xprv.vchFingerprint, xprv.vchFingerprint + 4, origin.fingerprint);
448  }
449  }
450  // Build the remaining path
451  KeyPath end_path;
452  for (; k < (int)m_path.size(); ++k) {
453  end_path.push_back(m_path.at(k));
454  }
455  // Build the string
456  std::string origin_str = HexStr(origin.fingerprint) + FormatHDKeypath(origin.path);
457  out = "[" + origin_str + "]" + (priv ? EncodeExtKey(xprv) : EncodeExtPubKey(xprv.Neuter())) + FormatHDKeypath(end_path);
458  if (IsRange()) {
459  out += "/*";
460  assert(m_derive == DeriveType::UNHARDENED);
461  }
462  return true;
463  }
464  bool GetPrivKey(int pos, const SigningProvider& arg, CKey& key) const override
465  {
466  CExtKey extkey;
467  if (!GetDerivedExtKey(arg, extkey)) return false;
468  if (m_derive == DeriveType::UNHARDENED) extkey.Derive(extkey, pos);
469  if (m_derive == DeriveType::HARDENED) extkey.Derive(extkey, pos | 0x80000000UL);
470  key = extkey.key;
471  return true;
472  }
473 };
474 
476 class DescriptorImpl : public Descriptor
477 {
479  const std::vector<std::unique_ptr<PubkeyProvider>> m_pubkey_args;
481  const std::string m_name;
482 
483 protected:
488  const std::vector<std::unique_ptr<DescriptorImpl>> m_subdescriptor_args;
489 
491  virtual std::string ToStringExtra() const { return ""; }
492 
503  virtual std::vector<CScript> MakeScripts(const std::vector<CPubKey>& pubkeys, Span<const CScript> scripts, FlatSigningProvider& out) const = 0;
504 
505 public:
506  DescriptorImpl(std::vector<std::unique_ptr<PubkeyProvider>> pubkeys, const std::string& name) : m_pubkey_args(std::move(pubkeys)), m_name(name), m_subdescriptor_args() {}
507  DescriptorImpl(std::vector<std::unique_ptr<PubkeyProvider>> pubkeys, std::unique_ptr<DescriptorImpl> script, const std::string& name) : m_pubkey_args(std::move(pubkeys)), m_name(name), m_subdescriptor_args(Vector(std::move(script))) {}
508 
509  bool IsSolvable() const override
510  {
511  for (const auto& arg : m_subdescriptor_args) {
512  if (!arg->IsSolvable()) return false;
513  }
514  return true;
515  }
516 
517  bool IsRange() const final
518  {
519  for (const auto& pubkey : m_pubkey_args) {
520  if (pubkey->IsRange()) return true;
521  }
522  for (const auto& arg : m_subdescriptor_args) {
523  if (arg->IsRange()) return true;
524  }
525  return false;
526  }
527 
528  virtual bool ToStringSubScriptHelper(const SigningProvider* arg, std::string& ret, bool priv, bool normalized) const
529  {
530  size_t pos = 0;
531  for (const auto& scriptarg : m_subdescriptor_args) {
532  if (pos++) ret += ",";
533  std::string tmp;
534  if (!scriptarg->ToStringHelper(arg, tmp, priv, normalized)) return false;
535  ret += std::move(tmp);
536  }
537  return true;
538  }
539 
540  bool ToStringHelper(const SigningProvider* arg, std::string& out, bool priv, bool normalized) const
541  {
542  std::string extra = ToStringExtra();
543  size_t pos = extra.size() > 0 ? 1 : 0;
544  std::string ret = m_name + "(" + extra;
545  for (const auto& pubkey : m_pubkey_args) {
546  if (pos++) ret += ",";
547  std::string tmp;
548  if (normalized) {
549  if (!pubkey->ToNormalizedString(*arg, tmp, priv)) return false;
550  } else if (priv) {
551  if (!pubkey->ToPrivateString(*arg, tmp)) return false;
552  } else {
553  tmp = pubkey->ToString();
554  }
555  ret += std::move(tmp);
556  }
557  std::string subscript;
558  if (!ToStringSubScriptHelper(arg, subscript, priv, normalized)) return false;
559  if (pos && subscript.size()) ret += ',';
560  out = std::move(ret) + std::move(subscript) + ")";
561  return true;
562  }
563 
564  std::string ToString() const final
565  {
566  std::string ret;
567  ToStringHelper(nullptr, ret, false, false);
568  return AddChecksum(ret);
569  }
570 
571  bool ToPrivateString(const SigningProvider& arg, std::string& out) const final
572  {
573  bool ret = ToStringHelper(&arg, out, true, false);
574  out = AddChecksum(out);
575  return ret;
576  }
577 
578  bool ToNormalizedString(const SigningProvider& arg, std::string& out, bool priv) const override final
579  {
580  bool ret = ToStringHelper(&arg, out, priv, true);
581  out = AddChecksum(out);
582  return ret;
583  }
584 
585  bool ExpandHelper(int pos, const SigningProvider& arg, const DescriptorCache* read_cache, std::vector<CScript>& output_scripts, FlatSigningProvider& out, DescriptorCache* write_cache) const
586  {
587  std::vector<std::pair<CPubKey, KeyOriginInfo>> entries;
588  entries.reserve(m_pubkey_args.size());
589 
590  // Construct temporary data in `entries`, `subscripts`, and `subprovider` to avoid producing output in case of failure.
591  for (const auto& p : m_pubkey_args) {
592  entries.emplace_back();
593  if (!p->GetPubKey(pos, arg, entries.back().first, entries.back().second, read_cache, write_cache)) return false;
594  }
595  std::vector<CScript> subscripts;
596  FlatSigningProvider subprovider;
597  for (const auto& subarg : m_subdescriptor_args) {
598  std::vector<CScript> outscripts;
599  if (!subarg->ExpandHelper(pos, arg, read_cache, outscripts, subprovider, write_cache)) return false;
600  assert(outscripts.size() == 1);
601  subscripts.emplace_back(std::move(outscripts[0]));
602  }
603  out = Merge(std::move(out), std::move(subprovider));
604 
605  std::vector<CPubKey> pubkeys;
606  pubkeys.reserve(entries.size());
607  for (auto& entry : entries) {
608  pubkeys.push_back(entry.first);
609  out.origins.emplace(entry.first.GetID(), std::make_pair<CPubKey, KeyOriginInfo>(CPubKey(entry.first), std::move(entry.second)));
610  }
611 
612  output_scripts = MakeScripts(pubkeys, MakeSpan(subscripts), out);
613  return true;
614  }
615 
616  bool Expand(int pos, const SigningProvider& provider, std::vector<CScript>& output_scripts, FlatSigningProvider& out, DescriptorCache* write_cache = nullptr) const final
617  {
618  return ExpandHelper(pos, provider, nullptr, output_scripts, out, write_cache);
619  }
620 
621  bool ExpandFromCache(int pos, const DescriptorCache& read_cache, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const final
622  {
623  return ExpandHelper(pos, DUMMY_SIGNING_PROVIDER, &read_cache, output_scripts, out, nullptr);
624  }
625 
626  void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const final
627  {
628  for (const auto& p : m_pubkey_args) {
629  CKey key;
630  if (!p->GetPrivKey(pos, provider, key)) continue;
631  out.keys.emplace(key.GetPubKey().GetID(), key);
632  }
633  for (const auto& arg : m_subdescriptor_args) {
634  arg->ExpandPrivate(pos, provider, out);
635  }
636  }
637 
638  std::optional<OutputType> GetOutputType() const override { return std::nullopt; }
639 };
640 
642 class AddressDescriptor final : public DescriptorImpl
643 {
644  const CTxDestination m_destination;
645 protected:
646  std::string ToStringExtra() const override { return EncodeDestination(m_destination); }
647  std::vector<CScript> MakeScripts(const std::vector<CPubKey>&, Span<const CScript>, FlatSigningProvider&) const override { return Vector(GetScriptForDestination(m_destination)); }
648 public:
649  AddressDescriptor(CTxDestination destination) : DescriptorImpl({}, "addr"), m_destination(std::move(destination)) {}
650  bool IsSolvable() const final { return false; }
651 
652  std::optional<OutputType> GetOutputType() const override
653  {
654  switch (m_destination.index()) {
655  case 1 /* PKHash */:
656  case 2 /* ScriptHash */: return OutputType::LEGACY;
657  case 3 /* WitnessV0ScriptHash */:
658  case 4 /* WitnessV0KeyHash */:
659  case 5 /* WitnessUnknown */: return OutputType::BECH32;
660  case 0 /* CNoDestination */:
661  default: return std::nullopt;
662  }
663  }
664  bool IsSingleType() const final { return true; }
665 };
666 
668 class RawDescriptor final : public DescriptorImpl
669 {
670  const CScript m_script;
671 protected:
672  std::string ToStringExtra() const override { return HexStr(m_script); }
673  std::vector<CScript> MakeScripts(const std::vector<CPubKey>&, Span<const CScript>, FlatSigningProvider&) const override { return Vector(m_script); }
674 public:
675  RawDescriptor(CScript script) : DescriptorImpl({}, "raw"), m_script(std::move(script)) {}
676  bool IsSolvable() const final { return false; }
677 
678  std::optional<OutputType> GetOutputType() const override
679  {
680  CTxDestination dest;
681  ExtractDestination(m_script, dest);
682  switch (dest.index()) {
683  case 1 /* PKHash */:
684  case 2 /* ScriptHash */: return OutputType::LEGACY;
685  case 3 /* WitnessV0ScriptHash */:
686  case 4 /* WitnessV0KeyHash */:
687  case 5 /* WitnessUnknown */: return OutputType::BECH32;
688  case 0 /* CNoDestination */:
689  default: return std::nullopt;
690  }
691  }
692  bool IsSingleType() const final { return true; }
693 };
694 
696 class PKDescriptor final : public DescriptorImpl
697 {
698 protected:
699  std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, Span<const CScript>, FlatSigningProvider&) const override { return Vector(GetScriptForRawPubKey(keys[0])); }
700 public:
701  PKDescriptor(std::unique_ptr<PubkeyProvider> prov) : DescriptorImpl(Vector(std::move(prov)), "pk") {}
702  bool IsSingleType() const final { return true; }
703 };
704 
706 class PKHDescriptor final : public DescriptorImpl
707 {
708 protected:
709  std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, Span<const CScript>, FlatSigningProvider& out) const override
710  {
711  CKeyID id = keys[0].GetID();
712  out.pubkeys.emplace(id, keys[0]);
714  }
715 public:
716  PKHDescriptor(std::unique_ptr<PubkeyProvider> prov) : DescriptorImpl(Vector(std::move(prov)), "pkh") {}
717  std::optional<OutputType> GetOutputType() const override { return OutputType::LEGACY; }
718  bool IsSingleType() const final { return true; }
719 };
720 
722 class WPKHDescriptor final : public DescriptorImpl
723 {
724 protected:
725  std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, Span<const CScript>, FlatSigningProvider& out) const override
726  {
727  CKeyID id = keys[0].GetID();
728  out.pubkeys.emplace(id, keys[0]);
730  }
731 public:
732  WPKHDescriptor(std::unique_ptr<PubkeyProvider> prov) : DescriptorImpl(Vector(std::move(prov)), "wpkh") {}
733  std::optional<OutputType> GetOutputType() const override { return OutputType::BECH32; }
734  bool IsSingleType() const final { return true; }
735 };
736 
738 class ComboDescriptor final : public DescriptorImpl
739 {
740 protected:
741  std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, Span<const CScript>, FlatSigningProvider& out) const override
742  {
743  std::vector<CScript> ret;
744  CKeyID id = keys[0].GetID();
745  out.pubkeys.emplace(id, keys[0]);
746  ret.emplace_back(GetScriptForRawPubKey(keys[0])); // P2PK
747  ret.emplace_back(GetScriptForDestination(PKHash(id))); // P2PKH
748  if (keys[0].IsCompressed()) {
750  out.scripts.emplace(CScriptID(p2wpkh), p2wpkh);
751  ret.emplace_back(p2wpkh);
752  ret.emplace_back(GetScriptForDestination(ScriptHash(p2wpkh))); // P2SH-P2WPKH
753  }
754  return ret;
755  }
756 public:
757  ComboDescriptor(std::unique_ptr<PubkeyProvider> prov) : DescriptorImpl(Vector(std::move(prov)), "combo") {}
758  bool IsSingleType() const final { return false; }
759 };
760 
762 class MultisigDescriptor final : public DescriptorImpl
763 {
764  const int m_threshold;
765  const bool m_sorted;
766 protected:
767  std::string ToStringExtra() const override { return strprintf("%i", m_threshold); }
768  std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, Span<const CScript>, FlatSigningProvider&) const override {
769  if (m_sorted) {
770  std::vector<CPubKey> sorted_keys(keys);
771  std::sort(sorted_keys.begin(), sorted_keys.end());
772  return Vector(GetScriptForMultisig(m_threshold, sorted_keys));
773  }
774  return Vector(GetScriptForMultisig(m_threshold, keys));
775  }
776 public:
777  MultisigDescriptor(int threshold, std::vector<std::unique_ptr<PubkeyProvider>> providers, bool sorted = false) : DescriptorImpl(std::move(providers), sorted ? "sortedmulti" : "multi"), m_threshold(threshold), m_sorted(sorted) {}
778  bool IsSingleType() const final { return true; }
779 };
780 
782 class SHDescriptor final : public DescriptorImpl
783 {
784 protected:
785  std::vector<CScript> MakeScripts(const std::vector<CPubKey>&, Span<const CScript> scripts, FlatSigningProvider& out) const override
786  {
787  auto ret = Vector(GetScriptForDestination(ScriptHash(scripts[0])));
788  if (ret.size()) out.scripts.emplace(CScriptID(scripts[0]), scripts[0]);
789  return ret;
790  }
791 public:
792  SHDescriptor(std::unique_ptr<DescriptorImpl> desc) : DescriptorImpl({}, std::move(desc), "sh") {}
793 
794  std::optional<OutputType> GetOutputType() const override
795  {
796  assert(m_subdescriptor_args.size() == 1);
797  if (m_subdescriptor_args[0]->GetOutputType() == OutputType::BECH32) return OutputType::P2SH_SEGWIT;
798  return OutputType::LEGACY;
799  }
800  bool IsSingleType() const final { return true; }
801 };
802 
804 class WSHDescriptor final : public DescriptorImpl
805 {
806 protected:
807  std::vector<CScript> MakeScripts(const std::vector<CPubKey>&, Span<const CScript> scripts, FlatSigningProvider& out) const override
808  {
809  auto ret = Vector(GetScriptForDestination(WitnessV0ScriptHash(scripts[0])));
810  if (ret.size()) out.scripts.emplace(CScriptID(scripts[0]), scripts[0]);
811  return ret;
812  }
813 public:
814  WSHDescriptor(std::unique_ptr<DescriptorImpl> desc) : DescriptorImpl({}, std::move(desc), "wsh") {}
815  std::optional<OutputType> GetOutputType() const override { return OutputType::BECH32; }
816  bool IsSingleType() const final { return true; }
817 };
818 
820 // Parser //
822 
823 enum class ParseScriptContext {
824  TOP,
825  P2SH,
826  P2WPKH,
827  P2WSH,
828 };
829 
831 [[nodiscard]] bool ParseKeyPath(const std::vector<Span<const char>>& split, KeyPath& out, std::string& error)
832 {
833  for (size_t i = 1; i < split.size(); ++i) {
834  Span<const char> elem = split[i];
835  bool hardened = false;
836  if (elem.size() > 0 && (elem[elem.size() - 1] == '\'' || elem[elem.size() - 1] == 'h')) {
837  elem = elem.first(elem.size() - 1);
838  hardened = true;
839  }
840  uint32_t p;
841  if (!ParseUInt32(std::string(elem.begin(), elem.end()), &p)) {
842  error = strprintf("Key path value '%s' is not a valid uint32", std::string(elem.begin(), elem.end()));
843  return false;
844  } else if (p > 0x7FFFFFFFUL) {
845  error = strprintf("Key path value %u is out of range", p);
846  return false;
847  }
848  out.push_back(p | (((uint32_t)hardened) << 31));
849  }
850  return true;
851 }
852 
854 std::unique_ptr<PubkeyProvider> ParsePubkeyInner(uint32_t key_exp_index, const Span<const char>& sp, ParseScriptContext ctx, FlatSigningProvider& out, std::string& error)
855 {
856  using namespace spanparsing;
857 
858  bool permit_uncompressed = ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH;
859  auto split = Split(sp, '/');
860  std::string str(split[0].begin(), split[0].end());
861  if (str.size() == 0) {
862  error = "No key provided";
863  return nullptr;
864  }
865  if (split.size() == 1) {
866  if (IsHex(str)) {
867  std::vector<unsigned char> data = ParseHex(str);
868  CPubKey pubkey(data);
869  if (pubkey.IsFullyValid()) {
870  if (permit_uncompressed || pubkey.IsCompressed()) {
871  return std::make_unique<ConstPubkeyProvider>(key_exp_index, pubkey);
872  } else {
873  error = "Uncompressed keys are not allowed";
874  return nullptr;
875  }
876  }
877  error = strprintf("Pubkey '%s' is invalid", str);
878  return nullptr;
879  }
880  CKey key = DecodeSecret(str);
881  if (key.IsValid()) {
882  if (permit_uncompressed || key.IsCompressed()) {
883  CPubKey pubkey = key.GetPubKey();
884  out.keys.emplace(pubkey.GetID(), key);
885  return std::make_unique<ConstPubkeyProvider>(key_exp_index, pubkey);
886  } else {
887  error = "Uncompressed keys are not allowed";
888  return nullptr;
889  }
890  }
891  }
892  CExtKey extkey = DecodeExtKey(str);
893  CExtPubKey extpubkey = DecodeExtPubKey(str);
894  if (!extkey.key.IsValid() && !extpubkey.pubkey.IsValid()) {
895  error = strprintf("key '%s' is not valid", str);
896  return nullptr;
897  }
898  KeyPath path;
899  DeriveType type = DeriveType::NO;
900  if (split.back() == MakeSpan("*").first(1)) {
901  split.pop_back();
902  type = DeriveType::UNHARDENED;
903  } else if (split.back() == MakeSpan("*'").first(2) || split.back() == MakeSpan("*h").first(2)) {
904  split.pop_back();
905  type = DeriveType::HARDENED;
906  }
907  if (!ParseKeyPath(split, path, error)) return nullptr;
908  if (extkey.key.IsValid()) {
909  extpubkey = extkey.Neuter();
910  out.keys.emplace(extpubkey.pubkey.GetID(), extkey.key);
911  }
912  return std::make_unique<BIP32PubkeyProvider>(key_exp_index, extpubkey, std::move(path), type);
913 }
914 
916 std::unique_ptr<PubkeyProvider> ParsePubkey(uint32_t key_exp_index, const Span<const char>& sp, ParseScriptContext ctx, FlatSigningProvider& out, std::string& error)
917 {
918  using namespace spanparsing;
919 
920  auto origin_split = Split(sp, ']');
921  if (origin_split.size() > 2) {
922  error = "Multiple ']' characters found for a single pubkey";
923  return nullptr;
924  }
925  if (origin_split.size() == 1) return ParsePubkeyInner(key_exp_index, origin_split[0], ctx, out, error);
926  if (origin_split[0].empty() || origin_split[0][0] != '[') {
927  error = strprintf("Key origin start '[ character expected but not found, got '%c' instead",
928  origin_split[0].empty() ? ']' : origin_split[0][0]);
929  return nullptr;
930  }
931  auto slash_split = Split(origin_split[0].subspan(1), '/');
932  if (slash_split[0].size() != 8) {
933  error = strprintf("Fingerprint is not 4 bytes (%u characters instead of 8 characters)", slash_split[0].size());
934  return nullptr;
935  }
936  std::string fpr_hex = std::string(slash_split[0].begin(), slash_split[0].end());
937  if (!IsHex(fpr_hex)) {
938  error = strprintf("Fingerprint '%s' is not hex", fpr_hex);
939  return nullptr;
940  }
941  auto fpr_bytes = ParseHex(fpr_hex);
942  KeyOriginInfo info;
943  static_assert(sizeof(info.fingerprint) == 4, "Fingerprint must be 4 bytes");
944  assert(fpr_bytes.size() == 4);
945  std::copy(fpr_bytes.begin(), fpr_bytes.end(), info.fingerprint);
946  if (!ParseKeyPath(slash_split, info.path, error)) return nullptr;
947  auto provider = ParsePubkeyInner(key_exp_index, origin_split[1], ctx, out, error);
948  if (!provider) return nullptr;
949  return std::make_unique<OriginPubkeyProvider>(key_exp_index, std::move(info), std::move(provider));
950 }
951 
953 std::unique_ptr<DescriptorImpl> ParseScript(uint32_t& key_exp_index, Span<const char>& sp, ParseScriptContext ctx, FlatSigningProvider& out, std::string& error)
954 {
955  using namespace spanparsing;
956 
957  auto expr = Expr(sp);
958  bool sorted_multi = false;
959  if (Func("pk", expr)) {
960  auto pubkey = ParsePubkey(key_exp_index, expr, ctx, out, error);
961  if (!pubkey) return nullptr;
962  ++key_exp_index;
963  return std::make_unique<PKDescriptor>(std::move(pubkey));
964  }
965  if (Func("pkh", expr)) {
966  auto pubkey = ParsePubkey(key_exp_index, expr, ctx, out, error);
967  if (!pubkey) return nullptr;
968  ++key_exp_index;
969  return std::make_unique<PKHDescriptor>(std::move(pubkey));
970  }
971  if (ctx == ParseScriptContext::TOP && Func("combo", expr)) {
972  auto pubkey = ParsePubkey(key_exp_index, expr, ctx, out, error);
973  if (!pubkey) return nullptr;
974  ++key_exp_index;
975  return std::make_unique<ComboDescriptor>(std::move(pubkey));
976  } else if (Func("combo", expr)) {
977  error = "Can only have combo() at top level";
978  return nullptr;
979  }
980  if ((sorted_multi = Func("sortedmulti", expr)) || Func("multi", expr)) {
981  auto threshold = Expr(expr);
982  uint32_t thres;
983  std::vector<std::unique_ptr<PubkeyProvider>> providers;
984  if (!ParseUInt32(std::string(threshold.begin(), threshold.end()), &thres)) {
985  error = strprintf("Multi threshold '%s' is not valid", std::string(threshold.begin(), threshold.end()));
986  return nullptr;
987  }
988  size_t script_size = 0;
989  while (expr.size()) {
990  if (!Const(",", expr)) {
991  error = strprintf("Multi: expected ',', got '%c'", expr[0]);
992  return nullptr;
993  }
994  auto arg = Expr(expr);
995  auto pk = ParsePubkey(key_exp_index, arg, ctx, out, error);
996  if (!pk) return nullptr;
997  script_size += pk->GetSize() + 1;
998  providers.emplace_back(std::move(pk));
999  key_exp_index++;
1000  }
1001  if (providers.empty() || providers.size() > 16) {
1002  error = strprintf("Cannot have %u keys in multisig; must have between 1 and 16 keys, inclusive", providers.size());
1003  return nullptr;
1004  } else if (thres < 1) {
1005  error = strprintf("Multisig threshold cannot be %d, must be at least 1", thres);
1006  return nullptr;
1007  } else if (thres > providers.size()) {
1008  error = strprintf("Multisig threshold cannot be larger than the number of keys; threshold is %d but only %u keys specified", thres, providers.size());
1009  return nullptr;
1010  }
1011  if (ctx == ParseScriptContext::TOP) {
1012  if (providers.size() > 3) {
1013  error = strprintf("Cannot have %u pubkeys in bare multisig; only at most 3 pubkeys", providers.size());
1014  return nullptr;
1015  }
1016  }
1017  if (ctx == ParseScriptContext::P2SH) {
1018  if (script_size + 3 > MAX_SCRIPT_ELEMENT_SIZE) {
1019  error = strprintf("P2SH script is too large, %d bytes is larger than %d bytes", script_size + 3, MAX_SCRIPT_ELEMENT_SIZE);
1020  return nullptr;
1021  }
1022  }
1023  return std::make_unique<MultisigDescriptor>(thres, std::move(providers), sorted_multi);
1024  }
1025  if ((ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH) && Func("wpkh", expr)) {
1026  auto pubkey = ParsePubkey(key_exp_index, expr, ParseScriptContext::P2WPKH, out, error);
1027  if (!pubkey) return nullptr;
1028  key_exp_index++;
1029  return std::make_unique<WPKHDescriptor>(std::move(pubkey));
1030  } else if (Func("wpkh", expr)) {
1031  error = "Can only have wpkh() at top level or inside sh()";
1032  return nullptr;
1033  }
1034  if (ctx == ParseScriptContext::TOP && Func("sh", expr)) {
1035  auto desc = ParseScript(key_exp_index, expr, ParseScriptContext::P2SH, out, error);
1036  if (!desc || expr.size()) return nullptr;
1037  return std::make_unique<SHDescriptor>(std::move(desc));
1038  } else if (Func("sh", expr)) {
1039  error = "Can only have sh() at top level";
1040  return nullptr;
1041  }
1042  if ((ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH) && Func("wsh", expr)) {
1043  auto desc = ParseScript(key_exp_index, expr, ParseScriptContext::P2WSH, out, error);
1044  if (!desc || expr.size()) return nullptr;
1045  return std::make_unique<WSHDescriptor>(std::move(desc));
1046  } else if (Func("wsh", expr)) {
1047  error = "Can only have wsh() at top level or inside sh()";
1048  return nullptr;
1049  }
1050  if (ctx == ParseScriptContext::TOP && Func("addr", expr)) {
1051  CTxDestination dest = DecodeDestination(std::string(expr.begin(), expr.end()));
1052  if (!IsValidDestination(dest)) {
1053  error = "Address is not valid";
1054  return nullptr;
1055  }
1056  return std::make_unique<AddressDescriptor>(std::move(dest));
1057  } else if (Func("addr", expr)) {
1058  error = "Can only have addr() at top level";
1059  return nullptr;
1060  }
1061  if (ctx == ParseScriptContext::TOP && Func("raw", expr)) {
1062  std::string str(expr.begin(), expr.end());
1063  if (!IsHex(str)) {
1064  error = "Raw script is not hex";
1065  return nullptr;
1066  }
1067  auto bytes = ParseHex(str);
1068  return std::make_unique<RawDescriptor>(CScript(bytes.begin(), bytes.end()));
1069  } else if (Func("raw", expr)) {
1070  error = "Can only have raw() at top level";
1071  return nullptr;
1072  }
1073  if (ctx == ParseScriptContext::P2SH) {
1074  error = "A function is needed within P2SH";
1075  return nullptr;
1076  } else if (ctx == ParseScriptContext::P2WSH) {
1077  error = "A function is needed within P2WSH";
1078  return nullptr;
1079  }
1080  error = strprintf("%s is not a valid descriptor function", std::string(expr.begin(), expr.end()));
1081  return nullptr;
1082 }
1083 
1084 std::unique_ptr<PubkeyProvider> InferPubkey(const CPubKey& pubkey, ParseScriptContext, const SigningProvider& provider)
1085 {
1086  std::unique_ptr<PubkeyProvider> key_provider = std::make_unique<ConstPubkeyProvider>(0, pubkey);
1087  KeyOriginInfo info;
1088  if (provider.GetKeyOrigin(pubkey.GetID(), info)) {
1089  return std::make_unique<OriginPubkeyProvider>(0, std::move(info), std::move(key_provider));
1090  }
1091  return key_provider;
1092 }
1093 
1094 std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptContext ctx, const SigningProvider& provider)
1095 {
1096  std::vector<std::vector<unsigned char>> data;
1097  TxoutType txntype = Solver(script, data);
1098 
1099  if (txntype == TxoutType::PUBKEY) {
1100  CPubKey pubkey(data[0].begin(), data[0].end());
1101  if (pubkey.IsValid()) {
1102  return std::make_unique<PKDescriptor>(InferPubkey(pubkey, ctx, provider));
1103  }
1104  }
1105  if (txntype == TxoutType::PUBKEYHASH) {
1106  uint160 hash(data[0]);
1107  CKeyID keyid(hash);
1108  CPubKey pubkey;
1109  if (provider.GetPubKey(keyid, pubkey)) {
1110  return std::make_unique<PKHDescriptor>(InferPubkey(pubkey, ctx, provider));
1111  }
1112  }
1113  if (txntype == TxoutType::WITNESS_V0_KEYHASH && ctx != ParseScriptContext::P2WSH) {
1114  uint160 hash(data[0]);
1115  CKeyID keyid(hash);
1116  CPubKey pubkey;
1117  if (provider.GetPubKey(keyid, pubkey)) {
1118  return std::make_unique<WPKHDescriptor>(InferPubkey(pubkey, ctx, provider));
1119  }
1120  }
1121  if (txntype == TxoutType::MULTISIG) {
1122  std::vector<std::unique_ptr<PubkeyProvider>> providers;
1123  for (size_t i = 1; i + 1 < data.size(); ++i) {
1124  CPubKey pubkey(data[i].begin(), data[i].end());
1125  providers.push_back(InferPubkey(pubkey, ctx, provider));
1126  }
1127  return std::make_unique<MultisigDescriptor>((int)data[0][0], std::move(providers));
1128  }
1129  if (txntype == TxoutType::SCRIPTHASH && ctx == ParseScriptContext::TOP) {
1130  uint160 hash(data[0]);
1131  CScriptID scriptid(hash);
1132  CScript subscript;
1133  if (provider.GetCScript(scriptid, subscript)) {
1134  auto sub = InferScript(subscript, ParseScriptContext::P2SH, provider);
1135  if (sub) return std::make_unique<SHDescriptor>(std::move(sub));
1136  }
1137  }
1138  if (txntype == TxoutType::WITNESS_V0_SCRIPTHASH && ctx != ParseScriptContext::P2WSH) {
1139  CScriptID scriptid;
1140  CRIPEMD160().Write(data[0].data(), data[0].size()).Finalize(scriptid.begin());
1141  CScript subscript;
1142  if (provider.GetCScript(scriptid, subscript)) {
1143  auto sub = InferScript(subscript, ParseScriptContext::P2WSH, provider);
1144  if (sub) return std::make_unique<WSHDescriptor>(std::move(sub));
1145  }
1146  }
1147 
1148  CTxDestination dest;
1149  if (ExtractDestination(script, dest)) {
1150  if (GetScriptForDestination(dest) == script) {
1151  return std::make_unique<AddressDescriptor>(std::move(dest));
1152  }
1153  }
1154 
1155  return std::make_unique<RawDescriptor>(script);
1156 }
1157 
1158 
1159 } // namespace
1160 
1162 bool CheckChecksum(Span<const char>& sp, bool require_checksum, std::string& error, std::string* out_checksum = nullptr)
1163 {
1164  using namespace spanparsing;
1165 
1166  auto check_split = Split(sp, '#');
1167  if (check_split.size() > 2) {
1168  error = "Multiple '#' symbols";
1169  return false;
1170  }
1171  if (check_split.size() == 1 && require_checksum){
1172  error = "Missing checksum";
1173  return false;
1174  }
1175  if (check_split.size() == 2) {
1176  if (check_split[1].size() != 8) {
1177  error = strprintf("Expected 8 character checksum, not %u characters", check_split[1].size());
1178  return false;
1179  }
1180  }
1181  auto checksum = DescriptorChecksum(check_split[0]);
1182  if (checksum.empty()) {
1183  error = "Invalid characters in payload";
1184  return false;
1185  }
1186  if (check_split.size() == 2) {
1187  if (!std::equal(checksum.begin(), checksum.end(), check_split[1].begin())) {
1188  error = strprintf("Provided checksum '%s' does not match computed checksum '%s'", std::string(check_split[1].begin(), check_split[1].end()), checksum);
1189  return false;
1190  }
1191  }
1192  if (out_checksum) *out_checksum = std::move(checksum);
1193  sp = check_split[0];
1194  return true;
1195 }
1196 
1197 std::unique_ptr<Descriptor> Parse(const std::string& descriptor, FlatSigningProvider& out, std::string& error, bool require_checksum)
1198 {
1199  Span<const char> sp{descriptor};
1200  if (!CheckChecksum(sp, require_checksum, error)) return nullptr;
1201  uint32_t key_exp_index = 0;
1202  auto ret = ParseScript(key_exp_index, sp, ParseScriptContext::TOP, out, error);
1203  if (sp.size() == 0 && ret) return std::unique_ptr<Descriptor>(std::move(ret));
1204  return nullptr;
1205 }
1206 
1207 std::string GetDescriptorChecksum(const std::string& descriptor)
1208 {
1209  std::string ret;
1210  std::string error;
1211  Span<const char> sp{descriptor};
1212  if (!CheckChecksum(sp, false, error, &ret)) return "";
1213  return ret;
1214 }
1215 
1216 std::unique_ptr<Descriptor> InferDescriptor(const CScript& script, const SigningProvider& provider)
1217 {
1218  return InferScript(script, ParseScriptContext::TOP, provider);
1219 }
1220 
1221 void DescriptorCache::CacheParentExtPubKey(uint32_t key_exp_pos, const CExtPubKey& xpub)
1222 {
1223  m_parent_xpubs[key_exp_pos] = xpub;
1224 }
1225 
1226 void DescriptorCache::CacheDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, const CExtPubKey& xpub)
1227 {
1228  auto& xpubs = m_derived_xpubs[key_exp_pos];
1229  xpubs[der_index] = xpub;
1230 }
1231 
1232 bool DescriptorCache::GetCachedParentExtPubKey(uint32_t key_exp_pos, CExtPubKey& xpub) const
1233 {
1234  const auto& it = m_parent_xpubs.find(key_exp_pos);
1235  if (it == m_parent_xpubs.end()) return false;
1236  xpub = it->second;
1237  return true;
1238 }
1239 
1240 bool DescriptorCache::GetCachedDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, CExtPubKey& xpub) const
1241 {
1242  const auto& key_exp_it = m_derived_xpubs.find(key_exp_pos);
1243  if (key_exp_it == m_derived_xpubs.end()) return false;
1244  const auto& der_it = key_exp_it->second.find(der_index);
1245  if (der_it == key_exp_it->second.end()) return false;
1246  xpub = der_it->second;
1247  return true;
1248 }
1249 
1251 {
1252  return m_parent_xpubs;
1253 }
1254 
1255 const std::unordered_map<uint32_t, ExtPubKeyMap> DescriptorCache::GetCachedDerivedExtPubKeys() const
1256 {
1257  return m_derived_xpubs;
1258 }
DescriptorCache::CacheParentExtPubKey
void CacheParentExtPubKey(uint32_t key_exp_pos, const CExtPubKey &xpub)
Cache a parent xpub.
Definition: descriptor.cpp:1221
CKey::IsCompressed
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:96
bip32.h
Parse
std::unique_ptr< Descriptor > Parse(const std::string &descriptor, FlatSigningProvider &out, std::string &error, bool require_checksum)
Parse a descriptor string.
Definition: descriptor.cpp:1197
ParseHex
std::vector< unsigned char > ParseHex(const char *psz)
Definition: strencodings.cpp:84
ToString
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:79
Descriptor::GetOutputType
virtual std::optional< OutputType > GetOutputType() const =0
Solver
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
Definition: standard.cpp:109
CRIPEMD160
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
DescriptorCache::m_derived_xpubs
std::unordered_map< uint32_t, ExtPubKeyMap > m_derived_xpubs
Map key expression index -> map of (key derivation index -> xpub)
Definition: descriptor.h:22
TxoutType
TxoutType
Definition: standard.h:120
key_io.h
DescriptorCache::GetCachedParentExtPubKey
bool GetCachedParentExtPubKey(uint32_t key_exp_pos, CExtPubKey &xpub) const
Retrieve a cached parent xpub.
Definition: descriptor.cpp:1232
OutputType::LEGACY
@ LEGACY
CRIPEMD160::Finalize
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:273
Descriptor::Expand
virtual bool Expand(int pos, const SigningProvider &provider, std::vector< CScript > &output_scripts, FlatSigningProvider &out, DescriptorCache *write_cache=nullptr) const =0
Expand a descriptor at a specified position.
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:307
CExtKey::Derive
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:293
DescriptorCache::GetCachedParentExtPubKeys
const ExtPubKeyMap GetCachedParentExtPubKeys() const
Retrieve all cached parent xpubs.
Definition: descriptor.cpp:1250
CExtKey::vchFingerprint
unsigned char vchFingerprint[4]
Definition: key.h:146
IsHex
bool IsHex(const std::string &str)
Definition: strencodings.cpp:61
spanparsing::Split
std::vector< Span< const char > > Split(const Span< const char > &sp, char sep)
Split a string on every instance of sep, returning a vector.
Definition: spanparsing.cpp:51
FlatSigningProvider::keys
std::map< CKeyID, CKey > keys
Definition: signingprovider.h:52
BaseHash::begin
unsigned char * begin()
Definition: standard.h:31
SigningProvider
An interface to be implemented by keystores that support signing.
Definition: signingprovider.h:18
spanparsing::Func
bool Func(const std::string &str, Span< const char > &sp)
Parse a function call.
Definition: spanparsing.cpp:23
Merge
FlatSigningProvider Merge(const FlatSigningProvider &a, const FlatSigningProvider &b)
Definition: signingprovider.cpp:58
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:21
pubkey.h
DUMMY_SIGNING_PROVIDER
const SigningProvider & DUMMY_SIGNING_PROVIDER
Definition: signingprovider.cpp:12
CExtPubKey::Derive
bool Derive(CExtPubKey &out, unsigned int nChild) const
Definition: pubkey.cpp:293
SigningProvider::GetPubKey
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
Definition: signingprovider.h:24
Descriptor::ExpandPrivate
virtual void ExpandPrivate(int pos, const SigningProvider &provider, FlatSigningProvider &out) const =0
Expand the private key for a descriptor at a specified position, if possible.
CheckChecksum
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.
Definition: descriptor.cpp:1162
CExtKey::nDepth
unsigned char nDepth
Definition: key.h:145
WitnessV0KeyHash
Definition: standard.h:170
FlatSigningProvider::scripts
std::map< CScriptID, CScript > scripts
Definition: signingprovider.h:49
GetDescriptorChecksum
std::string GetDescriptorChecksum(const std::string &descriptor)
Get the checksum for a descriptor.
Definition: descriptor.cpp:1207
Span::size
constexpr std::size_t size() const noexcept
Definition: span.h:182
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:92
strencodings.h
TxoutType::WITNESS_V0_SCRIPTHASH
@ WITNESS_V0_SCRIPTHASH
TxoutType::PUBKEY
@ PUBKEY
FlatSigningProvider::origins
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > origins
Definition: signingprovider.h:51
IsValidDestination
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:328
ParseScript
CScript ParseScript(const std::string &s)
Definition: core_read.cpp:55
EncodeSecret
std::string EncodeSecret(const CKey &key)
Definition: key_io.cpp:181
SigningProvider::GetKeyOrigin
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
Definition: signingprovider.h:27
GetScriptForRawPubKey
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
Definition: standard.cpp:312
CExtKey
Definition: key.h:144
GetScriptForMultisig
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:317
EncodeExtKey
std::string EncodeExtKey(const CExtKey &key)
Definition: key_io.cpp:230
span.h
CRIPEMD160::Write
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:247
FormatHDKeypath
std::string FormatHDKeypath(const std::vector< uint32_t > &path)
Definition: bip32.cpp:53
CKey::IsValid
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:93
Span::begin
constexpr C * begin() const noexcept
Definition: span.h:170
standard.h
DescriptorCache::m_parent_xpubs
ExtPubKeyMap m_parent_xpubs
Map key expression index -> parent xpub.
Definition: descriptor.h:24
Descriptor::ToNormalizedString
virtual bool ToNormalizedString(const SigningProvider &provider, std::string &out, bool priv) const =0
Convert the descriptor to a normalized string.
TxoutType::SCRIPTHASH
@ SCRIPTHASH
spanparsing.h
DecodeSecret
CKey DecodeSecret(const std::string &str)
Definition: key_io.cpp:163
CExtKey::nChild
unsigned int nChild
Definition: key.h:147
SigningProvider::GetCScript
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
Definition: signingprovider.h:22
DecodeDestination
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg)
Definition: key_io.cpp:246
FlatSigningProvider::pubkeys
std::map< CKeyID, CPubKey > pubkeys
Definition: signingprovider.h:50
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:184
spanparsing::Const
bool Const(const std::string &str, Span< const char > &sp)
Parse a constant.
Definition: spanparsing.cpp:14
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
ExtractDestination
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:179
DescriptorCache
Cache for single descriptor's derived extended pubkeys.
Definition: descriptor.h:19
DecodeExtPubKey
CExtPubKey DecodeExtPubKey(const std::string &str)
Definition: key_io.cpp:194
name
const char * name
Definition: rest.cpp:43
OutputType::P2SH_SEGWIT
@ P2SH_SEGWIT
TxoutType::PUBKEYHASH
@ PUBKEYHASH
OutputType::BECH32
@ BECH32
system.h
PKHash
Definition: standard.h:140
CExtKey::chaincode
ChainCode chaincode
Definition: key.h:148
DecodeExtKey
CExtKey DecodeExtKey(const std::string &str)
Definition: key_io.cpp:217
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
ExtPubKeyMap
std::unordered_map< uint32_t, CExtPubKey > ExtPubKeyMap
Definition: descriptor.h:16
uint160
160-bit opaque blob.
Definition: uint256.h:113
CPubKey
An encapsulated public key.
Definition: pubkey.h:31
ScriptContext::P2SH
@ P2SH
P2SH redeemScript.
DescriptorCache::CacheDerivedExtPubKey
void CacheDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, const CExtPubKey &xpub)
Cache an xpub derived at an index.
Definition: descriptor.cpp:1226
CKey
An encapsulated private key.
Definition: key.h:27
IsSolvable
bool IsSolvable(const SigningProvider &provider, const CScript &script)
Definition: sign.cpp:437
DescriptorCache::GetCachedDerivedExtPubKey
bool GetCachedDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, CExtPubKey &xpub) const
Retrieve a cached xpub derived at an index.
Definition: descriptor.cpp:1240
Descriptor::IsSolvable
virtual bool IsSolvable() const =0
Whether this descriptor has all information about signing ignoring lack of private keys.
Vector
std::vector< typename std::common_type< Args... >::type > Vector(Args &&... args)
Construct a vector with the specified elements.
Definition: vector.h:20
vector.h
spanparsing::Expr
Span< const char > Expr(Span< const char > &sp)
Extract the expression that sp begins with.
Definition: spanparsing.cpp:32
Descriptor::IsRange
virtual bool IsRange() const =0
Whether the expansion of this descriptor depends on the position.
TxoutType::MULTISIG
@ MULTISIG
SigningProvider::GetKey
virtual bool GetKey(const CKeyID &address, CKey &key) const
Definition: signingprovider.h:25
Descriptor::ToPrivateString
virtual bool ToPrivateString(const SigningProvider &provider, std::string &out) const =0
Convert the descriptor to a private string.
CTxDestination
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:212
Descriptor
Interface for parsed descriptor objects.
Definition: descriptor.h:77
EncodeExtPubKey
std::string EncodeExtPubKey(const CExtPubKey &key)
Definition: key_io.cpp:207
MAX_SCRIPT_ELEMENT_SIZE
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition: script.h:23
script.h
KeyOriginInfo::path
std::vector< uint32_t > path
Definition: keyorigin.h:14
CPubKey::IsValid
bool IsValid() const
Definition: pubkey.h:184
Span::first
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
Definition: span.h:199
TxoutType::WITNESS_V0_KEYHASH
@ WITNESS_V0_KEYHASH
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:50
spanparsing
Definition: spanparsing.cpp:12
CExtPubKey::pubkey
CPubKey pubkey
Definition: pubkey.h:245
Descriptor::ToString
virtual std::string ToString() const =0
Convert the descriptor back to a string, undoing parsing.
ScriptContext::TOP
@ TOP
Top-level scriptPubKey.
WitnessV0ScriptHash
Definition: standard.h:163
InferDescriptor
std::unique_ptr< Descriptor > InferDescriptor(const CScript &script, const SigningProvider &provider)
Find a descriptor for the specified script, using information from provider where possible.
Definition: descriptor.cpp:1216
DescriptorCache::GetCachedDerivedExtPubKeys
const std::unordered_map< uint32_t, ExtPubKeyMap > GetCachedDerivedExtPubKeys() const
Retrieve all cached derived xpubs.
Definition: descriptor.cpp:1255
CExtKey::Neuter
CExtPubKey Neuter() const
Definition: key.cpp:312
CScriptID
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:86
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:594
KeyOriginInfo::fingerprint
unsigned char fingerprint[4]
First 32 bits of the Hash160 of the public key at the root of the path.
Definition: keyorigin.h:13
MakeSpan
constexpr Span< A > MakeSpan(A(&a)[N])
MakeSpan for arrays:
Definition: span.h:222
GetPubKey
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
Definition: sign.cpp:55
assert
assert(std::addressof(::ChainstateActive().CoinsTip())==std::addressof(coins_cache))
CExtPubKey
Definition: pubkey.h:240
CExtKey::key
CKey key
Definition: key.h:149
Span::end
constexpr C * end() const noexcept
Definition: span.h:171
ScriptHash
Definition: standard.h:150
ParseUInt32
bool ParseUInt32(const std::string &str, uint32_t *out)
Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
Definition: strencodings.cpp:351
base_blob::begin
unsigned char * begin()
Definition: uint256.h:58
EncodeDestination
std::string EncodeDestination(const CTxDestination &dest)
Definition: key_io.cpp:241
Descriptor::ExpandFromCache
virtual bool ExpandFromCache(int pos, const DescriptorCache &read_cache, std::vector< CScript > &output_scripts, FlatSigningProvider &out) const =0
Expand a descriptor at a specified position using cached expansion data.
KeyOriginInfo
Definition: keyorigin.h:11
FlatSigningProvider
Definition: signingprovider.h:47
descriptor.h
ctx
static secp256k1_context * ctx
Definition: tests.c:36
CPubKey::GetID
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:159
it
auto it
Definition: validation.cpp:399