1

This hdkey library is creating a Buffer in the process of creating keys as an example.

var HDKey = require('hdkey')
var seed = 'a0c42a9c3ac6abf2ba6a9946ae83af18f51bf1c9fa7dacc4c92513cc4dd015834341c775dcd4c0fac73547c5662d81a9e9361a0aac604a73a321bd9103bce8af'
var hdkey = HDKey.fromMasterSeed(Buffer.from(seed, 'hex'))

It looks like this is a common practice, but I don't understand why we need a buffer.

I was wondering what happens if I don't use it.

So I tried:

import HDKey from 'hdkey';

const seed =
'a0c42a9c3ac6abf2ba6a9946ae83af18f51bf1c9fa7dacc4c92513cc4dd015834341c775dcd4c0fac73547c5662d81a9e9361$
const hdkey = HDKey.fromMasterSeed(seed);
console.log("hdkey ===>", hdkey);

const hdkeyWithBuffer = HDKey.fromMasterSeed(Buffer.from(seed, 'hex'))
console.log("hdkeyWithBuffer ===>", hdkeyWithBuffer);

Then I get this:

$ npm run index.mjs

> testHdkey@1.0.0 index.mjs /Users/tai/Dropbox/Codes/testHdkey
> node --experimental-modules index.mjs

(node:25246) ExperimentalWarning: The ESM module loader is experimental.
hdkey ===> HDKey {
  versions: { private: 76066276, public: 76067358 },
  depth: 0,
  index: 0,
  _privateKey:
   <Buffer 5c 31 9c 7f 1b 0a ea 87 97 f7 94 80 12 ef bd 0d 11 58 43 fc 9a 12 34 01 c1 fc ba bc 3b d3 14 d1>,
  _publicKey:
   <Buffer 02 55 73 29 fe 34 c4 0f 0d a1 87 d5 dd 8b c8 b8 a7 ab 69 ed be 63 13 c8 58 e6 56 3c 7e 98 af ab b8>,
  chainCode:
   <Buffer ed 1c 6d 32 88 99 d6 ac ad 8d 27 46 4d 79 29 53 83 bc 3d 47 9e 25 9d 0a 4d 59 1a 5b da 8f 4b f5>,
  _fingerprint: 747931124,
  parentFingerprint: 0,
  _identifier:
   <Buffer 2c 94 85 f4 30 bd 2c 99 6e ea 53 bd c7 ed 5b 22 e2 08 40 91> }
hdkeyWithBuffer ===> HDKey {
  versions: { private: 76066276, public: 76067358 },
  depth: 0,
  index: 0,
  _privateKey:
   <Buffer 15 49 3c 2b e2 a4 15 9c d9 82 b9 3c af 3d 70 5f d2 9b 9b ac 77 80 31 ca ea 21 6e fb eb fe 24 78>,
  _publicKey:
   <Buffer 02 2a ec b9 22 ae 00 1d 9d 0c b9 69 27 e5 50 ee 6c fd 60 74 1a c6 27 f1 02 89 fe 2c 29 3b 3c 73 41>,
  chainCode:
   <Buffer 26 07 1b 1d c7 92 44 cd 6b 8f fd 3e 12 56 5d 32 b9 bd ea 4c 21 96 43 7c d4 5f a4 7c 63 30 f5 5f>,
  _fingerprint: 722394980,
  parentFingerprint: 0,
  _identifier:
   <Buffer 2b 0e df 64 a4 76 bb 34 ed e2 5e 03 0b d6 02 cb df 7e 46 e4> }

It looks like a Buffer is created behind the scene anyway, but the value changes if I don't create a Buffer myself.

Why do we need a buffer? Should I create a buffer on my own?

  • Underneath hdkey uses node's crypto hmac.update. It interprets the input according of its type differently. Unless you know what are you doing it is better to just pass a Buffer. – Ismael Apr 28 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.