UNPKG

airgap-coin-lib

Version:

The airgap-coin-lib is a protocol agnostic library to prepare, sign and broadcast cryptocurrency transactions.

34 lines 1.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var errors_1 = require("../errors"); var Network_1 = require("./Network"); var protocolOptionsByIdentifier_1 = require("./protocolOptionsByIdentifier"); var supportedProtocols_1 = require("./supportedProtocols"); var getProtocolByIdentifier = function (identifier, network) { if (!identifier || typeof identifier !== 'string') { throw new Error('No protocol identifier provided'); } var targetNetwork = network ? network : protocolOptionsByIdentifier_1.getProtocolOptionsByIdentifier(identifier).network; // create a complete list of all protocols and subprotocols var candidates = supportedProtocols_1.supportedProtocols() .map(function (protocol) { var protocols = [protocol]; if (protocol.subProtocols) { protocols.push.apply(protocols, protocol.subProtocols); } return protocols; }) .reduce(function (current, next) { return current.concat(next); }, []); // filter out potential candidates, those where our identifier startsWith the identifier of the protocol var filteredCandidates = candidates.filter(function (protocol) { return identifier.startsWith(protocol.identifier) && Network_1.isNetworkEqual(protocol.options.network, targetNetwork); }); if (filteredCandidates.length === 0) { throw new errors_1.ProtocolNotSupported(); } // sort by length filteredCandidates.sort(function (a, b) { return b.identifier.length - a.identifier.length; }); return filteredCandidates[0]; }; exports.getProtocolByIdentifier = getProtocolByIdentifier; //# sourceMappingURL=protocolsByIdentifier.js.map