|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Stratum\Api; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Stratum\Connection; |
|
6
|
|
|
|
|
7
|
|
|
class ElectrumClient |
|
8
|
|
|
{ |
|
9
|
|
|
const SERVER_BANNER = 'server.banner'; |
|
10
|
|
|
const SERVER_VERSION = 'server.version'; |
|
11
|
|
|
const SERVER_DONATION_ADDR = 'server.donation_address'; |
|
12
|
|
|
const SERVER_PEERS_SUBSCRIBE = 'server.peers.subscribe'; |
|
13
|
|
|
const NUMBLOCKS_SUBSCRIBE = 'blockchain.numblocks.subscribe'; |
|
14
|
|
|
const TRANSACTION_BROADCAST = 'blockchain.transaction.broadcast'; |
|
15
|
|
|
const HEADERS_SUBSCRIBE = 'blockchain.headers.subscribe'; |
|
16
|
|
|
const TRANSACTION_GET_MERKLE = 'blockchain.transaction.get_merkle'; |
|
17
|
|
|
const TRANSACTION_GET = 'blockchain.transaction.get'; |
|
18
|
|
|
const ADDRESS_SUBSCRIBE = 'blockchain.address.subscribe'; |
|
19
|
|
|
const ADDRESS_GET_HISTORY = 'blockchain.address.get_history'; |
|
20
|
|
|
const ADDRESS_GET_BALANCE = 'blockchain.address.get_balance'; |
|
21
|
|
|
const ADDRESS_GET_PROOF = 'blockchain.address.get_proof'; |
|
22
|
|
|
const ADDRESS_GET_MEMPOOL = 'blockchain.address.get_mempool'; |
|
23
|
|
|
const ADDRESS_LIST_UNSPENT = 'blockchain.address.listunspent'; |
|
24
|
|
|
const ESTIMATE_FEE = 'blockchain.estimatefee'; |
|
25
|
|
|
const RELAY_FEE = 'blockchain.relayfee'; |
|
26
|
|
|
const UTXO_GET_ADDRESS = 'blockchain.utxo.get_address'; |
|
27
|
|
|
const BLOCK_GET_HEADER = 'blockchain.block.get_header'; |
|
28
|
|
|
const BLOCK_GET_CHUNK = 'blockchain.block.get_chunk'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Connection |
|
32
|
|
|
*/ |
|
33
|
|
|
private $conn; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* ElectrumServer constructor. |
|
37
|
|
|
* @param Connection $connection |
|
38
|
|
|
*/ |
|
39
|
21 |
|
public function __construct(Connection $connection) |
|
40
|
|
|
{ |
|
41
|
21 |
|
$this->conn = $connection; |
|
42
|
21 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string $clientVersion |
|
46
|
|
|
* @param string $protocolVersion |
|
47
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function getServerVersion($clientVersion, $protocolVersion) |
|
50
|
|
|
{ |
|
51
|
2 |
|
return $this->conn->request(self::SERVER_VERSION, [$clientVersion, $protocolVersion]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function getServerBanner() |
|
58
|
|
|
{ |
|
59
|
1 |
|
return $this->conn->request(self::SERVER_BANNER); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function getDonationAddress() |
|
66
|
|
|
{ |
|
67
|
1 |
|
return $this->conn->request(self::SERVER_DONATION_ADDR); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function getServerPeers() |
|
74
|
|
|
{ |
|
75
|
1 |
|
return $this->conn->request(self::SERVER_PEERS_SUBSCRIBE); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function subscribeNumBlocks() |
|
82
|
|
|
{ |
|
83
|
1 |
|
return $this->conn->request(self::NUMBLOCKS_SUBSCRIBE); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function subscribeHeaders() |
|
90
|
|
|
{ |
|
91
|
1 |
|
return $this->conn->request(self::HEADERS_SUBSCRIBE); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $address |
|
96
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function subscribeAddress($address) |
|
99
|
|
|
{ |
|
100
|
1 |
|
return $this->conn->request(self::ADDRESS_SUBSCRIBE, [$address]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $hex |
|
105
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
106
|
|
|
*/ |
|
107
|
1 |
|
public function transactionBroadcast($hex) |
|
108
|
|
|
{ |
|
109
|
1 |
|
return $this->conn->request(self::TRANSACTION_BROADCAST, [$hex]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $txid |
|
114
|
|
|
* @param int $height |
|
115
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
116
|
|
|
*/ |
|
117
|
1 |
|
public function transactionGetMerkle($txid, $height) |
|
118
|
|
|
{ |
|
119
|
1 |
|
return $this->conn->request(self::TRANSACTION_GET_MERKLE, [$txid, $height]); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param string $txid |
|
124
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
125
|
|
|
*/ |
|
126
|
1 |
|
public function transactionGet($txid) |
|
127
|
|
|
{ |
|
128
|
1 |
|
return $this->conn->request(self::TRANSACTION_GET, [$txid]); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param string $address |
|
133
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
134
|
|
|
*/ |
|
135
|
1 |
|
public function addressGetHistory($address) |
|
136
|
|
|
{ |
|
137
|
1 |
|
return $this->conn->request(self::ADDRESS_GET_HISTORY, [$address]); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $address |
|
142
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function addressGetBalance($address) |
|
145
|
|
|
{ |
|
146
|
1 |
|
return $this->conn->request(self::ADDRESS_GET_BALANCE, [$address]); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param string $address |
|
151
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
152
|
|
|
*/ |
|
153
|
1 |
|
public function addressGetProof($address) |
|
154
|
|
|
{ |
|
155
|
1 |
|
return $this->conn->request(self::ADDRESS_GET_PROOF, [$address]); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param string $address |
|
160
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
161
|
|
|
*/ |
|
162
|
1 |
|
public function addressListUnspent($address) |
|
163
|
|
|
{ |
|
164
|
1 |
|
return $this->conn->request(self::ADDRESS_LIST_UNSPENT, [$address]); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param string $address |
|
169
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
170
|
|
|
*/ |
|
171
|
1 |
|
public function addressGetMempool($address) |
|
172
|
|
|
{ |
|
173
|
1 |
|
return $this->conn->request(self::ADDRESS_GET_MEMPOOL, [$address]); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param string $txid |
|
178
|
|
|
* @param int $nOutput |
|
179
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
180
|
|
|
*/ |
|
181
|
1 |
|
public function utxoGetAddress($txid, $nOutput) |
|
182
|
|
|
{ |
|
183
|
1 |
|
return $this->conn->request(self::UTXO_GET_ADDRESS, [$txid, $nOutput]); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
188
|
|
|
*/ |
|
189
|
1 |
|
public function estimateFee() |
|
190
|
|
|
{ |
|
191
|
1 |
|
return $this->conn->request(self::ESTIMATE_FEE); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
196
|
|
|
*/ |
|
197
|
1 |
|
public function relayFee() |
|
198
|
|
|
{ |
|
199
|
1 |
|
return $this->conn->request(self::RELAY_FEE); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param int $blockHeight |
|
204
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
205
|
|
|
*/ |
|
206
|
1 |
|
public function blockGetHeader($blockHeight) |
|
207
|
|
|
{ |
|
208
|
1 |
|
return $this->conn->request(self::BLOCK_GET_HEADER, [$blockHeight]); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @param int $blockHeight |
|
213
|
|
|
* @return \React\Promise\Promise|\React\Promise\PromiseInterface |
|
214
|
|
|
*/ |
|
215
|
1 |
|
public function blockGetChunk($blockHeight) |
|
216
|
|
|
{ |
|
217
|
1 |
|
return $this->conn->request(self::BLOCK_GET_CHUNK, [$blockHeight]); |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|