0

console.log writes this datatype -

<key1 : value1 , key2 : value2>

the syntax is not similar to neither object nor array. So what datatype is this?

here's how to reproduce.

install bsv library

const bsv = require("bsv");
console.log(bsv.PrivateKey.fromRandom())

from node terminal

> bsv = require("bsv");
> bsv.PrivateKey.fromRandom()
0

1 Answer 1

2

TL;DR

What you're seeing is just an object, specifically a PrivateKey object from the bsv library. The bsv library has a custom representation of the object when logged by console.log in outdated versions of Node.js.

Details

I was finally able to replicate your output, getting this from an outdated copy of Node.js:

<PrivateKey: a909d298d55c8fa3afdb641eb46e90cebad4f2d3f5ee0d4149e3bac16f12ee5b, network: livenet>

As I said above, it's just a PrivateKey object from the bsv library. The reason you're seeing what you're seeing is that the bsv library uses a long-deprecated feature of Node.js: Node.js's console.log used to look for an inspect method and if it was present, would call that method to get a string representation of the object to log. bsv has an inspect method on PrivateKey, currently at line 372 (that line number will rot over time, of course).

You'll only see this in (slightly) outdated copies of Node.js. I stumbled across this solution because I tried it on CodeSandbox, which was using v10.20.1. This helpful message was just after the output in CodeSandbox:

(node:887) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated

I've since replicated that locally by installing Node.js v10.20.1.

With an up-to-date copy of Node.js (v12.16.3 is the current LTS as I write this, v14.1.0 is the current version), Node.js doesn't look for or use an inspect method anymore, so you see Node.js's standard object output, along these lines:

PrivateKey {
  bn: BN {
    negative: 0,
    words: [
      66428877, 21624768,
      45502785, 19604474,
      35981886, 10091365,
      29744843, 24740201,
      15504496,  1628002,
             0
    ],
    length: 10,
    red: null
  },
  compressed: true,
  network: Network {
    name: 'livenet',
    alias: 'mainnet',
    pubkeyhash: 0,
    privatekey: 128,
    scripthash: 5,
    xpubkey: 76067358,
    xprivkey: 76066276,
    cashAddrPrefix: 'bitcoincash',
    cashAddrPrefixArray: [
      2,  9, 20, 3, 15,
      9, 14,  3, 1, 19,
      8
    ],
    networkMagic: <Buffer e3 e1 f3 e8>,
    port: 8333,
    dnsSeeds: [ 'seed.bitcoinsv.org', 'seed.bitcoinunlimited.info' ]
  },
  publicKey: [Getter]
}

So, again: It's just an object, shown in nonstandard format by the bsv library.

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

Comments

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.