-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwalkchain
More file actions
executable file
·51 lines (43 loc) · 1.3 KB
/
walkchain
File metadata and controls
executable file
·51 lines (43 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/php
<?php
/**
* walkchain
*
* Walks through the blockchain and prints out the contents of each record.
*
* No verification of the blockchain validity is performed; this tool simply reads
* each of the records from the blockchain ordered as they are stored on disk.
*
*
*/
date_default_timezone_set('UTC');
define('_magic', 0xD5E8A97F);
define('_hashalg', 'sha256');
define('_hashlen', 32);
define('_fn', 'blockchain.dat');
if (!file_exists(_fn)) exit("Can't open "._fn);
$size = filesize(_fn);
$fp = fopen(_fn,'rb');
$height = 0;
while (ftell($fp) < $size) {
$header = fread($fp, (13+_hashlen));
$magic = unpack32($header,0);
$version = ord($header[4]);
$timestamp = unpack32($header,5);
$prevhash = bin2hex(substr($header,9,_hashlen));
$datalen = unpack32($header,-4);
$data = fread($fp, $datalen);
$hash = hash(_hashalg, $header.$data);
print "height...... ".++$height."\n";
print "magic....... ".dechex($magic)."\n";
print "version..... ".$version."\n";
print "timestamp... ".$timestamp." (".date("H:i:s m/d/Y",$timestamp).")\n";
print "prevhash.... ".$prevhash."\n";
print "blockhash... ".$hash."\n";
print "datalen..... ".$datalen."\n";
print "data........ ".wordwrap($data, 100)."\n\n";
}
fclose($fp);
function unpack32($data,$ofs) {
return unpack('V', substr($data,$ofs,4))[1];
}