Skip to content

Instantly share code, notes, and snippets.

@blmalone
Last active November 11, 2022 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save blmalone/d27beeda3b59eaf010965a3246c11b14 to your computer and use it in GitHub Desktop.
Save blmalone/d27beeda3b59eaf010965a3246c11b14 to your computer and use it in GitHub Desktop.
Creating an extended private key from bip32 test vector
import { algo, enc } from 'crypto-js';
import { ec as EC } from "elliptic";
import * as bs58check from "bs58check";
const ec = new EC("secp256k1");
const TEST_VECTOR_1_SEED = "000102030405060708090a0b0c0d0e0f";
const MASTER_KEY_DERIVATION_KEY = "Bitcoin seed";
const mainnetVersionBytesPriv = "0488ADE4";
const mainnetVersionBytesPub = "0488B21E";
const childDepth = "00"; // master node
const parentFingerPrint = "00000000"; // Calculated from http://bip32.org/ - First 32 bits of the public key identifier i.e. HASH160(publicKey)
const childNumber = "00000000";
const key = Buffer.from(MASTER_KEY_DERIVATION_KEY).toString("hex");
const hasher = algo.HMAC.create(algo.SHA512, enc.Hex.parse(key));
hasher.update(enc.Hex.parse(TEST_VECTOR_1_SEED));
const res = hasher.finalize().toString();
const left256Bits = res.substring(0, 64);
const right256Bits = res.substring(64, 128);
const keypair = ec.keyFromPrivate(Buffer.from(left256Bits, "hex"));
const masterPub = keypair.getPublic("hex");
const compressedMasterPub = `${(Buffer.from(masterPub, "hex")[64] % 2 === 0) ? "02" : "03"}${masterPub.substring(2, 66)}`;
const extendedPrivateKey = `${mainnetVersionBytesPriv}${childDepth}${parentFingerPrint}${childNumber}${right256Bits}00${left256Bits}`;
const extendedPublicKey = `${mainnetVersionBytesPub}${childDepth}${parentFingerPrint}${childNumber}${right256Bits}${compressedMasterPub}`;
const xprv = bs58check.encode(Buffer.from(extendedPrivateKey, "hex"));
const xpub = bs58check.encode(Buffer.from(extendedPublicKey, "hex"));
console.log(`Extended Private Key: ${xprv}`);
console.log(`Extended Public Key: ${xpub}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment