UNPKG

airgap-coin-lib

Version:

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

50 lines 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Network_1 = require("./Network"); var protocolOptionsByIdentifier_1 = require("./protocolOptionsByIdentifier"); var subProtocolMapper = {}; var getProtocolAndNetworkIdentifier = function (identifier, network) { return identifier + ":" + network.identifier; }; var getSubProtocolsByIdentifier = function (identifier, network) { if (!identifier || typeof identifier !== 'string') { throw new Error('No protocol identifier provided'); } var targetNetwork = network ? network : protocolOptionsByIdentifier_1.getProtocolOptionsByIdentifier(identifier).network; var protocolAndNetworkIdentifier = getProtocolAndNetworkIdentifier(identifier, targetNetwork); if (subProtocolMapper[protocolAndNetworkIdentifier]) { return Object.keys(subProtocolMapper[protocolAndNetworkIdentifier]).map(function (key) { return subProtocolMapper[protocolAndNetworkIdentifier][key]; }); } return []; }; exports.getSubProtocolsByIdentifier = getSubProtocolsByIdentifier; var addSubProtocol = function (protocol, subProtocol) { if (!subProtocol.identifier.startsWith(protocol.identifier)) { throw new Error("subprotocol " + subProtocol.name + " is not supported for protocol " + protocol.identifier); } if (!Network_1.isNetworkEqual(protocol.options.network, subProtocol.options.network)) { throw new Error("subprotocol " + subProtocol.name + " needs to have the same network as main protocol"); } var protocolAndNetworkIdentifier = getProtocolAndNetworkIdentifier(protocol.identifier, protocol.options.network); // make sure we can add subprotocols for this identifier if (!subProtocolMapper[protocolAndNetworkIdentifier]) { subProtocolMapper[protocolAndNetworkIdentifier] = {}; } subProtocolMapper[protocolAndNetworkIdentifier][subProtocol.identifier] = subProtocol; }; exports.addSubProtocol = addSubProtocol; var removeSubProtocol = function (protocol, subProtocol) { if (!subProtocol.identifier.startsWith(protocol.identifier)) { throw new Error("subprotocol " + subProtocol.name + " is not supported for protocol " + protocol.identifier); } if (!Network_1.isNetworkEqual(protocol.options.network, subProtocol.options.network)) { throw new Error("subprotocol " + subProtocol.name + " needs to have the same network as main protocol"); } var protocolAndNetworkIdentifier = getProtocolAndNetworkIdentifier(protocol.identifier, protocol.options.network); if (!subProtocolMapper[protocolAndNetworkIdentifier]) { return; } delete subProtocolMapper[protocolAndNetworkIdentifier][subProtocol.identifier]; }; // TODO: Add tests exports.removeSubProtocol = removeSubProtocol; //# sourceMappingURL=subProtocols.js.map