bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.66k stars 2.09k forks source link

P2SH Segwit address from uncompressed WIF #1405

Closed Overtorment closed 5 years ago

Overtorment commented 5 years ago

I happened to have old WIF for the uncompressed public key.

Then I made a p2sh segwit address out of it, using bitcoinjs v3:

      let keyPair = bitcoin.ECPair.fromWIF(secret);
      let pubKey = keyPair.getPublicKeyBuffer();
      let witnessScript = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey));
      let scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(witnessScript));
      address = bitcoin.address.fromOutputScript(scriptPubKey);

And sent some bitcoins there. Now apparently it is unspendable..? Or is there a way to recover those funds? Error Im getting is BIP143 rejects uncompressed public keys in P2WPKH or P2WSH

Thanks!

junderw commented 5 years ago

It has always been unspendable.

Since the beginning of segwit any segwit key must use compressed pubkeys.

Since v3 only had a very manual process (like you showed) there was no way to check, since encode only received a hash.

In v4 and higher, your code would be:

let keyPair = bitcoin.ECPair.fromWIF(secret);
let pubKey = keyPair.publicKey;
let address = bitcoin.payments.p2sh({
  redeem: bitcoin.payments.p2wpkh({
    pubkey: pubKey,
    network: keyPair.network,
  }),
  network: keyPair.network,
}).address;

If the pubkey is uncompressed it will throw an error.

Unfortunately, there is no way to recover coins sent to an uncompressed pubkey using segwit.