3

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?

2
  • 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
    Jun 2 at 9:05
  • 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 secretscan.org/Bech32 is also what I posted above.
    – Jus12
    Jun 2 at 9:55

2 Answers 2

7

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

6
  • how do you output the segwit address just to make sure it matches that private key? Mar 24, 2018 at 4:23
  • 1
    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
    Mar 24, 2018 at 4:28
  • thanks! I'm curious to how did you even figure this out to put "p2wpkh:" in front of the address? Mar 26, 2018 at 16:19
  • @Patoshiパトシ It's in the release notes for 3.1: github.com/spesmilo/electrum/blob/master/RELEASE-NOTES#L72
    – Abdussamad
    Mar 27, 2018 at 14:38
  • thanks. I'm trying to output the 3xxx address and tried p2sh:5Kkzs8XrJNAmf9VQDFe.... that doesnt work. what is the prefix for that? Mar 27, 2018 at 18:32
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.

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.