Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fc170c8

Browse files
Merge pull request #3 from code-architect/transaction_issue
created a coinbase transaction, added a new api for flask
2 parents 3079afb + 0b48442 commit fc170c8

File tree

13 files changed

+639
-61
lines changed

13 files changed

+639
-61
lines changed

‎Blockchain/Backend/API/Client_calls/BaseCurl.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
abstractclass BaseCurl
2+
class BaseCurl
33
{
44
public static function curlSkeletonIfDataSend($curlObj, $OpType, $data)
55
{
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+
}

‎Blockchain/Backend/util/util.php‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,33 @@ function hash160($data) {
1010
return hash('ripemd160', $sha256Hash, true);
1111
}
1212

13-
// Example usage
14-
$input = 'some data to hash';
15-
$hashed256 = hash256($input);
16-
$hashed160 = hash160($input);
13+
function intToLittleEndian($number, $length)
14+
{
15+
// Use 'V' format for little-endian and unsigned long
16+
$value = pack('V', $number);
17+
return bin2hex($value);
18+
}
19+
20+
function bytesNeeded($n)
21+
{
22+
if ($n == 0) {
23+
return 1;
24+
}
25+
return intval(log($n, 256)) + 1;
26+
}
1727

18-
echo "Hashed 256: " . bin2hex($hashed256) . PHP_EOL;
19-
echo "Hashed 160: " . bin2hex($hashed160) . PHP_EOL;
28+
function littleEndianToInt($b)
29+
{
30+
// Reverse the byte array and convert it to an integer
31+
$reversedBytes = array_reverse(str_split($b));
32+
$littleEndian = implode('', $reversedBytes);
33+
return hexdec(bin2hex($littleEndian));
34+
}
35+
36+
// Example usage
37+
//$input = 'some data to hash';
38+
//$hashed256 = hash256($input);
39+
//$hashed160 = hash160($input);
40+
//
41+
//echo "Hashed 256: " . bin2hex($hashed256) . PHP_EOL;
42+
//echo "Hashed 160: " . bin2hex($hashed160) . PHP_EOL;

‎Blockchain/Python_Package/test.py‎ renamed to ‎Blockchain/Python_Package/keyGenerate.py‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
from flask import Flask, jsonify
21
import secrets
32
from EllepticCurve.EllepticCurve import Sha256Point
43
from util import hash160, hash256
54

6-
app = Flask(__name__)
75

8-
9-
@app.route('/generate_keys', methods=['GET'])
10-
def generate_keys():
6+
def generatePrivateKeyPublicAddress():
117
Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
128
Gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
139

@@ -53,7 +49,3 @@ def generate_keys():
5349

5450
PublicAddress = prefix + result
5551
return {"privateKey": privateKey, "publicAddress": PublicAddress}
56-
57-
58-
if __name__ == '__main__':
59-
app.run(debug=True)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /