1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
crate::ix!();
///-----------------------
pub struct DescriptorScriptPubKeyMan {
base: ScriptPubKeyMan,
max_cached_index: i32, // default = -1
/**
| keeps track of whether Unlock has run
| a thorough check before
|
*/
decryption_thoroughly_checked: bool, // default = false
cs_desc_man: Arc<Mutex<DescriptorScriptPubkeyManInner>>,
}
pub struct DescriptorScriptPubkeyManInner {
map_script_pub_keys: DescriptorScriptPubkeyManScriptPubKeyMap,
map_pubkeys: DescriptorScriptPubkeyManPubKeyMap,
map_keys: DescriptorScriptPubkeyManKeyMap,
map_crypted_keys: DescriptorScriptPubkeyManCryptedKeyMap,
wallet_descriptor: WalletDescriptor,
}
/**
| Map of scripts to descriptor range index
|
*/
pub type DescriptorScriptPubkeyManScriptPubKeyMap = HashMap<Script,i32>;
/**
| Map of pubkeys involved in scripts to
| descriptor range index
|
*/
pub type DescriptorScriptPubkeyManPubKeyMap = HashMap<PubKey,i32>;
pub type DescriptorScriptPubkeyManCryptedKeyMap = HashMap<KeyID,(PubKey,Vec<u8>)>;
pub type DescriptorScriptPubkeyManKeyMap = HashMap<KeyID,Key>;
impl DescriptorScriptPubKeyMan {
pub fn new_with_descriptor(
storage: &mut dyn WalletStorage,
descriptor: &mut WalletDescriptor) -> Self {
todo!();
/*
: script_pub_key_man(storage),
: wallet_descriptor(descriptor),
*/
}
pub fn new<'a>(storage: &'a mut dyn WalletStorage) -> Self {
todo!();
/*
: script_pub_key_man(storage),
*/
}
/**
| Provide a descriptor at setup time
|
| Returns false if already setup or setup
| fails, true if setup is successful
|
*/
pub fn setup_descriptor(&mut self, desc: Box<dyn Descriptor>) -> bool {
todo!();
/*
*/
}
pub fn is_mine(&self, script: &Script) -> IsMineType {
todo!();
/*
LOCK(cs_desc_man);
if (m_map_script_pub_keys.count(script) > 0) {
return ISMINE_SPENDABLE;
}
return ISMINE_NO;
*/
}
pub fn check_decryption_key(&mut self,
master_key: &KeyingMaterial,
accept_no_keys: Option<bool>) -> bool {
let accept_no_keys: bool = accept_no_keys.unwrap_or(false);
todo!();
/*
LOCK(cs_desc_man);
if (!m_map_keys.empty()) {
return false;
}
bool keyPass = m_map_crypted_keys.empty(); // Always pass when there are no encrypted keys
bool keyFail = false;
for (const auto& mi : m_map_crypted_keys) {
const CPubKey &pubkey = mi.second.first;
const std::vector<unsigned char> &crypted_secret = mi.second.second;
CKey key;
if (!DecryptKey(master_key, crypted_secret, pubkey, key)) {
keyFail = true;
break;
}
keyPass = true;
if (m_decryption_thoroughly_checked)
break;
}
if (keyPass && keyFail) {
LogPrintf("The wallet is probably corrupted: Some keys decrypt but not all.\n");
throw std::runtime_error("Error unlocking wallet: some keys decrypt but not all. Your wallet file may be corrupt.");
}
if (keyFail || (!keyPass && !accept_no_keys)) {
return false;
}
m_decryption_thoroughly_checked = true;
return true;
*/
}
pub fn encrypt(&mut self,
master_key: &KeyingMaterial,
batch: *mut WalletBatch) -> bool {
todo!();
/*
LOCK(cs_desc_man);
if (!m_map_crypted_keys.empty()) {
return false;
}
for (const KeyMap::value_type& key_in : m_map_keys)
{
const CKey &key = key_in.second;
CPubKey pubkey = key.GetPubKey();
CKeyingMaterial secret(key.begin(), key.end());
std::vector<unsigned char> crypted_secret;
if (!EncryptSecret(master_key, secret, pubkey.GetHash(), crypted_secret)) {
return false;
}
m_map_crypted_keys[pubkey.GetID()] = make_pair(pubkey, crypted_secret);
batch->WriteCryptedDescriptorKey(GetID(), pubkey, crypted_secret);
}
m_map_keys.clear();
return true;
*/
}
pub fn get_reserved_destination(&mut self,
ty: OutputType,
internal: bool,
address: &mut TxDestination,
index: &mut i64,
keypool: &mut KeyPool,
error: &mut BilingualStr) -> bool {
todo!();
/*
LOCK(cs_desc_man);
bool result = GetNewDestination(type, address, error);
index = m_wallet_descriptor.next_index - 1;
return result;
*/
}
pub fn return_destination(&mut self,
index: i64,
internal: bool,
addr: &TxDestination) {
todo!();
/*
LOCK(cs_desc_man);
// Only return when the index was the most recent
if (m_wallet_descriptor.next_index - 1 == index) {
m_wallet_descriptor.next_index--;
}
WalletBatch(m_storage.GetDatabase()).WriteDescriptor(GetID(), m_wallet_descriptor);
NotifyCanGetAddressesChanged();
*/
}
#[EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man)]
pub fn get_keys(&self) -> HashMap<KeyID,Key> {
todo!();
/*
AssertLockHeld(cs_desc_man);
if (m_storage.HasEncryptionKeys() && !m_storage.IsLocked()) {
KeyMap keys;
for (auto key_pair : m_map_crypted_keys) {
const CPubKey& pubkey = key_pair.second.first;
const std::vector<unsigned char>& crypted_secret = key_pair.second.second;
CKey key;
DecryptKey(m_storage.GetEncryptionKey(), crypted_secret, pubkey, key);
keys[pubkey.GetID()] = key;
}
return keys;
}
return m_map_keys;
*/
}
/**
| Tops up the descriptor cache and
| m_map_script_pub_keys. The cache is stored
| in the wallet file and is used to expand
| the descriptor in
| GetNewDestination. DescriptorScriptPubKeyMan
| relies more on ephemeral data than
| LegacyScriptPubKeyMan. For wallets using
| unhardened derivation (with or without
| private keys), the "keypool" is a single
| xpub.
*/
pub fn top_up(&mut self, size: Option<u32>) -> bool {
let size: u32 = size.unwrap_or(0);
todo!();
/*
LOCK(cs_desc_man);
unsigned int target_size;
if (size > 0) {
target_size = size;
} else {
target_size = std::max(gArgs.GetIntArg("-keypool", DEFAULT_KEYPOOL_SIZE), (int64_t) 1);
}
// Calculate the new range_end
int32_t new_range_end = std::max(m_wallet_descriptor.next_index + (int32_t)target_size, m_wallet_descriptor.range_end);
// If the descriptor is not ranged, we actually just want to fill the first cache item
if (!m_wallet_descriptor.descriptor->IsRange()) {
new_range_end = 1;
m_wallet_descriptor.range_end = 1;
m_wallet_descriptor.range_start = 0;
}
FlatSigningProvider provider;
provider.keys = GetKeys();
WalletBatch batch(m_storage.GetDatabase());
uint256 id = GetID();
for (int32_t i = m_max_cached_index + 1; i < new_range_end; ++i) {
FlatSigningProvider out_keys;
std::vector<CScript> scripts_temp;
DescriptorCache temp_cache;
// Maybe we have a cached xpub and we can expand from the cache first
if (!m_wallet_descriptor.descriptor->ExpandFromCache(i, m_wallet_descriptor.cache, scripts_temp, out_keys)) {
if (!m_wallet_descriptor.descriptor->Expand(i, provider, scripts_temp, out_keys, &temp_cache)) return false;
}
// Add all of the scriptPubKeys to the scriptPubKey set
for (const CScript& script : scripts_temp) {
m_map_script_pub_keys[script] = i;
}
for (const auto& pk_pair : out_keys.pubkeys) {
const CPubKey& pubkey = pk_pair.second;
if (m_map_pubkeys.count(pubkey) != 0) {
// We don't need to give an error here.
// It doesn't matter which of many valid indexes the pubkey has, we just need an index where we can derive it and it's private key
continue;
}
m_map_pubkeys[pubkey] = i;
}
// Merge and write the cache
DescriptorCache new_items = m_wallet_descriptor.cache.MergeAndDiff(temp_cache);
if (!batch.WriteDescriptorCacheItems(id, new_items)) {
throw std::runtime_error(std::string(__func__) + ": writing cache items failed");
}
m_max_cached_index++;
}
m_wallet_descriptor.range_end = new_range_end;
batch.WriteDescriptor(GetID(), m_wallet_descriptor);
// By this point, the cache size should be the size of the entire range
assert(m_wallet_descriptor.range_end - 1 == m_max_cached_index);
NotifyCanGetAddressesChanged();
return true;
*/
}
pub fn mark_unused_addresses(&mut self, script: &Script) {
todo!();
/*
LOCK(cs_desc_man);
if (IsMine(script)) {
int32_t index = m_map_script_pub_keys[script];
if (index >= m_wallet_descriptor.next_index) {
WalletLogPrintf("%s: Detected a used keypool item at index %d, mark all keypool items up to this item as used\n", __func__, index);
m_wallet_descriptor.next_index = index + 1;
}
if (!TopUp()) {
WalletLogPrintf("%s: Topping up keypool failed (locked wallet)\n", __func__);
}
}
*/
}
pub fn add_descriptor_key(&mut self,
key: &Key,
pubkey: &PubKey) {
todo!();
/*
LOCK(cs_desc_man);
WalletBatch batch(m_storage.GetDatabase());
if (!AddDescriptorKeyWithDB(batch, key, pubkey)) {
throw std::runtime_error(std::string(__func__) + ": writing descriptor private key failed");
}
*/
}
#[EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man)]
pub fn add_descriptor_key_withdb(&mut self,
batch: &mut WalletBatch,
key: &Key,
pubkey: &PubKey) -> bool {
todo!();
/*
AssertLockHeld(cs_desc_man);
assert(!m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
// Check if provided key already exists
if (m_map_keys.find(pubkey.GetID()) != m_map_keys.end() ||
m_map_crypted_keys.find(pubkey.GetID()) != m_map_crypted_keys.end()) {
return true;
}
if (m_storage.HasEncryptionKeys()) {
if (m_storage.IsLocked()) {
return false;
}
std::vector<unsigned char> crypted_secret;
CKeyingMaterial secret(key.begin(), key.end());
if (!EncryptSecret(m_storage.GetEncryptionKey(), secret, pubkey.GetHash(), crypted_secret)) {
return false;
}
m_map_crypted_keys[pubkey.GetID()] = make_pair(pubkey, crypted_secret);
return batch.WriteCryptedDescriptorKey(GetID(), pubkey, crypted_secret);
} else {
m_map_keys[pubkey.GetID()] = key;
return batch.WriteDescriptorKey(GetID(), pubkey, key.GetPrivKey());
}
*/
}
/**
| Setup descriptors based on the given
| Extkey
|
*/
pub fn setup_descriptor_generation(&mut self,
master_key: &ExtKey,
addr_type: OutputType,
internal: bool) -> bool {
todo!();
/*
if (addr_type == OutputType::BECH32M) {
// Don't allow setting up taproot descriptors yet
// TODO: Allow setting up taproot descriptors
return false;
}
LOCK(cs_desc_man);
assert(m_storage.IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
// Ignore when there is already a descriptor
if (m_wallet_descriptor.descriptor) {
return false;
}
int64_t creation_time = GetTime();
std::string xpub = EncodeExtPubKey(master_key.Neuter());
// Build descriptor string
std::string desc_prefix;
std::string desc_suffix = "/\*)";
switch (addr_type) {
case OutputType::LEGACY: {
desc_prefix = "pkh(" + xpub + "/44'";
break;
}
case OutputType::P2SH_SEGWIT: {
desc_prefix = "sh(wpkh(" + xpub + "/49'";
desc_suffix += ")";
break;
}
case OutputType::BECH32: {
desc_prefix = "wpkh(" + xpub + "/84'";
break;
}
case OutputType::BECH32M: assert(false); // TODO: Setup taproot descriptor
} // no default case, so the compiler can warn about missing cases
assert(!desc_prefix.empty());
// Mainnet derives at 0', testnet and regtest derive at 1'
if (Params().IsTestChain()) {
desc_prefix += "/1'";
} else {
desc_prefix += "/0'";
}
std::string internal_path = internal ? "/1" : "/0";
std::string desc_str = desc_prefix + "/0'" + internal_path + desc_suffix;
// Make the descriptor
FlatSigningProvider keys;
std::string error;
std::unique_ptr<Descriptor> desc = Parse(desc_str, keys, error, false);
WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0);
m_wallet_descriptor = w_desc;
// Store the master private key, and descriptor
WalletBatch batch(m_storage.GetDatabase());
if (!AddDescriptorKeyWithDB(batch, master_key.key, master_key.key.GetPubKey())) {
throw std::runtime_error(std::string(__func__) + ": writing descriptor master private key failed");
}
if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) {
throw std::runtime_error(std::string(__func__) + ": writing descriptor failed");
}
// TopUp
TopUp();
m_storage.UnsetBlankWalletFlag(batch);
return true;
*/
}
pub fn is_hd_enabled(&self) -> bool {
todo!();
/*
LOCK(cs_desc_man);
return m_wallet_descriptor.descriptor->IsRange();
*/
}
pub fn can_get_addresses(&self, internal: Option<bool>) -> bool {
let internal: bool = internal.unwrap_or(false);
todo!();
/*
// We can only give out addresses from descriptors that are single type (not combo), ranged,
// and either have cached keys or can generate more keys (ignoring encryption)
LOCK(cs_desc_man);
return m_wallet_descriptor.descriptor->IsSingleType() &&
m_wallet_descriptor.descriptor->IsRange() &&
(HavePrivateKeys() || m_wallet_descriptor.next_index < m_wallet_descriptor.range_end);
*/
}
pub fn have_private_keys(&self) -> bool {
todo!();
/*
LOCK(cs_desc_man);
return m_map_keys.size() > 0 || m_map_crypted_keys.size() > 0;
*/
}
pub fn get_oldest_key_pool_time(&self) -> i64 {
todo!();
/*
// This is only used for getwalletinfo output and isn't relevant to descriptor wallets.
// The magic number 0 indicates that it shouldn't be displayed so that's what we return.
return 0;
*/
}
pub fn get_key_pool_size(&self) -> u32 {
todo!();
/*
LOCK(cs_desc_man);
return m_wallet_descriptor.range_end - m_wallet_descriptor.next_index;
*/
}
pub fn get_time_first_key(&self) -> i64 {
todo!();
/*
LOCK(cs_desc_man);
return m_wallet_descriptor.creation_time;
*/
}
/**
| Fetch the SigningProvider for the given
| script and optionally include private
| keys
|
*/
pub fn get_signing_provider_with_script(&self,
script: &Script,
include_private: Option<bool>) -> Box<FlatSigningProvider> {
let include_private: bool = include_private.unwrap_or(false);
todo!();
/*
LOCK(cs_desc_man);
// Find the index of the script
auto it = m_map_script_pub_keys.find(script);
if (it == m_map_script_pub_keys.end()) {
return nullptr;
}
int32_t index = it->second;
return GetSigningProvider(index, include_private);
*/
}
/**
| Fetch the SigningProvider for the given
| pubkey and always include private keys.
| This should only be called by signing
| code.
|
*/
pub fn get_signing_provider_with_pubkey(&self, pubkey: &PubKey) -> Box<FlatSigningProvider> {
todo!();
/*
LOCK(cs_desc_man);
// Find index of the pubkey
auto it = m_map_pubkeys.find(pubkey);
if (it == m_map_pubkeys.end()) {
return nullptr;
}
int32_t index = it->second;
// Always try to get the signing provider with private keys. This function should only be called during signing anyways
return GetSigningProvider(index, true);
*/
}
/**
| Fetch the SigningProvider for a given index
| and optionally include private keys. Called
| by the above functions.
|
*/
#[EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man)]
pub fn get_signing_provider_with_index(&self,
index: i32,
include_private: Option<bool>) -> Box<FlatSigningProvider> {
let include_private: bool = include_private.unwrap_or(false);
todo!();
/*
AssertLockHeld(cs_desc_man);
// Get the scripts, keys, and key origins for this script
std::unique_ptr<FlatSigningProvider> out_keys = std::make_unique<FlatSigningProvider>();
std::vector<CScript> scripts_temp;
if (!m_wallet_descriptor.descriptor->ExpandFromCache(index, m_wallet_descriptor.cache, scripts_temp, *out_keys)) return nullptr;
if (HavePrivateKeys() && include_private) {
FlatSigningProvider master_provider;
master_provider.keys = GetKeys();
m_wallet_descriptor.descriptor->ExpandPrivate(index, master_provider, *out_keys);
}
return out_keys;
*/
}
pub fn get_solving_provider(&self, script: &Script) -> Box<SigningProvider> {
todo!();
/*
return GetSigningProvider(script, false);
*/
}
pub fn can_provide(&mut self,
script: &Script,
sigdata: &mut SignatureData) -> bool {
todo!();
/*
return IsMine(script);
*/
}
pub fn sign_transaction(&self,
tx: &mut MutableTransaction,
coins: &HashMap<OutPoint,Coin>,
sighash: i32,
input_errors: &mut HashMap<i32,BilingualStr>) -> bool {
todo!();
/*
std::unique_ptr<FlatSigningProvider> keys = std::make_unique<FlatSigningProvider>();
for (const auto& coin_pair : coins) {
std::unique_ptr<FlatSigningProvider> coin_keys = GetSigningProvider(coin_pair.second.out.scriptPubKey, true);
if (!coin_keys) {
continue;
}
*keys = Merge(*keys, *coin_keys);
}
return ::SignTransaction(tx, keys.get(), coins, sighash, input_errors);
*/
}
pub fn sign_message(&self,
message: &String,
pkhash: &PKHash,
str_sig: &mut String) -> SigningResult {
todo!();
/*
std::unique_ptr<FlatSigningProvider> keys = GetSigningProvider(GetScriptForDestination(pkhash), true);
if (!keys) {
return SigningResult::PRIVATE_KEY_NOT_AVAILABLE;
}
CKey key;
if (!keys->GetKey(ToKeyID(pkhash), key)) {
return SigningResult::PRIVATE_KEY_NOT_AVAILABLE;
}
if (!MessageSign(key, message, str_sig)) {
return SigningResult::SIGNING_FAILED;
}
return SigningResult::OK;
*/
}
pub fn fillpsbt(&self,
psbtx: &mut PartiallySignedTransaction,
txdata: &PrecomputedTransactionData,
sighash_type: Option<i32>,
sign: Option<bool>,
bip_32derivs: Option<bool>,
n_signed: *mut i32) -> TransactionError {
let sighash_type: i32 = sighash_type.unwrap_or(1 );//SIGHASH_ALL
let sign: bool = sign.unwrap_or(true);
let bip_32derivs: bool = bip_32derivs.unwrap_or(false);
todo!();
/*
if (n_signed) {
*n_signed = 0;
}
for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
const CTxIn& txin = psbtx.tx->vin[i];
PSBTInput& input = psbtx.inputs.at(i);
if (PSBTInputSigned(input)) {
continue;
}
// Get the Sighash type
if (sign && input.sighash_type > 0 && input.sighash_type != sighash_type) {
return TransactionError::SIGHASH_MISMATCH;
}
// Get the scriptPubKey to know which SigningProvider to use
CScript script;
if (!input.witness_utxo.IsNull()) {
script = input.witness_utxo.scriptPubKey;
} else if (input.non_witness_utxo) {
if (txin.prevout.n >= input.non_witness_utxo->vout.size()) {
return TransactionError::MISSING_INPUTS;
}
script = input.non_witness_utxo->vout[txin.prevout.n].scriptPubKey;
} else {
// There's no UTXO so we can just skip this now
continue;
}
SignatureData sigdata;
input.FillSignatureData(sigdata);
std::unique_ptr<FlatSigningProvider> keys = std::make_unique<FlatSigningProvider>();
std::unique_ptr<FlatSigningProvider> script_keys = GetSigningProvider(script, sign);
if (script_keys) {
*keys = Merge(*keys, *script_keys);
} else {
// Maybe there are pubkeys listed that we can sign for
script_keys = std::make_unique<FlatSigningProvider>();
for (const auto& pk_pair : input.hd_keypaths) {
const CPubKey& pubkey = pk_pair.first;
std::unique_ptr<FlatSigningProvider> pk_keys = GetSigningProvider(pubkey);
if (pk_keys) {
*keys = Merge(*keys, *pk_keys);
}
}
}
SignPSBTInput(HidingSigningProvider(keys.get(), !sign, !bip32derivs), psbtx, i, &txdata, sighash_type);
bool signed_one = PSBTInputSigned(input);
if (n_signed && (signed_one || !sign)) {
// If sign is false, we assume that we _could_ sign if we get here. This
// will never have false negatives; it is hard to tell under what i
// circumstances it could have false positives.
(*n_signed)++;
}
}
// Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change
for (unsigned int i = 0; i < psbtx.tx->vout.size(); ++i) {
std::unique_ptr<SigningProvider> keys = GetSolvingProvider(psbtx.tx->vout.at(i).scriptPubKey);
if (!keys) {
continue;
}
UpdatePSBTOutput(HidingSigningProvider(keys.get(), true, !bip32derivs), psbtx, i);
}
return TransactionError::OK;
*/
}
pub fn get_metadata(&self, dest: &TxDestination) -> Box<KeyMetadata> {
todo!();
/*
std::unique_ptr<SigningProvider> provider = GetSigningProvider(GetScriptForDestination(dest));
if (provider) {
KeyOriginInfo orig;
CKeyID key_id = GetKeyForDestination(*provider, dest);
if (provider->GetKeyOrigin(key_id, orig)) {
LOCK(cs_desc_man);
std::unique_ptr<CKeyMetadata> meta = std::make_unique<CKeyMetadata>();
meta->key_origin = orig;
meta->has_key_origin = true;
meta->nCreateTime = m_wallet_descriptor.creation_time;
return meta;
}
}
return nullptr;
*/
}
pub fn getid(&self) -> u256 {
todo!();
/*
LOCK(cs_desc_man);
std::string desc_str = m_wallet_descriptor.descriptor->ToString();
uint256 id;
CSHA256().Write((unsigned char*)desc_str.data(), desc_str.size()).Finalize(id.begin());
return id;
*/
}
pub fn set_cache(&mut self, cache: &DescriptorCache) {
todo!();
/*
LOCK(cs_desc_man);
m_wallet_descriptor.cache = cache;
for (int32_t i = m_wallet_descriptor.range_start; i < m_wallet_descriptor.range_end; ++i) {
FlatSigningProvider out_keys;
std::vector<CScript> scripts_temp;
if (!m_wallet_descriptor.descriptor->ExpandFromCache(i, m_wallet_descriptor.cache, scripts_temp, out_keys)) {
throw std::runtime_error("Error: Unable to expand wallet descriptor from cache");
}
// Add all of the scriptPubKeys to the scriptPubKey set
for (const CScript& script : scripts_temp) {
if (m_map_script_pub_keys.count(script) != 0) {
throw std::runtime_error(strprintf("Error: Already loaded script at index %d as being at index %d", i, m_map_script_pub_keys[script]));
}
m_map_script_pub_keys[script] = i;
}
for (const auto& pk_pair : out_keys.pubkeys) {
const CPubKey& pubkey = pk_pair.second;
if (m_map_pubkeys.count(pubkey) != 0) {
// We don't need to give an error here.
// It doesn't matter which of many valid indexes the pubkey has, we just need an index where we can derive it and it's private key
continue;
}
m_map_pubkeys[pubkey] = i;
}
m_max_cached_index++;
}
*/
}
pub fn add_key(&mut self,
key_id: &KeyID,
key: &Key) -> bool {
todo!();
/*
LOCK(cs_desc_man);
m_map_keys[key_id] = key;
return true;
*/
}
pub fn add_crypted_key(&mut self,
key_id: &KeyID,
pubkey: &PubKey,
crypted_key: &Vec<u8>) -> bool {
todo!();
/*
LOCK(cs_desc_man);
if (!m_map_keys.empty()) {
return false;
}
m_map_crypted_keys[key_id] = make_pair(pubkey, crypted_key);
return true;
*/
}
pub fn has_wallet_descriptor(&self, desc: &WalletDescriptor) -> bool {
todo!();
/*
LOCK(cs_desc_man);
return m_wallet_descriptor.descriptor != nullptr && desc.descriptor != nullptr && m_wallet_descriptor.descriptor->ToString() == desc.descriptor->ToString();
*/
}
pub fn write_descriptor(&mut self) {
todo!();
/*
LOCK(cs_desc_man);
WalletBatch batch(m_storage.GetDatabase());
if (!batch.WriteDescriptor(GetID(), m_wallet_descriptor)) {
throw std::runtime_error(std::string(__func__) + ": writing descriptor failed");
}
*/
}
#[EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man)]
pub fn get_wallet_descriptor(&self) -> WalletDescriptor {
todo!();
/*
return m_wallet_descriptor;
*/
}
pub fn get_script_pub_keys(&self) -> Vec<Script> {
todo!();
/*
LOCK(cs_desc_man);
std::vector<CScript> script_pub_keys;
script_pub_keys.reserve(m_map_script_pub_keys.size());
for (auto const& script_pub_key: m_map_script_pub_keys) {
script_pub_keys.push_back(script_pub_key.first);
}
return script_pub_keys;
*/
}
pub fn get_descriptor_string(&self,
out: &mut String,
priv_: bool) -> bool {
todo!();
/*
LOCK(cs_desc_man);
FlatSigningProvider provider;
provider.keys = GetKeys();
if (priv) {
// For the private version, always return the master key to avoid
// exposing child private keys. The risk implications of exposing child
// private keys together with the parent xpub may be non-obvious for users.
return m_wallet_descriptor.descriptor->ToPrivateString(provider, out);
}
return m_wallet_descriptor.descriptor->ToNormalizedString(provider, out, &m_wallet_descriptor.cache);
*/
}
pub fn upgrade_descriptor_cache(&mut self) {
todo!();
/*
LOCK(cs_desc_man);
if (m_storage.IsLocked() || m_storage.IsWalletFlagSet(WALLET_FLAG_LAST_HARDENED_XPUB_CACHED)) {
return;
}
// Skip if we have the last hardened xpub cache
if (m_wallet_descriptor.cache.GetCachedLastHardenedExtPubKeys().size() > 0) {
return;
}
// Expand the descriptor
FlatSigningProvider provider;
provider.keys = GetKeys();
FlatSigningProvider out_keys;
std::vector<CScript> scripts_temp;
DescriptorCache temp_cache;
if (!m_wallet_descriptor.descriptor->Expand(0, provider, scripts_temp, out_keys, &temp_cache)){
throw std::runtime_error("Unable to expand descriptor");
}
// Cache the last hardened xpubs
DescriptorCache diff = m_wallet_descriptor.cache.MergeAndDiff(temp_cache);
if (!WalletBatch(m_storage.GetDatabase()).WriteDescriptorCacheItems(GetID(), diff)) {
throw std::runtime_error(std::string(__func__) + ": writing cache items failed");
}
*/
}
pub fn update_wallet_descriptor(&mut self, descriptor: &mut WalletDescriptor) {
todo!();
/*
LOCK(cs_desc_man);
std::string error;
if (!CanUpdateToWalletDescriptor(descriptor, error)) {
throw std::runtime_error(std::string(__func__) + ": " + error);
}
m_map_pubkeys.clear();
m_map_script_pub_keys.clear();
m_max_cached_index = -1;
m_wallet_descriptor = descriptor;
*/
}
pub fn can_update_to_wallet_descriptor(&mut self,
descriptor: &WalletDescriptor,
error: &mut String) -> bool {
todo!();
/*
LOCK(cs_desc_man);
if (!HasWalletDescriptor(descriptor)) {
error = "can only update matching descriptor";
return false;
}
if (descriptor.range_start > m_wallet_descriptor.range_start ||
descriptor.range_end < m_wallet_descriptor.range_end) {
// Use inclusive range for error
error = strprintf("new range must include current range = [%d,%d]",
m_wallet_descriptor.range_start,
m_wallet_descriptor.range_end - 1);
return false;
}
return true;
*/
}
}
impl GetNewDestination for DescriptorScriptPubKeyMan {
fn get_new_destination(&mut self,
ty: OutputType,
dest: &mut TxDestination,
error: &mut BilingualStr) -> bool {
todo!();
/*
// Returns true if this descriptor supports getting new addresses. Conditions where we may be unable to fetch them (e.g. locked) are caught later
if (!CanGetAddresses()) {
error = _("No addresses available");
return false;
}
{
LOCK(cs_desc_man);
assert(m_wallet_descriptor.descriptor->IsSingleType()); // This is a combo descriptor which should not be an active descriptor
std::optional<OutputType> desc_addr_type = m_wallet_descriptor.descriptor->GetOutputType();
assert(desc_addr_type);
if (type != *desc_addr_type) {
throw std::runtime_error(std::string(__func__) + ": Types are inconsistent");
}
TopUp();
// Get the scriptPubKey from the descriptor
FlatSigningProvider out_keys;
std::vector<CScript> scripts_temp;
if (m_wallet_descriptor.range_end <= m_max_cached_index && !TopUp(1)) {
// We can't generate anymore keys
error = _("Error: Keypool ran out, please call keypoolrefill first");
return false;
}
if (!m_wallet_descriptor.descriptor->ExpandFromCache(m_wallet_descriptor.next_index, m_wallet_descriptor.cache, scripts_temp, out_keys)) {
// We can't generate anymore keys
error = _("Error: Keypool ran out, please call keypoolrefill first");
return false;
}
std::optional<OutputType> out_script_type = m_wallet_descriptor.descriptor->GetOutputType();
if (out_script_type && out_script_type == type) {
ExtractDestination(scripts_temp[0], dest);
} else {
throw std::runtime_error(std::string(__func__) + ": Types are inconsistent. Stored type does not match type of newly generated address");
}
m_wallet_descriptor.next_index++;
WalletBatch(m_storage.GetDatabase()).WriteDescriptor(GetID(), m_wallet_descriptor);
return true;
}
*/
}
}