0

I am using bitcoin core library in node js.

const privateKey = new bitcore.PrivateKey('testnet');
const address = privateKey.toAddress('testnet);

When i console.log it, it get result as below. How can i extract the address part only? I try to use address.Address but it returns undefined

<Address: mmNeqV9HWKUDRUAVdknRKDPHyKYGpBEDzm, type: pubkeyhash, network: testnet>

3 Answers 3

1

it's better to get public key first then convert it to address

var publicKey = privateKey.toPublicKey();
var address = publicKey.toAddress(Networks.livenet);

address is an object, you can use it as a string. if you want to see it in console try

console.log("Address: ", address);
3
  • why is it better to get public key first before convert it to address? I found that it returns the same result also even without convert to public key first. any technical explanation on that?
    – vincentsty
    Commented Jun 5, 2018 at 7:26
  • Because you're creating the address from the public key not from the private key. so technically for better understanding when you call privateKey.toAddress bitcore by default get the publicKey and then the address.
    – Tailer
    Commented Jun 5, 2018 at 8:40
  • please see this answer here: bitcoin.stackexchange.com/questions/75910/…
    – Tailer
    Commented Jun 5, 2018 at 10:07
1

You should have the public key first to get bitcoin address.

var publicKey = privateKey.toPublicKey();
var address = publicKey.toAddress(Networks.livenet);

Check out the link to know "How bitcoin address is generated from the public key".

Better to try yourself in this Playground.

0

Highly confusing. Just console log it AS A STRING.

This line

console.log("address", "" + address);

will print out

address 1HSfafDJ6ijRxSjj2aTAq2Vb63TAnQK9fz

On the other hand if you just log the address object

console.log("address", address);

You will get

address Address {
  hashBuffer: <Buffer b4 5c 9d e7 ce 0c 89 ee aa 5e 8a 24 b2 5a ff 0b dd f6 9d 42>,
  network: Network {
    name: 'livenet',
    alias: 'mainnet',
    pubkeyhash: 0,
    privatekey: 128,
    scripthash: 5,
    bech32prefix: 'bc',
    xpubkey: 76067358,
    xprivkey: 76066276,
    networkMagic: <Buffer f9 be b4 d9>,
    port: 8333,
    dnsSeeds: [
      'seed.bitcoin.sipa.be',
      'dnsseed.bluematt.me',
      'dnsseed.bitcoin.dashjr.org',
      'seed.bitcoinstats.com',
      'seed.bitnodes.io',
      'bitseed.xf2.org'
    ]
  },
  type: 'pubkeyhash'
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.