UNPKG

xdb-digitalbits-base

Version:
99 lines (82 loc) 3.84 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; exports.setTrustLineFlags = setTrustLineFlags; var _digitalbitsXdr_generated = require('../generated/digitalbits-xdr_generated'); var _digitalbitsXdr_generated2 = _interopRequireDefault(_digitalbitsXdr_generated); var _keypair = require('../keypair'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Creates a trustline flag configuring operation. * * For the flags, set them to true to enable them and false to disable them. Any * unmodified operations will be marked `undefined` in the result. * * Note that you can only **clear** the clawbackEnabled flag set; it must be set * account-wide via operations.SetOptions (setting * xdr.AccountFlags.clawbackEnabled). * * @function * @alias Operation.setTrustLineFlags * * @param {object} opts - Options object * @param {string} opts.trustor - the account whose trustline this is * @param {Asset} opts.asset - the asset on the trustline * @param {object} opts.flags - the set of flags to modify * * @param {bool} [opts.flags.authorized] - authorize account to perform * transactions with its credit * @param {bool} [opts.flags.authorizedToMaintainLiabilities] - authorize * account to maintain and reduce liabilities for its credit * @param {bool} [opts.flags.clawbackEnabled] - stop claimable balances on * this trustlines from having clawbacks enabled (this flag can only be set * to false!) * @param {string} [opts.source] - The source account for the operation. * Defaults to the transaction's source account. * * @note You must include at least one flag. * * @return {xdr.SetTrustLineFlagsOp} * * @link xdr.AccountFlags * @link xdr.TrustLineFlags * @see https://developers.digitalbits.io/guides/concepts/list-of-operations.html#set-options */ function setTrustLineFlags() { var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var attributes = {}; if (_typeof(opts.flags) !== 'object' || Object.keys(opts.flags).length === 0) { throw new Error('opts.flags must be an map of boolean flags to modify'); } var mapping = { authorized: _digitalbitsXdr_generated2.default.TrustLineFlags.authorizedFlag(), authorizedToMaintainLiabilities: _digitalbitsXdr_generated2.default.TrustLineFlags.authorizedToMaintainLiabilitiesFlag(), clawbackEnabled: _digitalbitsXdr_generated2.default.TrustLineFlags.trustlineClawbackEnabledFlag() }; /* eslint no-bitwise: "off" */ var clearFlag = 0; var setFlag = 0; Object.keys(opts.flags).forEach(function (flagName) { if (!Object.prototype.hasOwnProperty.call(mapping, flagName)) { throw new Error('unsupported flag name specified: ' + flagName); } var flagValue = opts.flags[flagName]; var bit = mapping[flagName].value; if (flagValue === true) { setFlag |= bit; } else if (flagValue === false) { clearFlag |= bit; } }); attributes.trustor = _keypair.Keypair.fromPublicKey(opts.trustor).xdrAccountId(); attributes.asset = opts.asset.toXDRObject(); attributes.clearFlags = clearFlag; attributes.setFlags = setFlag; var opAttributes = { body: _digitalbitsXdr_generated2.default.OperationBody.setTrustLineFlags(new _digitalbitsXdr_generated2.default.SetTrustLineFlagsOp(attributes)) }; this.setSourceAccount(opAttributes, opts); return new _digitalbitsXdr_generated2.default.Operation(opAttributes); }