i'm trying to understand how wallets remember a redeem script for p2sh/p2wsh for spending, after recovering from their seed, because the utxo set only has the hash in it, right? there are a nearly infinite number of script possibilities, aren't there? what do i miss?
1 Answer
It depends on what your scripts are. If you are for example talking about a single-sig wrapped segwit address, the redeemscript can be recalculated from the corresponding public key. The wallet would derive some number of keys from the backup, then derive the corresponding redeemscripts and addresses. As the wallet is scanning the blockchain, whenever it gets a hit for any of the addresses, it will backfill the wallet with more "unused addresses" beyond the used address. The count of the unused addresses to scan for is defined by something usually called the "gap limit".
To regenerate redeemscripts of multisig addresses, the backup needs to include the full set and order of the involved public keys! Even if you have a sufficient quorum of the keys to spend, you cannot craft a valid transaction without the remaining keys due to the missing redeemscript. Most wallets today are set up using hierarchical deterministic key derivation, so again, the wallet would be able to regenerate as many addresses as needed from the original tuple of extended pubkeys. Of course that also requires the backup to store which derivation path was used.
If you are talking about arbitrary scripts, the backup better have the full redeemscript, otherwise you'll likely be out of luck.