GardenBlockchain

What Bitcoin WIF encodes

An older private-key encoding example, with network and compressed-public-key context.

Wallet Import Format encodes private-key bytes with network and compressed-public-key information. It is an encoding of signing material, not an encryption scheme or an address format.

The original note installed the wif package:

npm i wif --save

The historical example

/**
   * Convert privateKey to Wallet Import Format: https://learnmeabitcoin.com/guide/wif
   */
privateKeyToWIF(isMainNet = false) {
    let wif = require('wif');
    let privateKey = Buffer.from('0000000000000000000000000000000000000000000000000000000000000001', 'hex');
    const mainNetVersionPrefix = 0x80;
    const testNetVersionPrefix = 0xEF;
    const netVersion = isMainNet ? mainNetVersionPrefix : testNetVersionPrefix;
    let key = wif.encode(netVersion, privateKey, true);
    console.log(key);
    // => KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn

    let obj = wif.decode(key);
    console.log(obj);
    // => {
    //	version: 239,
    //	privateKey: <Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01>,
    //	compressed: true
    //}

    try {
      wif.decode(key, 0x09);
    } catch (e) {
      console.error(e.message);
      // => Error: Invalid network version, because we encode with version 239 not 0x09
    }

    // alternative syntax
    const encode = wif.encode(obj);
    console.log(encode);
    // => KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn
}

The scalar-one key in this listing is a public test vector. It must never be used to hold funds. The source’s output comments mix a mainnet-looking WIF with a testnet version example; they are retained as historical comments, not a newly verified round trip.

A key does not select one script form

The same key material can participate in different address/script constructions. Importing a WIF therefore does not, by itself, specify exactly one address without the surrounding network and script convention.

The snippet uses an older wif.encode API and method-style wrapper. No keys were generated or imported for this revision. Source: Gist revision.

← Back to the garden