UNPKG

golos-lib-js

Version:

Golos-js the JavaScript library with API for GOLOS blockchain

51 lines (50 loc) 2.63 kB
"use strict"; var _api = _interopRequireDefault(require("../api")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const defaultWeight = 1; exports = module.exports = steemBroadcast => { steemBroadcast.addAccountAuth = function (activeWif, username, authorizedUsername) { let role = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : "posting"; let cb = arguments.length > 4 ? arguments[4] : undefined; _api.default.getAccountsAsync([username]).then(_ref => { let [userAccount] = _ref; const updatedAuthority = userAccount[role]; const authorizedAccounts = updatedAuthority.account_auths.map(auth => auth[0]); const hasAuthority = authorizedAccounts.indexOf(authorizedUsername) !== -1; if (hasAuthority) { // user does already exist in authorized list return cb(null, null); } updatedAuthority.account_auths.push([authorizedUsername, defaultWeight]); const owner = role === "owner" ? updatedAuthority : undefined; const active = role === "active" ? updatedAuthority : undefined; const posting = role === "posting" ? updatedAuthority : undefined; /** Add authority on user account */ steemBroadcast.accountUpdate(activeWif, userAccount.name, owner, active, posting, userAccount.memo_key, userAccount.json_metadata, cb); }); }; steemBroadcast.removeAccountAuth = function (activeWif, username, authorizedUsername) { let role = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : "posting"; let cb = arguments.length > 4 ? arguments[4] : undefined; _api.default.getAccountsAsync([username]).then(_ref2 => { let [userAccount] = _ref2; const updatedAuthority = userAccount[role]; const totalAuthorizedUser = updatedAuthority.account_auths.length; for (let i = 0; i < totalAuthorizedUser; i++) { const user = updatedAuthority.account_auths[i]; if (user[0] === authorizedUsername) { updatedAuthority.account_auths.splice(i, 1); break; } } // user does not exist in authorized list if (totalAuthorizedUser === updatedAuthority.account_auths.length) { return cb(null, null); } const owner = role === "owner" ? updatedAuthority : undefined; const active = role === "active" ? updatedAuthority : undefined; const posting = role === "posting" ? updatedAuthority : undefined; steemBroadcast.accountUpdate(activeWif, userAccount.name, owner, active, posting, userAccount.memo_key, userAccount.json_metadata, cb); }); }; };