github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/blockapps-ba-master/server/lib/user/user.js (about) 1 const ba = require('blockapps-rest'); 2 const rest = ba.rest; 3 const util = ba.common.util; 4 const config = ba.common.config; 5 6 const contractName = 'User'; 7 const contractFilename = `${config.libPath}/user/contracts/User.sol`; 8 9 const ErrorCodes = rest.getEnums(`${config.libPath}/common/ErrorCodes.sol`).ErrorCodes; 10 const UserRole = rest.getEnums(`${config.libPath}/user/contracts/UserRole.sol`).UserRole; 11 12 function* uploadContract(admin, args) { 13 const contract = yield rest.uploadContract(admin, contractName, contractFilename, args); 14 yield compileSearch(contract); 15 contract.src = 'removed'; 16 return setContract(admin, contract); 17 } 18 19 function setContract(admin, contract) { 20 contract.getState = function* () { 21 return yield rest.getState(contract); 22 } 23 contract.authenticate = function* (pwHash) { 24 return yield authenticate(admin, contract, pwHash); 25 } 26 return contract; 27 } 28 29 function* compileSearch(contract) { 30 rest.verbose('compileSearch', contract.codeHash); 31 if (yield rest.isSearchable(contract.codeHash)) { 32 return; 33 } 34 35 const searchable = [contractName]; 36 yield rest.compileSearch(searchable, contractName, contractFilename); 37 } 38 39 function* getUsers(addresses) { 40 const csv = util.toCsv(addresses); // generate csv string 41 const results = yield rest.query(`${contractName}?address=in.${csv}`); 42 return results; 43 } 44 45 function* getUserById(id) { 46 const baUser = (yield rest.waitQuery(`${contractName}?id=eq.${id}`, 1))[0]; 47 return baUser; 48 } 49 50 function* getUserByAddress(address) { 51 const baUser = (yield rest.waitQuery(`${contractName}?address=eq.${address}`, 1))[0]; 52 return baUser; 53 } 54 55 function* authenticate(admin, contract, pwHash) { 56 rest.verbose('authenticate', pwHash); 57 // function authenticate(bytes32 _pwHash) return (bool) { 58 const method = 'authenticate'; 59 const args = { 60 _pwHash: pwHash, 61 }; 62 const result = yield rest.callMethod(admin, contract, method, args); 63 const isAuthenticated = (result[0] === true); 64 return isAuthenticated; 65 } 66 67 68 module.exports = { 69 uploadContract: uploadContract, 70 compileSearch: compileSearch, 71 72 // constants 73 contractName: contractName, 74 ErrorCodes: ErrorCodes, 75 UserRole: UserRole, 76 77 // business logic 78 authenticate: authenticate, 79 getUserByAddress: getUserByAddress, 80 getUsers: getUsers, 81 getUserById: getUserById, 82 contractName: contractName, 83 };