How to get bitcoin address from script?

(Дмитрий Малахов) #1

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?

0 Likes

(Kirill Fomichev) #2

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()
console.log(address)
// mpXwg4jMtRhuSpVq4xS3HFHmCmWp9NyGKt

or you can use my package sript2addresses:

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

(Braydon Fuller) #3

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.

Related: https://github.com/bitpay/bitcore/pull/1310

0 Likes

(Bitstampclone) #4

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.

0 Likes