UNPKG

stellar-plus

Version:

beta version of stellar-plus, an all-in-one sdk for the Stellar blockchain

212 lines (211 loc) 9.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClassicSignRequirementsPipeline = void 0; const tslib_1 = require("tslib"); const stellar_sdk_1 = require("@stellar/stellar-sdk"); const types_1 = require("../../../../stellar-plus/core/pipelines/classic-sign-requirements/types"); const types_2 = require("../../../../stellar-plus/core/types"); const conveyor_belt_1 = require("../../../../stellar-plus/error/helpers/conveyor-belt"); const conveyor_belts_1 = require("../../../../stellar-plus/utils/pipeline/conveyor-belts"); const errors_1 = require("./errors"); class ClassicSignRequirementsPipeline extends conveyor_belts_1.ConveyorBelt { constructor(plugins) { super({ type: types_1.ClassicSignRequirementsPipelineType.id, plugins: plugins || [], }); } process(item, _itemId) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const transaction = item; try { const operations = transaction instanceof stellar_sdk_1.FeeBumpTransaction ? [] : transaction.operations; const sourceRequirement = this.getSignatureThresholdForSource(transaction); const operationRequirements = transaction instanceof stellar_sdk_1.FeeBumpTransaction ? [] : this.getOperationsSignatureRequirements(operations); const bundledRequirements = this.bundleSignatureRequirements(operationRequirements, sourceRequirement); return bundledRequirements; } catch (e) { throw errors_1.CSRError.processFailed(e, (0, conveyor_belt_1.extractConveyorBeltErrorMeta)(item, this.getMeta(_itemId)), transaction); } }); } getOperationsSignatureRequirements(operations) { const requirements = []; for (const op of operations) { const signerRequirements = this.getRequiredSignatureThresholdForClassicOperation(op); if (signerRequirements) requirements.push(signerRequirements); } return requirements; } getSignatureThresholdForSource(transaction) { if (transaction.innerTransaction) { const fbTx = transaction; return { publicKey: fbTx.feeSource, thresholdLevel: types_2.SignatureThreshold.low, }; } const tx = transaction; return { publicKey: tx.source, thresholdLevel: types_2.SignatureThreshold.medium, }; } getRequiredSignatureThresholdForClassicOperation(operation) { const setSourceSigner = (source) => { return source ? source : 'source'; }; let thresholdLevel = types_2.SignatureThreshold.medium; switch (operation.type) { case stellar_sdk_1.xdr.OperationType.createAccount().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; case stellar_sdk_1.xdr.OperationType.payment().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; case stellar_sdk_1.xdr.OperationType.pathPaymentStrictSend().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; case stellar_sdk_1.xdr.OperationType.pathPaymentStrictReceive().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; case stellar_sdk_1.xdr.OperationType.manageSellOffer().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; case stellar_sdk_1.xdr.OperationType.manageBuyOffer().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; case stellar_sdk_1.xdr.OperationType.createPassiveSellOffer().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; case stellar_sdk_1.xdr.OperationType.setOptions().name: if (operation.masterWeight || operation.signer || operation.lowThreshold || operation.medThreshold || operation.highThreshold) { thresholdLevel = types_2.SignatureThreshold.high; } return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; case stellar_sdk_1.xdr.OperationType.changeTrust().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; case stellar_sdk_1.xdr.OperationType.allowTrust().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel: types_2.SignatureThreshold.low, }; case stellar_sdk_1.xdr.OperationType.accountMerge().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel: types_2.SignatureThreshold.high, }; case stellar_sdk_1.xdr.OperationType.manageData().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; case stellar_sdk_1.xdr.OperationType.bumpSequence().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel: types_2.SignatureThreshold.low, }; case stellar_sdk_1.xdr.OperationType.createClaimableBalance().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; case stellar_sdk_1.xdr.OperationType.claimClaimableBalance().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; case stellar_sdk_1.xdr.OperationType.beginSponsoringFutureReserves().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; case stellar_sdk_1.xdr.OperationType.endSponsoringFutureReserves().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; case stellar_sdk_1.xdr.OperationType.revokeSponsorship().name: case 'revokeAccountSponsorship': case 'revokeTrustlineSponsorship': case 'revokeOfferSponsorship': case 'revokeDataSponsorship': case 'revokeClaimableBalanceSponsorship': case 'revokeLiquidityPoolSponsorship': case 'revokeSignerSponsorship': return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; case stellar_sdk_1.xdr.OperationType.clawback().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; case stellar_sdk_1.xdr.OperationType.clawbackClaimableBalance().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; case stellar_sdk_1.xdr.OperationType.setTrustLineFlags().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel: types_2.SignatureThreshold.low, }; case stellar_sdk_1.xdr.OperationType.liquidityPoolDeposit().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; case stellar_sdk_1.xdr.OperationType.liquidityPoolWithdraw().name: return { publicKey: setSourceSigner(operation.source), thresholdLevel, }; } } bundleSignatureRequirements(operationRequirements, sourceRequirement) { const bundledRequirements = [sourceRequirement]; for (const requirement of operationRequirements) { const publicKey = requirement.publicKey === 'source' ? sourceRequirement.publicKey : requirement.publicKey; const index = bundledRequirements.findIndex((r) => r.publicKey === publicKey); if (index === -1) { bundledRequirements.push({ publicKey, thresholdLevel: requirement.thresholdLevel, }); } else { bundledRequirements[index].thresholdLevel = Math.max(bundledRequirements[index].thresholdLevel, requirement.thresholdLevel); } } return bundledRequirements; } } exports.ClassicSignRequirementsPipeline = ClassicSignRequirementsPipeline;