|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Blocktrail\SDK; |
|
4
|
|
|
|
|
5
|
|
|
abstract class Util { |
|
6
|
4 |
|
public static function all(callable $fn, array $arr) { |
|
7
|
4 |
|
$allvalues = array_map($fn, $arr); |
|
8
|
4 |
|
return count(array_unique($allvalues)) === 1 && end($allvalues) === true; |
|
9
|
|
|
} |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Given a $network string and $testnet bool, this function |
|
13
|
|
|
* will apply the old method (conditionally prepending 't' |
|
14
|
|
|
* to the network if ($testnet).. but also some new special |
|
15
|
|
|
* cases which ignore the testnet setting. |
|
16
|
|
|
* |
|
17
|
|
|
* @param string $network |
|
18
|
|
|
* @param bool $testnet |
|
19
|
|
|
* @return array |
|
20
|
|
|
*/ |
|
21
|
118 |
|
public static function parseApiNetwork($network, $testnet) { |
|
22
|
118 |
|
$network = strtoupper($network); |
|
23
|
|
|
|
|
24
|
|
|
// deal with testnet special cases |
|
25
|
118 |
|
if (strlen($network) === 4 && $network[0] === 'T') { |
|
26
|
4 |
|
$testnet = true; |
|
27
|
4 |
|
$network = substr($network, 1); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
118 |
|
if (strlen($network) === 3) { |
|
31
|
|
|
// work out apiNetwork |
|
32
|
118 |
|
$apiNetwork = $network; |
|
33
|
118 |
|
if ($testnet) { |
|
34
|
118 |
|
$apiNetwork = "t{$network}"; |
|
35
|
|
|
} |
|
36
|
4 |
|
} else if ($network === "RBTC") { |
|
37
|
|
|
// Regtest is magic |
|
38
|
2 |
|
$apiNetwork = "rBTC"; |
|
39
|
2 |
|
$testnet = true; |
|
40
|
2 |
|
} else if ($network === "RBCH") { |
|
41
|
|
|
// Regtest is magic |
|
42
|
1 |
|
$apiNetwork = "rBCH"; |
|
43
|
1 |
|
$testnet = true; |
|
44
|
|
|
} else { |
|
45
|
|
|
// Default to bitcoin if they make no sense. |
|
46
|
1 |
|
$apiNetwork = "BTC"; |
|
47
|
1 |
|
$testnet = false; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
118 |
|
return [$apiNetwork, $testnet]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* normalize network string |
|
55
|
|
|
* |
|
56
|
|
|
* @param $network |
|
57
|
|
|
* @param $testnet |
|
58
|
|
|
* @return array |
|
59
|
|
|
* @throws \Exception |
|
60
|
|
|
*/ |
|
61
|
118 |
|
public static function normalizeNetwork($network, $testnet) { |
|
62
|
118 |
|
$regtest = false; |
|
63
|
118 |
|
switch (strtolower($network)) { |
|
64
|
118 |
|
case 'btc': |
|
65
|
14 |
|
case 'bitcoin': |
|
66
|
118 |
|
$network = 'bitcoin'; |
|
67
|
118 |
|
break; |
|
68
|
|
|
|
|
69
|
14 |
|
case 'tbtc': |
|
70
|
11 |
|
case 'bitcoin-testnet': |
|
71
|
3 |
|
$network = 'bitcoin'; |
|
72
|
3 |
|
$testnet = true; |
|
73
|
3 |
|
break; |
|
74
|
11 |
|
case 'bcc': |
|
75
|
6 |
|
case 'bch': |
|
76
|
6 |
|
case 'bitcoincash': |
|
77
|
5 |
|
$network = 'bitcoincash'; |
|
78
|
5 |
|
break; |
|
79
|
|
|
|
|
80
|
6 |
|
case 'tbch': |
|
81
|
6 |
|
case 'tbcc': |
|
82
|
3 |
|
case 'bitcoincash-testnet': |
|
83
|
3 |
|
$network = 'bitcoincash'; |
|
84
|
3 |
|
$testnet = true; |
|
85
|
3 |
|
break; |
|
86
|
|
|
|
|
87
|
3 |
|
case 'rbtc': |
|
88
|
1 |
|
case 'bitcoin-regtest': |
|
89
|
2 |
|
$network = 'bitcoin'; |
|
90
|
2 |
|
$testnet = true; |
|
91
|
2 |
|
$regtest = true; |
|
92
|
2 |
|
break; |
|
93
|
|
|
|
|
94
|
1 |
|
case 'rbch': |
|
95
|
|
|
case 'bitcoincash-regtest': |
|
96
|
1 |
|
$network = 'bitcoincash'; |
|
97
|
1 |
|
$testnet = true; |
|
98
|
1 |
|
$regtest = true; |
|
99
|
1 |
|
break; |
|
100
|
|
|
|
|
101
|
|
|
default: |
|
102
|
|
|
throw new \Exception("Unknown network [{$network}]"); |
|
103
|
|
|
// this comment silences a phpcs error. |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
118 |
|
return [$network, $testnet, $regtest]; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
26 |
|
public static function arrayMapWithIndex(callable $fn, $arr) { |
|
110
|
26 |
|
$result = []; |
|
111
|
26 |
|
$assoc = null; |
|
112
|
26 |
|
foreach ($arr as $idx => $value) { |
|
113
|
26 |
|
list($newidx, $newvalue) = $fn($idx, $value); |
|
114
|
|
|
|
|
115
|
26 |
|
if ($assoc === null) { |
|
116
|
26 |
|
$assoc = $newidx !== null; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
26 |
|
if ($newidx === null && $assoc || $newidx !== null && !$assoc) { |
|
120
|
|
|
throw new \Exception("Mix of non assoc and assoc keys"); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
26 |
|
if ($assoc) { |
|
124
|
26 |
|
$result[$newidx] = $newvalue; |
|
125
|
|
|
} else { |
|
126
|
26 |
|
$result[] = $newvalue; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
26 |
|
return $result; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|