Author

Topic: Electrumx protocol (Read 240 times)

newbie
Activity: 19
Merit: 12
October 27, 2019, 04:59:21 AM
#5
Thanks a lot!
I finally managed to hash it like this:

Code:
$p2pkh = sprintf("76a914%s88ac", $hash160);
$hash = hash('sha256', hex2bin($p2pkh));
$hsah = ReverseEndianness($hash);

function ReverseEndianness($num)
{
    $strlen = strlen($num);
    if ($strlen % 2) {
        throw new \Exception('ReverseEndianness: input string must have an even length');
    }
    $r = '';
    for ($i = strlen($num); $i>=0; $i=$i-2) {
        $r .= substr($num, $i, 2);
    }
    return $r;
}


legendary
Activity: 2128
Merit: 1293
There is trouble abrewing
October 26, 2019, 02:09:36 PM
#4
Quote
now you hash it using the hash you specified for the server (it is apparently SHA256 by default)
Code:
Code:
SHA256('76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac') =
6191c3b590bfcfa0475e877c302da1e323497acf3b42c08d8fa28e364edf018b

But I failed to get it using sha256. For example in PHP:
Code:
hash('sha256', '76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac')
gives:
5f3f330fccb56d6b43f19b7e235f5b146ac92add3a3ab99380a39e7f7406959b

it is because you are reading "76a9..." as an ASCII (or UTF8) string not a hexadecimal string. you should convert it to its byte array representation (0x76=118, 0xa9=169,...) and then hash that.

also you shouldn't be working with hex in first place. your code must convert the address (1A1zP1....) to bytes then add the extra bytes that come from the script to it and the pass that to SHA256 and then reverse the result (bytes) and send that.
that is both faster and has less chance of having any errors like this. after all that you can convert the final result to hex if you wanted.
newbie
Activity: 19
Merit: 12
October 26, 2019, 01:25:01 PM
#3
Quote
now you hash it using the hash you specified for the server (it is apparently SHA256 by default)
Code:
Code:
SHA256('76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac') =
6191c3b590bfcfa0475e877c302da1e323497acf3b42c08d8fa28e364edf018b

But I failed to get it using sha256. For example in PHP:
Code:
hash('sha256', '76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac')
gives:
5f3f330fccb56d6b43f19b7e235f5b146ac92add3a3ab99380a39e7f7406959b
legendary
Activity: 2128
Merit: 1293
There is trouble abrewing
October 26, 2019, 09:41:06 AM
#2
you have an address that you want to check, each address (based on its type) corresponds to a certain type of script in this example the address starting with 1 is a P2PKH address so the script of it is DUP HASH160 EQUALVERIFY CHECKSIG

so you start from the address:
1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa
base58decodecheck to get the hash (after removing the version byte)
62e907b15cbf27d5425399ebf6f0fb50ebb88f18
build the script from the hash
76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac

now you hash it using the hash you specified for the server (it is apparently SHA256 by default)
Code:
SHA256('76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac') =
6191c3b590bfcfa0475e877c302da1e323497acf3b42c08d8fa28e364edf018b
but the document says that for sending this hash digest to the server you must reverse its endianness so you simply reverse your bytes that you get from the hash result then send it to the server. the reverse of the hash is:
Code:
8b01df4e368ea28f8dc0423bcf7a4923e3a12d307c875e47a0cfbf90b5c39161
(note the start of previous code block with the end of this last code block: 0x61, 0x91,... turns into ...., 0x91, 0x61
newbie
Activity: 19
Merit: 12
October 26, 2019, 07:55:27 AM
#1
The description states:
Code:
A script hash is the hash of the binary bytes of the locking script (ScriptPubKey), expressed as a hexadecimal string. The hash function to use is given by the ?hash_function? member of server.features() (currently sha256() only). Like for block and transaction hashes, when converting the big-endian binary hash to a hexadecimal string the least-significant byte appears first, and the most-significant byte last.

For example, the legacy Bitcoin address from the genesis block:

1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa
has P2PKH script:

76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac
with SHA256 hash:

6191c3b590bfcfa0475e877c302da1e323497acf3b42c08d8fa28e364edf018b
which is sent to the server reversed as:

8b01df4e368ea28f8dc0423bcf7a4923e3a12d307c875e47a0cfbf90b5c39161
By subscribing to this hash you can find P2PKH payments to that address.
Can anybody explain what are these numbers:

6191c3b590bfcfa0475e877c302da1e323497acf3b42c08d8fa28e364edf018b

8b01df4e368ea28f8dc0423bcf7a4923e3a12d307c875e47a0cfbf90b5c39161

and how to get them?
Jump to: