I have to query addresses in ElectrumX server. How do I generate scripthash for a known scriptPubKey in PHP?

Example address:

array (
  'isvalid' => true,
  'address' => '1MUz4VMYui5qY1mxUiG8BQ1Luv6tqkvaiL',
  'scriptPubKey' => '76a914e0ad60c897901128662623c500a4a6079e99cd3e88ac',
  'isscript' => false,
  'iswitness' => false,

I tried:

hash('sha256', hex2bin('76a914e0ad60c897901128662623c500a4a6079e99cd3e88ac'))

But it seems to be wrong.


I found the solution replicating https://github.com/janoside/btc-rpc-explorer/blob/master/app/api/electrumAddressApi.js

$scriptPubKey = '76a914e0ad60c897901128662623c500a4a6079e99cd3e88ac';

$addrScripthash = hash('sha256', hex2bin($scriptPubKey));
$re = '/.{2}/';
preg_match_all($re, $addrScripthash, $matches, PREG_PATTERN_ORDER, 0);
$addrScripthash = implode('', array_reverse($matches[0]));


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.