UNPKG

@test-org122/merchant-iframe

Version:

Mediator iFrame package. This is used to sandbox the connector code away from the running hypernet core.

63 lines 3.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MerchantService = void 0; const neverthrow_1 = require("neverthrow"); const utils_1 = require("@test-org122/utils"); const ethers_1 = require("ethers"); const errors_1 = require("@merchant-iframe/interfaces/objects/errors"); class MerchantService { constructor(merchantConnectorRepository, contextProvider) { this.merchantConnectorRepository = merchantConnectorRepository; this.contextProvider = contextProvider; } activateMerchantConnector() { const context = this.contextProvider.getMerchantContext(); console.log("activateMerchantConnector"); // If we don't have validated code, that's a problem. if (context.validatedMerchantCode == null) { return neverthrow_1.errAsync(new errors_1.MerchantValidationError("Cannot activate merchant connector, no validated code available!")); } // Now we eval the connector code, which is supposed to return an IMerchantConnector //const merchantConnector = eval(context.validatedMerchantCode) as IMerchantConnector; var newScript = document.createElement("script"); var inlineScript = document.createTextNode(context.validatedMerchantCode); newScript.appendChild(inlineScript); document.head.appendChild(newScript); const merchantConnector = window.connector; console.log(merchantConnector); if (merchantConnector == null) { return neverthrow_1.errAsync(new errors_1.MerchantConnectorError("Validated code does not evaluate to an object")); } context.merchantConnector = merchantConnector; this.contextProvider.setMerchantContext(context); context.onMerchantConnectorActivated.next(merchantConnector); return neverthrow_1.okAsync(merchantConnector); } validateMerchantConnector() { // This is going to connect to the merchantUrl/connector and pull down the connector code. // That code is expected to be signed, with the public key available at merchantUrl/publicKey // The code will be cached in local storage but the signing key will be const context = this.contextProvider.getMerchantContext(); return utils_1.ResultUtils.combine([ this.merchantConnectorRepository.getMerchantCode(context.merchantUrl), this.merchantConnectorRepository.getMerchantSignature(context.merchantUrl), this.merchantConnectorRepository.getMerchantAddress(context.merchantUrl), ]).andThen((vals) => { const [merchantCode, signature, address] = vals; const calculatedAddress = ethers_1.ethers.utils.verifyMessage(merchantCode, signature); if (calculatedAddress !== address) { return neverthrow_1.errAsync(new errors_1.MerchantValidationError("Merchant code does not match signature!")); } // Merchant's code passes muster. Store the merchant code in the context as validated. console.log("Connector validated!"); const context = this.contextProvider.getMerchantContext(); context.validatedMerchantCode = merchantCode; context.validatedMerchantSignature = signature; this.contextProvider.setMerchantContext(context); // Return the valid signature return neverthrow_1.okAsync(signature); }); } } exports.MerchantService = MerchantService; //# sourceMappingURL=MerchantService.js.map