1.0.0 • Published 6 months ago

@synchronic/bip32 v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

scure-bip32

Current version: https://github.com/paulmillr/scure-bip32/releases/tag/1.3.2

Synchronic fork @scure/bip32.

Difference from the original library: 1. ESM JavaScript Code Format 2. The function response format has been changed from Uint8Array to Buffer 3. Minor name changes

Usage

npm install @synchronic/bip32

import { HDKey } from '@synchronic/bip32';

//Use the bip39 library to get the seed (this function is not in this library)
const seedBuffer = bip39.mnemonicToSeedSync('rice blast wife alert include spring sign flash brisk awake clap holiday');

//To get the correct keys, you need the correct version for each coin. Below is the version for BTC
const VERSION = { private: 0x0488ade4, public: 0x0488b21e };

//We get all the keys for the BTC coin
const childKey = HDKey.fromMasterSeed(seedBuffer, VERSION).derive("m/84'/0'/0'/0/0");
console.log(childKey); 
//HDKey {
//  versions: { private: 76066276, public: 76067358 },
//  depth: 5,
//  index: 0,
//  chainCode: <Buffer b4 27 68 2c 46 d3 49 27 1d dc 9a e0 f0 39 7e 01 90 a6 b5 9e 4b 93 d6 16 30 78 f7 a3 37 a5 d5 82>,
//  parentFingerprint: 443898929,
//  privKey: 62756129312100052524292535245820304861917842446822627195264750096328035606895n,
//  privateKeyBuffer: <Buffer 8a be b5 e0 60 b5 54 d0 67 38 ae 96 10 dd dd ee 81 3b 7d 3e 3f 96 17 2b 6e d5 33 01 d1 e3 21 6f>,
//  publicKeyBuffer: <Buffer 02 3b f6 ab de 5a 64 ba 49 63 ac 18 81 06 8f 0b 70 f9 17 d3 d4 f3 3d 57 28 57 63 6e 42 09 0e d2 6a>,
//  pubHash: <Buffer a3 df 44 ac 95 6e 76 f9 4a 0a a4 5e 6b 57 a6 16 49 34 0a bc>
//}

//We use WIF from BTC and get the private key in the correct format.
const privateKey = HDKey.toWIF(0x80, childKey.privateKeyBuffer, true)
console.log(privateKey); //L1sQuSo2jeS6e9kjWTGxFcBCqfpD8HrWYj5f9Ldshm4zSVYVdd9v

It is difficult to tell the essence of the library without the compatible work of bip32, bip39, bitcoinjs-lib. This is what you get if you use all three libraries

import * as bip39 from '@synchronic/bip39';
import { HDKey } from "@synchronic/bip32";
import bitcoin from 'bitcoinjs-lib';

const mnemonic = 'rice blast wife alert include spring sign flash brisk awake clap holiday';
const seedBuffer = bip39.mnemonicToSeedSync(mnemonic);

const network = {
  path: "m/84'/0'/0'/0/0",
  messagePrefix: '\x18Bitcoin Signed Message:\n',
  bech32: 'bc',
  bip32: {
    public: 0x0488b21e,
    private: 0x0488ade4
  },
  pubKeyHash: 0x00,
  scriptHash: 0x05,
  wif: 0x80
}

const childKey = HDKey.fromMasterSeed(seedBuffer, network.bip32).derive(network.path);
const pubkey = childKey.publicKeyBuffer;
const address = bitcoin.payments.p2wpkh({ pubkey, network }).address;

const wallet = {
  address: address,
  public: pubkey.toString('hex'),
  private: HDKey.toWIF(network.wif, childKey.privateKeyBuffer, true)
}
console.log(wallet);
//{
//  address: 'bc1q5005fty4dem0jjs2530xk4axzeyngz4u7wvmrg',
//  public: '023bf6abde5a64ba4963ac1881068f0b70f917d3d4f33d572857636e42090ed26a',
//  private: 'L1sQuSo2jeS6e9kjWTGxFcBCqfpD8HrWYj5f9Ldshm4zSVYVdd9v'
//}

MIT License

Copyright (c) 2018 cryptocoinjs, 2022 Patricio Palladino, Paul Miller (paulmillr.com)