Skip to content

electrum compatibility and P2SH addresses #37

@melaxon

Description

@melaxon

Hello I noticed 2 things when I run such commands:
./hd-wallet-derive.php -g --key="xprv......" --addr-type=p2sh-segwit --preset=electrum

./hd-wallet-derive.php -g --key="yprv......" --addr-type=p2sh-segwit --preset=electrum

The first one is meaningless for those who wish to generate the same addresses in Electrum because Electrum will never generate 3bitcoinaddres... addresses from XPRV (or XPUB). There is no such an option (currently)

The second command will generate the same addresses and public keys as Electrum does from given YPRV (or YPUB) but pubkeyhash seems to be WRONG (or maybe I'm wrong and missed something).

To get the address balance I inquire electrumx using this code:

...
$pubkeyhash = !empty($array['pubkeyhash']) ? $array['pubkeyhash'] : fn_get_hash160($address, $addrCreator, $network);  //if pubkeyhash exists we'll use it
$data = Base58::decodeCheck($address);
$prefixByte = $data->slice(0, $network->getP2shPrefixLength())->getHex();
if ($prefixByte === $network->getP2shByte()) { // P2SH: BTC: 3.., LTC: M...
            $script = "a914" . $pubkeyhash . "87";
} 
elseif ($prefixByte === $network->getAddressByte()) { // P2PKH: BTC: 1..., LTC: L...
            $script = "76a914" . $pubkeyhash . "88ac";
}

$hash = hash('sha256', hex2bin($script));
$hsah = ReverseEndianness($hash);
$result = $electrum->blockchainAddressGetBalance($hsah);
...

In case of XPRV everything is okay. For YPRV, if I use pubkeyhashes generated by hd-wallet-derive ($array['pubkeyhash']) the balance is always zero. If I delete pubkeyhashes and calculate them with some function fn_get_hash160 it works smoothly.
I guess I have to look into Bitwasp code (?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions