How to get bitcoin address from script?

I try to parse script and get bitcoin address. So I do this:

var script = new bitcoreLib.Script('4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac');
var address = script.toAddress().toString();
var addressInfo = script.addressInfo();

Both address and addressInfo is false, but script.classify() returns “Pay to public key”. I tried to change raw data to “65 0x04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f OP_CHECKSIG”, but I got fail.
So how to extract address or public key from script?

You need get public key, apply sha256 hash and ripemd160, after this create address for specific network and convert to string (but this code works only for pay-to-pubkey!):

var bitcore = require('bitcore-lib')
var script = bitcore.Script('4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac')
var hash = bitcore.crypto.Hash.sha256ripemd160(s.chunks[0].buf)
var address = bitcore.Address(hash, bitcore.Networks.testnet).toString()
// mpXwg4jMtRhuSpVq4xS3HFHmCmWp9NyGKt

or you can use my package sript2addresses:

var script2addresses = require('script2addresses')
console.log(script2addresses('4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac', bitcore.Networks.testnet))
// { type: 'pubkey',
//   addresses: [ 'mpXwg4jMtRhuSpVq4xS3HFHmCmWp9NyGKt' ] }

The script isn’t a pay-to-public-key-hash or pay-to-script-hash, and is pay-to-public key. However it’s fairly common to calculate the address from the public key.


As i’m new to this bitcoin community i wonder that we can get bitcoin script address for free. I thought it is all about web development.

It seems that you have mistake in your script, I suggest you downloading program which will correct them and underline. Programmers use it, otherwise programming is insane hard. I used it when I was writing algorithm for hashing, before I invited for myself services which do hashing for you and your GPU is not needed. I used and it is even faster than I did it with my own gpu.

1 Like