GardenBlockchain

Deriving legacy and wrapped-SegWit addresses from WIF

An older BitcoinJS example that chooses a network and constructs P2PKH and P2SH-P2WPKH payments.

A WIF import still needs an intended network and address construction. This older BitcoinJS note used one public key to build two payment forms.

npm i bitcoinjs-lib --save
const bitcoin = require('bitcoinjs-lib');
const net = process.env.USDT_OMNI_NETWORK === 'testnet'
      ? bitcoin.networks.testnet
      : bitcoin.networks.bitcoin;
const alice = bitcoin.ECPair.fromWIF(omniWIF, net);
const alice_p2pkh_legacy_address = bitcoin.payments.p2pkh({
  pubkey: alice.publicKey,
  network: net
}).address;// NEW** New Payments API for p2pkh
console.log(alice_p2pkh_legacy_address);


const alice_p2wpkh_segwit_address = bitcoin.payments.p2sh({
  redeem: bitcoin.payments.p2wpkh({pubkey: alice.publicKey, network: net}),
  network: net
}).address;
console.log(alice_p2wpkh_segwit_address);

The first result is P2PKH. The second wraps P2WPKH inside P2SH; despite the old variable’s broad SegWit label, it is not a native bc1/tb1 P2WPKH address.

omniWIF comes from the surrounding application, and the environment variable selects the source’s network branch. The old bitcoin.ECPair API is version-specific. This fragment does not include a WIF value or establish compatibility with a later BitcoinJS major. No key was imported for this revision.

← Back to the garden