Skip to content

Commit

Permalink
Added output for witness in coinbase txn
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushj05 committed Apr 7, 2024
1 parent 71d3503 commit 9fa256c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
Binary file modified bin/main
Binary file not shown.
6 changes: 3 additions & 3 deletions output.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
200000000000000000000000000000000000000000000000000000000000000000000000b97612673f1ac935cf09ce4b0b5427ef0d47976230922347bc316438a2ac876b21d612661f00ffff0000bc99
01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0403951a06ffffffff0177bf4025000000001976a9142c30a6aaac6d96687291475d7d52f4b469f665a688ac00000000
1c078a0a38861757e7b63af37b8061c7862d7946d9a801d1282042f3b2e1abf7
200000000000000000000000000000000000000000000000000000000000000000000000415e963e489fb68a4beb66e670655b3a5243f5a069a48f09023cb7479a08f3a7d0ea1266ffff001f00007dd8
01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0403951a06ffffffff0277bf4025000000001976a9142c30a6aaac6d96687291475d7d52f4b469f665a688ac0000000000000000266a24aa21a9ed645ca51edf54d5cca1ec2f190d5738d86970c506b86310ce835e08c1b92715770120000000000000000000000000000000000000000000000000000000000000000000000000
86bd466461b021a26c20c8365c0a19e3318fcb6a808cf66f67b2f7ed14f59a8d
4ab3cc4296fee78153d60d2884323a84260157db0b83a72309272f109ad9dd32
12 changes: 8 additions & 4 deletions src/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ string getMerkleRoot(vector<string> &txns_included) {
return hashQ.front();
}

string genCoinbaseTxn (int64_t reward) {
string genCoinbaseTxn (int64_t reward, string wTXID_commitment) {
string coinbase_txn = "";

coinbase_txn += int2bin(1); // Version
Expand All @@ -56,11 +56,17 @@ string genCoinbaseTxn (int64_t reward) {
coinbase_txn += string{(int8_t) 0x04, (int8_t) 0x03, (int8_t) 0x95, (int8_t) 0x1a, (int8_t) 0x06}; // scriptSig
coinbase_txn += int2bin(0xffffffff); // Sequence

coinbase_txn += int2compact(1); // Number of outputs
coinbase_txn += int2compact(2); // Number of outputs

coinbase_txn += int2bin(reward, IS_INT64_T); // Reward
coinbase_txn += int2compact(0x19); // Output script length
coinbase_txn += hexstr2bstr("76a9142c30a6aaac6d96687291475d7d52f4b469f665a688ac"); // Output script

coinbase_txn += int2bin(0, IS_INT64_T); // Reward
coinbase_txn += int2compact(0x26); // Output script length
coinbase_txn += hexstr2bstr("6a24aa21a9ed"); // Output script
coinbase_txn += wTXID_commitment; // Output script
coinbase_txn += hexstr2bstr("01200000000000000000000000000000000000000000000000000000000000000000");

coinbase_txn += int2bin(0); // Locktime

Expand All @@ -73,14 +79,12 @@ string genBlockHeader (vector<string> &txns_included) {
block_header += string(32, (int8_t) 0); // Previous block hash

string merkle_root = getMerkleRoot(txns_included);
// reverse(merkle_root.begin(), merkle_root.end());
block_header += merkle_root;

time_t timestamp = time(nullptr);
block_header += int2bin(timestamp);

block_header += string{(int8_t) 255, (int8_t) 255, (int8_t) 0, (int8_t) 31}; // Bits
// block_header += string{(int8_t) 31, (int8_t) 0, (int8_t) 255, (int8_t) 255}; // Bits

return block_header;
}
Expand Down
6 changes: 4 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ void mine() {
blockTxnIds.push_back(txid);
}

string coinbaseTxn = genCoinbaseTxn(reward);
vector<string> wTXIDs = blockTxnIds;
wTXIDs.push_back(string(32, 0));
string wTXID_commitment = hash256(getMerkleRoot(wTXIDs) + string(32, 0));
string coinbaseTxn = genCoinbaseTxn(reward, wTXID_commitment);
block_size += coinbaseTxn.size();

string coinbaseTxnId = "";
Expand All @@ -93,7 +96,6 @@ void mine() {
block = bstr2hexstr(block, block.length());
block.push_back('\n');

cout << bstr2hexstr(coinbaseTxn, coinbaseTxn.length()) << endl;
block += bstr2hexstr(coinbaseTxn, coinbaseTxn.length());
block.push_back('\n');

Expand Down
11 changes: 11 additions & 0 deletions src/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ extern set<string> inputs_used;
string serialized_txn = "", serialized_segwit_1 = "", serialized_segwit_inp = "", serialized_segwit_2 = "";


string hash256(string data) {
string hash = "";
char sha[SHA256_DIGEST_LENGTH];
SHA256((unsigned char*) data.c_str(), data.length(), (unsigned char*) sha);
SHA256((unsigned char*) sha, SHA256_DIGEST_LENGTH, (unsigned char*) sha);
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
hash.push_back(sha[i]);
return hash;
}


std::vector<std::string> getOps(std::string asmScript) {
std::vector<std::string> ops;

Expand Down

0 comments on commit 9fa256c

Please sign in to comment.