Skip to content

Commit 218bea4

Browse files
committed
refactoring
1 parent 5c48bf4 commit 218bea4

32 files changed

+382
-240
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,28 @@ composer require andkom/php-bitcoin-blockchain
2626
### Examples
2727

2828
```php
29-
$blockchainReader = new BlockchainReader('/path/to/bitcoin');
29+
$databaseReader = new DatabaseReader('/path/to/bitcoin');
3030

3131
// read ordered blocks
32-
foreach ($blockchainReader->readBlocks() as $block) {
32+
foreach ($databaseReader->readBlocks() as $block) {
3333
}
3434

3535
// read unordered blocks
36-
foreach ($blockchainReader->readBlocksUnordered() as $block) {
36+
foreach ($databaseReader->readBlocksUnordered() as $block) {
3737
}
3838

3939
// read UTXO
40-
foreach ($blockchainReader->getChainState()->read() as $utxo) {
40+
foreach ($databaseReader->getChainstate()->read() as $utxo) {
4141
}
4242

4343
// get block by hash
44-
$block = $blockchainReader->getBlockByHash('binary hash in little endian');
44+
$block = $databaseReader->getBlockByHash('binary hash in little endian');
4545

4646
// get block by height
47-
$block = $blockchainReader->getBlockByHeight(12345);
47+
$block = $databaseReader->getBlockByHeight(12345);
4848

4949
// get best block hash
50-
$hash = $blockchainReader->getChainstate()->getBestBlock();
50+
$hash = $databaseReader->getChainstate()->getBestBlock();
5151
```
5252

5353
See more examples in the examples dir.

src/Block.php renamed to src/Block/Block.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
declare(strict_types=1);
44

5-
namespace AndKom\Bitcoin\Blockchain;
5+
namespace AndKom\Bitcoin\Blockchain\Block;
66

77
use AndKom\BCDataStream\Reader;
88
use AndKom\BCDataStream\Writer;
9+
use AndKom\Bitcoin\Blockchain\Transaction\Transaction;
910

1011
/**
1112
* Class Block
12-
* @package AndKom\Bitcoin\Blockchain
13+
* @package AndKom\Bitcoin\Blockchain\Block
1314
*/
1415
class Block
1516
{
@@ -34,7 +35,7 @@ class Block
3435
*/
3536
static public function parse(Reader $stream): self
3637
{
37-
$block = new self;
38+
$block = new static;
3839
$block->header = Header::parse($stream);
3940
$block->transactionsCount = $stream->readCompactSize();
4041

src/Header.php renamed to src/Block/Header.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
declare(strict_types=1);
44

5-
namespace AndKom\Bitcoin\Blockchain;
5+
namespace AndKom\Bitcoin\Blockchain\Block;
66

77
use AndKom\BCDataStream\Reader;
88
use AndKom\BCDataStream\Writer;
9+
use AndKom\Bitcoin\Blockchain\Utils;
910

1011
/**
1112
* Class Header
12-
* @package AndKom\Bitcoin\Blockchain
13+
* @package AndKom\Bitcoin\Blockchain\Block
1314
*/
1415
class Header
1516
{
@@ -49,7 +50,7 @@ class Header
4950
*/
5051
static public function parse(Reader $stream): self
5152
{
52-
$header = new self;
53+
$header = new static;
5354
$header->version = $stream->readUInt32();
5455
$header->prevBlockHash = $stream->read(32);
5556
$header->merkleRootHash = $stream->read(32);
Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?php
22

3-
namespace AndKom\Bitcoin\Blockchain;
3+
namespace AndKom\Bitcoin\Blockchain\Crypto;
44

5-
use AndKom\Bitcoin\Blockchain\Exception\PublicKeyException;
5+
use AndKom\Bitcoin\Blockchain\Exception\CryptoException;
6+
use AndKom\Bitcoin\Blockchain\Utils;
67
use Mdanter\Ecc\EccFactory;
78

89
/**
910
* Class PublicKey
10-
* @package AndKom\Bitcoin\Blockchain
11+
* @package AndKom\Bitcoin\Blockchain\Crypto
1112
*/
1213
class PublicKey
1314
{
@@ -56,12 +57,12 @@ public function getX(): \GMP
5657

5758
/**
5859
* @return \GMP
59-
* @throws PublicKeyException
60+
* @throws CryptoException
6061
*/
6162
public function getY(): \GMP
6263
{
6364
if ($this->isCompressed()) {
64-
throw new PublicKeyException("Compressed public key doesn't have Y coordinate.");
65+
throw new CryptoException("Compressed public key doesn't have Y coordinate.");
6566
}
6667

6768
return $this->y;
@@ -83,28 +84,14 @@ public function isCompressed(): bool
8384
return is_null($this->y);
8485
}
8586

86-
/**
87-
* @return bool
88-
*/
89-
public function isValid(): bool
90-
{
91-
try {
92-
$this->getEccPublicKey();
93-
} catch (\Exception $exception) {
94-
return false;
95-
}
96-
97-
return true;
98-
}
99-
10087
/**
10188
* @return PublicKey
102-
* @throws PublicKeyException
89+
* @throws CryptoException
10390
*/
10491
public function compress(): self
10592
{
10693
if ($this->isCompressed()) {
107-
throw new PublicKeyException('Public key is already compressed.');
94+
throw new CryptoException('Public key is already compressed.');
10895
}
10996

11097
$wasOdd = gmp_cmp(
@@ -117,12 +104,12 @@ public function compress(): self
117104

118105
/**
119106
* @return PublicKey
120-
* @throws PublicKeyException
107+
* @throws CryptoException
121108
*/
122109
public function decompress(): self
123110
{
124111
if (!$this->isCompressed()) {
125-
throw new PublicKeyException('Public key is already decompressed.');
112+
throw new CryptoException('Public key is already decompressed.');
126113
}
127114

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

134121
/**
135122
* @return \Mdanter\Ecc\Crypto\Key\PublicKey
136-
* @throws PublicKeyException
123+
* @throws CryptoException
137124
*/
138125
public function getEccPublicKey(): \Mdanter\Ecc\Crypto\Key\PublicKey
139126
{
@@ -155,7 +142,7 @@ public function getEccPublicKey(): \Mdanter\Ecc\Crypto\Key\PublicKey
155142
/**
156143
* @param string $data
157144
* @return PublicKey
158-
* @throws PublicKeyException
145+
* @throws CryptoException
159146
*/
160147
static public function parse(string $data): self
161148
{
@@ -165,7 +152,7 @@ static public function parse(string $data): self
165152
$prefix = $data[0];
166153

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

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

176163
if ($prefix != static::PREFIX_UNCOMPRESSED) {
177-
throw new PublicKeyException('Invalid uncompressed public key prefix.');
164+
throw new CryptoException('Invalid uncompressed public key prefix.');
178165
}
179166

180167
$x = Utils::binToGmp(substr($data, 1, 32));
181168
$y = Utils::binToGmp(substr($data, 33, 32));
182169
} else {
183-
throw new PublicKeyException('Invalid public key size.');
170+
throw new CryptoException('Invalid public key size.');
184171
}
185172

186173
return new static($x, $y, $prefix == static::PREFIX_COMPRESSED_ODD);
@@ -203,4 +190,43 @@ public function serialize(): string
203190

204191
return $prefix . $x . $y;
205192
}
193+
194+
/**
195+
* @param string $pubKey
196+
* @return bool
197+
*/
198+
static public function isValid(string $pubKey): bool
199+
{
200+
$length = strlen($pubKey);
201+
202+
if ($length == static::LENGTH_COMPRESSED && ($pubKey[0] == static::PREFIX_COMPRESSED_ODD || $pubKey[0] == static::PREFIX_COMPRESSED_EVEN)) {
203+
return true;
204+
}
205+
206+
if ($length == static::LENGTH_UNCOMPRESSED && $pubKey[0] == static::PREFIX_UNCOMPRESSED) {
207+
return true;
208+
}
209+
210+
return false;
211+
}
212+
213+
/**
214+
* @param string $pubKey
215+
* @return bool
216+
*/
217+
static public function isFullyValid(string $pubKey): bool
218+
{
219+
if (!static::isValid($pubKey)) {
220+
return false;
221+
}
222+
223+
try {
224+
$key = static::parse($pubKey);
225+
$key->getEccPublicKey();
226+
} catch (\Exception $exception) {
227+
return false;
228+
}
229+
230+
return true;
231+
}
206232
}

src/Index.php renamed to src/Database/BlockIndex.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
declare(strict_types=1);
44

5-
namespace AndKom\Bitcoin\Blockchain;
5+
namespace AndKom\Bitcoin\Blockchain\Database;
66

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

99
/**
10-
* Class Index
11-
* @package AndKom\Bitcoin\Blockchain
10+
* Class BlockIndex
11+
* @package AndKom\Bitcoin\Blockchain\Database
1212
*/
13-
class Index
13+
class BlockIndex
1414
{
1515
/**
1616
* @var array
@@ -35,12 +35,12 @@ class Index
3535
/**
3636
* @param string $hash
3737
* @return BlockInfo
38-
* @throws Exception
38+
* @throws DatabaseException
3939
*/
4040
public function getBlockInfoByHash(string $hash): BlockInfo
4141
{
4242
if (!isset($this->blockIndex[$hash])) {
43-
throw new Exception("Unknown block hash.");
43+
throw new DatabaseException('Unknown block hash.');
4444
}
4545

4646
return $this->blockIndex[$hash];
@@ -49,12 +49,12 @@ public function getBlockInfoByHash(string $hash): BlockInfo
4949
/**
5050
* @param int $height
5151
* @return BlockInfo
52-
* @throws Exception
52+
* @throws DatabaseException
5353
*/
5454
public function getBlockInfoByHeight(int $height): BlockInfo
5555
{
5656
if (!isset($this->heightIndex[$height])) {
57-
throw new Exception("Unknown block height.");
57+
throw new DatabaseException('Unknown block height.');
5858
}
5959

6060
return $this->getBlockInfoByHash($this->heightIndex[$height]);
@@ -63,25 +63,25 @@ public function getBlockInfoByHeight(int $height): BlockInfo
6363
/**
6464
* @param int $number
6565
* @return FileInfo
66-
* @throws Exception
66+
* @throws DatabaseException
6767
*/
6868
public function getFileInfo(int $number): FileInfo
6969
{
7070
if (!isset($this->fileIndex[$number])) {
71-
throw new Exception("Unknown file number.");
71+
throw new DatabaseException('Unknown file number.');
7272
}
7373

7474
return $this->fileIndex[$number];
7575
}
7676

7777
/**
7878
* @return int
79-
* @throws Exception
79+
* @throws DatabaseException
8080
*/
8181
public function getLastFile(): int
8282
{
8383
if (is_null($this->lastFile)) {
84-
throw new Exception('Unknown last file.');
84+
throw new DatabaseException('Unknown last file.');
8585
}
8686

8787
return $this->lastFile;
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
declare(strict_types=1);
44

5-
namespace AndKom\Bitcoin\Blockchain;
5+
namespace AndKom\Bitcoin\Blockchain\Database;
66

77
use AndKom\BCDataStream\Reader;
8-
use AndKom\Bitcoin\Blockchain\Exception\Exception;
8+
use AndKom\Bitcoin\Blockchain\Block\Header;
9+
use AndKom\Bitcoin\Blockchain\Exception\DatabaseException;
910

1011
/**
1112
* Class BlockInfo
12-
* @package AndKom\Bitcoin\Blockchain
13+
* @package AndKom\Bitcoin\Blockchain\Database
1314
*/
1415
class BlockInfo
1516
{
@@ -77,7 +78,7 @@ class BlockInfo
7778
*/
7879
static public function parse(Reader $reader): self
7980
{
80-
$block = new self;
81+
$block = new static;
8182
$block->version = $reader->readVarInt();
8283
$block->height = $reader->readVarInt();
8384
$block->status = $reader->readVarInt();
@@ -102,12 +103,12 @@ static public function parse(Reader $reader): self
102103

103104
/**
104105
* @return string
105-
* @throws Exception
106+
* @throws DatabaseException
106107
*/
107108
public function getFileName(): string
108109
{
109110
if (is_null($this->file)) {
110-
throw new Exception('Unknown block file number.');
111+
throw new DatabaseException('Unknown block file number.');
111112
}
112113

113114
return sprintf('blk%05d.dat', $this->file);

0 commit comments

Comments
 (0)