Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Implementation of the Stratum protocol (for electrum and mining) using ReactPHP

License

NotificationsYou must be signed in to change notification settings

Bit-Wasp/stratum-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build StatusCode CoverageScrutinizer Code Quality

Implementation of the Stratum protocol (for electrum and mining) using ReactPHP

Client

The Client class is used to make a connection to a host. It takes aConnectorInterfaceandRequestFactory.

react/socket-client provides a number of connectors, which can be combinedto produce the desired functionality.

use \BitWasp\Stratum\Client;use \BitWasp\Stratum\Connection;use \BitWasp\Stratum\Request\RequestFactory;$loop = \React\EventLoop\Factory::create();$resolver =new \React\Dns\Resolver\Factory();// Raw TCP, cannot perform DNS resolution$tcp =new \React\SocketClient\TcpConnector($loop);// TCP Connector with a DNS resolver$dns =new \React\SocketClient\DnsConnector($tcp,$resolver->create('8.8.8.8',$loop));// Encrypted connection$context_options = [];$tls =new \React\SocketClient\SecureConnector($dns,$loop,$context_options);$requests =newRequestFactory;$client =newClient($tls,$requests);$host ='';$port ='';$client->connect($host,$port)->then(function (Connection$conn) {/* success */},function (\Exception$e) {/*  error  */print_r($e->getMessage());});$loop->run();

The SecureConnector initiates a TLS session to encrypt your connection. $context_options is an optionalvalue, but many Electrum servers have misconfigured SSL certificates! (incorrect CN field, or are self-signed)These will not be accepted with the default verification settings, and can be disabled by changing the $context_options

$context_options = ["verify_name" => false, "allow_self_signed" => true];

Connection

AConnection represents a connection to a peer.

Requests can be sent to the peer usingConnection::request($method, $params = []),which returns a Promise for the pending result. When a response with the same ID isreceived, the promise will resolve this as the result.

$conn->request('server.banner')->then(function (Response$response) {print_r($response->getResult());},function (\Exception$e) {echo$e->getMessage();});

Request instances can be sent usingConnection::sendRequest(Request $request)which also returns a promise.

For a list of methods for the electrum and mining clients, see the respective Api classes.The constants are method's for these APIs.

$conn->sendRequest(newRequest(null,'server.banner'))->then(function (Response$response) {print_r($response->getResult());},function (\Exception$e) {echo$e->getMessage();});

NotificationInterface's can be sent usingConnection::sendNotify(NotificationInterface $note)Notifications are not requests, and don't receive a response. This method is only relevant ifusingConnection from a servers perspective.

$conn->sendNotification(newNumBlocksNotification(123123));

Api's

The Stratum protocol is implemented by electrum servers and stratum mining pools.Their methods are exposed byElectrumClient andMiningClient respectively.

The api methods cause a Request to be sent, returning a promise to capture the result.

use \BitWasp\Stratum\Api\ElectrumClient;use \BitWasp\Stratum\Client;use \BitWasp\Stratum\Connection;use \BitWasp\Stratum\Request\Response;use \BitWasp\Stratum\Request\RequestFactory;$loop = \React\EventLoop\Factory::create();$tcp =new \React\SocketClient\TcpConnector($loop)   ;$resolver =new \React\Dns\Resolver\Factory();$dns =new \React\SocketClient\DnsConnector($tcp,$resolver->create('8.8.8.8',$loop));$tls =new \React\SocketClient\SecureConnector($dns,$loop);$requests =newRequestFactory;$client =newClient($tls,$requests);$host ='anduck.net';$port =50002;$client->connect($host,$port)->then(function (Connection$conn) {$electrum =newElectrumClient($conn);$electrum->addressListUnspent('1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L')->then(function (Response$response) {print_r($response->getResult());    });},function (\Exception$e) {echo'error';echo$e->getMessage().PHP_EOL;/*  error  */});$loop->run();

Events

Connection emits amessage event when a message is received whichwas not initiated by a Request. These messages are typically due to subscriptions.

The following events are emitted automatically by the library when encountered.The event name is the method used to enable the subscription.

  • 'blockchain.headers.subscribe' emits aHeadersNotification
  • 'blockchain.address.subscribe' emits aAddressNotification
  • 'blockchain.numblocks.subscribe' emits aNumBlocksNotification
  • 'mining.subscribe' emits aMiningNotification
  • 'mining.set_difficulty' emits aSetDifficultyNotification
use \BitWasp\Stratum\Api\ElectrumClient;use \BitWasp\Stratum\Client;use \BitWasp\Stratum\Connection;use \BitWasp\Stratum\Notification\AddressNotification;use \BitWasp\Stratum\Request\RequestFactory;$loop =React\EventLoop\Factory::create();$tcp =new \React\SocketClient\TcpConnector($loop);$resolver =new \React\Dns\Resolver\Factory();$dns =new \React\SocketClient\DnsConnector($tcp,$resolver->create('8.8.8.8',$loop));$tls =new \React\SocketClient\SecureConnector($dns,$loop);$requests =newRequestFactory;$client =newClient($tls,$requests);$host ='anduck.net';$port =50002;$client->connect($host,$port)->then(function (Connection$conn) {$conn->on('message',function ($message) {echo"Message received:".PHP_EOL;print_r($message);    });$conn->on(ElectrumClient::ADDRESS_SUBSCRIBE,function (AddressNotification$address) {echo"Received address update\n";    });$electrum =newElectrumClient($conn);$electrum->subscribeAddress('1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L')->then(function () {echo"subscribed\n";    });},function (\Exception$e) {echo"ERROR:" .$e->getMessage().PHP_EOL;});$loop->run();

Further Information

About

Implementation of the Stratum protocol (for electrum and mining) using ReactPHP

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp