<p>it's better to get public key first then convert it to address</p>

<pre><code>var publicKey = privateKey.toPublicKey();
var address = publicKey.toAddress(Networks.livenet);
</code></pre>

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

<pre><code>console.log("Address: ", address);
</code></pre>

CC BY-SA 4.0
2018-06-05T07:26:02.870Z
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
CC BY-SA 4.0
2018-06-05T08:40:14.110Z
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
CC BY-SA 4.0
2018-06-05T10:07:11.230Z
please see this answer here: https://bitcoin.stackexchange.com/questions/75910/how-to-generate-a-native-segwit-address-and-p2sh-segwit-address-from-a-standard
Tailer
CC BY-SA 4.0
2018-06-05T07:08:20.117Z
2018-06-05T07:08:20.117Z
Tailer
<p>You should have the public key first to get bitcoin address.</p>

<pre><code>var publicKey = privateKey.toPublicKey();
var address = publicKey.toAddress(Networks.livenet);
</code></pre>

<p>Check out the <a href="https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses#How_to_create_Bitcoin_Address" rel="nofollow noreferrer">link</a> to know "How bitcoin address is generated from the public key".</p>

<p>Better to try yourself in this <a href="http://gobittest.appspot.com/Address" rel="nofollow noreferrer">Playground</a>. </p>

CC BY-SA 4.0
2018-06-05T08:37:19.307Z
2018-06-05T08:37:19.307Z
idk
<p>Highly confusing.
Just console log it <strong>AS A STRING</strong>.</p>
<p>This line</p>
<pre><code>console.log(&quot;address&quot;, &quot;&quot; + address);
</code></pre>
<p>will print out</p>
<pre><code>address 1HSfafDJ6ijRxSjj2aTAq2Vb63TAnQK9fz
</code></pre>
<p>On the other hand if you just log the address object</p>
<pre><code>console.log(&quot;address&quot;, address);
</code></pre>
<p>You will get</p>
<pre><code>address Address {
  hashBuffer: &lt;Buffer b4 5c 9d e7 ce 0c 89 ee aa 5e 8a 24 b2 5a ff 0b dd f6 9d 42&gt;,
  network: Network {
    name: 'livenet',
    alias: 'mainnet',
    pubkeyhash: 0,
    privatekey: 128,
    scripthash: 5,
    bech32prefix: 'bc',
    xpubkey: 76067358,
    xprivkey: 76066276,
    networkMagic: &lt;Buffer f9 be b4 d9&gt;,
    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'
}
</code></pre>

CC BY-SA 4.0
2022-10-26T10:46:46.190Z
2022-10-26T10:46:46.190Z
Dick Larsson
<p>I am using bitcoin core library in node js.</p>

<pre><code>const privateKey = new bitcore.PrivateKey('testnet');
const address = privateKey.toAddress('testnet);
</code></pre>

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

<pre><code>&lt;Address: mmNeqV9HWKUDRUAVdknRKDPHyKYGpBEDzm, type: pubkeyhash, network: testnet&gt;
</code></pre>

<bitcoin-core><wallet>
extract wallet address from private key
vincentsty
# extract wallet address from private key

(Asked by: username_0 on 2018-06-05)

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>

```

**Tags:** bitcoin-core, wallet


---

## 3 answers

### Answer by username_1 on 2018-06-05. Score: 1 | Accepted answer

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);

```

> **Comments:**
> **username_0**: 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?  
> **username_1**: 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.  
> **username_1**: please see this answer here: https://bitcoin.stackexchange.com/questions/75910/how-to-generate-a-native-segwit-address-and-p2sh-segwit-address-from-a-standard  

---

### Answer by username_2 on 2018-06-05. Score: 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](https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses#How_to_create_Bitcoin_Address) to know "How bitcoin address is generated from the public key".

Better to try yourself in this [Playground](http://gobittest.appspot.com/Address).


---

### Answer by username_3 on 2022-10-26. Score: 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'
}

```


---

