@@ -7,35 +7,37 @@ class AddressValidator
7
7
private $ includeTestnet = false ;
8
8
private $ onlyTestnet = false ;
9
9
10
+ public function __construct ()
11
+ {
12
+ if (extension_loaded ('bcmath ' ) === false ) {
13
+ throw new \RuntimeException (
14
+ 'The required BCMath extension is missing. Please install it to use this package. '
15
+ );
16
+ }
17
+ }
18
+
10
19
/**
11
20
* Allow both mainnet and testnet addresses.
12
- *
13
- * @return AddressValidator
14
21
*/
15
- public function includeTestnet ()
22
+ public function includeTestnet (): AddressValidator
16
23
{
17
24
$ this ->includeTestnet = true ;
18
25
return $ this ;
19
26
}
20
27
21
28
/**
22
29
* Allow only testnet addresses.
23
- *
24
- * @return AddressValidator
25
30
*/
26
- public function onlyTestnet ()
31
+ public function onlyTestnet (): AddressValidator
27
32
{
28
33
$ this ->onlyTestnet = true ;
29
34
return $ this ;
30
35
}
31
36
32
37
/**
33
38
* Validates a given address.
34
- *
35
- * @param string $address
36
- * @return boolean
37
39
*/
38
- public function isValid ($ address )
40
+ public function isValid (string $ address ): bool
39
41
{
40
42
if ($ this ->isPayToPublicKeyHash ($ address )) {
41
43
return true ;
@@ -58,53 +60,46 @@ public function isValid($address)
58
60
59
61
/**
60
62
* Validates a P2PKH address.
61
- *
62
- * @param string $address
63
- * @return boolean
64
63
*/
65
- public function isPayToPublicKeyHash ($ address )
64
+ public function isPayToPublicKeyHash (string $ address ): bool
66
65
{
67
66
$ prefix = $ this ->onlyTestnet ? 'nm ' : ($ this ->includeTestnet ? '1nm ' : '1 ' );
68
67
$ expr = sprintf ('/^[%s][a-km-zA-HJ-NP-Z1-9]{25,34}$/ ' , $ prefix );
69
68
70
69
if (preg_match ($ expr , $ address ) === 1 ) {
71
70
try {
72
- $ base58 = new Base58 ;
73
- return $ base58 ->verify ($ address );
71
+ return (new Base58 )->verify ($ address );
74
72
} catch (\Throwable $ th ) {
75
73
return false ;
76
74
}
77
75
}
76
+
77
+ return false ;
78
78
}
79
79
80
80
/**
81
81
* Validates a P2SH (segwit) address.
82
- *
83
- * @param string $address
84
- * @return boolean
85
82
*/
86
- public function isPayToScriptHash ($ address )
83
+ public function isPayToScriptHash (string $ address ): bool
87
84
{
88
85
$ prefix = $ this ->onlyTestnet ? '2 ' : ($ this ->includeTestnet ? '23 ' : '3 ' );
89
86
$ expr = sprintf ('/^[%s][a-km-zA-HJ-NP-Z1-9]{25,34}$/ ' , $ prefix );
90
87
91
88
if (preg_match ($ expr , $ address ) === 1 ) {
92
89
try {
93
- $ base58 = new Base58 ;
94
- return $ base58 ->verify ($ address );
90
+ return (new Base58 )->verify ($ address );
95
91
} catch (\Throwable $ th ) {
96
92
return false ;
97
93
}
98
94
}
95
+
96
+ return false ;
99
97
}
100
98
101
99
/**
102
100
* Validates a P2TR (taproot) address.
103
- *
104
- * @param string $address
105
- * @return boolean
106
101
*/
107
- public function isPayToTaproot ($ address )
102
+ public function isPayToTaproot (string $ address ): bool
108
103
{
109
104
if (in_array (substr ($ address , 0 , 4 ), ['bc1p ' , 'bcrt1p ' , 'tb1p ' ]) === false ) {
110
105
return false ;
@@ -131,11 +126,8 @@ public function isPayToTaproot($address)
131
126
132
127
/**
133
128
* Validates a bech32 (native segwit) address.
134
- *
135
- * @param string $address
136
- * @return boolean
137
129
*/
138
- public function isBech32 ($ address )
130
+ public function isBech32 (string $ address ): bool
139
131
{
140
132
$ prefix = $ this ->onlyTestnet ? 'tb ' : ($ this ->includeTestnet ? 'bc|tb ' : 'bc ' );
141
133
$ expr = sprintf (
0 commit comments