Bitcoin Forum
April 23, 2024, 09:59:55 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Electrumx protocol  (Read 210 times)
ilib (OP)
Newbie
*
Offline Offline

Activity: 19
Merit: 12


View Profile
October 26, 2019, 12:55:27 PM
Merited by OmegaStarScream (2), ABCbits (1)
 #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?
It is a common myth that Bitcoin is ruled by a majority of miners. This is not true. Bitcoin miners "vote" on the ordering of transactions, but that's all they do. They can't vote to change the network rules.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1713909595
Hero Member
*
Offline Offline

Posts: 1713909595

View Profile Personal Message (Offline)

Ignore
1713909595
Reply with quote  #2

1713909595
Report to moderator
1713909595
Hero Member
*
Offline Offline

Posts: 1713909595

View Profile Personal Message (Offline)

Ignore
1713909595
Reply with quote  #2

1713909595
Report to moderator
1713909595
Hero Member
*
Offline Offline

Posts: 1713909595

View Profile Personal Message (Offline)

Ignore
1713909595
Reply with quote  #2

1713909595
Report to moderator
BrewMaster
Legendary
*
Offline Offline

Activity: 2114
Merit: 1292


There is trouble abrewing


View Profile
October 26, 2019, 02:41:06 PM
Merited by ranochigo (2), hugeblack (1)
 #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 <hash> 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

There is a FOMO brewing...
ilib (OP)
Newbie
*
Offline Offline

Activity: 19
Merit: 12


View Profile
October 26, 2019, 06: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
BrewMaster
Legendary
*
Offline Offline

Activity: 2114
Merit: 1292


There is trouble abrewing


View Profile
October 26, 2019, 07: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.

There is a FOMO brewing...
ilib (OP)
Newbie
*
Offline Offline

Activity: 19
Merit: 12


View Profile
October 27, 2019, 09:59:21 AM
Merited by BrewMaster (2)
 #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;
}


Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!