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>
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);
  • 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 Jun 5 '18 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. – Adam Jun 5 '18 at 8:40
  • please see this answer here: bitcoin.stackexchange.com/questions/75910/… – Adam Jun 5 '18 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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