Skip to content

Instantly share code, notes, and snippets.

@Meshugah
Created September 11, 2019 14:01
Show Gist options
  • Save Meshugah/af7d72f4599883fa6f2e536e9e199ec6 to your computer and use it in GitHub Desktop.
Save Meshugah/af7d72f4599883fa6f2e536e9e199ec6 to your computer and use it in GitHub Desktop.
Bitcoin HD wallet implementation in node.
const bip39 = require("bip39");
const hdkey = require("hdkey");
const createHash = require("create-hash");
const bs58check = require("bs58check");
const mnemonic = bip39.generateMnemonic(); //generates string
const seed = bip39.mnemonicToSeedSync(mnemonic).toString('hex'); //creates seed buffer
const root = hdkey.fromMasterSeed(seed);
const masterPrivateKey = root.privateKey.toString('hex');
const addrnode = root.derive("m/44'/0'/0'/0/0");
const step1 = addrnode._publicKey;
const step2 = createHash('sha256').update(step1).digest(); // sha hash
const step3 = createHash('rmd160').update(step2).digest(); // riped hash
var step4 = Buffer.allocUnsafe(21);
step4.writeUInt8(0x00, 0); // 0x00 for mainnet and 0x6f for testnet, extended riped60
const step5 = bs58check.encode(step4);
// base 58 check:
//// Perform SHA-256 hash on the extended RIPEMD-160 result
//// Perform SHA-256 hash on the result of the previous SHA-256 hash
//// Take the first 4 bytes of the second SHA-256 hash. This is the address checksum
//// Add the 4 checksum bytes from the previous step, at the end of extended RIPEMD-160 hash from step4. This is the 25-byte binary Bitcoin Address.
console.log('Base58Check: ' + step5);
@hamed-elahifar
Copy link

you didn't use "step3" any where!
there is no connection between step3 and step4
could you please explain in more detail

@hamed-elahifar
Copy link

how can i change this for 'BCH'& 'LTC' HD wallet ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment