@colony/colony-js-client
Version:
Reference implementation of the Colony API
775 lines (610 loc) • 29 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getDomainIdFromPot = undefined;
var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');
var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);
var _defineProperty2 = require('babel-runtime/helpers/defineProperty');
var _defineProperty3 = _interopRequireDefault(_defineProperty2);
var _toArray2 = require('babel-runtime/helpers/toArray');
var _toArray3 = _interopRequireDefault(_toArray2);
var _extends3 = require('babel-runtime/helpers/extends');
var _extends4 = _interopRequireDefault(_extends3);
var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _get2 = require('babel-runtime/helpers/get');
var _get3 = _interopRequireDefault(_get2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
var _colonyJsContractClient = require('@colony/colony-js-contract-client');
var _colonyJsContractClient2 = _interopRequireDefault(_colonyJsContractClient);
var _constants = require('../../constants');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/* eslint-disable no-underscore-dangle */
var getDomainIdFromPot = exports.getDomainIdFromPot = function () {
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(potId, colonyClient) {
var _ref2, type, typeId, _ref3, domainId, _ref4, _domainId, _ref5, _domainId2;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if (!(potId === _constants.REWARDS_POT_ID)) {
_context.next = 2;
break;
}
return _context.abrupt('return', _constants.DEFAULT_DOMAIN_ID);
case 2:
_context.next = 4;
return colonyClient.getFundingPot.call({ potId: potId });
case 4:
_ref2 = _context.sent;
type = _ref2.type;
typeId = _ref2.typeId;
if (!(type === _constants.FUNDING_POT_TYPE_DOMAIN)) {
_context.next = 9;
break;
}
return _context.abrupt('return', typeId);
case 9:
if (!(type === _constants.FUNDING_POT_TYPE_TASK)) {
_context.next = 15;
break;
}
_context.next = 12;
return colonyClient.getTask.call({ taskId: typeId });
case 12:
_ref3 = _context.sent;
domainId = _ref3.domainId;
return _context.abrupt('return', domainId);
case 15:
if (!(type === _constants.FUNDING_POT_TYPE_PAYMENT)) {
_context.next = 21;
break;
}
_context.next = 18;
return colonyClient.getPayment.call({
paymentId: typeId
});
case 18:
_ref4 = _context.sent;
_domainId = _ref4.domainId;
return _context.abrupt('return', _domainId);
case 21:
if (!(type === _constants.FUNDING_POT_TYPE_EXPENDITURE)) {
_context.next = 27;
break;
}
_context.next = 24;
return colonyClient.getExpenditure.call({
expenditureId: typeId
});
case 24:
_ref5 = _context.sent;
_domainId2 = _ref5.domainId;
return _context.abrupt('return', _domainId2);
case 27:
throw new Error('Rewards pot has no domain!');
case 28:
case 'end':
return _context.stop();
}
}
}, _callee, undefined);
}));
return function getDomainIdFromPot(_x, _x2) {
return _ref.apply(this, arguments);
};
}();
// eslint-disable-next-line max-len
var DomainAuth = function (_ContractClient$Sende) {
(0, _inherits3.default)(DomainAuth, _ContractClient$Sende);
function DomainAuth(_ref6) {
var client = _ref6.client,
defaultGasLimit = _ref6.defaultGasLimit,
permissions = _ref6.permissions,
validateEmpty = _ref6.validateEmpty,
rest = (0, _objectWithoutProperties3.default)(_ref6, ['client', 'defaultGasLimit', 'permissions', 'validateEmpty']);
(0, _classCallCheck3.default)(this, DomainAuth);
var _this = (0, _possibleConstructorReturn3.default)(this, (DomainAuth.__proto__ || Object.getPrototypeOf(DomainAuth)).call(this, (0, _extends4.default)({
client: client,
defaultGasLimit: defaultGasLimit,
validateEmpty: validateEmpty
}, rest)));
_this._permissions = permissions;
return _this;
}
(0, _createClass3.default)(DomainAuth, [{
key: 'estimate',
value: function () {
var _ref7 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(inputValues) {
var inputValuesWithDefaults, proofs;
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (this.client.networkClient && this.client.networkClient.getParentSkillId && this.client.hasColonyRole) {
_context2.next = 2;
break;
}
throw new Error('Client not compatible with DomainAuth sender');
case 2:
// combine with default values
inputValuesWithDefaults = (0, _extends4.default)({}, this.defaultValues || {}, inputValues);
// get proof input values
_context2.next = 5;
return this.getPermissionProofs(inputValuesWithDefaults);
case 5:
proofs = _context2.sent;
return _context2.abrupt('return', (0, _get3.default)(DomainAuth.prototype.__proto__ || Object.getPrototypeOf(DomainAuth.prototype), 'estimate', this).call(this, (0, _extends4.default)({}, inputValuesWithDefaults, proofs)));
case 7:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
function estimate(_x3) {
return _ref7.apply(this, arguments);
}
return estimate;
}()
}, {
key: 'send',
value: function () {
var _ref8 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(inputValues, options) {
var inputValuesWithDefaults, proofs;
return _regenerator2.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
if (this.client.networkClient && this.client.networkClient.getParentSkillId && this.client.hasColonyRole) {
_context3.next = 2;
break;
}
throw new Error('Client not compatible with DomainAuth sender');
case 2:
// combine with default values
inputValuesWithDefaults = (0, _extends4.default)({}, this.defaultValues || {}, inputValues);
// get proof input values
_context3.next = 5;
return this.getPermissionProofs(inputValuesWithDefaults);
case 5:
proofs = _context3.sent;
return _context3.abrupt('return', (0, _get3.default)(DomainAuth.prototype.__proto__ || Object.getPrototypeOf(DomainAuth.prototype), 'send', this).call(this, (0, _extends4.default)({}, inputValuesWithDefaults, proofs), options));
case 7:
case 'end':
return _context3.stop();
}
}
}, _callee3, this);
}));
function send(_x4, _x5) {
return _ref8.apply(this, arguments);
}
return send;
}()
/**
* For each required permission, get the permissionDomainId and
* childSkillIndex as proof of permission, under the specified input value
* keys. Return an object of input values.
*/
}, {
key: 'getPermissionProofs',
value: function () {
var _ref9 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee9(inputValues) {
var _this2 = this;
var proofs;
return _regenerator2.default.wrap(function _callee9$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
_context9.next = 2;
return Promise.all(this._permissions.map(function () {
var _ref11 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee8(_ref10) {
var roles = _ref10.roles,
inputDomainIds = _ref10.domainIds,
permissionDomainIdName = _ref10.permissionDomainIdName,
childSkillIndexNames = _ref10.childSkillIndexNames,
inputAddress = _ref10.address;
var _roles, role, otherRoles, domainIds, address, permissionDomains, highestDomain, allHasRoles, childSkillIndexes;
return _regenerator2.default.wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
// Just check the first role, initially
_roles = (0, _toArray3.default)(roles), role = _roles[0], otherRoles = _roles.slice(1);
// resolve the functions or fetch from inputValues
if (!(typeof inputDomainIds === 'function')) {
_context8.next = 7;
break;
}
_context8.next = 4;
return inputDomainIds(inputValues);
case 4:
_context8.t0 = _context8.sent;
_context8.next = 10;
break;
case 7:
_context8.next = 9;
return Promise.all(inputDomainIds.map(function () {
var _ref12 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4(inputDomainId) {
return _regenerator2.default.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
return _context4.abrupt('return', typeof inputDomainId === 'function' ? inputDomainId(inputValues) : inputValues[inputDomainId]);
case 1:
case 'end':
return _context4.stop();
}
}
}, _callee4, _this2);
}));
return function (_x8) {
return _ref12.apply(this, arguments);
};
}()));
case 9:
_context8.t0 = _context8.sent;
case 10:
domainIds = _context8.t0;
if (!(domainIds.length !== childSkillIndexNames.length)) {
_context8.next = 13;
break;
}
throw new Error('Number of domainIds must match number of childSkillIndexes');
case 13:
if (!(typeof inputAddress === 'function')) {
_context8.next = 19;
break;
}
_context8.next = 16;
return inputAddress();
case 16:
_context8.t2 = _context8.sent;
_context8.next = 20;
break;
case 19:
_context8.t2 = inputAddress;
case 20:
_context8.t1 = _context8.t2;
if (_context8.t1) {
_context8.next = 25;
break;
}
_context8.next = 24;
return _this2.client.adapter.wallet.getAddress();
case 24:
_context8.t1 = _context8.sent;
case 25:
address = _context8.t1;
_context8.next = 28;
return Promise.all(domainIds.map(function () {
var _ref13 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5(domainId) {
return _regenerator2.default.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
return _context5.abrupt('return', _this2.hasPermission(domainId, role, address));
case 1:
case 'end':
return _context5.stop();
}
}
}, _callee5, _this2);
}));
return function (_x9) {
return _ref13.apply(this, arguments);
};
}()));
case 28:
permissionDomains = _context8.sent;
_context8.next = 31;
return _this2.getHighestDomain(permissionDomains, role);
case 31:
highestDomain = _context8.sent;
_context8.next = 34;
return Promise.all(otherRoles.map(function () {
var _ref14 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee6(r) {
var _ref15, hasRole;
return _regenerator2.default.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
_context6.next = 2;
return _this2.client.hasColonyRole.call({
address: address,
domainId: highestDomain,
role: r
});
case 2:
_ref15 = _context6.sent;
hasRole = _ref15.hasRole;
return _context6.abrupt('return', hasRole);
case 5:
case 'end':
return _context6.stop();
}
}
}, _callee6, _this2);
}));
return function (_x10) {
return _ref14.apply(this, arguments);
};
}()));
case 34:
allHasRoles = _context8.sent;
if (allHasRoles.every(Boolean)) {
_context8.next = 37;
break;
}
throw new Error('We are missing one or more of the permissions ' + JSON.stringify(otherRoles) + ' in domain ' + highestDomain);
case 37:
_context8.next = 39;
return Promise.all(domainIds.map(function () {
var _ref16 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee7(domainId) {
return _regenerator2.default.wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
return _context7.abrupt('return', _this2.getChildSkillIndex(highestDomain, domainId));
case 1:
case 'end':
return _context7.stop();
}
}
}, _callee7, _this2);
}));
return function (_x11) {
return _ref16.apply(this, arguments);
};
}()));
case 39:
childSkillIndexes = _context8.sent;
return _context8.abrupt('return', childSkillIndexes.reduce(function (acc, childSkillIndex, i) {
return (0, _extends4.default)({}, acc, (0, _defineProperty3.default)({}, childSkillIndexNames[i], childSkillIndex));
}, (0, _defineProperty3.default)({}, permissionDomainIdName, highestDomain)));
case 41:
case 'end':
return _context8.stop();
}
}
}, _callee8, _this2);
}));
return function (_x7) {
return _ref11.apply(this, arguments);
};
}()));
case 2:
proofs = _context9.sent;
return _context9.abrupt('return', proofs.reduce(function (acc, proof) {
return (0, _extends4.default)({}, acc, proof);
}));
case 4:
case 'end':
return _context9.stop();
}
}
}, _callee9, this);
}));
function getPermissionProofs(_x6) {
return _ref9.apply(this, arguments);
}
return getPermissionProofs;
}()
}, {
key: 'getHighestDomain',
value: function () {
var _ref17 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee11(domainIds, role) {
var _this3 = this;
var skills, filteredSkills;
return _regenerator2.default.wrap(function _callee11$(_context11) {
while (1) {
switch (_context11.prev = _context11.next) {
case 0:
_context11.next = 2;
return Promise.all(domainIds.map(function () {
var _ref18 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee10(domainId) {
var _ref19, skillId, skill;
return _regenerator2.default.wrap(function _callee10$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
if (!(domainId < 0)) {
_context10.next = 2;
break;
}
return _context10.abrupt('return', [domainId, null]);
case 2:
_context10.next = 4;
return _this3.client.getDomain.call({
domainId: domainId
});
case 4:
_ref19 = _context10.sent;
skillId = _ref19.skillId;
_context10.next = 8;
return _this3.client.networkClient.getSkill.call({
skillId: skillId
});
case 8:
skill = _context10.sent;
return _context10.abrupt('return', [domainId, skill]);
case 10:
case 'end':
return _context10.stop();
}
}
}, _callee10, _this3);
}));
return function (_x14) {
return _ref18.apply(this, arguments);
};
}()));
case 2:
skills = _context11.sent;
filteredSkills = skills.filter(function (_ref20) {
var _ref21 = (0, _slicedToArray3.default)(_ref20, 2),
skill = _ref21[1];
return skill !== null;
}).sort(
// $FlowFixMe these will all be skills because of the filter
function (_ref22, _ref23) {
var _ref25 = (0, _slicedToArray3.default)(_ref22, 2),
a = _ref25[1];
var _ref24 = (0, _slicedToArray3.default)(_ref23, 2),
b = _ref24[1];
return b.children.length - a.children.length;
});
if (filteredSkills.length) {
_context11.next = 6;
break;
}
throw new Error('No permission for role ' + role + ' in appropriate domain(s) for method ' + this.name);
case 6:
return _context11.abrupt('return', filteredSkills[0][0]);
case 7:
case 'end':
return _context11.stop();
}
}
}, _callee11, this);
}));
function getHighestDomain(_x12, _x13) {
return _ref17.apply(this, arguments);
}
return getHighestDomain;
}()
}, {
key: 'getChildSkillIndex',
value: function () {
var _ref26 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee12(permissionDomainId, childDomainId) {
var _ref27, permissionDomainSkillId, _ref28, childDomainSkillId, _ref29, permissionDomainSkillChildren, childSkillIndex;
return _regenerator2.default.wrap(function _callee12$(_context12) {
while (1) {
switch (_context12.prev = _context12.next) {
case 0:
if (!(permissionDomainId < 0)) {
_context12.next = 4;
break;
}
throw new Error('Current wallet address does not have permission');
case 4:
if (!(permissionDomainId === childDomainId)) {
_context12.next = 6;
break;
}
return _context12.abrupt('return', 0);
case 6:
_context12.next = 8;
return this.client.getDomain.call({
domainId: permissionDomainId
});
case 8:
_ref27 = _context12.sent;
permissionDomainSkillId = _ref27.skillId;
_context12.next = 12;
return this.client.getDomain.call({
domainId: childDomainId
});
case 12:
_ref28 = _context12.sent;
childDomainSkillId = _ref28.skillId;
_context12.next = 16;
return this.client.networkClient.getSkill.call({
skillId: permissionDomainSkillId
});
case 16:
_ref29 = _context12.sent;
permissionDomainSkillChildren = _ref29.children;
// find the index of the child domain skill in the permission domain skill children
childSkillIndex = permissionDomainSkillChildren.findIndex(function (childSkillId) {
return childSkillId === childDomainSkillId;
});
// this should never happen, but if we can't find the index then throw
if (!(childSkillIndex < 0)) {
_context12.next = 21;
break;
}
throw new Error('Unable to find child domain skill in permission domain skill children');
case 21:
return _context12.abrupt('return', childSkillIndex);
case 22:
case 'end':
return _context12.stop();
}
}
}, _callee12, this);
}));
function getChildSkillIndex(_x15, _x16) {
return _ref26.apply(this, arguments);
}
return getChildSkillIndex;
}()
/**
* For colonies which are limited to one level of domain, determine whether
* the current wallet address has permission for a role in a given domain.
* Returns the domainId in which we have permission.
*/
}, {
key: 'hasPermission',
value: function () {
var _ref30 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee13(domainId, role, address) {
var _ref31, hasDomainPermission, _ref32, hasRootPermission;
return _regenerator2.default.wrap(function _callee13$(_context13) {
while (1) {
switch (_context13.prev = _context13.next) {
case 0:
_context13.next = 2;
return this.client.hasColonyRole.call({
address: address,
domainId: domainId,
role: role
});
case 2:
_ref31 = _context13.sent;
hasDomainPermission = _ref31.hasRole;
if (!(hasDomainPermission && role !== _constants.COLONY_ROLE_ARCHITECTURE)) {
_context13.next = 6;
break;
}
return _context13.abrupt('return', domainId);
case 6:
_context13.next = 8;
return this.client.hasColonyRole.call({
address: address,
domainId: 1,
role: role
});
case 8:
_ref32 = _context13.sent;
hasRootPermission = _ref32.hasRole;
return _context13.abrupt('return', hasRootPermission ? 1 : -1);
case 11:
case 'end':
return _context13.stop();
}
}
}, _callee13, this);
}));
function hasPermission(_x17, _x18, _x19) {
return _ref30.apply(this, arguments);
}
return hasPermission;
}()
}]);
return DomainAuth;
}(_colonyJsContractClient2.default.Sender);
exports.default = DomainAuth;
//# sourceMappingURL=DomainAuth.js.map