@@ -895,55 +895,25 @@ bool wallet_add_onchaind_utxo(struct wallet *w,
895895bool wallet_can_spend (struct wallet * w , const u8 * script ,
896896 u32 * index )
897897{
898- struct ext_key ext ;
899898 u64 bip32_max_index ;
900- size_t script_len = tal_bytelen (script );
901- u32 i ;
902- bool output_is_p2sh ;
903-
904- /* If not one of these, can't be for us. */
905- if (is_p2sh (script , script_len , NULL ))
906- output_is_p2sh = true;
907- else if (is_p2wpkh (script , script_len , NULL ))
908- output_is_p2sh = false;
909- else if (is_p2tr (script , script_len , NULL ))
910- output_is_p2sh = false;
911- else
912- return false;
899+ const struct wallet_address * waddr ;
913900
901+ /* Update hash table if we need to */
914902 bip32_max_index = db_get_intvar (w -> db , "bip32_max_index" , 0 );
915- for (i = 0 ; i <= bip32_max_index + w -> keyscan_gap ; i ++ ) {
916- const u32 flags = BIP32_FLAG_KEY_PUBLIC | BIP32_FLAG_SKIP_HASH ;
917- u8 * s ;
903+ while (w -> our_addresses_maxindex < bip32_max_index + w -> keyscan_gap )
904+ our_addresses_add_for_index (w , ++ w -> our_addresses_maxindex );
918905
919- if (bip32_key_from_parent (w -> ld -> bip32_base , i ,
920- flags , & ext ) != WALLY_OK ) {
921- abort ();
922- }
923- s = scriptpubkey_p2wpkh_derkey (w , ext .pub_key );
924- if (output_is_p2sh ) {
925- u8 * p2sh = scriptpubkey_p2sh (w , s );
926- tal_free (s );
927- s = p2sh ;
928- }
929- if (!scripteq (s , script )) {
930- /* Try taproot output now */
931- tal_free (s );
932- s = scriptpubkey_p2tr_derkey (w , ext .pub_key );
933- if (!scripteq (s , script ))
934- s = tal_free (s );
935- }
936- tal_free (s );
937- if (s ) {
938- /* If we found a used key in the keyscan_gap we should
939- * remember that. */
940- if (i > bip32_max_index )
941- db_set_intvar (w -> db , "bip32_max_index" , i );
942- * index = i ;
943- return true;
944- }
945- }
946- return false;
906+ waddr = wallet_address_htable_get (w -> our_addresses , script );
907+ if (!waddr )
908+ return false;
909+
910+ /* If we found a used key in the keyscan_gap we should
911+ * remember that. */
912+ if (waddr -> index > bip32_max_index )
913+ db_set_intvar (w -> db , "bip32_max_index" , waddr -> index );
914+
915+ * index = waddr -> index ;
916+ return true;
947917}
948918
949919s64 wallet_get_newindex (struct lightningd * ld , enum addrtype addrtype )
0 commit comments