github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/build-blockchain-insurance-app-master/web/www/blockchain/shopPeer.js (about)

     1  'use strict';
     2  
     3  import config from './config';
     4  import { wrapError } from './utils';
     5  import { shopClient as client, isReady } from './setup';
     6  import uuidV4 from 'uuid/v4';
     7  
     8  export async function getContractTypes(shopType) {
     9    if (!isReady()) {
    10      return;
    11    }
    12    try {
    13      return await query('contract_type_ls', { shopType });
    14    } catch (e) {
    15      throw wrapError(
    16        `Error getting contract types for shop type ${shopType} : ${e.message}`
    17        , e);
    18    }
    19  }
    20  
    21  export async function createContract(contract) {
    22    if (!isReady()) {
    23      return;
    24    }
    25    try {
    26      let c = Object.assign({}, contract, { uuid: uuidV4() });
    27      const loginInfo = await invoke('contract_create', c);
    28      if (!loginInfo
    29        ^ !!(loginInfo && loginInfo.username && loginInfo.password)) {
    30        return Object.assign(loginInfo || {}, { uuid: c.uuid });
    31      } else {
    32        throw new Error(loginInfo);
    33      }
    34    } catch (e) {
    35      throw wrapError(`Error creating contract: ${e.message}`, e);
    36    }
    37  }
    38  
    39  export async function createUser(user) {
    40    if (!isReady()) {
    41      return;
    42    }
    43    try {
    44      const loginInfo = await invoke('user_create', user);
    45      if (!loginInfo ^
    46        !!(loginInfo && loginInfo.username && loginInfo.password)) {
    47        return loginInfo;
    48      } else {
    49        throw new Error(loginInfo);
    50      }
    51    } catch (e) {
    52      throw wrapError(`Error creating user: ${e.message}`, e);
    53    }
    54  }
    55  
    56  export async function authenticateUser(username, password) {
    57    if (!isReady()) {
    58      return;
    59    }
    60    try {
    61      let authenticated =
    62        await query('user_authenticate', { username, password });
    63      if (authenticated === undefined || authenticated === null) {
    64        throw new Error('Unknown error, invalid response!');
    65      }
    66      return authenticated;
    67    } catch (e) {
    68      throw wrapError(`Error authenticating user: ${e.message}`, e);
    69    }
    70  }
    71  
    72  export async function getUserInfo(username) {
    73    if (!isReady()) {
    74      return;
    75    }
    76    try {
    77      const user = await query('user_get_info', { username });
    78      return user;
    79    } catch (e) {
    80      throw wrapError(`Error getting user info: ${e.message}`, e);
    81    }
    82  }
    83  
    84  export function getBlocks(noOfLastBlocks) {
    85    return client.getBlocks(noOfLastBlocks);
    86  }
    87  
    88  export const on = client.on.bind(client);
    89  export const once = client.once.bind(client);
    90  export const addListener = client.addListener.bind(client);
    91  export const prependListener = client.prependListener.bind(client);
    92  export const removeListener = client.removeListener.bind(client);
    93  
    94  function invoke(fcn, ...args) {
    95    return client.invoke(
    96      config.chaincodeId, config.chaincodeVersion, fcn, ...args);
    97  }
    98  
    99  function query(fcn, ...args) {
   100    return client.query(
   101      config.chaincodeId, config.chaincodeVersion, fcn, ...args);
   102  }