extract wallet address from private key

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>

vincentsty

Posted 2018-06-05T06:48:25.083

Reputation: 137

Answers

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

Adam

Posted 2018-06-05T06:48:25.083

Reputation: 3 215

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 2018-06-05T07:26:02.870

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.Adam 2018-06-05T08:40:14.110

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.

idk

Posted 2018-06-05T06:48:25.083

Reputation: 49