{"_id":"@dariotecchia/qrchain","_rev":"1-d6f247e6bd259c19b7a7bb5e1e19bd69","name":"@dariotecchia/qrchain","dist-tags":{"latest":"1.0.1"},"versions":{"1.0.0":{"name":"@dariotecchia/qrchain","version":"1.0.0","description":"A simple QR PoS blockchain","main":"main.js","author":{"name":"dariotecchia","email":"dariotecchia@gmail.com"},"license":"GPL-3.0","homepage":"https://github.com/darioTecchia/QRChain","repository":{"type":"git","url":"git+https://github.com/darioTecchia/QRChain.git"},"bugs":{"url":"https://github.com/darioTecchia/QRChain"},"keywords":["blockchain","PoS","proof of stake","qrchain"],"engines":{"node":">=16.0.0"},"dependencies":{"ip":"^1.1.5","lodash":"^4.17.21","lowdb":"^3.0.0","mkdirp":"^1.0.4","object-hash":"^2.2.0","public-ip":"^5.0.0","sphincs":"^3.0.1"},"devDependencies":{"eslint":"^8.6.0","eslint-plugin-node":"^11.1.0"},"gitHead":"e4b9668bf1670ae36180c1c5c186c0d4d3c2d665","_id":"@dariotecchia/qrchain@1.0.0","_nodeVersion":"16.14.2","_npmVersion":"8.18.0","dist":{"integrity":"sha512-ex0WzE7VqiVA3c7jximXCNEbXKQ3lnCW3bVlDltR6nvdTaKWSGuIsph+hRbglfnmbq3/5WgAU45Yf6H3M2RzZQ==","shasum":"e1df4abe51e44d8e80c1a557e38da4f00cc38a8e","tarball":"https://registry.npmjs.org/@dariotecchia/qrchain/-/qrchain-1.0.0.tgz","fileCount":19,"unpackedSize":1873642,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCJQhjTzgzWXYhR8Tipb9yUt/ucYaJZfZ+FG1t8GU5ZEAIgZamWdAgHa0KaU3lHmEweK9tg3T+O0zdVLzk7V1VnWCk="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjCgMdACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqwEBAAj9aRVVbJMCT0jo8mE8UQiKpiO62Z0rojssEwo1AvXMH3kJbK\r\ny6a5o2VUGMqEwGP155pQujnLup05vKy7AcwiiwZw9x2iKlCHBFQhFHLOGOR2\r\n3FNXgO2iLB/elRwI8xaTzPZa6raLsumtqC/QYahGmBZs4tn02BhQciyh0aS4\r\nXFIvQmHO9YP9/NDSsyg5xrLphFX8Ba7OO3Us5FuxazrcpItirXFDBF/MOyX9\r\nKVmrCkYuKBsH6Pk33nPIoiKFeedcHVHJfhx3Roqr1fEqGz8cG2smbQnT0G94\r\ncWFM4cQMBSAW86ACYdi7PJdYvQ1Cc+dgh530SsOgv+Kn0+ND4cBcPT2yCwY0\r\nTplLiu8cFUKy+gp2M9o4hZT4ASn1bkmrAh5EmWgLf33P6SfVzKgEjkgkyLOQ\r\nuTwSZkBkGgL5ermSJlCilC+hbQbx9L703Lgah252qSUySFDBLrG/KA0t9VIy\r\n6gw2emlHQn4JvENX2DEsVinKeEjrsU7Kq+Sjzp5g15VfQeQktGguGlwxeK/i\r\nCSTMk4QT4X04NEokzrnaLhRxFP8+YFCxZkHSZhgXxm8+g7gsIAYBR+ERnlRp\r\ntftzJu215Dc57+vBo1zI6yxkNK5MC45scWPGSSAOd/JnHE751Y0AY+nI7d+j\r\nCSnAcscJMvnIVB/cBrKO6lwkGkvvw2KYAR4=\r\n=itjT\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"dariotecchia","email":"dariotecchia@gmail.com"},"directories":{},"maintainers":[{"name":"dariotecchia","email":"dariotecchia@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/qrchain_1.0.0_1661600541630_0.0031241809885593774"},"_hasShrinkwrap":false},"1.0.1":{"name":"@dariotecchia/qrchain","version":"1.0.1","description":"A simple QR PoS blockchain","main":"main.js","author":{"name":"dariotecchia","email":"dariotecchia@gmail.com"},"license":"GPL-3.0","homepage":"https://github.com/darioTecchia/QRChain","repository":{"type":"git","url":"git+https://github.com/darioTecchia/QRChain.git"},"bugs":{"url":"https://github.com/darioTecchia/QRChain"},"keywords":["blockchain","PoS","proof of stake","qrchain"],"engines":{"node":">=16.0.0"},"dependencies":{"ip":"^1.1.5","lodash":"^4.17.21","lowdb":"^3.0.0","mkdirp":"^1.0.4","object-hash":"^2.2.0","public-ip":"^5.0.0","sphincs":"^3.0.1"},"devDependencies":{"eslint":"^8.6.0","eslint-plugin-node":"^11.1.0"},"gitHead":"aa53c3d14e162e6976b7cccce4e67f4d54beb5df","_id":"@dariotecchia/qrchain@1.0.1","_nodeVersion":"16.14.2","_npmVersion":"8.18.0","dist":{"integrity":"sha512-l0hFmtZC7j+4JsQYCBWMyiunSVbtf2o1ndMRIw0g9OpF7H++QZT81Nd7jTmm5CWzk7I/bax7yE7czc/AFymFpQ==","shasum":"a086e6a5afb64e2e52463b1a5ec48e27430fe3b2","tarball":"https://registry.npmjs.org/@dariotecchia/qrchain/-/qrchain-1.0.1.tgz","fileCount":19,"unpackedSize":1873638,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICAeAd5To2URIZbU+4BwSJl4lBeqpsGZmIrEZY+Ux9QGAiB9m4obT/b9wcDH+uuXoLXGrezlu2mvZ3+ihBeq04pbmA=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjCgQ8ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmqq4g/+OJFnW9AcsYJUzbgEW6+FamldCvSYup1ELw8KLbI1H2E0WpEL\r\nKzxSpoXSdx0NAFFXhI35Z3Cpx0GKKSPxgLW3hCrqYWB0bMSvqAQlOg+pRHmH\r\n9/zKbtNvTNP+42uyKVNIbYs5mcNZlI9ec2HrJBk/lsyzjcpGN7rodWomq9eV\r\nt8barMlG1N8xPw4IN232bTE5YGUdbMtN4EtVUMPuNDUs1lX/2SCwit/O63Ds\r\nnC0QEhPWIg0rWRMYPVjQPylhuJtwYWfaO/wryI1c7URJ9FVqYpU0Dg9VkPHW\r\nVtYplTU9UbO63syayEarlNDW628dHLMSq00jztzae3iz/SEzS+BQGvnX+9pq\r\n2nLtz/oZnYD7MCuchmbM8pR0s2DUZS6zlylVEQA5E9+xrCjt1JXkYl1OANBi\r\nK+NHWGH7a4TCk96L6PuO36rrU0voiduruqkWgZD7SAtU2ye+9kjEqzRT2GAr\r\nh/j8wBs+sbXr772UOP+XEP/D3gkGerHEfoQ2JuXE2AWNz465r2B88yR7cqUy\r\nFEeXO99VOMKMrMqKwOcxF6+SUJtgRFUtdk7nKOQ+Yu8zE/Pqzgft8xhhLsxy\r\nOXif3zPwWUCUtlgohwRwT9exoYC5wxLEKfNTfH38EhPnt2hOW2UBbDeeUFGA\r\nC5UZp5hbtCP5ilfgIITCaPzwjvUpwftJgCY=\r\n=IXC9\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"dariotecchia","email":"dariotecchia@gmail.com"},"directories":{},"maintainers":[{"name":"dariotecchia","email":"dariotecchia@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/qrchain_1.0.1_1661600827788_0.30784931097638957"},"_hasShrinkwrap":false}},"time":{"created":"2022-08-27T11:42:21.569Z","1.0.0":"2022-08-27T11:42:21.920Z","modified":"2022-08-27T11:47:08.095Z","1.0.1":"2022-08-27T11:47:08.010Z"},"maintainers":[{"name":"dariotecchia","email":"dariotecchia@gmail.com"}],"description":"A simple QR PoS blockchain","homepage":"https://github.com/darioTecchia/QRChain","keywords":["blockchain","PoS","proof of stake","qrchain"],"repository":{"type":"git","url":"git+https://github.com/darioTecchia/QRChain.git"},"author":{"name":"dariotecchia","email":"dariotecchia@gmail.com"},"bugs":{"url":"https://github.com/darioTecchia/QRChain"},"license":"GPL-3.0","readme":"# QRChain\n\n**QRChain** is a decentralized **PoS** blockchain. Written in `Node.js` from scratch.  \nA blockchain designed to be easy to understand and use.\n\nIf you are interested in learning how blockchains technically work, this project will hopefully give you a deeper understanding of the concepts.  \n<!-- You can read my [`step-by-step tutorial`](./) article, on how to write this blockchain. -->\n\n## Features\n\n* Written in `Node.js` from scratch\n* Unique fair **Proof of Stake** algorithm\n* Unique **Wealth Distribution** method\n* Uses `json` as the database\n* Only **1k** lines of code\n\n## What is QRChain?\n\n**QRChain** is a simple implantation of **State Base**, **Proof of Stake** blockchains.  \nAnyone can be a miner or a validator, there are no pre-conditions.  \n\n**QRChain** has two native coins: `GTC` and `MCT`. `GTC` is what is used to pay for transaction fees and `MCT` is used to pay for validating a block. `MCT` can be earned by staking `GTC`.  \n\nEach time a validator **mines** a block, it receives some `GTC` as the block reward plus transactions fees.  \nValidators also burn some `MCT` as the mining fee. The amount of `MCT` burned is a percentage of the total amount of `MCT` that the validator has.  \nThis approach gives better chance to the miners with fewer stakes to get a reward. It also makes the network more distributed.  \n\nI believe that the world's current wealth and resources distribution is not fair. So here at `QRChain`, we introduced **fairer** distribution. Each time a validator mines a block, **20-25%** of the block reward plus the transactions fees goes to charities and other similar organizations' accounts.\n\n**QRChain**  keeps state and chain in two `JSON` files. To achieve this purpose, We use [lowdb](https://github.com/typicode/lowdb), A local `JSON` database, which is **fast** and **easy to use**.\n\n## Consensus Mechanism\n\nBy consensus, we mean the **majority** of validators have agreed on the same block.  \n`QRChain` uses a customized version of the **Proof of Stake** algorithm to achieve this. It simply selects the block candidate from the validator with the highest amount of `MCT` (not `GCT`).  \nThe network is kept safe by the fact that malicious nodes must constantly have **51%** of the total amount of `MCT` in their accounts. remember that the **mining fee** is a percentage of the total amount of `MCT` that the validator has. So constantly having **51%** of the total `MCT` is almost impossible.\n\n## Chain Selection Mechanism\n\nIn `PoW` blockchains like Bitcoin, the **trust** (or correct) chain is the **longest chain**, which is determined by the chain's **total cumulative** proof of work difficulty. in other words, the chain that took the most energy to build.  \nIn `PoS` blockchains, there is no CPU work. So another approach is needed to determine the **trust** chain.  \n\nin `QRChain`, every node, process the next block and adds it to its **Block Candidate List**. Then they start getting other nodes **Block Candidate List** and will add them to their own **Block Candidate List**.  \nThen each node selects the block candidate with the highest amount of `MCT`, and update his state and chain.  \nThen again each node wants to make sure that its chain is the correct chain (it has chosen the block with the highest amount of `MCT`). So it asks other nodes for their last block. if they are the same then everything is fine, else she needs to update or replace her chain.  \n\nFor this, `QRChain` uses a simple reputation mechanism. The confused node will choose the chain from the nodes she **trusts** most.  This list can be inserted by the validator manually or she can use the default algorithm to make the list.  \n\nThe **default algorithm** reviews the confused node's chain, and calculates a trust point for each validator based on how many times a validator has mined a block.  \nThen the node will choose the chain with the **highest trust point**.\n\n## Installation\n\n```bash\nnpm install qrchain\n```\n\n## Usage\n\n```javascript\nconst QRChain = require(\"qrchain\");\nconst path = require( \"path\" );\nconst fs = require( \"fs\" );\nconst { inspect } = require( \"util\" );\n\nconst scriptPath = path.dirname( process.argv[1] );\n\nconst userKeysPath = path.join( scriptPath, \"./my_keys/\" );\nconst userPublicKey = fs.readFileSync( path.join( userKeysPath, \"public_key.pem\" ), \"utf8\" );\nconst userPrivateKey = fs.readFileSync( path.join( userKeysPath, \"private_key.pem\" ), \"utf8\" );\n\nconst validatorKeyPath = path.join( scriptPath, \"./validator_keys/\" );\nconst validatorPublicKey = fs.readFileSync( path.join( validatorKeyPath, \"public_key.pem\" ), \"utf8\" );\nconst validatorPrivateKey = fs.readFileSync( path.join( validatorKeyPath, \"private_key.pem\" ), \"utf8\" );\nconst validator = {\n publicKey: validatorPublicKey,\n privateKey: validatorPrivateKey\n};\n\nconst chain = await new QRChain( { validator } );\n\nconst new_transaction = {\n index: 1, // userPublicKey N transaction\n from: userPublicKey,\n to: validatorPublicKey, // it is null for programs\n amount: 1,\n fee: 1,\n tickPrice: 0,\n};\nnew_transaction.sign = chain.signTransaction( new_transaction, userPrivateKey ); // sign the transaction by the private key of the sender\nnew_transaction.hash = chain.hash( new_transaction );\nawait chain.new_transaction( new_transaction );\nawait chain.new_block();\n// console.log(chain.chain);\nconsole.log( inspect( chain.chain, { colors: true, depth: null } ) );\n```\n\n## Architecture\n\nIt is pretty much just like any other blockchains. each block contains a `previous hash` which is the hash of the previous block. And a `hash` which is a `sha3-512` hash of the block object.\n\n### Blocks\n\nStraight forward, this is an example of a block:\n\n```json\n{\n \"index\": 1,\n \"timestamp\": 1642880361445,\n \"transactions\": [],\n \"block_reward\": 30,\n \"previous_hash\": \"0000000000000000000000000000000000000000\",\n \"extra\": \"The intention of the donations is to help all the beings not only human kinds\",\n \"validator_address\": \"base64 of a public key\",\n \"state_hash\": \"sha3-512 hash of the state json file\",\n \"validator_sign\": \"base64 result of block encryption using validator's private key, RSA algorithm\",\n \"hash\": \"sha3-512 hash of the block object\"\n}\n```\n\n### Transactions\n\nHere is a example of a transaction:\n\n```json\n{\n \"index\": 1,\n \"from\": \"base64 of a public key\",\n \"to\": \"base64 of a public key\",\n \"amount\": 1,\n \"fee\": 1,\n \"tickPrice\": 0,\n \"sign\": \"base64 result of transaction encryption using sender's private key, RSA algorithm\",\n \"hash\": \"sha3-512 hash of the transaction object\"\n}\n```\n\n## Glossary\n\n### Candidate block\n\nEach node has a `Candidate Blocks` list. Nodes add a Candidate block to the list only if it is has a valid signature.\n","readmeFilename":"readme.md"}