0

I'm using Node.js to access this hdPrivateKey but it look like

 <hdPrivateKey...>

Not look like normal JS object.

And console.log(address) looks like

<Address: 19o9ghmkUrNVf4d57tQJuUBb2gT8sbzKyq, type: pubkeyhash, network: livenet>

console.log(Object.keys(address)) look like

[ 'hashBuffer', 'network', 'type' ]

Why the key inside address are different?

var bitcore = require('bitcore');
var HDPrivateKey = bitcore.HDPrivateKey;

var hdPrivateKey = new HDPrivateKey();
console.log(hdPrivateKey)
var retrieved = new HDPrivateKey(hdPrivateKey);
var derived = hdPrivateKey.derive("m/0");
var derivedByNumber = hdPrivateKey.derive(1).derive(2, true);
var derivedByArgument = hdPrivateKey.derive("m/1/2");

var address = derived.privateKey.toAddress();
console.log(Object.keys(address))
console.log(address)
// obtain HDPublicKey
var hdPublicKey = hdPrivateKey.hdPublicKey;
1

2 Answers 2

1

The behavior you're seeing is because the object has its own inspect property which is a function and returns a string. When console.log sees that it's logging an object, it looks for that function and uses it if available. So on Node, this logs <foo>:

const o = {
    inspect() {
        return "<foo>";
    }
};
console.log(o);

That's all that the HDPrivateKey object is doing.

If you want to properly inspect the object, use a debugger. Alternately, use utils.inspect with customInspect set to false.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this answer.
1

In Node.js, a console.log call the function inspect of the object. In the bitcore-lib there is this method :

HDPrivateKey.prototype.inspect = function() {
  return '<HDPrivateKey: ' + this.xprivkey + '>';
};

And this method:

Address.prototype.inspect = function() {
  return '<Address: ' + this.toString() + ', type: ' + this.type + ', network: ' + this.network + '>';
};

1 Comment

Thanks for this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.