Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Electrum protocol 1.4 #7

Open
webworker01 opened this issue Oct 24, 2019 · 1 comment
Open

Update to Electrum protocol 1.4 #7

webworker01 opened this issue Oct 24, 2019 · 1 comment
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@webworker01
Copy link
Owner

Protocol version 1.3+ removed some methods that allowed direct querying of addresses and now require scripthash to be passed instead.

  • Investigate the new methods
  • Update the Electrum.php library to match
  • Create new sample docs

kyuupichan/electrumx@7911f0d

@webworker01 webworker01 added enhancement New feature or request help wanted Extra attention is needed labels Oct 24, 2019
@webworker01 webworker01 self-assigned this Oct 24, 2019
@melaxon
Copy link

melaxon commented Oct 27, 2019

I managed to connect to TCP server and receive the responses as follow (I still could not connect to SSL servers):

  1. In Electrum.php I replaced ...address... to ...scripthash... where needed. E.g.
    public function blockchainAddressListunspent($address)
    {
        return $this->request('blockchain.scripthash.listunspent', [$address]);
    }
  1. I used the following code to connect to Bitcoin server (I also added Bitcoin.php, in case of KMD some parameters must be changed):
use webworker01\Komodo\Electrum;
use BitWasp\Bitcoin\Address\AddressCreator;
use BitWasp\Bitcoin\Network\Networks\Bitcoin;
use webworker01\Komodo\Komodo;

require_once('app/addons/bitcoin_xmv/lib/komodophp/vendor/autoload.php');
$bitcoin = new Bitcoin;
$komodo = new Komodo;
$addrCreator = new AddressCreator();


$addressInput = '1Kr6QSydW9bFQG1mXiPNNu6WpJGmUa9i1g';

echo "<br>AddressInput:<br>$addressInput <br>";

$address = null;
try {
   $address = $addrCreator->fromString($addressInput, $bitcoin)->getHash();

} catch ( \BitWasp\Bitcoin\Exceptions\UnrecognizedAddressException $e) {
   $errormessage = 'The address you entered is not valid.';
   echo "Error message: ".$errormessage;
}

$hash160 = bin2hex(getProtectedValue($address, 'buffer'));
fn_printr($address);

$p2pkh = sprintf("76a914%s88ac", $hash160);

echo "<br>P2PKH:<br>$p2pkh <br>";

$hash = hash('sha256', hex2bin($p2pkh));
echo "<br>Hash:<br>$hash <br>";

$hsah = ReverseEndianness($hash);
echo "<br>Hsah:<br>$hsah <br>";

$electrumhost = 'electrum2.privateservers.network';
$electrumport = 50001;

//$electrumhost = 'electrum3.cipig.net';
//$electrumport = 10001;

$electrum = new Electrum();

$electrum->connect($electrumhost, $electrumport);

$result = $electrum->blockchainAddressGetBalance($hsah);
echo "<br>Address balance:";
echo "<br>Confirmed:" . $result->result->confirmed;
echo "<br>Unconfirmed:" . $result->result->unconfirmed;
echo "<br>";

echo "<br>Server version:<br>";
$result = $electrum->serverVersion();
fn_printr($result);

echo "<br>Server features:<br>";
$result = $electrum->serverFeatures();
fn_printr($result);


echo "<br>List unspent:<br>";
$result = $electrum->blockchainAddressListunspent($hsah);
fn_printr($result);

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;

}

function getProtectedValue($obj,$name)
{
    $array = (array)$obj;
    $prefix = chr(0).'*'.chr(0);
    return $array[$prefix.$name];
}

function fn_printr($val)
{
    echo '<pre>';
    print_r($val);
    echo '</pre>';
}

exit;

This is a printout:


AddressInput:
1Kr6QSydW9bFQG1mXiPNNu6WpJGmUa9i1g
BitWasp\Buffertools\Buffer Object
(
    [size] => 20
    [buffer] => 0xcebb2851a9c7cfe2582c12ecaf7f3ff4383d1dc0
)

P2PKH:
76a914cebb2851a9c7cfe2582c12ecaf7f3ff4383d1dc088ac

Hash:
897b7e368b606a68791cf8414505768d4a51c84153f9d97126a4ddc49df06591

Hsah:
9165f09dc4dda42671d9f95341c8514a8d76054541f81c79686a608b367e7b89

Address balance:
Confirmed:162565630831
Unconfirmed:0

Server version:
stdClass Object
(
    [jsonrpc] => 2.0
    [result] => Array
        (
            [0] => ElectrumX 1.13.0
            [1] => 1.4
        )

    [id] => 0
)

Server features:
stdClass Object
(
    [jsonrpc] => 2.0
    [result] => stdClass Object
        (
            [hosts] => stdClass Object
                (
                    [electrum2.privateservers.network] => stdClass Object
                        (
                            [tcp_port] => 50001
                            [ssl_port] => 50002
                            [ws_port] => 50003
                        )

                )

            [pruning] => 
            [server_version] => ElectrumX 1.13.0
            [protocol_min] => 1.4
            [protocol_max] => 1.4.2
            [genesis_hash] => 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
            [hash_function] => sha256
            [services] => Array
                (
                    [0] => tcp://electrum2.privateservers.network:50001
                    [1] => ssl://electrum2.privateservers.network:50002
                    [2] => ws://electrum2.privateservers.network:50003
                )

        )

    [id] => 0
)

List unspent:
stdClass Object
(
    [jsonrpc] => 2.0
    [result] => Array
        (
            [0] => stdClass Object
                (
                    [tx_hash] => d044917c01e547efa8721821356822f9ae579a9c66f950881fdfc011a9770559
                    [tx_pos] => 1
                    [height] => 601217
                    [value] => 54424412706
                )

            [1] => stdClass Object
                (
                    [tx_hash] => 7426ad002d143cb2cf210783a0dbe1f3a1db32066127f5500b185db703f0ed01
                    [tx_pos] => 1
                    [height] => 601221
                    [value] => 53169104516
                )

            [2] => stdClass Object
                (
                    [tx_hash] => 295af749ef5b211e278200ee95fe8f1492f63e9e366632d27b9e783f352e1cc8
                    [tx_pos] => 1
                    [height] => 601225
                    [value] => 252004714
                )

            [3] => stdClass Object
                (
                    [tx_hash] => 9fe0b29628ff4491cd8eece27b06f664011cda79fc472aeb87d90f36bbd815c1
                    [tx_pos] => 1
                    [height] => 601225
                    [value] => 51333383928
                )

            [4] => stdClass Object
                (
                    [tx_hash] => 581ab3c94f31bb42b0a8164bb36e88bf0f2dc8d887a2b45a0cc01f9dfe6fae17
                    [tx_pos] => 4
                    [height] => 601225
                    [value] => 3386724967
                )

        )

    [id] => 0
)

Next step is to find out how to manage SSL connections. Please advise anybody.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants