TAGS

peerai-api

CREATED

UPDATED

STARS

INSTALL

...

REPOSITORY
GitHub Download
Please wait: Fetching readme...

b'# README\n\n---\nPEER-AI\n---\n\n* About - Peerism API built with Truffle, Node.js, Express.js, Mongoose, MongoDB, Solidity, and Ganache CLI (TestRPC).\n\n* Instructions - Setup instructions (for macOS) are provided in the [Quick Start Guide](https://github.com/peerism/peerai-api#quick-start-guide-) shown below.\n\n* Usage Capabilities - Use cURL to simulate a HTTP POST request to a Peerism API endpoint (instead of sending a request from the Peerism React Native \xc3\x90App https://github.com/peerism/peer.ai). Alternatively allows triggering Middleware functions individually. API routes use an Express.js Middleware Chain that allow a request to compile the Peerism Smart Contract using Solidity Compiler (Solc) and Ether Pudding, then deploy it to the Ethereum TestRPC test network blockchain and responds with the contract address.\n\n# Table of Contents\n * [Quick Start Guide](#chapter-0)\n * [Log](#chapter-log)\n * [FAQ](#chapter-faq)\n * [TODO](#chapter-todo)\n * [References](#chapter-references)\n\n## Quick Start Guide \n\n* Terminal Tab #1 - Install dependencies including Ganache CLI (TestRPC from the Truffle Suite)\n ```\n nvm install v9.3.0;\n nvm use;\n nvm use v9.3.0;\n yarn install;\n npm install -g ganache-cli\n ```\n\n* Terminal Tab #1 - Run MongoDB Server in a separate Terminal tab\n ```\n mongod\n ```\n\n* Terminal Tab #2 - Run Ethereum Client using Ganache CLI (TestRPC).\n\n```\nrm -rf ./db;\nmkdir -p db/chaindb;\nganache-cli --account \'0x0000000000000000000000000000000000000000000000000000000000000001, 10002471238800000000000\' \\\n --account \'0x0000000000000000000000000000000000000000000000000000000000000002, 10004471238800000000000\' \\\n --unlock \'0x0000000000000000000000000000000000000000000000000000000000000001\' \\\n --unlock \'0x0000000000000000000000000000000000000000000000000000000000000002\' \\\n --unlock \'0x7e5f4552091a69125d5dfcb7b8c2659029395bdf\' \\\n --unlock \'0x2b5ad5c4795c026514f8317c7a215e218dccd6cf\' \\\n --blocktime 0 \\\n --deterministic true \\\n --port 8545 \\\n --hostname localhost \\\n --gasPrice 20000000000 \\\n --gasLimit 0x8000000 \\\n --debug true \\\n --mem true \\\n --networkId 1337 \\\n --db \'./db/chaindb\'\n```\n\n* Terminal Tab #3 - Compile and Deploy Smart Contracts to TestRPC blockchain\n\n * Compile Smart Contracts\n\n * Option 1: Generates build/contracts/Peerism.sol.js\n ```\n node lib/compileContract.js Peerism\n ```\n\n * Option 2: Genertes build/contracts/Peerism.json (DEPRECATED)\n ```\n truffle compile --compile-all;\n ```\n\n * Deploy Smart Contracts\n\n * Option 1: Deploy without Truffle\n\n * Modify Bitcore dependency before running the next command to avoid error `Error: More than one instance of bitcore-lib found. Please make sure to require bitcore-lib and check that submodules do not also include their own bitcore-lib dependency.`, as described here: https://github.com/bitpay/bitcore/issues/1454, by opening node_modules/bitcore-mnemonic/node_modules/bitcore-lib/index.js and commented out the following lines of code to avoid an error.\n ```\n bitcore.versionGuard = function(version) {\n // if (version !== undefined) {\n // var message = \'More than one instance of bitcore-lib found. \' +\n // \'Please make sure to require bitcore-lib and check that submodules do\' +\n // \' not also include their own bitcore-lib dependency.\';\n // throw new Error(message);\n // }\n };\n ```\n\n ```\n node lib/deployContract.js Peerism\n ```\n\n * Option 2: Deploy with Truffle (DEPRECATED)\n ```\n truffle migrate --reset --network development;\n ```\n\n * Note: Watch the deployment transactions being send to the blockchain in Terminal Tab #2\n\n* Terminal Tab #3 - Run Tests\n ```\n truffle test;\n ```\n\n* Terminal Tab #4 - Drop the server. Run server, then try cURL requests\n ```\n yarn run drop; yarn run dev;\n ``` \n\n* Terminal Tab #4 - Send request to server and receive response for authentication and authorisation to access specific API endpoints.\n * cURL\n * Register with email/password. JWT provided in response (i.e. `{"token":"xyz"}`)\n ```\n curl -v -X POST http://localhost:7000/users/auth/register -d "email=luke@schoen.com&password=123456&name=Luke" -H "Content-Type: application/x-www-form-urlencoded"\n curl -v -X POST http://localhost:7000/users/auth/register -d \'{"email":"gavin@wood.com", "password":"123456", "name":"Gavin"}\' -H "Content-Type: application/json"\n ```\n * Sign in with email/password. JWT provided in response (i.e. `{"token":"xyz"}`)\n ```\n curl -v -X POST http://localhost:7000/users/auth -d "email=luke@schoen.com&password=123456" -H "Content-Type: application/x-www-form-urlencoded"\n curl -v -X POST http://localhost:7000/users/auth -d \'{"email":"gavin@wood.com", "password":"123456"}\' -H "Content-Type: application/json"\n ```\n * Access a restricted endpoint by providing JWT\n ```\n curl -v -X GET http://localhost:7000/users -H "Content-Type: application/json" -H "Authorization: Bearer "\n ```\n * Create user by providing JWT\n ```\n curl -v -X POST http://localhost:7000/users/create --data \'[{"email":"test@fake.com", "name":"Test"}]\' -H "Content-Type: application/json" -H "Authorization: JWT "\n curl -v -X POST http://localhost:7000/users/create -d "email=test2@fake.com&name=Test2" -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: JWT "\n ```\n\n* Terminal Tab #4 - Send request to server with Smart Contract Name to be Compiled and Deployed to the Ethereum TestRPC and receive response with the Contract Address.\n * cURL\n ```\n curl -v -X POST http://localhost:7000/contracts/generate -d \'{"contractName":"Peerism"}\' -H "Content-Type: application/json"\n ```\n\n* Terminal Tab #4 - Experiment in REPL\n\n * Use Truffle Console\n\n * Run Truffle Console\n ```\n truffle console --network development;\n ```\n\n * Run commands\n ```\n web3\n web3.currentProvider\n web3.eth.getBalance(\'0x7e5f4552091a69125d5dfcb7b8c2659029395bdf\')\n ```\n\n * Attach to EthereumJS TestRPC using Go Ethereum (Geth)\n * [Install Geth](https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-Mac)\n\n * Start Geth JavaScript console\n\n ```\n geth attach rpc:http://localhost:8545\n ```\n\n * Run commands\n\n ```\n web3\n web3.currentProvider\n web3.eth.getBalance(\'0x7e5f4552091a69125d5dfcb7b8c2659029395bdf\')\n eth.accounts\n ```\n\n * Optional: Try to perform RPC calls to Ganache TestRPC using cURL. https://github.com/trufflesuite/ganache-cli/issues/383\n\n* Terminal Tab #5 - Run Tests on port 7111\n ```\n yarn run drop; yarn run test-watch\n ```\n\n* Terminal Tab #1 - Drop the database. Seed the database\n ```\n yarn run drop;\n yarn run seed;\n ```\n\n## Log \n\n* Initial setup\n ```\n git init; touch README.md; touch .gitignore;\n code .;\n ```\n * [Add boilerplate contents to .gitignore for Node.js](https://github.com/github/gitignore/blob/master/Node.gitignore)\n\n* Setup API\n ```\n yarn init -y; \n yarn add express body-parser;\n yarn add nodemon --dev;\n touch server.js;\n ```\n* Add boilerplate contents to server.js\n* Add "dev" in "scripts" section of package.json\n\n* Add Mongoose\n ```\n yarn add mongoose;\n mkdir models; touch models/init.js;\n touch models/User.js;\n touch models/seeds.js;\n touch models/drop.js\n ```\n\n* Create Models for Mongoose\n* Add boilerplate contents to models\n* Add scripts to package.json\n\n* Run MongoDB Server\n ```\n mongod\n ```\n\n* MongoDB Client\n ```\n mongo\n\n show dbs\n use peerai\n show collections\n db.users.find({})\n db.skills.find({})\n ```\n\n* Create routes\n ```\n mkdir routes\n ```\n* Modify server.js. Add routes/users.js\n\n* Add authentication with [Passport, Passport-Local, and Passport-Local-Mongoose](https://github.com/saintedlama/passport-local-mongoose):\n ```\n yarn add passport passport-local passport-local-mongoose\n ```\n* Rename Person and people to User and users\n* Add User Registration route\n* Add User Sign in route\n* Add JWT library to return a token instead of a user\n ```\n yarn add jsonwebtoken;\n ```\n* Add Passport JWT library\n ```\n yarn add passport-jwt\n ```\n* Add restricted endpoint that requires valid JWT to access\n* Add Controllers https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/routes\n* Add Route Tests\n ```\n yarn add mocha chai chai-http --dev;\n mkdir -p test/routes;\n touch test/routes/users_test.js;\n ```\n* Add Model Tests\n ```\n mkdir test/models;\n touch test/models/users_test.js\n ```\n* Add Dotenv library to use different database in development and testing\n ```\n yarn add lodash;\n yarn add dotenv --dev;\n touch .sample-env;\n echo \'NODE_ENV=development\' >> ./.sample-env;\n ```\n* Add Ethereum dependencies including TestRPC\n ```\n yarn add web3@0.19 ethereumjs-util@4.4 ethereumjs-tx@1.3 eth-lightwallet@2.5;\n yarn add ethereumjs-testrpc --dev;\n yarn add solc ether-pudding --dev;\n yarn add truffle-artifactor --dev;\n ```\n * References\n * https://medium.com/@codetractio/try-out-ethereum-using-only-nodejs-and-npm-eabaaaf97c80\n * Smart Contracts without Truffle - https://medium.com/@doart3/ethereum-dapps-without-truffle-compile-deploy-use-it-e6daeefcf919\n * EthereumJS Util - Library for cryptographic hashes for Ethereum addresses - https://github.com/ethereumjs/ethereumjs-util\n * EthereumJS Tx - library to create, edit, and sign Ethereum transactions - https://github.com/ethereumjs/ethereumjs-tx\n * EthereumJS LightWallet - https://github.com/ConsenSys/eth-lightwallet\n * Solc - Compile Solidity Contract - https://www.npmjs.com/package/solc\n * Ether Pudding - Manage Solidity Contracts and Packages - https://www.npmjs.com/package/ether-pudding\n * Truffle Artifactor - replaces Ether Pudding - https://github.com/trufflesuite/truffle-artifactor\n * Reading from JSON files - https://www.codementor.io/codementorteam/how-to-use-json-files-in-node-js-85hndqt32\n\n* Problem: Tried to manually compile using Solc with `node lib/compileContract.js ConvertLib`, which generates ConvertLib.solc.js in build/contracts. However it does not compile MetaCoin.sol, as it returns error `1:27: ParserError: Source "ConvertLib.sol" not found: File not supplied initially.\\n ... import "./ConvertLib.sol"`.\n * Solution: Use Truffle to compile Solidity contracts with `truffle compile --compile-all`\n\n* Run shell script in new Terminal tab (copy from https://github.com/ltfschoen/solidity_test/blob/master/testrpc.sh)\n ```\n rm -rf ./db;\n mkdir db && mkdir db/chaindb;\n cd ~/code/blockchain/solidity_test; testrpc --account \'0x0000000000000000000000000000000000000000000000000000000000000001, 10002471238800000000000\' \\\n --account \'0x0000000000000000000000000000000000000000000000000000000000000002, 10004471238800000000000\' \\\n --account \'0x0000000000000000000000000000000000000000000000000000000000000003, 10004471238800000000000\' \\\n --account \'0x0000000000000000000000000000000000000000000000000000000000000004, 10004471238800000000000\' \\\n --account \'0x0000000000000000000000000000000000000000000000000000000000000005, 10004471238800000000000\' \\\n --account \'0x0000000000000000000000000000000000000000000000000000000000000006, 10004471238800000000000\' \\\n --account \'0x0000000000000000000000000000000000000000000000000000000000000007, 10004471238800000000000\' \\\n --unlock \'0x0000000000000000000000000000000000000000000000000000000000000001\' \\\n --unlock \'0x0000000000000000000000000000000000000000000000000000000000000002\' \\\n --unlock \'0x0000000000000000000000000000000000000000000000000000000000000003\' \\\n --unlock \'0x0000000000000000000000000000000000000000000000000000000000000004\' \\\n --unlock \'0x0000000000000000000000000000000000000000000000000000000000000005\' \\\n --unlock \'0x0000000000000000000000000000000000000000000000000000000000000006\' \\\n --unlock \'0x0000000000000000000000000000000000000000000000000000000000000007\' \\\n --unlock \'0x7e5f4552091a69125d5dfcb7b8c2659029395bdf\' \\\n --unlock \'0x2b5ad5c4795c026514f8317c7a215e218dccd6cf\' \\\n --blocktime 0 \\\n --deterministic true \\\n --port 8545 \\\n --hostname localhost \\\n --gasPrice 20000000000 \\\n --gasLimit 1000000 \\\n --debug true \\\n --mem true \\\n --db \'./db/chaindb\'\n ```\n* Install Truffle\n ```\n npm install -g truffle;\n truffle init;\n ```\n* Run Truffle Unbox in separate directory to get template Metacoin example and move relevant boilerplate contracts and tests into the the root folder\n* Update package.json tests script to run tests for Smart Contracts and API tests:\n ```\n "test": "truffle test; NODE_ENV=testing mocha --recursive test/**/*_test.js",\n ```\n* Remove truffle-config.js and add the following to truffle.js:\n ```\n module.exports = {\n // http://truffleframework.com/docs/advanced/configuration\n networks: {\n development: {\n host: "localhost",\n port: 8545,\n network_id: "*" // Match any network id\n }\n }\n };\n ```\n* Add ethpm.json for EthPM Package Management \n ```\n {\n "package_name": "truffle-box-peerism-api-node-express",\n "version": "0.0.1",\n "description": "Truffle Box of Peerism API built with Truffle, Node.js, Express.js,\n Solidity, Ether Pudding, and Ethereum TestRPC",\n "authors": [\n "Luke Schoen "\n ],\n "keywords": [\n "ethereum",\n "express.js",\n "node.js",\n "middleware",\n "api"\n ],\n "license": "MIT"\n }\n ```\n * References: \n * http://truffleframework.com/docs/getting_started/packages-ethpm\n\n* Open node_modules/bitcore-mnemonic/node_modules/bitcore-lib/index.js and commented out the following lines of code to avoid an error.\n ```\n bitcore.versionGuard = function(version) {\n // if (version !== undefined) {\n // var message = \'More than one instance of bitcore-lib found. \' +\n // \'Please make sure to require bitcore-lib and check that submodules do\' +\n // \' not also include their own bitcore-lib dependency.\';\n // throw new Error(message);\n // }\n };\n ```\n\n* Run Truffle Console experimentation\n ```\n truffle console --network development;\n ```\n\n* Build script for Smart Contract (generates .sol.js file in build/contracts/)\n ```\n mkdir lib;\n node lib/compileContract.js Peerism\n ```\n * Alternatively compile with Truffle\n\n* Deployment script for Smart Contract\n * Reference: https://medium.com/@codetractio/try-out-ethereum-using-only-nodejs-and-npm-eabaaaf97c80\n ```\n touch lib/deployContract.js;\n node lib/deployContract.js Peerism;\n ```\n * References:\n * http://truffleframework.com/docs/getting_started/contracts\n * Gas Limits - https://bitcoin.stackexchange.com/questions/39132/what-is-gas-limit-in-ethereum\n\n## FAQ \n\n* How to understand how to use Passport JWT library?\n * Refer to the library codebase on Github or in node_modules/jsonwebtoken/ i.e. [verify.js](https://github.com/auth0/node-jsonwebtoken/blob/master/verify.js)\n * Use breakpoints\n * Experiment using Node. i.e. Run `node` then\n ```\n npm install jsonwebtoken\n\n const JWT = require(\'jsonwebtoken\');\n JWT.decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6Imx0ZnNjaG9lbkBnbWFpbC5jb20iLCJpYXQiOjE1MTMwNjY3NTEsImV4cCI6MTUxMzY3MTU1MSwic3ViIjoiNWEyZjkwZmZiNTI5YjI0YzM5MTA1NWM3In0.MkcCR1YD2c21x_WOQObyY-UPAQDWTcooOiO69saUVMI")\n ```\n\n## References \n\n* [Express.js server API with JWT authorisation](https://www.youtube.com/watch?v=ggv3rnaHuK8)\n* [Express.js Routes](https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/routes)\n\n## TODO \n\n* [ ] Integrate with [Peerism React Native app](https://github.com/peerism/peer.ai)\n* [X] Integrate Solidity smart contract using TestRPC\n* [ ] Create a Truffle Box\n * https://github.com/trufflesuite/truffle/issues/433\n * http://truffleframework.com/boxes/\n* [ ] Upgrade to latest Web3 1.0.0 Beta-27 that has been successfully used in\nhttps://github.com/ltfschoen/geth-node to deploy a FixedSupplyToken.sol smart contract to a Private Network with Geth'