Skip to content

Commit 963546f

Browse files
committed
better exceptions
1 parent 274eab0 commit 963546f

18 files changed

+135
-72
lines changed

src/Crypto/PublicKey.php

+12-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace AndKom\Bitcoin\Blockchain\Crypto;
44

5-
use AndKom\Bitcoin\Blockchain\Exception\CryptoException;
5+
use AndKom\Bitcoin\Blockchain\Exceptions\PublicKeyException;
66
use AndKom\Bitcoin\Blockchain\Utils;
77
use Mdanter\Ecc\EccFactory;
88

@@ -57,12 +57,12 @@ public function getX(): \GMP
5757

5858
/**
5959
* @return \GMP
60-
* @throws CryptoException
60+
* @throws PublicKeyException
6161
*/
6262
public function getY(): \GMP
6363
{
6464
if ($this->isCompressed()) {
65-
throw new CryptoException("Compressed public key doesn't have Y coordinate.");
65+
throw new PublicKeyException("Compressed public key doesn't have Y coordinate.");
6666
}
6767

6868
return $this->y;
@@ -86,12 +86,12 @@ public function isCompressed(): bool
8686

8787
/**
8888
* @return PublicKey
89-
* @throws CryptoException
89+
* @throws PublicKeyException
9090
*/
9191
public function compress(): self
9292
{
9393
if ($this->isCompressed()) {
94-
throw new CryptoException('Public key is already compressed.');
94+
throw new PublicKeyException('Public key is already compressed.');
9595
}
9696

9797
$wasOdd = gmp_cmp(
@@ -104,12 +104,12 @@ public function compress(): self
104104

105105
/**
106106
* @return PublicKey
107-
* @throws CryptoException
107+
* @throws PublicKeyException
108108
*/
109109
public function decompress(): self
110110
{
111111
if (!$this->isCompressed()) {
112-
throw new CryptoException('Public key is already decompressed.');
112+
throw new PublicKeyException('Public key is already decompressed.');
113113
}
114114

115115
$curve = EccFactory::getSecgCurves()->generator256k1()->getCurve();
@@ -120,7 +120,7 @@ public function decompress(): self
120120

121121
/**
122122
* @return \Mdanter\Ecc\Crypto\Key\PublicKey
123-
* @throws CryptoException
123+
* @throws PublicKeyException
124124
*/
125125
public function getEccPublicKey(): \Mdanter\Ecc\Crypto\Key\PublicKey
126126
{
@@ -142,7 +142,7 @@ public function getEccPublicKey(): \Mdanter\Ecc\Crypto\Key\PublicKey
142142
/**
143143
* @param string $data
144144
* @return PublicKey
145-
* @throws CryptoException
145+
* @throws PublicKeyException
146146
*/
147147
static public function parse(string $data): self
148148
{
@@ -152,7 +152,7 @@ static public function parse(string $data): self
152152
$prefix = $data[0];
153153

154154
if ($prefix != static::PREFIX_COMPRESSED_ODD && $prefix != static::PREFIX_COMPRESSED_EVEN) {
155-
throw new CryptoException('Invalid compressed public key prefix.');
155+
throw new PublicKeyException('Invalid compressed public key prefix.');
156156
}
157157

158158
$x = Utils::binToGmp(substr($data, 1, 32));
@@ -161,13 +161,13 @@ static public function parse(string $data): self
161161
$prefix = $data[0];
162162

163163
if ($prefix != static::PREFIX_UNCOMPRESSED) {
164-
throw new CryptoException('Invalid uncompressed public key prefix.');
164+
throw new PublicKeyException('Invalid uncompressed public key prefix.');
165165
}
166166

167167
$x = Utils::binToGmp(substr($data, 1, 32));
168168
$y = Utils::binToGmp(substr($data, 33, 32));
169169
} else {
170-
throw new CryptoException('Invalid public key size.');
170+
throw new PublicKeyException('Invalid public key size.');
171171
}
172172

173173
return new static($x, $y, $prefix == static::PREFIX_COMPRESSED_ODD);

src/Database/BlockIndex.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace AndKom\Bitcoin\Blockchain\Database;
66

7-
use AndKom\Bitcoin\Blockchain\Exception\DatabaseException;
7+
use AndKom\Bitcoin\Blockchain\Exceptions\DatabaseException;
88

99
/**
1010
* Class BlockIndex

src/Database/BlockInfo.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use AndKom\BCDataStream\Reader;
88
use AndKom\Bitcoin\Blockchain\Block\Header;
9-
use AndKom\Bitcoin\Blockchain\Exception\DatabaseException;
9+
use AndKom\Bitcoin\Blockchain\Exceptions\DatabaseException;
1010

1111
/**
1212
* Class BlockInfo

src/Database/UnspentOutput.php

+19-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use AndKom\BCDataStream\Reader;
88
use AndKom\Bitcoin\Blockchain\Crypto\PublicKey;
9-
use AndKom\Bitcoin\Blockchain\Exception\ScriptException;
9+
use AndKom\Bitcoin\Blockchain\Exceptions\OutputDecodeException;
1010
use AndKom\Bitcoin\Blockchain\Script\Opcodes;
1111
use AndKom\Bitcoin\Blockchain\Script\ScriptPubKey;
1212

@@ -16,8 +16,6 @@
1616
*/
1717
class UnspentOutput
1818
{
19-
const SPECIAL_SCRIPTS = 6;
20-
2119
/**
2220
* @var string
2321
*/
@@ -104,9 +102,25 @@ static public function decompressAmount(int $x)
104102
return $n;
105103
}
106104

105+
/**
106+
* @param string $compressed
107+
* @return string
108+
* @throws OutputDecodeException
109+
*/
110+
static public function decompressPubKey(string $compressed): string
111+
{
112+
try {
113+
$decompressed = PublicKey::parse($compressed)->decompress()->serialize();
114+
} catch (\Exception $exception) {
115+
throw new OutputDecodeException('Unable to decompress public key.', 0, $exception);
116+
}
117+
118+
return $decompressed;
119+
}
120+
107121
/**
108122
* @return ScriptPubKey
109-
* @throws ScriptException
123+
* @throws OutputDecodeException
110124
*/
111125
public function decompressScript(): ScriptPubKey
112126
{
@@ -137,14 +151,8 @@ public function decompressScript(): ScriptPubKey
137151

138152
case 4:
139153
case 5:
140-
$compressed = chr($this->type - 2) . $this->script;
141-
try {
142-
$decompressed = PublicKey::parse($compressed)->decompress()->serialize();
143-
} catch (\Exception $exception) {
144-
throw new ScriptException('Unable to decompress public key.', 0, $exception);
145-
}
146154
$script = chr(65);
147-
$script .= $decompressed;
155+
$script .= static::decompressPubKey(chr($this->type - 2) . $this->script);
148156
$script .= chr(Opcodes::OP_CHECKSIG);
149157
break;
150158

src/Exception/CryptoException.php

-13
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AndKom\Bitcoin\Blockchain\Exceptions;
6+
7+
/**
8+
* Class AddressSerializeException
9+
* @package AndKom\Bitcoin\Blockchain\Exceptions
10+
*/
11+
class AddressSerializeException extends Exception
12+
{
13+
}

src/Exception/DatabaseException.php src/Exceptions/DatabaseException.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
declare(strict_types=1);
44

5-
namespace AndKom\Bitcoin\Blockchain\Exception;
5+
namespace AndKom\Bitcoin\Blockchain\Exceptions;
66

77
/**
88
* Class DatabaseException
9-
* @package AndKom\Bitcoin\Blockchain\Exception
9+
* @package AndKom\Bitcoin\Blockchain\Exceptions
1010
*/
1111
class DatabaseException extends Exception
1212
{

src/Exception/Exception.php src/Exceptions/Exception.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
declare(strict_types=1);
44

5-
namespace AndKom\Bitcoin\Blockchain\Exception;
5+
namespace AndKom\Bitcoin\Blockchain\Exceptions;
66

77
/**
88
* Class Exception
9-
* @package AndKom\Bitcoin\Blockchain\Exception
9+
* @package AndKom\Bitcoin\Blockchain\Exceptions
1010
*/
1111
class Exception extends \Exception
1212
{
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AndKom\Bitcoin\Blockchain\Exceptions;
6+
7+
/**
8+
* Class OutputDecodeException
9+
* @package AndKom\Bitcoin\Blockchain\Exceptions
10+
*/
11+
class OutputDecodeException extends Exception
12+
{
13+
}

src/Exceptions/PublicKeyException.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AndKom\Bitcoin\Blockchain\Exceptions;
6+
7+
/**
8+
* Class PublicKeyException
9+
* @package AndKom\Bitcoin\Blockchain\Exceptions
10+
*/
11+
class PublicKeyException extends Exception
12+
{
13+
}

src/Exception/ScriptException.php src/Exceptions/ScriptException.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
declare(strict_types=1);
44

5-
namespace AndKom\Bitcoin\Blockchain\Exception;
5+
namespace AndKom\Bitcoin\Blockchain\Exceptions;
66

77
/**
88
* Class ScriptException
9-
* @package AndKom\Bitcoin\Blockchain\Exception
9+
* @package AndKom\Bitcoin\Blockchain\Exceptions
1010
*/
1111
class ScriptException extends Exception
1212
{

src/Reader/BlockFileReader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use AndKom\BCDataStream\Reader;
88
use AndKom\Bitcoin\Blockchain\Block\Block;
9-
use AndKom\Bitcoin\Blockchain\Exception\DatabaseException;
9+
use AndKom\Bitcoin\Blockchain\Exceptions\DatabaseException;
1010

1111
/**
1212
* Class BlockFileReader

src/Reader/BlockIndexReader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use AndKom\Bitcoin\Blockchain\Database\BlockIndex;
99
use AndKom\Bitcoin\Blockchain\Database\BlockInfo;
1010
use AndKom\Bitcoin\Blockchain\Database\FileInfo;
11-
use AndKom\Bitcoin\Blockchain\Exception\DatabaseException;
11+
use AndKom\Bitcoin\Blockchain\Exceptions\DatabaseException;
1212

1313
/**
1414
* Class BlockIndexReader

src/Reader/ChainstateReader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use AndKom\BCDataStream\Reader;
88
use AndKom\Bitcoin\Blockchain\Database\UnspentOutput;
9-
use AndKom\Bitcoin\Blockchain\Exception\DatabaseException;
9+
use AndKom\Bitcoin\Blockchain\Exceptions\DatabaseException;
1010
use AndKom\Bitcoin\Blockchain\Utils;
1111

1212
/**

src/Reader/DatabaseReader.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use AndKom\Bitcoin\Blockchain\Block\Block;
88
use AndKom\Bitcoin\Blockchain\Database\BlockIndex;
99
use AndKom\Bitcoin\Blockchain\Database\BlockInfo;
10-
use AndKom\Bitcoin\Blockchain\Exception\DatabaseException;
10+
use AndKom\Bitcoin\Blockchain\Exceptions\DatabaseException;
1111

1212
/**
1313
* Class DatabaseReader

src/Script/Script.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace AndKom\Bitcoin\Blockchain\Script;
66

77
use AndKom\BCDataStream\Reader;
8-
use AndKom\Bitcoin\Blockchain\Exception\ScriptException;
8+
use AndKom\Bitcoin\Blockchain\Exceptions\ScriptException;
99

1010
/**
1111
* Class Script

src/Script/ScriptPubKey.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
namespace AndKom\Bitcoin\Blockchain\Script;
66

77
use AndKom\Bitcoin\Blockchain\Crypto\PublicKey;
8-
use AndKom\Bitcoin\Blockchain\Exception\ScriptException;
8+
use AndKom\Bitcoin\Blockchain\Exceptions\AddressSerializeException;
9+
use AndKom\Bitcoin\Blockchain\Exceptions\OutputDecodeException;
910
use AndKom\Bitcoin\Blockchain\Network\NetworkInterface;
1011
use AndKom\Bitcoin\Blockchain\Serializer\AddressSerializer;
1112

@@ -158,11 +159,10 @@ public function isPayToWitnessScriptHash(): bool
158159
}
159160

160161
/**
161-
* @param NetworkInterface $network
162+
* @param NetworkInterface|null $network
162163
* @return string
163-
* @throws ScriptException
164-
* @throws \Exception
165-
* @throws \BitWasp\Bech32\Exception\Bech32Exception
164+
* @throws OutputDecodeException
165+
* @throws AddressSerializeException
166166
*/
167167
public function getOutputAddress(NetworkInterface $network = null): string
168168
{
@@ -184,7 +184,7 @@ public function getOutputAddress(NetworkInterface $network = null): string
184184
}
185185

186186
if (!PublicKey::isFullyValid($pubKey)) {
187-
throw new ScriptException('Unable to decode output address (invalid public key).');
187+
throw new OutputDecodeException('Unable to decode output address (invalid public key).');
188188
}
189189

190190
return $addressSerializer->getPayToPubKey($pubKey);
@@ -199,21 +199,21 @@ public function getOutputAddress(NetworkInterface $network = null): string
199199
}
200200

201201
if ($this->isMultisig()) {
202-
throw new ScriptException('Unable to decode output address (multisig).');
202+
throw new OutputDecodeException('Unable to decode output address (multisig).');
203203
}
204204

205205
if ($this->isReturn()) {
206-
throw new ScriptException('Unable to decode output address (OP_RETURN).');
206+
throw new OutputDecodeException('Unable to decode output address (OP_RETURN).');
207207
}
208208

209209
if ($this->isEmpty()) {
210-
throw new ScriptException('Unable to decode output address (empty).');
210+
throw new OutputDecodeException('Unable to decode output address (empty).');
211211
}
212212

213213
if ($this->isPayToPubKeyHashAlt()) {
214214
return $addressSerializer->getPayToPubKeyHash(substr($this->data, 4, 20));
215215
}
216216

217-
throw new ScriptException('Unable to decode output address.');
217+
throw new OutputDecodeException('Unable to decode output address.');
218218
}
219219
}

0 commit comments

Comments
 (0)