<p>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.</p>
<p>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.</p>
<p>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.</p>
<p>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.</p>

CC BY-SA 4.0
2021-01-12T22:16:11.250Z
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
CC BY-SA 4.0
2021-01-12T22:21:04.167Z
There are multiple approaches. Don't expect wallets to be interoperable, especially not when doing more complicated things than just single signatures.
Pieter Wuille
CC BY-SA 4.0
2021-01-12T21:42:28.963Z
2021-01-12T21:47:37.063Z
2021-01-12T21:47:37.063Z
Pieter Wuille
<p>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.</p>
<p>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.</p>

CC BY-SA 4.0
2021-01-12T20:24:07.420Z
2021-01-12T20:24:07.420Z
Sachin Meier
<p>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</p>

<bitcoin-core><wallet><wallet-recovery><p2sh>
How a new wallet knows balances locked in P2SH transactions?
DeLorean88
# How a new wallet knows balances locked in P2SH transactions?

(Asked by: username_0 on 2021-01-12)

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

**Tags:** bitcoin-core, wallet, wallet-recovery, p2sh


---

## 2 answers

### Answer by username_1 on 2021-01-12. Score: 2 | Accepted answer

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.

> **Comments:**
> **username_0**: Thanks for answering, username_1! 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?  
> **username_1**: There are multiple approaches. Don't expect wallets to be interoperable, especially not when doing more complicated things than just single signatures.  

---

### Answer by username_2 on 2021-01-12. Score: 1

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.


---

