Skip to content

Commit 277ddf3

Browse files
Merge pull request #4 from code-architect/master
Master
2 parents 99b71ee + fc170c8 commit 277ddf3

27 files changed

+1999
-58
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.idea
22
test.txt
33
test.php
4-
4+
vendor
5+
**/__pycache__/
6+
Blockchain/Python_Package/virtual_env/
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
class BaseCurl
3+
{
4+
public static function curlSkeletonIfDataSend($curlObj, $OpType, $data)
5+
{
6+
curl_setopt($curlObj, CURLOPT_CUSTOMREQUEST, $OpType);
7+
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $data);
8+
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, true);
9+
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array(
10+
'Content-Type: application/json',
11+
'Content-Length: ' . strlen($data))
12+
// Set an authorization header if needed
13+
// 'Authorization: Bearer YourAccessToken',
14+
);
15+
$response = curl_exec($curlObj);
16+
// Get the HTTP response code
17+
$httpCode = curl_getinfo($curlObj, CURLINFO_HTTP_CODE);
18+
19+
// Check for cURL errors and handle response code
20+
if (curl_errno($curlObj))
21+
{
22+
return ["error"=>true, "errorType" => "Curl Error", "data"=>curl_error($curlObj)];
23+
}
24+
elseif ($httpCode >= 400)
25+
{
26+
return ["error" => true, "errorType" => "HTTP Error", "data" => $httpCode];
27+
} else
28+
{
29+
return ["data" => $response];
30+
}
31+
}
32+
33+
public static function closeCurl($curlObj)
34+
{
35+
curl_close($curlObj);
36+
}
37+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
require_once 'Blockchain/Backend/API/Client_calls/BaseCurl.php';
3+
class ClientAPI extends BaseCurl
4+
{
5+
public static $clientAddress = 'http://localhost:5000/generate_keys';
6+
7+
public static function postRequestAPI($curlObject)
8+
{
9+
curl_setopt($curlObject, CURLOPT_RETURNTRANSFER, true);
10+
$response = curl_exec($curlObject);
11+
12+
if (curl_errno($curlObject)) {
13+
return ["error"=>true, "errorType" => "Curl Error", "data" => curl_error($curlObject)];
14+
} else {
15+
return ["data" => $response];
16+
}
17+
}
18+
19+
public static function errorHandleResponse($data): bool
20+
{
21+
return array_key_exists('error', $data);
22+
}
23+
24+
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
require_once 'Blockchain/Backend/API/Client_calls/BaseCurl.php';
3+
class PluginHelperAPI extends BaseCurl
4+
{
5+
public static $clientAddress = 'http://localhost:5000/';
6+
7+
public static function postRequestAPI($url, $postData)
8+
{
9+
$ch = curl_init();
10+
curl_setopt($ch, CURLOPT_URL, $url);
11+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
12+
curl_setopt($ch, CURLOPT_POST, 1);
13+
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
14+
$response = curl_exec($ch);
15+
if (curl_errno($ch)) {
16+
echo "cURL Error: " . curl_error($ch);
17+
die();
18+
}
19+
return $response;
20+
}
21+
22+
}

Blockchain/Backend/core/Blockchain.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_once 'Blockchain/Backend/core/BlockHeader.php'; // Assuming the blockheader.php file path
44
require_once 'Blockchain/Backend/core/database/BaseDB.php'; // Assuming the blockheader.php file path
55
require_once 'Blockchain/Backend/util/util.php'; // Assuming the util.php file path
6+
require_once 'Blockchain/Backend/core/transactions/Coinbase.php';
67

78
$ZERO_HASH = str_repeat('0', 64);
89
$VERSION = 1;
@@ -32,12 +33,15 @@ private function GenesisBlock() {
3233

3334
public function addBlock($BlockHeight, $prevBlockHash) {
3435
$timestamp = time();
35-
$Transaction = "Code Architect sent {$BlockHeight} Bitcoins to Indranil";
36-
$merkleRoot = bin2hex(hash256($Transaction));
36+
$coinbaseInstance = new Coinbase($BlockHeight);
37+
$coinbaseTx = $coinbaseInstance->coinbaseTransaction();
38+
$merkleRoot = ' ';
3739
$bits = 'ffff001f';
3840
$blockheader = new BlockHeader($GLOBALS['VERSION'], $prevBlockHash, $merkleRoot, $timestamp, $bits);
3941
$blockheader->mine();
40-
$block = new Block($BlockHeight, 1, (array)$blockheader, 1, $Transaction);
42+
$block = new Block($BlockHeight, 1, (array)$blockheader, 1, $coinbaseTx);
43+
// print_r((array)$block);
44+
// die();
4145
$this->writeOnDisk((array)$block);
4246
}
4347

Blockchain/Backend/core/Script.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
class Script
3+
{
4+
public $cmds;
5+
6+
public function __construct($cmds = null)
7+
{
8+
if ($cmds === null) {
9+
$this->cmds = [];
10+
} else {
11+
$this->cmds = $cmds;
12+
}
13+
}
14+
15+
public static function p2pkhScript($h160)
16+
{
17+
// Takes a hash160 and returns the p2 public key hash ScriptPubKey
18+
$script = new Script([0x76, 0xA9, $h160, 0x88, 0xAC]);
19+
return $script;
20+
}
21+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
require_once 'Blockchain/Backend/core/transactions/TxIn.php';
3+
require_once 'Blockchain/Backend/core/transactions/TxOut.php';
4+
require_once 'Blockchain/Backend/core/transactions/Tx.php';
5+
require_once 'Blockchain/Backend/core/Script.php';
6+
require_once 'Blockchain/Backend/util/util.php';
7+
require_once 'Blockchain/Backend/API/Client_calls/PluginHelperAPI.php';
8+
9+
const ZERO_HASH = "0000000000000000000000000000000000000000000000000000000000000000";
10+
const REWARD = 50;
11+
const PRIVATE_KEY = "56114968769095885066321288702375272595970830268400415922098497799492460020984";
12+
const MINER_ADDRESS = "1K3if2mFojLAWVtdD1eeYYKNVCwghpBvgb";
13+
14+
class Coinbase {
15+
public function __construct($blockHeight) {
16+
$this->blockHeightIntLittleEndian = intToLittleEndian($blockHeight, bytesNeeded($blockHeight));
17+
}
18+
19+
public function coinbaseTransaction() {
20+
$prevTx = hex2bin(ZERO_HASH);
21+
$prevIndex = 0xFFFFFFFF;
22+
23+
$txIns = [];
24+
$txIns[] = new TxIn($prevTx, $prevIndex);
25+
$txIns[0]->scriptSig->cmds[] = $this->blockHeightIntLittleEndian;
26+
27+
$txOuts = [];
28+
$targetAmount = REWARD * 100000000;
29+
$hexValue = $this->decodeBase58API(MINER_ADDRESS);
30+
$targetH160 = $hexValue;
31+
$targetScript = Script::p2pkhScript($targetH160);
32+
$txOuts[] = new TxOut($targetAmount, $targetScript);
33+
34+
return new Tx(1, $txIns, $txOuts, 0);
35+
}
36+
37+
public function decodeBase58API($value)
38+
{
39+
$address = PluginHelperAPI::$clientAddress;
40+
$url = $address."get_decode_base58";
41+
$ch = curl_init($url);
42+
$data = json_encode(array(
43+
"value" => $value
44+
));
45+
$val = PluginHelperAPI::curlSkeletonIfDataSend($ch, "POST", $data);
46+
$data = json_decode($val['data'], true);
47+
return $data['byte_data'];
48+
}
49+
50+
}
51+
52+
//$address = PluginHelperAPI::$clientAddress;
53+
//$url = $address."get_decode_base58";
54+
//$ch = curl_init($url);
55+
//$data = json_encode(array(
56+
// "value" => "1K3if2mFojLAWVtdD1eeYYKNVCwghpBvgb"
57+
//));
58+
//$val = PluginHelperAPI::curlSkeletonIfDataSend($ch, "POST", $data);
59+
//$data = json_decode($val['data'], true);
60+
//echo "\n\n";
61+
//print_r($data['byte_data']);
62+
//function decodeBase58API($value)
63+
//{
64+
// $address = PluginHelperAPI::$clientAddress;
65+
// $url = $address . "get_decode_base58";
66+
// $ch = curl_init($url);
67+
// $data = json_encode(array(
68+
// "value" => $value
69+
// ));
70+
// $val = PluginHelperAPI::curlSkeletonIfDataSend($ch, "POST", $data);
71+
// $data = json_decode($val['data'], true);
72+
// return $data['byte_data'];
73+
//}
74+
//
75+
//$data = decodeBase58API("1K3if2mFojLAWVtdD1eeYYKNVCwghpBvgb");
76+
//echo "\n\n";
77+
//print_r($data);
78+
//echo "\n\n";
79+
//
80+
//
81+
//$binaryData = hex2bin($data);
82+
//print_r($binaryData);
83+
//echo "\n\n";
84+
//
85+
//// Perform operations on the binary data (if needed)
86+
//
87+
//// Convert the binary data back to a hexadecimal string
88+
//$resultHexadecimal = bin2hex($binaryData);
89+
//
90+
//// Output the result
91+
//echo $resultHexadecimal;
92+
93+
////$hexString = $data;
94+
////$byteString = 'b"' . implode('\x', str_split($hexString, 2)) . '"';
95+
//$byteString = hex2bin($data);
96+
//$formattedBinary = 'b"';
97+
//foreach (str_split($byteString) as $byte) {
98+
// $formattedBinary .= '\x' . bin2hex($byte);
99+
//}
100+
//$formattedBinary .= '",';
101+
//
102+
//echo $formattedBinary;
103+
//echo "\n\n";
104+
//
105+
//$hexString = '';
106+
//$matches = [];
107+
//if (preg_match('/b"(.+)",/', $formattedBinary, $matches)) {
108+
// $hexBytes = explode('\x', $matches[1]);
109+
// foreach ($hexBytes as $hexByte) {
110+
// $hexString .= chr(hexdec($hexByte));
111+
// }
112+
// $hexString = bin2hex($hexString);
113+
// echo $hexString;
114+
//} else {
115+
// echo "Invalid format.";
116+
//}
117+
//echo "\n\n";
118+
//
119+
//$hexString = '';
120+
//$matches = [];
121+
//
122+
//if (preg_match('/b"(.+)",/', $formattedBinary, $matches)) {
123+
// $hexBytes = explode('\x', $matches[1]);
124+
// foreach ($hexBytes as $hexByte) {
125+
// $hexString .= bin2hex(hex2bin($hexByte));
126+
// }
127+
// echo $hexString;
128+
//} else {
129+
// echo "Invalid format.";
130+
//}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
class Tx
4+
{
5+
public $locktime;
6+
public $txOuts;
7+
public $txIns;
8+
public $version;
9+
10+
public function __construct($version, $txIns, $txOuts, $locktime) {
11+
$this->version = $version;
12+
$this->txIns = $txIns;
13+
$this->txOuts = $txOuts;
14+
$this->locktime = $locktime;
15+
}
16+
17+
public function serialize() {
18+
$result = intToLittleEndian($this->version, 4);
19+
$result .= count($this->txIns);
20+
21+
// Add serialization logic here
22+
23+
return $result;
24+
}
25+
26+
public function isCoinbase() {
27+
if (count($this->txIns) !== 1) {
28+
return false;
29+
}
30+
31+
$firstInput = $this->txIns[0];
32+
if ($firstInput->prevTx !== hex2bin(ZERO_HASH)) {
33+
return false;
34+
}
35+
36+
if ($firstInput->prevIndex !== 0xFFFFFFFF) {
37+
return false;
38+
}
39+
40+
return true;
41+
}
42+
43+
public function toDict() {
44+
// Convert Transaction Input to dict
45+
foreach ($this->txIns as $txIndex => $txIn) {
46+
if ($this->isCoinbase()) {
47+
$txIn->scriptSig->cmds[0] = littleEndianToInt($txIn->scriptSig->cmds[0]);
48+
}
49+
50+
$txIn->prevTx = bin2hex($txIn->prevTx);
51+
52+
foreach ($txIn->scriptSig->cmds as $index => $cmd) {
53+
if (is_string($cmd)) {
54+
$txIn->scriptSig->cmds[$index] = bin2hex($cmd);
55+
}
56+
}
57+
58+
$txIn->scriptSig = (array) $txIn->scriptSig;
59+
$this->txIns[$txIndex] = (array) $txIn;
60+
}
61+
62+
// Convert Transaction Output to dict
63+
foreach ($this->txOuts as $index => $txOut) {
64+
$txOut->scriptPubkey->cmds[2] = bin2hex($txOut->scriptPubkey->cmds[2]);
65+
$txOut->scriptPubkey = (array) $txOut->scriptPubkey;
66+
$this->txOuts[$index] = (array) $txOut;
67+
}
68+
69+
return (array) $this;
70+
}
71+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
require_once 'Blockchain/Backend/core/Script.php';
3+
4+
class TxIn
5+
{
6+
public $prevTx;
7+
public $prevIndex;
8+
public $scriptSig;
9+
public $sequence;
10+
11+
public function __construct($prevTx, $prevIndex, $scriptSig = null, $sequence = 0xFFFFFFFF) {
12+
$this->prevTx = $prevTx;
13+
$this->prevIndex = $prevIndex;
14+
$this->scriptSig = $scriptSig ?? new Script();
15+
$this->sequence = $sequence;
16+
}
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
class TxOut
4+
{
5+
public $amount;
6+
public $scriptPubkey;
7+
8+
public function __construct($amount, $scriptPubkey) {
9+
$this->amount = $amount;
10+
$this->scriptPubkey = $scriptPubkey;
11+
}
12+
}

0 commit comments

Comments
 (0)