MultiversX Tracker is Live!

How to sweep a segwit bech32 private key?

Bitcoin Stack Exchange

Bitcoin News / Bitcoin Stack Exchange 156 Views

You will need:

  • the private key in WIF format
  • to know the context in which this key was used (for what type of scripts)
  • optionally, the approximate date at which you started using this key

Rescan the block chain for transactions

Let's take the OP's information. The private key is 5Kkzs8XrJNAmf9VQDFeGBfaRvSByAvPK6DbDXw5BVqswWaXSG2Y, and it was used for native segwit v0 scripts ("bech32 addresses"). Since no date were provided, we'll use the first Segwit block as the birthdate (block height 481824).

This WIF private key is not compressed. We need to mark it as such for Bitcoin Core to accept it in a Segwit Script. Note this is not necessary for any key, just in the specific case of the key provided by the OP. A simple way to achieve this is by using Python:

$ python3 -m venv venv
$ . venv/bin/activate
$ pip install base58 # Caution! Make sure this library hasn't been tampered with!
$ python3 -c 'import base58;print(base58.b58encode_check(base58.b58decode_check("5Kkzs8XrJNAmf9VQDFeGBfaRvSByAvPK6DbDXw5BVqswWaXSG2Y") + b"\x01"))'
b'L5oCQGFAbeS87NUMT5dU25GYzrBQvndUr2a2KXfTLJjqbX5MuJow'

Start by creating a new wallet [0]:

$ bitcoin-cli createwallet "recovery"
{ "name": "recovery", "warning": ""
}

Then we'll import the Output Script descriptor. In your case it's wpkh(L5oCQGFAbeS87NUMT5dU25GYzrBQvndUr2a2KXfTLJjqbX5MuJow) [1]. We'll need to get the checksum for your descriptor first as it is required by the next RPC:

$ bitcoin-cli getdescriptorinfo getdescriptorinfo "wpkh(L5oCQGFAbeS87NUMT5dU25GYzrBQvndUr2a2KXfTLJjqbX5MuJow)"
{ "descriptor": "wpkh(038bfdd30b45815c08876cea5e52781e18cbfa7f7155e9704a97f1b8fd5bb925d2)#3s232wd0", "checksum": "507weecs", "isrange": false, "issolvable": true, "hasprivatekeys": true
}

Attach the checksum to the descriptor and import it to the wallet:

$ bitcoin-cli -rpcwallet=recovery importdescriptors '[{"desc":"wpkh(L5oCQGFAbeS87NUMT5dU25GYzrBQvndUr2a2KXfTLJjqbX5MuJow)#507weecs","timestamp":"now"}]'
[ { "success": true }
]

Finally, rescan the blockchain from the first Segwit block up to the current one:

$ bitcoin-cli rescanblockchain 481824 $(bitcoin-cli getblockcount)

Note you can combine those 2 commands by passing a timestamp other than now to importdescriptors.

You may now check your balance using the getbalance call and sweep your funds using sendtoaddress.

Check if there are any unspent coins beforehand

Before going through the longer process of rescanning the chain for transactions, you may rescan the UTxO set (you can do that with a pruned node too) to make sure there are unspent coins to be recovered:

$ bitcoin-cli scantxoutset start '["wpkh(L5oCQGFAbeS87NUMT5dU25GYzrBQvndUr2a2KXfTLJjqbX5MuJow)#507weecs"]'
{ "success": true, "txouts": 82495790, "height": 741163, "bestblock": "0000000000000000000373c1925852f896f04f6c762ed92341b5a62e71889db7", "unspents": [ ], "total_amount": 0.00000000
}

A quicker check that does not require an unpruned node


[0] If you are using bitcoind version 22.0 you will have to manually set the descriptors parameter to true there.

[1] If not 100% sure the key was not used in other scripts, one may use the combo() descriptor instead of wpkh() to scan the most common script types using this key.


Get BONUS $200 for FREE!

You can get bonuses upto $100 FREE BONUS when you:
πŸ’° Install these recommended apps:
πŸ’² SocialGood - 100% Crypto Back on Everyday Shopping
πŸ’² xPortal - The DeFi For The Next Billion
πŸ’² CryptoTab Browser - Lightweight, fast, and ready to mine!
πŸ’° Register on these recommended exchanges:
🟑 Binance🟑 Bitfinex🟑 Bitmart🟑 Bittrex🟑 Bitget
🟑 CoinEx🟑 Crypto.com🟑 Gate.io🟑 Huobi🟑 Kucoin.



Comments