Skip to content

Commit

Permalink
serialize version and input count
Browse files Browse the repository at this point in the history
  • Loading branch information
110CodingP committed Apr 28, 2024
1 parent 687633e commit e1b43f7
Show file tree
Hide file tree
Showing 7 changed files with 8,208 additions and 8,139 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"files.associations": {
"iostream": "cpp"
"iostream": "cpp",
"iomanip": "cpp"
}
}
21 changes: 19 additions & 2 deletions Solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ LeftAsExercise
bitcoin repo

# Thinking Process
First, we need to parse the json files to get the required info.
First, we need to parse the json files to get the required info.//done

Second, we need to validate the transactions using that info, but then how do we order the transactions?
//serialise
//dp problem (recursively build utxo)
Expand All @@ -20,4 +21,20 @@ We need to output the coinbase txn also
// since cpp doesn't have a function for iterating over files in a directory had to make "txns.txt".

//not all have witness
//not all have scriptpubkey_address
//not all have scriptpubkey_address

needed
Your script must generate an output file named `output.txt` with the following structure:
- First line: The block header.
- Second line: The serialized coinbase transaction.
- Following lines: The transaction IDs (txids) of the transactions mined in the block, in order. The first txid should be that of the coinbase transaction

//Credits: Dane
You have to:
serialize the transaction as per the input address type (scriptpubkey_type in the prevout)
append the sighash_type (present at the end of the signature you are verifying) at the end of the trimmed tx byte sequence
double hash 256(trimmed_tx)
parse the signature, publickey, tx_hash into SIGNATURE, PUBLIC KEY and MESSAGE objects using Secp256k1 libraries,
then verify the message against the public key and signature using ecdsa verification functions

basic procedure is this, you have do research for the whole thing.
Binary file modified mine
Binary file not shown.
47 changes: 42 additions & 5 deletions mine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include <string>
#include <vector>
#include <json.hpp>
#include <sstream>
#include <ios>

#define VERSION_MOD 4294967296

using json = nlohmann::json;

Expand Down Expand Up @@ -30,7 +34,7 @@ struct Txn {
int locktime;
std::vector<Input> vin;
std::vector<Output> vout;

bool is_segwit;
};

void from_json(const json& j, Output& p) {
Expand Down Expand Up @@ -63,9 +67,42 @@ void from_json(const json& j, Txn& p) {
j.at("vout").get_to(p.vout);
}

std::string big_to_little(std::string s, int len) {
std::string little;
for (int i=len-1;i>=0;i-=2) {
little +=s[i-1];
little +=s[i];
}
return little;
}

std::string serialise(Txn t) {
std::string serialised;
return t.vin[0].scriptSig;
std::string serialised;

//version
int version = t.version%(VERSION_MOD);
std::stringstream ss;
ss<<std::setfill('0')<<std::setw(8)<<std::hex<<version;
serialised+= big_to_little(ss.str(),8);
ss.str("");

//input
int in_sz = t.vin.size();
int temp=in_sz;
int exp=1;
while (true) {
temp=temp/256;
if (temp==0) {
break;
}
else {
exp++;
}
}
ss<<std::setfill('0')<<std::setw(2*exp)<<in_sz;
serialised+=ss.str();

return serialised;
}

int main() {
Expand All @@ -76,8 +113,8 @@ int main() {
std::vector<std::string> txns;

while (myfile.good()) {
getline(myfile,filename);
txns.push_back(filename);
getline(myfile,filename);
txns.push_back(filename);
}

myfile.close();
Expand Down
Loading

0 comments on commit e1b43f7

Please sign in to comment.