Satoshi-SN / Bitcoin-

Bitcoin: A Peer-to-Peer Electronic Cash System
1 stars 0 forks source link

Here’s the Bitcoin code snippet written in C++ that demonstrates how transactions are verified and added to the blockchain: #13

Open Thisdick69 opened 2 months ago

Thisdick69 commented 2 months ago

include

include

include

include <openssl/sha.h>

// Function to calculate SHA-256 hash std::string sha256(const std::string str) { unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256_CTX sha256; SHA256_Init(&sha256); SHA256_Update(&sha256, str.c_str(), str.size()); SHA256_Final(hash, &sha256); std::stringstream ss; for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i]; } return ss.str(); }

// Block structure struct Block { int index; std::string previousHash; std::string timestamp; std::string data; std::string hash;

Block(int idx, std::string prevHash, std::string time, std::string dat)
    : index(idx), previousHash(prevHash), timestamp(time), data(dat) {
    hash = sha256(std::to_string(index) + previousHash + timestamp + data);
}

};

// Blockchain structure class Blockchain { public: Blockchain() { chain.emplace_back(Block(0, "0", "2024-08-27 16:44:32", "Genesis Block")); }

void addBlock(Block newBlock) {
    newBlock.previousHash = getLatestBlock().hash;
    newBlock.hash = sha256(std::to_string(newBlock.index) + newBlock.previousHash + newBlock.timestamp + newBlock.data);
    chain.push_back(newBlock);
}

Block getLatestBlock() const {
    return chain.back();
}

private: std::vector chain; };

int main() { Blockchain myBlockchain;

Block newBlock1(1, "", "2024-08-27 16:45:00", "Block 1 Data");
myBlockchain.addBlock(newBlock1);

Block newBlock2(2, "", "2024-08-27 16:46:00", "Block 2 Data");
myBlockchain.addBlock(newBlock2);

for (const auto& block : myBlockchain.chain) {
    std::cout << "Index: " << block.index << "\n";
    std::cout << "Previous Hash: " << block.previousHash << "\n";
    std::cout << "Timestamp: " << block.timestamp << "\n";
    std::cout << "Data: " << block.data << "\n";
    std::cout << "Hash: " << block.hash << "\n\n";
}

return 0;

}

Thisdick69 commented 2 months ago

avatar_1147963.jpg

Thisdick69 commented 2 months ago

Here's your incredible journey documented in binary code:


// Transaction: Tribute to Satoshi Nakamoto and Family

// Inputs: Family Support and Memories
01000001 01101110 01110100 01101000 01101111 01101110 01111001 01011111 01001000 01101111 01110111 01100001 01110010 01100100 01011111 01001000 01110101 01101110 01110100 00100000 01001111 01010000 01011111 01000011 01001000 01000101 01000011 01001011 01010011 01001001 01000111
01001010 01100101 01101110 01101110 01101001 01100110 01100101 01110010 01011111 01010110 01101001 01100011 01110100 01101111 01110010 01101001 01100001 01011111 01001000 01110101 01101110 01110100 00100000 01001111 01010000 01011111 01000011 01001000 01000101 01000011 01001011 01010011 01001001 01000111
01000101 01110110 01100101 01110010 01100101 01110100 01110100 01011111 01001100 01100101 01100101 01011111 01001000 01110101 01101110 01110100 00100000 01001111 01010000 01011111 01000011 01001000 01000101 01000011 01001011 01010011 01001001 01000111
01000111 01101100 01100101 01100001 01101110 01101110 01100001 01011111 01001010 01100101 01110111 01100101 01101100 01011111 01001000 01110101 01101110 01110100 00100000 01001111 01010000 01011111 01000011 01001000 01000101 01000011 01001011 01010011 01001001 01000111

// Outputs: Legacy and Inspiration
01000001 01101110 01110100 01101000 01101111 01101110 01111001 01011111 01000100 01100101 01110111 01100001 01111001 01101110 01100101 01011111 01001000 01110101 01101110 01110100 00100000 01001111 01010000 01011111 01000011 01001000 01000101 01000011 01001011 01010011 01001001 01000111
01001010 01100101 01101110 01101110 01101001 01100110 01100101 01110010 01011111 01010110 01101001 01100011 01110100 01101111 01110010 01101001 01100001 01011111 01001000 01110101 01101110 01110100 00100000 01001111 01010000 01011111 01000011 01001000 01000101 01000011 01001011 01010011 01001001 01000111
01000101 01110110 01100101 01110010 01100101 01110100 01110100 01011111 01001100 01100101 01100101 01011111 01001000 01110101 01101110 01110100 00100000 01001111 01010000 01011111 01000011 01001000 01000101 01000011 01001011 01010011 01001001 01000111
01000111 01101100 01100101 01100001 01101110 01101110 01100001 01011111 01001010 01100101 01110111 01100101 01101100 01011111 01001000 01110101 01101110 01110100 00100000 01001111 01010000 01011111 01000011 01001000 01000101 01000011 01001011 01010011 01001001 01000111
01001000 01100001 01101100 01011111 01000110 01101001 01101110 01101110 01100101 01111001 00100000 01001111 01010000 01011111 01000011 01001000 01000101 01000011 01001011 01010011 01001001 01000111

// Special Outputs: Bitcoin White Paper and First Ten Bitcoin
01000010 01101001 01110100 01100011 01101111 01101001 01101110 01011111 01010111 01101000 01101001 01110100 01100101 01011111 01010000 01100001 01110000 01100101 01110010 00100000 01001111 01010000 01011111 01000011 01001000 01000101 01000011 01001011 01010011 01001001 01000111
01000110 01101001 01110010 01110011 01110100 01011111 01010100 01100101 01101110 01011111 01000010 01101001 01110100 01100011 01101111 01101001 01101110 00100000 01001111 01010000 01011111 01000011 01001000 01000101 01000011 01001011 01010011 01001001 01000111

// Detailed Outputs: Family Moments
01010011 01100001 01110101 01110011 01100001 01100111 01100101 01011111 01000111 01110010 01100001 01110110 01111001 01011111 01000010 01101001 01110011 01100011 01110101 01101001 01110100 01110011 01011111 01000010 01110010 01100101 01100001 01101011 01100110 01100001 01110011 01110100 00100000 01001111 01010000 01011111 01000011 01001000 01000101 01000011 01001011 01010011 01001001 01000111
01010101 01101110 01101111 01011111 01000111 01100001 01101101 01100101 01011111 01001110 01101001 01100111 01101000 01110100 00100000 01001111 01010000 01011111 01000011 01001000 01000101 01000011 01001011 01010011 01001001 01000111
01010000 01100001 01110000 01100001 01110111 01011111 01010111 01101000 01101001 01110100 01100101 01011111 01000010 01110101 01110100 01110100 01101111 01101110 01011111 01010101 01110000 01011111 01010011 01101000 01101001 01110010 01110100 00100000 01001111 01010000 01011111 01000011 01001000 01000101 01000011 01001011 01010011 01001001 01000111
01010011 01101001 01110011 01110100 01100101 01110010 01011111 01010100 01101001 01100101 01100100 01011111 01010100 01101001 01100101 00100000 01001111 01010000 01011111 01000011 01001000 01000101 01000011 01001011 01010011 01001001 01000111
01010000 01100001 01110000 01100001 01110111 01011111 01010100 01101000 01101001 01100011 01101011 01011111 01000010 01101111 01110100 01110100 01101100 01100101 01011111 01000111 01101100 01100001 01110011 01110011 01100101 01110011 00100000 01001111 01010000 01011111 01000011 01001000 010001