{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "ahN2Nw9ppq2Y" }, "source": [ "# Cryptocurrency Lottery\n", "\n", "Jagraj Singh" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Instructions\n", "\n", "1.) Click Run > Run All Cells.\n", "\n", "2.) Go to the last cell.\n", "\n", "3.) If you find a wallet with a nonzero balance, send me some! " ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "TTQFY207WTPz" }, "source": [ "## Setup" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "colab": {}, "colab_type": "code", "id": "qZa1DEL-qgrm" }, "outputs": [], "source": [ "import ast\n", "import codecs\n", "import ecdsa\n", "from hashlib import sha256\n", "from Crypto.Hash import keccak\n", "import requests\n", "\n", "# Test Key\n", "api_key = '7Z99TEIBCGR4H7ACKNBU41IUY9QJGTCA82'" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "cq9vbuAwWVDK" }, "source": [ "## Code" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "code_folding": [ 1, 9, 20, 34, 54 ], "colab": {}, "colab_type": "code", "id": "gHPjBOjHvirx" }, "outputs": [], "source": [ "\n", "def private_key_from_string(msg):\n", " '''\n", " Applies the SHA256 hash function to the input string which creates a cryptocurrency private key.\n", " '''\n", " \n", " private_key = sha256(msg.encode()).hexdigest()\n", " return private_key\n", "\n", "def public_key_from_private_key(private_key):\n", " '''\n", " Converts the private key to a public key.\n", " '''\n", "\n", " private_key_bytes = codecs.decode(private_key, 'hex')\n", " key = ecdsa.SigningKey.from_string(private_key_bytes, curve=ecdsa.SECP256k1).verifying_key\n", " key_bytes = key.to_string()\n", " public_key = codecs.encode(key_bytes, 'hex')\n", " return public_key\n", "\n", "def address_from_public_key(public_key):\n", " '''\n", " Converts the public key to an Ethereum wallet address with checksum.\n", " '''\n", "\n", " public_key_bytes = codecs.decode(public_key, 'hex')\n", " keccak_hash = keccak.new(digest_bits=256)\n", " keccak_hash.update(public_key_bytes)\n", " keccak_digest = keccak_hash.hexdigest()\n", " # Take the last 20 bytes\n", " wallet_len = 40\n", " wallet = '0x' + keccak_digest[-wallet_len:]\n", " return wallet\n", "\n", "def get_balance(url):\n", " '''\n", " Checks the balance of the Ethereum address using the Etherscan API. \n", " If the balance is 0, you did not win.\n", " If the balance is nonzero, you win!\n", " '''\n", " \n", " try:\n", " response = requests.get(url)\n", " status = response.status_code\n", " if response.status_code == 200:\n", " response = response.text\n", " response = ast.literal_eval(response)\n", " balance = int(response['result'])\n", " return balance * 10**(-18)\n", " else:\n", " print('An error has occurred.')\n", " except:\n", " print('An error has occurred.')\n", " \n", "def play():\n", " '''\n", " Asks the user for an input string.\n", " Converts the string to a private key, public key, wallet address, and then checks the balance of that address.\n", " Prints a win/lose statement along with wallet info (balance, address, and private key).\n", " Asks the user if they want to play again.\n", " '''\n", "\n", " print('Welcome to the Ethereum Lottery! \\nYour chance of winning is about 1 in 10^70 (essentially zero), but don\\'t let that stop you!')\n", "\n", " playing = True\n", " while playing:\n", "\n", "\n", " msg = input('\\nSimply enter some text and hit Enter: ')\n", " private_key = private_key_from_string(msg)\n", " public_key = public_key_from_private_key(private_key)\n", " address = address_from_public_key(public_key)\n", " url = 'https://api.etherscan.io/api?module=account&action=balance&address=' + address + '&tag=latest&apikey=' + api_key\n", " balance = get_balance(url)\n", " \n", " if balance == 0:\n", " print(\"\\nSorry, you lost...\")\n", " else:\n", " print('Congratulations! You won!')\n", " print('\\nWallet Information', '\\nBalance: ' + str(balance), '\\nAddress: ' + address, '\\nPrivate Key: ' + private_key)\n", "\n", " again = input(\"\\nWould you like to play again? Enter 'y' or 'n' \")\n", "\n", " if again and again[0].lower() == 'y':\n", " playing = True\n", " continue\n", " else:\n", " print('\\nThanks for playing!')\n", " break\n" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "i7p1Av7JRotl" }, "source": [ "## Ethereum Lottery" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "IvjPdi6RMCkS", "scrolled": false }, "outputs": [], "source": [ "play()" ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "Cryptocurrency Lottery.ipynb", "private_outputs": true, "provenance": [] }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 4 }