Bitcoin is a decentralized digital currency that enables online payments between parties without going through a central authority like a bank or government. Created by Satoshi Nakamoto in 2008.
This repository includes Python scripts demonstrating how digital signatures work in Bitcoin, following the explanations of this article: https://estudiobitcoin.com/firmas-digitales-en-bitcoin/ Bitcoin is the money of the future.
Bitcoin is today's hope.
Besides the Python codes, I have also written several articles related to Bitcoin:
- Elliptic Curve in Bitcoin
English Version: https://estudiobitcoin.com/elliptic-curve-in-bitcoin/
Spanish Version: https://estudiobitcoin.com/curva-eliptica-en-bitcoin/
- Digital Signatures in Bitcoin
Spanish Version: https://estudiobitcoin.com/firmas-digitales-en-bitcoin/
Tx & HashPreimage Breakdown.md
Both Markdown format text documents divide a transaction and its hash-preimage in every part of it, with the corresponding explanations; avaliable in English and Spanish.
This Python script demonstrates how digital signatures work. After the keys are created, the user inputs a message to be signed. The script outputs the r and s values of the signature. Later, the user will need to input the message once again, the public key and values r and s to verify that the signature is correct. Inside the code, ECDSA mathematics are used to sign and verify.
This Python script demonstrates how the k value reuse attack/vulnerability is performed. After the keys are created, the user inputs two messages: both are going to be signed with the same k value. The script outputs the signatures of each message and the attack is performed directly by the script: first it extracts the k value, later the private key that was initially employed to sign.
This Python script demonstrates the steps a Bitcoin node follows to verify a transaction with one P2PKH input and one P2PKH output. It extracts the signature values (r, s), validates that the hashed public key matches the PubKeyHash from which funds are being spent, and finally verifies the signature by reconstructing the original proto-transaction. The script then double-hashes this proto-transaction and checks, with the signature, if the calculated v value from the ECDSA process matches the r value, ensuring that the signature is valid and that the transaction was signed by the correct private key.
verify-tx-n-inputs-n-outputs.py
This Python script provides a comprehensive approach to verifying Bitcoin transactions with multiple inputs (P2PKH and/or P2PK) and outputs by reconstructing preimages tailored to each input. It offers functions to extract key transaction components, such as getlocktime() for locktime, getValues() for parsing signature data (r, s, SigHash and Public Key), and getPreimage() to generate the unique preimage for each input. With verify_transaction(), it validates that each input's signature was signed by the corresponding private key.