1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Rpc\Client; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Address\AddressInterface; |
6
|
|
|
use BitWasp\Bitcoin\Block\BlockHeader; |
7
|
|
|
use BitWasp\Bitcoin\Math\Math; |
8
|
|
|
use BitWasp\Bitcoin\Network\NetworkInterface; |
9
|
|
|
use BitWasp\Bitcoin\Script\ScriptFactory; |
10
|
|
|
use BitWasp\Bitcoin\Transaction\OutPoint; |
11
|
|
|
use BitWasp\Bitcoin\Transaction\TransactionFactory; |
12
|
|
|
use BitWasp\Bitcoin\Transaction\TransactionInterface; |
13
|
|
|
use BitWasp\Bitcoin\Transaction\TransactionOutput; |
14
|
|
|
use BitWasp\Bitcoin\Utxo\Utxo; |
15
|
|
|
use BitWasp\Buffertools\Buffer; |
16
|
|
|
use BitWasp\Stratum\Client; |
17
|
|
|
use BitWasp\Stratum\Request\Response; |
18
|
|
|
|
19
|
|
|
class ElectrumServer |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Client |
23
|
|
|
*/ |
24
|
|
|
private $client; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Math |
28
|
|
|
*/ |
29
|
|
|
private $math; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Math $math |
33
|
|
|
* @param Client $client |
34
|
|
|
*/ |
35
|
48 |
|
public function __construct(Math $math, Client $client) |
36
|
|
|
{ |
37
|
48 |
|
$this->client = $client; |
38
|
48 |
|
$this->math = $math; |
39
|
48 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param TransactionInterface $transaction |
43
|
|
|
* @return \React\Promise\Promise |
44
|
|
|
*/ |
45
|
6 |
|
public function transactionBroadcast(TransactionInterface $transaction) |
46
|
|
|
{ |
47
|
6 |
|
return $this->client->request('blockchain.transaction.broadcast', [$transaction->getHex()]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param $txid |
52
|
|
|
* @return \React\Promise\Promise |
53
|
|
|
*/ |
54
|
6 |
|
public function transactionGet($txid) |
55
|
|
|
{ |
56
|
6 |
|
return $this->client->request('blockchain.transaction.get', [$txid]) |
57
|
|
|
->then(function (Response $response) { |
58
|
6 |
|
return TransactionFactory::fromHex($response->getResult()); |
59
|
6 |
|
}); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $txid |
64
|
|
|
* @param int $height |
65
|
|
|
* @return \React\Promise\Promise |
66
|
|
|
*/ |
67
|
6 |
|
public function transactionGetMerkle($txid, $height) |
68
|
|
|
{ |
69
|
6 |
|
return $this->client->request('blockchain.transaction.get_merkle', [$txid, $height]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param AddressInterface $address |
74
|
|
|
* @param NetworkInterface $network |
75
|
|
|
* @return \React\Promise\Promise |
76
|
|
|
*/ |
77
|
|
|
public function addressGetHistory(AddressInterface $address, NetworkInterface $network = null) |
78
|
|
|
{ |
79
|
|
|
return $this->client->request('blockchain.address.get_history', [$address->getAddress($network)]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param AddressInterface $address |
84
|
|
|
* @param NetworkInterface $network |
85
|
|
|
* @return \React\Promise\Promise |
86
|
|
|
*/ |
87
|
6 |
|
public function addressGetBalance(AddressInterface $address, NetworkInterface $network = null) |
88
|
|
|
{ |
89
|
6 |
|
return $this->client->request('blockchain.address.get_balance', [$address->getAddress($network)]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param AddressInterface $address |
94
|
|
|
* @param NetworkInterface $network |
95
|
|
|
* @return \React\Promise\Promise |
96
|
|
|
*/ |
97
|
|
|
public function addressGetProof(AddressInterface $address, NetworkInterface $network = null) |
98
|
|
|
{ |
99
|
|
|
return $this->client->request('blockchain.address.get_proof', [$address->getAddress($network)]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param AddressInterface $address |
104
|
|
|
* @param NetworkInterface $network |
105
|
|
|
* @return \React\Promise\Promise |
106
|
|
|
*/ |
107
|
6 |
|
public function addressListUnspent(AddressInterface $address, NetworkInterface $network = null) |
108
|
|
|
{ |
109
|
6 |
|
return $this->client->request('blockchain.address.listunspent', [$address->getAddress($network)]) |
110
|
|
|
->then(function (Response $response) use ($address) { |
111
|
6 |
|
return array_map( |
112
|
|
|
function (array $value) use ($address) { |
113
|
6 |
|
return new Utxo( |
114
|
6 |
|
new OutPoint( |
115
|
6 |
|
Buffer::hex($value['tx_hash'], 32), |
116
|
6 |
|
$value['tx_pos'] |
117
|
6 |
|
), |
118
|
6 |
|
new TransactionOutput( |
119
|
6 |
|
$value['value'], |
120
|
6 |
|
ScriptFactory::scriptPubKey()->payToAddress($address) |
121
|
6 |
|
) |
122
|
6 |
|
); |
123
|
6 |
|
}, |
124
|
6 |
|
$response->getResult() |
125
|
6 |
|
); |
126
|
6 |
|
}); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $txid |
131
|
|
|
* @param int|string $vout |
132
|
|
|
* @return \React\Promise\Promise |
133
|
|
|
*/ |
134
|
6 |
|
public function utxoGetAddress($txid, $vout) |
135
|
|
|
{ |
136
|
6 |
|
return $this->client->request('blockchain.utxo.get_address', [$txid, $vout]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param $height |
141
|
|
|
* @return \React\Promise\Promise |
142
|
|
|
*/ |
143
|
6 |
|
public function blockGetHeader($height) |
144
|
|
|
{ |
145
|
6 |
|
return $this->client->request('blockchain.block.get_header', [$height]) |
146
|
6 |
|
->then(function (Response $response) { |
147
|
6 |
|
$content = $response->getResult(); |
148
|
6 |
|
return new BlockHeader( |
149
|
6 |
|
$content['version'], |
150
|
6 |
|
isset($content['prev_block_hash']) ? Buffer::hex($content['prev_block_hash'], 32) : new Buffer('', 32), |
151
|
6 |
|
Buffer::hex($content['merkle_root'], 32), |
152
|
6 |
|
$content['timestamp'], |
153
|
6 |
|
Buffer::int($content['bits'], 4, $this->math), |
154
|
6 |
|
$content['nonce'] |
155
|
6 |
|
); |
156
|
6 |
|
}); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param int $height |
161
|
|
|
* @return \React\Promise\Promise |
162
|
|
|
*/ |
163
|
|
|
public function blockGetChunk($height) |
164
|
|
|
{ |
165
|
|
|
return $this->client->request('blockchain.block.get_chunk', [$height]); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|