{"id":18928611,"url":"https://github.com/phishman3579/bitcoin","last_synced_at":"2025-09-17T16:50:47.865Z","repository":{"id":148065755,"uuid":"38892915","full_name":"phishman3579/Bitcoin","owner":"phishman3579","description":"An example Bitcoin implementation which can be used to learn about Bitcoin/Blockchain. This implementations is for educational use only!","archived":false,"fork":false,"pushed_at":"2017-06-02T20:29:16.000Z","size":149,"stargazers_count":122,"open_issues_count":0,"forks_count":54,"subscribers_count":17,"default_branch":"master","last_synced_at":"2025-03-28T22:34:38.123Z","etag":null,"topics":["blockchain","java"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phishman3579.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2015-07-10T17:22:56.000Z","updated_at":"2024-11-25T04:09:58.000Z","dependencies_parsed_at":"2023-03-31T17:02:34.967Z","dependency_job_id":null,"html_url":"https://github.com/phishman3579/Bitcoin","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/phishman3579%2FBitcoin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phishman3579%2FBitcoin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phishman3579%2FBitcoin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phishman3579%2FBitcoin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phishman3579","download_url":"https://codeload.github.com/phishman3579/Bitcoin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249088783,"owners_count":21210863,"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":["blockchain","java"],"created_at":"2024-11-08T11:26:43.041Z","updated_at":"2025-09-17T16:50:42.803Z","avatar_url":"https://github.com/phishman3579.png","language":"Java","readme":"# Bitcoin\nAn example Bitcoin implementation which can be used to learn about Bitcoin/Blockchain. This implementations is for educational use only.\n\n# Overview.\n\n## Wallet\n\nThe Wallet is how peers interact with the Bitcoin peer-to-peer network. The Wallet generates a public key and a private key which it uses to sign each Transaction. The pulic key is the send-to address used by the Bitcoin network. Each Wallet has the ability to send coins from your account to another account and it also has the ability to confirm Transactions (except it's own) which it receives from the Bitcoin peer-to-peer network.\n\n```\n    Wallet {\n      sendCoin(entity, value); // Creates a new Transaction\n      handleTransaction(Transaction); // Receives a unconfirmed Transaction\n      handleConfirmation(Transaction); // Receives a confirmed Transaction and adds to blockchain\n    }\n```\n\n## Transaction\n\nTransactions are just a collection of input transactions, output transactions, a value, and a signature. \n\n```\n    Transaction {\n        byte[] header;\n        Transaction[] inputs;\n        Transaction[] outputs;\n        long value;\n        byte[] signature;\n    }\n```\n\nSee the [Transaction Class](https://github.com/phishman3579/Bitcoin/blob/master/src/com/jwetherell/bitcoin/data_model/Transaction.java) for reference.\n\n#### The Wallet also has a number of Transaction rules:\n\n* Once a Transaction has been used as an input, it cannot be used again. \n* All inputs on a Transaction have to be completely consumed on a transaction.\n\nNote: To send a Bitcoin transaction, you have to already own a Bitcoin. Getting an initial Bitcoin is usually done by trading something for a number of Bitcoins. One caveat of, having to own a Bitcoin to make a transaction, is the first transaction. The first transaction is called the genesis transaction, it is the only transaction which does not need input transactions.\n\n### An example Transaction\n\nIf Justin wants to send 6 coins to George:\n\nLedger:\n\n|  Justin's unused Transactions  |  George's unused Transaction  |\n|  ----------------------------- | ----------------------------- | \n| Transaction #1 : 5 Coins       |                               |\n| Transaction #2 : 3 Coins       |                               |\n| Transaction #3 : 7 Coins       |                               |\n\n```\n    Aggregate Transaction #4 {\n      byte[]        header      \"6 coins for George and 2 coins to Justin\"\n      Transaction[] input       { Transaction #1, Transaction #2 }\n      Transaction[] output      { Transaction #5, Transaction #6 }\n      int           value       0\n      byte[]        signature   \"Justin's signature based on the Header\"\n    }\n```\nNote: The 'value' on the Aggregate Transaction (#4) is a reward for anyone who confirms the Transaction. The higher the reward, the better chance the Transaction will be processed quicker.\n\n```\n    Transaction #5 {\n      byte[]        header      \"2 coins to Justin\"\n      Transaction[] input       { Transaction #1, Transaction #2 }\n      Transaction[] output      { }\n      int           value       2\n      byte[]        signature   \"Justin's signature based on the Header\"\n    }\n\n    Transaction #6 {\n      byte[]        header      \"6 coins for George\"\n      Transaction[] input       { Transaction #1, Transaction #2 }\n      Transaction[] output      { }\n      int           value       6\n      byte[]        signature   \"Justin's signature based on the Header\"\n    }\n```\n\nThe Aggregate Transaction (#4) will remove Transaction #1 and #2 from Justin's unused Transactions. Since the total of all inputs is 8 coins, which is 2 more than what Justin wants to send to George, the output will contain a Transaction which sends 2 coins back to Justin.\n\nThe Wallet will use it's private key to sign the Header of the Aggregate Transactions (#4) and it will also sign each of the output Transactions (#5 \u0026 #6). It will then send Transaction #4 to the Bitcoin network for confirmation. \n\nEach peer on the Bitcoin network will receive the Transaction and try to confirm it. \n\nTo confirm a Transaction, a Peer will:\n* Check the Signature of Transaction against the public key of the sender. \n\nIf it passes:\n* Send the confirmed Transaction to the Bitcoin network.\n\n## Block\n\nThe confirmed Transaction (#4) is added to a pool of confirmed Transactions. Peers (also called Miners) will gather confirmed Transactions from the pool and put them into a Block. A Block contains a number of confirmed Transactions, the Miner's signature, and a couple of other fields used for \"Proof of work\" processing.\n\n```\n    Block {\n      Transaction[]     transactions\n      int               nonce\n      int               zeros\n      byte[]            previousHash\n      byte[]            nextHash\n      byte[]            signature\n    }\n```\n\nSee the [Block Class](https://github.com/phishman3579/Bitcoin/blob/master/src/com/jwetherell/bitcoin/data_model/Block.java) for reference.\n\nMiners will create a single 'block hash' from all the confirmed Transactions in the Block. They will then go through the process of \"Proof of work\". The goal of the \"Proof of work\" is to create a hash which begins with a random number of zeros (see the 'zeros' field). \"Proof of work\" is designed to be processor intensive which adds randomness to the time it takes to process a Block. A Miner will take the 'block hash' and append a random integer (called a 'nonce') to it. It will then create a new hash from 'block hash + nonce' and see if it satisfies the \"Proof of work\", this process will repeat until it finds a 'nonce' which satisfies the \"Proof of work\"\n\nSee the [Proof of work](https://github.com/phishman3579/Bitcoin/blob/master/src/com/jwetherell/bitcoin/ProofOfWork.java) for reference.\n\nOnce a Miner finds a 'nonce' which satisfies the \"Proof of work\", it will:\n* Create another hash (see 'nextHash') using the Blockchain's current hash (see 'previousHash') and the 'block hash' \n* Send the Block to the Bitcoin network.\n\n```\n    Block #1 {\n      Transaction[]     transactions    { Transaction #4 }\n      int               nonce           453;\n      int               zeros           3;\n      byte[]            previousHash    \"Blockchain hash #1\";\n      byte[]            nextHash        \"Blockchain hash #2\";\n      byte[]            signature       \"Miner's signature\";\n    }\n```\nPeers on the Bitcoin network will receive the Block and start confirming it. \n\nTo confirm the Block, A Peer will:\n* Make sure the 'nonce' satisfies the \"Proof of work\"\n* Check the Block's signature \n* Check the signature of each Trasaction in the Block.\n\nIf everything passes:\n* Add the block to it's Blockchain.\n* Send the confirmed Block to the Bitcoin network\n\n## Blockchain\n\nThe Blockchain is a simple structure which contains a list of confirmed Blocks, a list of Transactions in chronological order, a list of unused Transactions, and the current hash.\n\nNote: all transactions in the same block are said to have happened at the same time.\n\n```\n    Blockchain {\n        List\u003cBlock\u003e         blockchain\n        List\u003cTransactions\u003e  transactions\n        List\u003cTransaction\u003e   unused\n        byte[]              currentHash\n    }\n```\n\nSee the [Blockchain](https://github.com/phishman3579/Bitcoin/blob/master/src/com/jwetherell/bitcoin/BlockChain.java) for reference.\n\n\nWhen the Peer adds the Block to the Blockchain, the Blockchain will:\n* Check to see if the 'previousHash' from the Block matches it's 'currentHash', \n* Check to see if the input Transactions from all the Transactions in the Block are 'unused'\n\nIf everything passes:\n* The Block is added to the 'blockChain'\n* The Transaction is added to the 'transactions' list\n* All 'input' transactions are removed from the 'unused' list\n* All the 'output' transactions are added to the 'unused' list\n* The 'currentHash' is updated to 'nextHash' from the current Block.\n\n```\n    Blockchain {\n        List\u003cBlock\u003e         blockchain      { Block #0 }\n        List\u003cTransactions\u003e  transactions    { Transaction #0 }\n        List\u003cTransaction\u003e   unused          { Transaction #1, Transaction #2, Transaction #3 }\n        byte[]              currentHash     \"Blockchain hash #1\"\n    }\n```\n\nUpdated Blockchain.\n\n```\n    Blockchain {\n        List\u003cBlock\u003e         blockchain      { Block #0, Block #1 };\n        List\u003cTransactions\u003e  transactions    { Transaction #0, Transaction #4 }\n        List\u003cTransaction\u003e   unused          { Transaction #3, Transaction #5, Transaction #6 }\n        byte[]              currentHash     \"Blockchain hash #2\"\n    }\n```\n\nLedger:\n\n|  Justin's unused Transactions  |  George's unused Transaction  |\n|  ----------------------------- | ----------------------------- | \n| Transaction #3 : 7 Coins       | Transaction #6 : 6 Coins      |\n| Transaction #5 : 2 Coins       |                               |\n|                                |                               |\n\nBased off of [1](http://www.michaelnielsen.org/ddi/how-the-bitcoin-protocol-actually-works/) and [2](http://www.imponderablethings.com/2013/07/how-bitcoin-works-under-hood.html)\n\nAlso see the [original paper](https://bitcoin.org/bitcoin.pdf)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphishman3579%2Fbitcoin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphishman3579%2Fbitcoin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphishman3579%2Fbitcoin/lists"}