Description
Problem 1 – Standard Bitcoin Script
To start, we are going to write a standard transaction script that is used everywhere. Major parts of the code is already provided in ex1.py. Complete the TODO parts and run the code. You will get the transaction data. Copy the transaction hash and use it to find the transaction in blockcypher. If it is confirmed, copy this hash to transactions.py. You will repeat similar steps in the next problems as well.
Hint: Just put the appropriate script commands and variables in return part of the function. For example: return [OP DUP,address,OP HASH160,OP EQUALVERIFY] (This is not the right answer!!)
Problem 2 – Linear Redeemer
A student has suggested his own protocol to redeem an output. Redeeming will happen when the solution (x, y) to the following set of linear equations have been found and submitted in the
ScriptSig:
x + y = the first half of your studentID x − y = the second half of your studentID
(If x and y turn out to be fractional, add or subtract one value from your Student ID to make sure the solution is integer.)
In this part, you will edit ex2a.py and ex2b.py, where in ex2a.py you will make a transaction that can only be redeemed with the solution and in ex2b.py, you will redeem it to verify that your script works. Your scripts should be as small as possible. Note that the ScriptSig must only consist of pushing x and y to the stack. Don’t forget to add the hashes of the produced transactions to transactions.py.
Problem 3 – Bitcoin company
Faraz and Ata want to start a company together. They have decided to put their companies funds in the Bitcoin blockchain to invest in cryptocoins and also show how up to date they are. They decided to use the MULTISIG feature of the Bitcoin script, such that if both sides agree, the transaction is confirmed.
After some years the company grew and now has 5 other shareholders. They changed their policy and now each transaction is confirmed if:
- Faraz and Ata both agree on the transaction.
- Either Faraz or Ata and 3 other shareholders agree on the transaction.(note that shareholderscan not spend the funds without agreeing with Ata or Faraz)
1.Using conditional branches (OP IF) implement a script code for the company’s policy by creating two transactions for the idea In this solution, the first one creates an output that can only be redeemed by the way you proposed. The second transaction redeems that output, with the method you proposed, to verify your idea works.
2.Now suppose Faraz and Ata are having problems and never can agree on a transaction (case i never happens); implement a script code for the case ii without using conditional branches (OP IF).
Copy the code from ex2a.py to ex31a.py and ex32a.py and copy the content of ex2b.py to ex31b.py and ex32b.py. Modify the codes to make the two transactions.
The modification may require you to change parts beyond than the TODO parts, possibly even functions that are defined in utils.py. Don’t change utils.py though. Copy the function declaration to the new files and proceed from there.
Don’t forget to copy the transaction hashes to transactions.py.
Problem 4 – Happy Birthday
- Uncle Saeed decided to give a birthday present for his nephew, Hamed, so when his birthdaycomes, he can use the money. After getting introduced to Bitcoins, he has become extremely fond of this technology and decided to put the funds in the Bitcoin blockchain. Propose a way he can make sure Hamed can’t use the bitcoins sent to her address before his birthday, even if he has the private key.
After this, copy the code from ex2a.py and ex2b.py to ex4a.py and ex4b.py respectively. Modify the codes to make two transactions. One whose output cannot be redeemed until a specific time in the future. One who redeems that output after the specified time. Again, like before, write the hashes of these transactions in transcations.py
The modification might mean you have to change codes further than the TODO parts, possibly even functions that are defined in utils.py. Don’t change utils.py though. Copy the function declaration to ex4a.py or ex4b.py and proceed from there.(150 points)
- His other uncle, Homayoun, wants to write a happy birthday letter to Hamed. He found outthat you can put messages on bitcoin’s blockchain and they last for as long as bitcoin lasts. He really liked the idea and decided to put the happy birthday message on bitcoin.
Write a python code (happybirthday.py) to help Homayoun by getting a costume message as an input and putting it on bitcoin’s blockchain. Try your code with ’Happy Birthday Hamed’ and save the transaction hash in transactions.py
Problem 5 – I have it
Salar wrote an incredible article recently and decided to have it published in Nature in a few months. Mohammad told him that if he proves he has the article right now, Mohammad will give him 400$ . Salar has known Mohammad for a long time and knows that if he gives the article to him, he will publish it with his own name. Yet he still wants that 400$. Easy enough, he computes the hash of the article he wrote and gives it to MOhammad, and tells him to check if the hash is valid when it is published. Mohammad doesn’t accept this and says he is not organized enough to keep the hash for a few months and find it at that time and will probably lose the hash. Salar knows Mohammad likes Bitcoins. He also wants the 400$. Salar thought of this solution:
- Salar uses the SHA256 hash of the file to construct an address (using the algorithm that can generate an address based on public keys) and sends 1 satoshi to it
Part 1 –
Write a Python scripts (fileverify.py) who take an input file and implement the two ideas. Try your script with the file data.hex. Save the transaction hashes in transactions.py.
Part 2 –
Now, If there are 10 articles and Salar needs to prove he has written all of them beforehand, what should he do? Clearly he can do the same steps 10 times. Can you suggest an easier, better and cheaper solution?
Explain your idea and implement it in a script called multifileverify.py, which takes n, the number of files and then takes n files and using your idea, it achieves Parsa’s goal. You don’t need to test it, but make sure it works.
Problem 6 – Atomic Swap
Last but not least, you will create a transaction called a cross-chain atomic swap, allowing two entities to securely trade ownership over cryptocurrencies on different blockchains. In this case, Alice and Bob will swap coins between the Bitcoin testnet and BlockCypher testnet. As you recall from setup, Alice has bitcoin on BTC Testnet3, and Bob has bitcoin on BCY Testnet. They want to trade ownership of their respective coins securely, something that can’t be done with a simple transaction because they are on different blockchains. The idea here is to set up transactions around a secret x, that only one party (Alice) knows. In these transactions only H(x) will be published, leaving x secret. Transactions will be set up in such a way that once x is revealed, both parties can redeem the coins sent by the other party. If x is never revealed, both parties will be able to retrieve their original coins safely, without help from the other. Before you start, make sure to read swap.py, alice.py, and bob.py. Compare to the pseudocode in here(open your vpn :D). This will be very helpful in understanding this assignment. Note that for this question, you will only be editing ex6.py and you can test your code by running python swap.py.
- Consider the ScriptPubKey required for creating a transaction necessary for a cross- chain atomic swap. This transaction must be redeemable by the recipient (if they have a secret x that corresponds to Hash(x)), or redeemable with signatures from both the sender and the recipient. Write this ScriptPubKey in coinExchangeScript in ex6.py.
- Write the accompanying ScriptSigs:
- Write the ScriptSig necessary to redeem the transaction in the case where the re- cipient knows the secret x. Write this in coinExchangeScriptSig1 in ex6.py.
- Write the ScriptSig necessary to redeem the transaction in the case where both the sender and the recipient sign the transaction. Write this in coinExchangeScriptSig2 in ex6.py.
- Run your code using python swap.py. We aren’t requiring that the transactions be broadcasted, as that requires some waiting to validate transactions. Running with broad- cast transactions=False will validate that ScriptSig + ScriptPK return true. Try this for alice redeems=True as well as alice redeems=False.



