Skip to content

Commit 274eab0

Browse files
committed
fixes
1 parent a3f6f36 commit 274eab0

File tree

7 files changed

+37
-28
lines changed

7 files changed

+37
-28
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
],
1616
"require": {
1717
"php": "^7.1",
18-
"ext-gmp": "^7.1",
19-
"ext-leveldb": "^0.2.1",
18+
"ext-gmp": "*",
19+
"ext-leveldb": "*",
2020
"stephenhill/base58": "^1.1",
2121
"bitwasp/bech32": "^0.0.1",
2222
"andkom/php-bcdatastream": "^1.1",

examples/read_blockchain.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
$dataDir = getenv('HOME') . '/Library/Application Support/Bitcoin';
66

7-
$reader = new \AndKom\Bitcoin\Blockchain\BlockchainReader($dataDir);
7+
$reader = new \AndKom\Bitcoin\Blockchain\Reader\DatabaseReader($dataDir);
88

99
foreach ($reader->readBlocks(0, 100) as $height => $block) {
1010
echo $height . " => " . \AndKom\Bitcoin\Blockchain\Utils::hashToHex($block->header->getHash()) . "\n";

examples/read_blocks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
$blocksDir = "$dataDir/blocks";
77
$blockFile = "$blocksDir/blk00001.dat";
88

9-
$reader = new \AndKom\Bitcoin\Blockchain\BlockFileReader();
9+
$reader = new \AndKom\Bitcoin\Blockchain\Reader\BlockFileReader();
1010

1111
foreach ($reader->read($blockFile) as $block) {
1212
foreach ($block->transactions as $tx) {

examples/read_chainstate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
$dataDir = getenv('HOME') . '/Library/Application Support/Bitcoin';
66
$chainstateDir = "$dataDir/chainstate";
77

8-
$reader = new \AndKom\Bitcoin\Blockchain\ChainstateReader($chainstateDir);
8+
$reader = new \AndKom\Bitcoin\Blockchain\Reader\ChainstateReader($chainstateDir);
99

1010
foreach ($reader->read() as $unspentOutput) {
1111
echo "TX: " . \AndKom\Bitcoin\Blockchain\Utils::hashToHex($unspentOutput->hash) . "\n";

examples/read_index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
$blocksDir = "$dataDir/blocks";
77
$indexDir = "$blocksDir/index";
88

9-
$reader = new \AndKom\Bitcoin\Blockchain\IndexReader($indexDir);
9+
$reader = new \AndKom\Bitcoin\Blockchain\Reader\BlockIndexReader($indexDir);
1010

1111
echo "Reading block index...\n";
1212

src/Database/UnspentOutput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function decompressScript(): ScriptPubKey
141141
try {
142142
$decompressed = PublicKey::parse($compressed)->decompress()->serialize();
143143
} catch (\Exception $exception) {
144-
throw new ScriptException('Unable to decompress public key.');
144+
throw new ScriptException('Unable to decompress public key.', 0, $exception);
145145
}
146146
$script = chr(65);
147147
$script .= $decompressed;

src/Script/Script.php

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace AndKom\Bitcoin\Blockchain\Script;
66

77
use AndKom\BCDataStream\Reader;
8+
use AndKom\Bitcoin\Blockchain\Exception\ScriptException;
89

910
/**
1011
* Class Script
@@ -87,31 +88,39 @@ public function parse(): array
8788
return $this->operations;
8889
}
8990

91+
if (!$this->data) {
92+
throw new ScriptException('Empty script.');
93+
}
94+
9095
$this->operations = [];
9196
$stream = new Reader($this->data);
9297

93-
while ($stream->getPosition() < $stream->getSize()) {
94-
$code = ord($stream->read(1));
95-
$data = null;
96-
$size = 0;
97-
98-
if ($code == Opcodes::OP_0) {
99-
$data = '';
100-
} elseif ($code >= 0x01 && $code <= 0x4b) {
101-
$data = $stream->read($code);
102-
$size = $code;
103-
} elseif ($code >= Opcodes::OP_PUSHDATA1 && $code <= Opcodes::OP_PUSHDATA4) {
104-
$size = $stream->readCompactSize();
105-
$data = $stream->read($size);
106-
} elseif ($code == Opcodes::OP_1NEGATE) {
107-
$data = chr(-1);
108-
$size = 1;
109-
} elseif ($code >= Opcodes::OP_1 && $code <= Opcodes::OP_16) {
110-
$data = chr($code - Opcodes::OP_1 + 1);
111-
$size = 1;
98+
try {
99+
while ($stream->getPosition() < $stream->getSize()) {
100+
$code = ord($stream->read(1));
101+
$data = null;
102+
$size = 0;
103+
104+
if ($code == Opcodes::OP_0) {
105+
$data = '';
106+
} elseif ($code >= 0x01 && $code <= 0x4b) {
107+
$data = $stream->read($code);
108+
$size = $code;
109+
} elseif ($code >= Opcodes::OP_PUSHDATA1 && $code <= Opcodes::OP_PUSHDATA4) {
110+
$size = $stream->readCompactSize();
111+
$data = $stream->read($size);
112+
} elseif ($code == Opcodes::OP_1NEGATE) {
113+
$data = chr(-1);
114+
$size = 1;
115+
} elseif ($code >= Opcodes::OP_1 && $code <= Opcodes::OP_16) {
116+
$data = chr($code - Opcodes::OP_1 + 1);
117+
$size = 1;
118+
}
119+
120+
$this->operations[] = new Operation($code, $data, $size);
112121
}
113-
114-
$this->operations[] = new Operation($code, $data, $size);
122+
} catch (\Exception $exception) {
123+
throw new ScriptException('Script parse error.', 0, $exception);
115124
}
116125

117126
return $this->operations;

0 commit comments

Comments
 (0)