| 1024 | } |
| 1025 | |
| 1026 | bool wallet_can_spend(struct wallet *w, const u8 *script, size_t script_len, |
| 1027 | u32 *index, enum addrtype *addrtype) |
| 1028 | { |
| 1029 | u64 bip32_max_index, bip86_max_index; |
| 1030 | const struct wallet_address *waddr; |
| 1031 | struct script_with_len scriptwl = {script, script_len}; |
| 1032 | |
| 1033 | /* Update hash table if we need to */ |
| 1034 | bip32_max_index = db_get_intvar(w->db, "bip32_max_index", 0); |
| 1035 | bip86_max_index = db_get_intvar(w->db, "bip86_max_index", 0); |
| 1036 | |
| 1037 | /* Scan both BIP32 and BIP86 addresses */ |
| 1038 | u64 max_index = (bip32_max_index > bip86_max_index) ? bip32_max_index : bip86_max_index; |
| 1039 | while (w->our_addresses_maxindex < max_index + w->keyscan_gap) |
| 1040 | our_addresses_add_for_index(w, ++w->our_addresses_maxindex); |
| 1041 | |
| 1042 | waddr = wallet_address_htable_get(w->our_addresses, &scriptwl); |
| 1043 | if (!waddr) |
| 1044 | return false; |
| 1045 | |
| 1046 | /* If we found a used key in the keyscan_gap we should |
| 1047 | * remember that. */ |
| 1048 | if (w->ld->bip86_base) { |
| 1049 | /* BIP86-based wallet: all addresses use BIP86 derivation */ |
| 1050 | if (waddr->index > bip86_max_index) |
| 1051 | db_set_intvar(w->db, "bip86_max_index", waddr->index); |
| 1052 | } else { |
| 1053 | /* Legacy wallet: all addresses use BIP32 derivation */ |
| 1054 | if (waddr->index > bip32_max_index) |
| 1055 | db_set_intvar(w->db, "bip32_max_index", waddr->index); |
| 1056 | } |
| 1057 | |
| 1058 | *index = waddr->index; |
| 1059 | if (addrtype) |
| 1060 | *addrtype = waddr->addrtype; |
| 1061 | return true; |
| 1062 | } |
| 1063 | |
| 1064 | s64 wallet_get_newindex(struct lightningd *ld, enum addrtype addrtype) |
| 1065 | { |
no test coverage detected