If my old wallet created and submitted a P2SH TX to the blockchain, and I migrate to a completely different wallet, how can the new wallet detect that that UTXO belongs to me? Since the script is hashed, it can be anything and contain unpredictable information, like other PKHs in multisig transactions


In short, it knows because it knows. Whatever information was used to make the original wallet construct the addresses (whether that consists of a seed, a set of keys, a master key, a descriptor, a mnemonic phrase, ...) must be imported into the new wallet too.

The P2SH redeemscript is not created randomly: there is a procedure to generate it from keys the wallet has, and those keys may themselves also be generated from such information. What that information is depends on the standards or schemes involved, so it's hard to be very specific here.

Deterministic wallets have been standard since 2013 or so, and pretty much all wallets have some means of deterministically constructing keys from some fixed immutable piece of data. For single key policies (simple wallets) a wallet also needs information to turn those keys into scripts/addresses. Common ways include P2PKH (address contains a hash of the public key), P2WPKH (similar, but uses segwit for spending), or P2SH-P2WPKH (where the address is a P2SH address containing the hash of a segwit program that contains the hash of the public key). The wallet needs to know which (possibly more than one of these) is used to convert the keys into scripts, and that implies also a way for computing the redeemscripts involved. Sometimes this choice is implicit in the standard being followed; sometimes it needs to be imported/chosen specifically.

For multisignature or other policies it is more complicated, as wallets also need to know how the other participants generate their keys, but this information is always needed - both in the first and the second wallet.

  • Thanks for answering, Pieter! I guess I was under the impression that all you need to access your funds is your mnemonic and then you can scan over deterministic paths and trivial spending scripts. But apparently, this is not the case if your funds are in multisig or other nontrivial script placed into P2SH UTXO. Need to be careful not create such "exotic" transactions in your wallet, to avoid loosing funds in case the wallet is lost. Or at least export and backup that extra information somehow, but is there a compatibility standard for that, so that wallets can be inter-operable? – DeLorean88 Jan 12 at 22:16
  • There are multiple approaches. Don't expect wallets to be interoperable, especially not when doing more complicated things than just single signatures. – Pieter Wuille Jan 12 at 22:21

P2SH addresses can either be P2SH-P2WPKH addresses (wrapped segwit) or they can be multisig or something else. If your wallet is using P2SH-P2WPKH addresses, the wallet should be able to recognize your address upon initialization. Otherwise, you will probably have to manually import that address.

Regardless, the amount of btc held in that address is on the blockchain, so your node or wallet will know that amount of bitcoin held in that address once you have imported (if necessary) the address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.