{"id":23646354,"url":"https://github.com/code-architect/python-blockchain","last_synced_at":"2025-11-11T22:30:15.168Z","repository":{"id":85256643,"uuid":"497738559","full_name":"code-architect/Python-Blockchain","owner":"code-architect","description":null,"archived":false,"fork":false,"pushed_at":"2022-06-10T23:42:59.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-28T13:33:57.498Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/code-architect.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-30T00:11:58.000Z","updated_at":"2022-05-31T22:40:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"0260a8e9-78bc-4ed1-bf50-7e6c508cee73","html_url":"https://github.com/code-architect/Python-Blockchain","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-architect%2FPython-Blockchain","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-architect%2FPython-Blockchain/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-architect%2FPython-Blockchain/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-architect%2FPython-Blockchain/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/code-architect","download_url":"https://codeload.github.com/code-architect/Python-Blockchain/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239592972,"owners_count":19664855,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-28T13:32:52.093Z","updated_at":"2025-11-11T22:30:15.132Z","avatar_url":"https://github.com/code-architect.png","language":"Python","readme":"# Python-Blockchain\nStep by step guide if anyone wants to build it in any other language\n\n1. Create a class name \u003cb\u003eTransaction\u003c/b\u003e and add ```senderPublicKey, receiverPublicKey, amount, transType``` variables to be \ninitialize at the very beginning. Three other variables to be initialize in the constructor ```timestamp, id, signature```, \nid can be uuid1, timestamp is pretty self explanatory, and signature will be added later for now it's an empty string.\n```toJson()``` method is for printing a proper data. can also be achieved by doing ```print(transaction.__dict__)``` instead of \n```print(transaction.toJson())```\n\n2. Create a \u003cb\u003eWallet\u003c/b\u003e class, I am using Crypto package (RSA) for key pair. Initialize a```keyPair``` variable in the constructor.\nCreate a \u003cb\u003esign method\u003c/b\u003e which in takes some data and generate a key pair. Create a \u003cb\u003eBlockchainUtils\u003c/b\u003e class, implement a \nhash method, in here I am using ```json.dumps(data)``` to return a string. then converting it a byte representation, because we\nare using ```Crypto.Hash import SHA256``` here to hash. From ```Crypto.Signature import PKCS1_v1_5``` to generate signature and\nalso to validate signatures. In the ```sign()``` function we are generating a signature from the data hash. Whiting a ```sign()```\n function in \u003cb\u003eTransaction\u003c/b\u003e class which will set the signature in \u003cb\u003eTransaction\u003c/b\u003e class.\n \n3. In \u003cb\u003eWallet\u003c/b\u003e class create a static method name ```signatureValid``` with three parameters named ```data, signature, publicKeyString```\n\u003cb\u003edata\u003c/b\u003e is the signature created on initially, \u003cb\u003esignature\u003c/b\u003e to check if its true, \u003cb\u003epublicKeyString\u003c/b\u003e is basically \nthe public key from the wallet RSA key pair(this was used to sign the data)[we need it in string format]. Create a method\n```publicKeyString``` which will return the public key extracted from ```self.keyPair``` as a string.\n \n4. In \u003cb\u003eTransaction\u003c/b\u003e class create a method ```payload```. We need a consistent representation, so for that we are\ncreating this function. In here we create a deep copy, so we are using ```copy``` library. in \u003cb\u003eWallet\u003c/b\u003e class create a method name \n```createTransaction```, what we have to provide is basically same which is in init method in \u003cb\u003eTransaction\u003c/b\u003e apart from\nsending a public key. Use the ```transaction.payload()``` to create the signature then sign it with it and then return the \n```transaction```. So now we can create sign transaction with one call. In the ```Main.py``` file test are we are testing if\na fraud wallet public key will work or not.\n\n5. Create a new file name ```Transaction Pool```. So here we want ot create some kind of list which which is the transaction pool\nwhich adds new transactions but make sure same transaction will not be added twice. Create a method ```addTransaction```\nwhich will add new transaction, but we have to make sure that the same transaction must not exists twice, so we will create\nanother method ```transactionExists```. There are many ways to compare two transaction, we can use the uuid field\n```self.id = uuid.uuid1().hex``` in \u003cb\u003eTransaction\u003c/b\u003e  class which we have declared previously. To do that we are writing\na method in \u003cb\u003eTransaction\u003c/b\u003e class named ```equals``` which will compare two ids of a transaction. Now we can call it\nin ```transactionExists``` to check and verify.\n\n6. Create \u003cb\u003eBlock\u003c/b\u003e class, a block is a container which holds a bunch of transactions, which later on integrated in the\nchain of blocks. In the class we declare the class constructor, few inputs required ```transactions, lastHash, forger, blockCount```.\nFirst parameter is \u003c/b\u003etransactions\u003c/b\u003e, as we are going to build a chain of blocks we need some kind of reference to the\nlatest block in the blockchain, to get the reference of this block we use the hash of this block \u003c/b\u003elastHast\u003c/b\u003e,\n\u003c/b\u003eforger\u003c/b\u003e is just a node, later we will have a whole network of nodes. The nodes will try to be the one to who create a\nnew block to get rewarded, if a node is entitled to create the new block then it's called \u003cb\u003eforger\u003c/b\u003e. \u003cb\u003eblockCount\u003c/b\u003e is\nnothing more then an incrementing number to keep the count of the blocks. In ```tojson()``` method of \u003cb\u003eBlock\u003c/b\u003e class\nare we getting the dictionary format of all the transaction and also serializing all the information.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcode-architect%2Fpython-blockchain","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcode-architect%2Fpython-blockchain","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcode-architect%2Fpython-blockchain/lists"}