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

Lines changed: 12 additions & 12 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 19 additions & 11 deletions
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

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
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 renamed to src/Exceptions/DatabaseException.php

Lines changed: 2 additions & 2 deletions
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 renamed to src/Exceptions/Exception.php

Lines changed: 2 additions & 2 deletions
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
{
Lines changed: 13 additions & 0 deletions
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+
}
Lines changed: 13 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)