UNPKG

wehelpjs

Version:

wehelpjs is the JavaScript API Library for the WeYouMe blockchain

102 lines (78 loc) 3.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAccountCreationFee = exports.createSuggestedPassword = exports.authorize = exports.addPostingAuthority = exports.hasAuthority = exports.login = void 0; var _isomorphicFetch = _interopRequireDefault(require("isomorphic-fetch")); var _api = require("../api"); var _broadcast = require("../broadcast"); var _memo = require("../auth/memo"); var _ecc = require("../auth/ecc"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // eslint-disable-line camelcase const login = ({ username, wif, role = 'posting' }, cb) => { (0, _isomorphicFetch.default)(`/api/login/challenge?username=${username}&role=${role}`).then(res => res.json()).then(data => { const token = (0, _memo.decode)(wif, data.code).substring(1); localStorage.setItem('token', token); cb(null, data); }).catch(err => cb(err, null)); }; exports.login = login; const hasAuthority = (user, clientId, role = 'posting') => { const auths = user[role].account_auths.map(auth => auth[0]); return auths.indexOf(clientId) !== -1; }; exports.hasAuthority = hasAuthority; const addPostingAuthority = ({ username, wif, clientId }, cb) => { _api.api.getAccounts([username], (err, result) => { const { posting, memoKey, json } = result[0]; const postingNew = posting; if (!hasAuthority(result[0], clientId)) { postingNew.account_auths.push([clientId, parseInt(posting.weight_threshold, 10)]); _broadcast.broadcast.accountUpdate(wif, username, undefined, undefined, postingNew, memoKey, json, (errA, resultA) => cb(errA, resultA)); } else { cb(null, {}); } }); }; exports.addPostingAuthority = addPostingAuthority; const authorize = ({ clientId, scope, responseType = 'token' }, cb) => { const token = localStorage.getItem('token'); (0, _isomorphicFetch.default)(`/api/oauth2/authorize?client_id=${clientId}&scope=${scope}&response_type=${responseType}`, { headers: new Headers({ Authorization: token }) }).then(res => res.json()).then(data => cb(null, data)).catch(err => cb(err, null)); }; // https://github.com/WeYouMe/weauth/blob/634c13cd0d2fafa28592e9d5f43589e201198248/app/components/elements/SuggestPassword.jsx#L97 exports.authorize = authorize; const createSuggestedPassword = () => { const PASSWORD_LENGTH = 32; const privateKey = _ecc.key_utils.get_random_key(); return privateKey.toWif().substring(3, 3 + PASSWORD_LENGTH); }; exports.createSuggestedPassword = createSuggestedPassword; const getAccountCreationFee = async () => { const chainConfig = await _api.api.getConfigAsync(); const chainProps = await _api.api.getChainPropertiesAsync(); const accountCreationFee = chainProps.account_creation_fee; const priceModifier = chainConfig.CREATE_ACCOUNT_WITH_TME_MODIFIER; const accountCreationFeeInTME = parseFloat(accountCreationFee.split(' ')[0]) * priceModifier; return `${accountCreationFeeInTME.toFixed(3)} TME`; }; exports.getAccountCreationFee = getAccountCreationFee;