1 | <?php |
||
9 | |||
10 | class RegtestBitcoinFactory |
||
11 | { |
||
12 | const TESTS_DIR = "BITCOIND_TEST_DIR"; |
||
13 | const BITCOIND = "BITCOIND_PATH"; |
||
14 | |||
15 | /** |
||
16 | * @var array|false|null|string |
||
17 | */ |
||
18 | private $testsDirPath; |
||
19 | |||
20 | /** |
||
21 | * @var array|false|null|string |
||
22 | */ |
||
23 | private $bitcoindPath; |
||
24 | |||
25 | /** |
||
26 | * @var string[] |
||
27 | */ |
||
28 | private $testDir = []; |
||
29 | |||
30 | /** |
||
31 | * @var RpcServer[] |
||
32 | */ |
||
33 | private $server = []; |
||
34 | |||
35 | /** |
||
36 | * @var \BitWasp\Bitcoin\Network\NetworkInterface |
||
37 | */ |
||
38 | private $network; |
||
39 | |||
40 | /** |
||
41 | * @var RpcCredential |
||
42 | */ |
||
43 | private $credential; |
||
44 | |||
45 | public function __construct() |
||
46 | { |
||
47 | $this->testsDirPath = $this->envOrDefault("BITCOIND_TEST_DIR", "/tmp"); |
||
48 | $this->bitcoindPath = $this->envOrDefault("BITCOIND_PATH"); |
||
49 | if (null === $this->bitcoindPath) { |
||
50 | throw new \RuntimeException("Missing BITCOIND_PATH variable"); |
||
51 | } |
||
52 | |||
53 | $this->network = NetworkFactory::bitcoinTestnet(); |
||
54 | $this->credential = new RpcCredential("127.0.0.1", 18332, "rpcuser", "rpcpass", false); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param string $var |
||
59 | * @param string|null $default |
||
60 | * @return string |
||
61 | */ |
||
62 | private function envOrDefault(string $var, string $default = null): string |
||
63 | { |
||
64 | $value = getenv($var); |
||
65 | if (in_array($value, [null, false, ""])) { |
||
66 | $value = $default; |
||
67 | } |
||
68 | return $value; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @return string |
||
73 | * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure |
||
74 | */ |
||
75 | protected function createRandomTestDir(): string |
||
76 | { |
||
77 | $this->testDir[] = $dir = $this->testsDirPath . "/" . (new Random())->bytes(5)->getHex(); |
||
78 | if (!mkdir($dir)) { |
||
79 | throw new \RuntimeException("Failed to create test dir!"); |
||
80 | } |
||
81 | return $dir; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param array $options |
||
86 | * @return RpcServer |
||
87 | * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure |
||
120 |