|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoind\Config; |
|
6
|
|
|
|
|
7
|
|
|
use BitWasp\Bitcoind\Exception\BitcoindException; |
|
8
|
|
|
use BitWasp\Bitcoind\Exception\ServerException; |
|
9
|
|
|
|
|
10
|
|
|
class Config |
|
11
|
|
|
{ |
|
12
|
|
|
private $options = []; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct(array $options = []) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->withOptions($options); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
private function withOptions(array $options) |
|
20
|
|
|
{ |
|
21
|
|
|
foreach ($options as $key => $option) { |
|
22
|
|
|
if (!is_string($key)) { |
|
23
|
|
|
throw new BitcoindException("Invalid config key"); |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
if ((array_key_exists('regtest', $options) && (bool) $options['regtest']) && |
|
28
|
|
|
(array_key_exists('testnet', $options) && (bool) $options['testnet'])) { |
|
29
|
|
|
throw new ServerException("Configuration conflicts, cannot be regtest and testnet"); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$this->options = $options; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function has(string $option): bool |
|
36
|
|
|
{ |
|
37
|
|
|
return array_key_exists($option, $this->options); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function get(string $option, string $default = null) |
|
41
|
|
|
{ |
|
42
|
|
|
if ($this->has($option)) { |
|
43
|
|
|
return $this->options[$option]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $default; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function all(): array |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->options; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function isRpcServer(): bool |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->has('server') && |
|
57
|
|
|
(bool) $this->get('server') |
|
58
|
|
|
; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function hasRpcCredential(): bool |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->has('rpcuser') && |
|
64
|
|
|
$this->has('rpcpassword'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getDefaultRpcPort(): int |
|
68
|
|
|
{ |
|
69
|
|
|
if ($this->isTestnet()) { |
|
70
|
|
|
return 18332; |
|
71
|
|
|
} else if ($this->isRegtest()) { |
|
72
|
|
|
return 18443; |
|
73
|
|
|
} else { |
|
74
|
|
|
return 8332; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getRpcDsn(): string |
|
79
|
|
|
{ |
|
80
|
|
|
$host = $this->get('rpcconnect', '127.0.0.1'); |
|
81
|
|
|
$port = (int) $this->get('rpcport', (string) $this->getDefaultRpcPort()); |
|
82
|
|
|
|
|
83
|
|
|
if (!$this->hasRpcCredential()) { |
|
84
|
|
|
throw new BitcoindException("Missing rpc credential fields"); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$user = $this->get('rpcuser'); |
|
88
|
|
|
$pass = $this->get('rpcpassword'); |
|
89
|
|
|
|
|
90
|
|
|
return "http://{$user}:{$pass}@{$host}:{$port}"; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function isTestnet(): bool |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->has('testnet') && (bool) $this->get('testnet'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function isRegtest(): bool |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->has('regtest') && (bool) $this->get('regtest'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function getRelativeChainPath(): string |
|
104
|
|
|
{ |
|
105
|
|
|
if ($this->isTestnet()) { |
|
106
|
|
|
return "testnet3/"; |
|
107
|
|
|
} else if ($this->isRegtest()) { |
|
108
|
|
|
return "regtest/"; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return ""; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private function getPathInChainDir($path): string |
|
115
|
|
|
{ |
|
116
|
|
|
return "{$this->getRelativeChainPath()}$path"; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function getRelativePidPath(): string |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->getPathInChainDir("bitcoind.pid"); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function getRelativeLogPath(): string |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->getPathInChainDir("debug.log"); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|