<h2>Instructions for Bitcoin Core <code>23.0</code></h2>
<p>You will need:</p>
<ul>
<li>the private key in WIF format</li>
<li>to know the context in which this key was used (for what type of scripts)</li>
<li>optionally, the approximate date at which you started using this key</li>
</ul>
<h3>Rescan the block chain for transactions</h3>
<p>Let's take the OP's information. The private key is <code>5Kkzs8XrJNAmf9VQDFeGBfaRvSByAvPK6DbDXw5BVqswWaXSG2Y</code>, and it was used for native segwit v0 scripts (&quot;bech32 addresses&quot;). Since no date were provided, we'll use the first Segwit block as the birthdate (block height <code>481824</code>).</p>
<p>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 <strong>not necessary for any key</strong>, just in the specific case of the key provided by the OP.
A simple way to achieve this is by using Python:</p>
<pre><code>$ 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(&quot;5Kkzs8XrJNAmf9VQDFeGBfaRvSByAvPK6DbDXw5BVqswWaXSG2Y&quot;) + b&quot;\x01&quot;))'
b'L5oCQGFAbeS87NUMT5dU25GYzrBQvndUr2a2KXfTLJjqbX5MuJow'
</code></pre>
<p>Start by creating a new wallet [0]:</p>
<pre><code>$ bitcoin-cli createwallet &quot;recovery&quot;
{                                                                   
  &quot;name&quot;: &quot;recovery&quot;,
  &quot;warning&quot;: &quot;&quot;     
}
</code></pre>
<p>Then we'll import the Output Script descriptor. In your case it's <code>wpkh(L5oCQGFAbeS87NUMT5dU25GYzrBQvndUr2a2KXfTLJjqbX5MuJow)</code> [1]. We'll need to get the checksum for your descriptor first as it is required by the next RPC:</p>
<pre><code>$ bitcoin-cli getdescriptorinfo getdescriptorinfo &quot;wpkh(L5oCQGFAbeS87NUMT5dU25GYzrBQvndUr2a2KXfTLJjqbX5MuJow)&quot;
{
  &quot;descriptor&quot;: &quot;wpkh(038bfdd30b45815c08876cea5e52781e18cbfa7f7155e9704a97f1b8fd5bb925d2)#3s232wd0&quot;,
  &quot;checksum&quot;: &quot;507weecs&quot;,
  &quot;isrange&quot;: false,
  &quot;issolvable&quot;: true,
  &quot;hasprivatekeys&quot;: true
}
</code></pre>
<p>Attach the checksum to the descriptor and import it to the wallet:</p>
<pre><code>$ bitcoin-cli -rpcwallet=recovery importdescriptors '[{&quot;desc&quot;:&quot;wpkh(L5oCQGFAbeS87NUMT5dU25GYzrBQvndUr2a2KXfTLJjqbX5MuJow)#507weecs&quot;,&quot;timestamp&quot;:&quot;now&quot;}]'                                                                                
[
  {
    &quot;success&quot;: true
  }
]
</code></pre>
<p>Finally, rescan the blockchain from the first Segwit block up to the current one:</p>
<pre><code>$ bitcoin-cli rescanblockchain 481824 $(bitcoin-cli getblockcount)
</code></pre>
<p>Note you can combine those 2 commands by passing a timestamp other than <code>now</code> to <code>importdescriptors</code>.</p>
<p>You may now check your balance using the <code>getbalance</code> call and sweep your funds using <code>sendtoaddress</code>.</p>
<h3>Check if there are any unspent coins beforehand</h3>
<p>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:</p>
<pre><code>$ bitcoin-cli scantxoutset start '[&quot;wpkh(L5oCQGFAbeS87NUMT5dU25GYzrBQvndUr2a2KXfTLJjqbX5MuJow)#507weecs&quot;]'
{
  &quot;success&quot;: true,
  &quot;txouts&quot;: 82495790,
  &quot;height&quot;: 741163,
  &quot;bestblock&quot;: &quot;0000000000000000000373c1925852f896f04f6c762ed92341b5a62e71889db7&quot;,
  &quot;unspents&quot;: [
  ],
  &quot;total_amount&quot;: 0.00000000
}
</code></pre>
<p>A quicker check that does not require an unpruned node</p>
<hr />
<p>[0] If you are using <code>bitcoind</code> version <code>22.0</code> you will have to manually set the <code>descriptors</code> parameter to <code>true</code> there.</p>
<p>[1] If not 100% sure the key was not used in other scripts, one may use the <code>combo()</code> descriptor instead of <code>wpkh()</code> to scan the most common script types using this key.</p>

CC BY-SA 4.0
2022-06-17T13:51:17.387Z
2022-06-17T14:08:29.240Z
2022-06-17T14:08:29.240Z
Antoine Poinsot
<p>Use Electrum 3.1.1 and prepend "p2wpkh:" to the private key before importing or sweeping it. For example:</p>

<p><code>p2wpkh:5Kkzs8XrJNAmf9VQDFeGBfaRvSByAvPK6DbDXw5BVqswWaXSG2Y</code></p>

<p><a href="https://i.sstatic.net/NeQOr.png" rel="noreferrer"><img src="https://i.sstatic.net/NeQOr.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2018-03-24T04:23:28.013Z
how do you output the segwit address just to make sure it matches that private key?
Patoshi パトシ
CC BY-SA 3.0
2018-03-24T04:28:58.410Z
`bitcoin.address_from_private_key("p2wpkh:5Kkzs8XrJNAmf9VQDFeGBfaRvSByAvPK6DbDXw5BVqswWaXSG2Y")` in the console tab of electrum (view menu> show console if you can't see that tab)
Abdussamad
CC BY-SA 3.0
2018-03-26T16:19:59.400Z
thanks! I'm curious to how did you even figure this out to put "p2wpkh:" in front of the address?
Patoshi パトシ
CC BY-SA 3.0
2018-03-27T14:38:56.370Z
@Patoshiパトシ It's in the release notes for 3.1: https://github.com/spesmilo/electrum/blob/master/RELEASE-NOTES#L72
Abdussamad
CC BY-SA 3.0
2018-03-27T18:32:22.047Z
thanks. I'm trying to output the 3xxx address and tried p2sh:5Kkzs8XrJNAmf9VQDFe.... that doesnt work. what is the prefix for that?
Patoshi パトシ
CC BY-SA 3.0
2018-03-28T08:22:45.743Z
use "p2wpkh-p2sh:"
Abdussamad
CC BY-SA 3.0
2018-03-24T04:20:42.147Z
2018-03-31T21:08:29.090Z
2018-03-31T21:08:29.090Z
Abdussamad
<p>I'm testing segwit bech32 addresses and generated the following using bitcoinjs:</p>

<p>address: <strong>bc1qc7u5njf4nf2rj3zys642mfc7nct6kdven8j4e9</strong></p>

<p>private key: <strong>5Kkzs8XrJNAmf9VQDFeGBfaRvSByAvPK6DbDXw5BVqswWaXSG2Y</strong></p>

<p>When I import this private key into electrum 3.0.5, it imports it as the following address: <strong>1KD3SE71kGAcxw57BvcmUzsHwx3QxdPBW7</strong></p>

<p>How do I go about sweeping funds from the bc1... address if electrum 3.0.5 sees the legacy format address instead? </p>

CC BY-SA 4.0
2022-06-02T09:05:48.210Z
I extracted the private key (bigInt) and then converted to bech32. I'm getting `bc1qc9k7798hapdv5qkqdq3wxf2phlyfsktkxrcyed`, which does not match what you posted. Can anyone else confirm?
Jus12
CC BY-SA 4.0
2022-06-02T09:55:04.757Z
It indeed looks like a bug in bitcoinjs or perhaps you made a mistake in posting. The bech32 address for the given private key as per https://secretscan.org/Bech32 is also what I posted above.
Jus12
<private-key><electrum><segregated-witness><address-generation><bitcoinjs>
How to sweep a segwit bech32 private key?
Patoshi パトシ
# How to sweep a segwit bech32 private key?

(Asked by: username_0 on 2018-03-23)

I'm testing segwit bech32 addresses and generated the following using bitcoinjs:

address: **bc1qc7u5njf4nf2rj3zys642mfc7nct6kdven8j4e9**

private key: **5Kkzs8XrJNAmf9VQDFeGBfaRvSByAvPK6DbDXw5BVqswWaXSG2Y**

When I import this private key into electrum 3.0.5, it imports it as the following address: **1KD3SE71kGAcxw57BvcmUzsHwx3QxdPBW7**

How do I go about sweeping funds from the bc1... address if electrum 3.0.5 sees the legacy format address instead?

**Tags:** private-key, electrum, segregated-witness, address-generation, bitcoinjs

> **Comments:**
> **username_1**: I extracted the private key (bigInt) and then converted to bech32. I'm getting `bc1qc9k7798hapdv5qkqdq3wxf2phlyfsktkxrcyed`, which does not match what you posted. Can anyone else confirm?  
> **username_1**: It indeed looks like a bug in bitcoinjs or perhaps you made a mistake in posting. The bech32 address for the given private key as per https://secretscan.org/Bech32 is also what I posted above.  

---

## 2 answers

### Answer by username_3 on 2018-03-24. Score: 9 | Accepted answer

Use Electrum 3.1.1 and prepend "p2wpkh:" to the private key before importing or sweeping it. For example:

`p2wpkh:5Kkzs8XrJNAmf9VQDFeGBfaRvSByAvPK6DbDXw5BVqswWaXSG2Y`

[![enter image description here](https://i.sstatic.net/NeQOr.png)](https://i.sstatic.net/NeQOr.png)

> **Comments:**
> **username_0**: how do you output the segwit address just to make sure it matches that private key?  
> **username_3**: `bitcoin.address_from_private_key("p2wpkh:5Kkzs8XrJNAmf9VQDFeGBfaRvSByAvPK6DbDXw5BVqswWaXSG2Y")` in the console tab of electrum (view menu> show console if you can't see that tab)  
> **username_0**: thanks! I'm curious to how did you even figure this out to put "p2wpkh:" in front of the address?  
> **username_3**: @username_0 It's in the release notes for 3.1: https://github.com/spesmilo/electrum/blob/master/RELEASE-NOTES#L72  
> **username_0**: thanks. I'm trying to output the 3xxx address and tried p2sh:5Kkzs8XrJNAmf9VQDFe.... that doesnt work. what is the prefix for that?  
> **username_3**: use "p2wpkh-p2sh:"  

---

### Answer by username_2 on 2022-06-17. Score: 1

## Instructions for Bitcoin Core `23.0`

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.


---

