github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/blockapps-ba-master/server/lib/user/userManager.js (about)

     1  const ba = require('blockapps-rest');
     2  const rest = ba.rest;
     3  const util = ba.common.util;
     4  const BigNumber = ba.common.BigNumber;
     5  const config = ba.common.config;
     6  
     7  const contractName = 'UserManager';
     8  const contractFilename = `${config.libPath}/user/contracts/UserManager.sol`;
     9  
    10  const ErrorCodes = rest.getEnums(`${config.libPath}/common/ErrorCodes.sol`).ErrorCodes;
    11  const UserRole = rest.getEnums(`${config.libPath}/user/contracts/UserRole.sol`).UserRole;
    12  
    13  function* uploadContract(admin, args) {
    14    const contract = yield rest.uploadContract(admin, contractName, contractFilename, args);
    15    yield compileSearch(contract);
    16    contract.src = 'removed';
    17    return setContract(admin, contract);
    18  }
    19  
    20  function setContract(admin, contract) {
    21    contract.getState = function* () {
    22      return yield rest.getState(contract);
    23    }
    24    contract.createUser = function* (args) {
    25      return yield createUser(admin, contract, args);
    26    }
    27    contract.exists = function* (username) {
    28      return yield exists(admin, contract, username);
    29    }
    30    contract.getUser = function* (username) {
    31      return yield getUser(admin, contract, username);
    32    }
    33    contract.getUsers = function* () {
    34      return yield getUsers(admin, contract);
    35    }
    36    contract.login = function* (args) {
    37      return yield login(admin, contract, args);
    38    }
    39    contract.getBalance = function* (username, node) {
    40      return yield getBalance(admin, contract, username, node);
    41    }
    42    return contract;
    43  }
    44  
    45  function* compileSearch(contract) {
    46    rest.verbose('compileSearch', contract.codeHash);
    47    if (yield rest.isSearchable(contract.codeHash)) {
    48      return;
    49    }
    50    // compile
    51    const userJs = require('./user');
    52    const searchable = [userJs.contractName, contractName];
    53    yield rest.compileSearch(searchable, contractName, contractFilename);
    54  }
    55  
    56  function* getBalance(admin, contract, username, node) {
    57    rest.verbose('getBalance', username);
    58    const baUser = yield getUser(admin, contract, username);
    59    const accounts = yield rest.getAccount(baUser.account);
    60    const balance = new BigNumber(accounts[0].balance);
    61    return balance;
    62  }
    63  
    64  // throws: ErrorCodes
    65  // returns: user record from search
    66  function* createUser(admin, contract, args) {
    67    rest.verbose('createUser', args);
    68  
    69    // create bloc user
    70    const blocUser = yield rest.createUser(args.username, args.password);
    71    args.account = blocUser.address;
    72    args.pwHash = util.toBytes32(args.password); // FIXME this is not a hash
    73  
    74    // function createUser(address account, string username, bytes32 pwHash, UserRole role) returns (ErrorCodes) {
    75    const method = 'createUser';
    76  
    77    // create the user, with the eth account
    78    const result = yield rest.callMethod(admin, contract, method, args);
    79    const errorCode = parseInt(result[0]);
    80    if (errorCode != ErrorCodes.SUCCESS) {
    81      throw new Error(errorCode);
    82    }
    83    // block until the user shows up in search
    84    const baUser = yield getUser(admin, contract, args.username);
    85    baUser.blocUser = blocUser;
    86    return baUser;
    87  }
    88  
    89  function* exists(admin, contract, username) {
    90      rest.verbose('exists', username);
    91      // function exists(string username) returns (bool) {
    92      const method = 'exists';
    93      const args = {
    94        username: username,
    95      };
    96      const result = yield rest.callMethod(admin, contract, method, args);
    97      const exist = (result[0] === true);
    98      return exist;
    99  }
   100  
   101  function* getUser(admin, contract, username) {
   102    rest.verbose('getUser', username);
   103    // function getUser(string username) returns (address) {
   104    const method = 'getUser';
   105    const args = {
   106      username: username,
   107    };
   108  
   109    // get the use address
   110    const userAddress = (yield rest.callMethod(admin, contract, method, args))[0];
   111    if (userAddress == 0) {
   112      throw new Error(ErrorCodes.NOT_FOUND);
   113    }
   114    // found - query for the full user record
   115    const userJs = require('./user');
   116    const baUser = yield userJs.getUserByAddress(userAddress);
   117    return baUser;
   118  }
   119  
   120  function* getUsers(admin, contract) {
   121    rest.verbose('getUsers');
   122    const state = yield rest.getState(contract);
   123    const users = state.users;
   124    const userJs = require('./user');
   125    const results = yield userJs.getUsers(users);
   126    return results;
   127  }
   128  
   129  function* login(admin, contract, args) {
   130    rest.verbose('login', args);
   131  
   132    // function login(string username, bytes32 pwHash) returns (bool) {
   133    const method = 'login';
   134    args.pwHash = util.toBytes32(args.password);
   135    const result = (yield rest.callMethod(admin, contract, method, args))[0];
   136    const isOK = (result == true);
   137    return isOK;
   138  }
   139  
   140  module.exports = {
   141    uploadContract: uploadContract,
   142    compileSearch: compileSearch,
   143    setContract: setContract,
   144    contractName: contractName,
   145  };