@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
134 lines • 5.7 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setup = exports.getAccountBridgeByFamily = exports.getAccountBridge = exports.getCurrencyBridge = void 0;
const index_1 = require("@ledgerhq/coin-framework/sanction/index");
const errors_1 = require("@ledgerhq/errors");
const account_1 = require("../account");
const live_env_1 = require("@ledgerhq/live-env");
const index_2 = require("../account/index");
const js_1 = __importDefault(require("../generated/bridge/js"));
const mock_1 = __importDefault(require("../generated/bridge/mock"));
const accountBridge_1 = require("./generic-alpaca/accountBridge");
const currencyBridge_1 = require("./generic-alpaca/currencyBridge");
const errors_2 = require("@ledgerhq/coin-framework/sanction/errors");
const index_3 = require("@ledgerhq/coin-framework/crypto-assets/index");
const crypto_assets_1 = require("./crypto-assets");
const alpacaized = {
xrp: true,
stellar: true,
tezos: true,
};
// let accountBridgeInstance: AccountBridge<any> | null = null;
const bridgeCache = {};
const currencyBridgeCache = {};
const getCurrencyBridge = (currency) => {
if ((0, live_env_1.getEnv)("MOCK")) {
const mockBridge = mock_1.default[currency.family];
if (mockBridge)
return mockBridge.currencyBridge;
throw new errors_1.CurrencyNotSupported("no mock implementation available for currency " + currency.id, {
currencyName: currency.id,
});
}
if (alpacaized[currency.family]) {
if (!currencyBridgeCache[currency.family]) {
currencyBridgeCache[currency.family] = (0, currencyBridge_1.getAlpacaCurrencyBridge)(currency.family, "local");
}
return currencyBridgeCache[currency.family];
}
const jsBridge = js_1.default[currency.family];
if (jsBridge) {
return jsBridge.currencyBridge;
}
throw new errors_1.CurrencyNotSupported("no implementation available for currency " + currency.id, {
currencyName: currency.id,
});
};
exports.getCurrencyBridge = getCurrencyBridge;
const getAccountBridge = (account, parentAccount) => {
const mainAccount = (0, account_1.getMainAccount)(account, parentAccount);
const { currency } = mainAccount;
const supportedError = (0, index_2.checkAccountSupported)(mainAccount);
if (supportedError) {
throw supportedError;
}
try {
return getAccountBridgeByFamily(currency.family, mainAccount.id);
}
catch {
throw new errors_1.CurrencyNotSupported("currency not supported " + currency.id, {
currencyName: currency.id,
});
}
};
exports.getAccountBridge = getAccountBridge;
function getAccountBridgeByFamily(family, accountId) {
if (accountId) {
const { type } = (0, account_1.decodeAccountId)(accountId);
if (type === "mock") {
const mockBridge = mock_1.default[family];
if (mockBridge)
return wrapAccountBridge(mockBridge.accountBridge);
}
}
if (alpacaized[family]) {
if (!bridgeCache[family]) {
bridgeCache[family] = (0, accountBridge_1.getAlpacaAccountBridge)(family, "local");
}
return bridgeCache[family];
}
const jsBridge = js_1.default[family];
if (!jsBridge) {
throw new errors_1.CurrencyNotSupported("account bridge not found " + family);
}
return wrapAccountBridge(jsBridge.accountBridge);
}
exports.getAccountBridgeByFamily = getAccountBridgeByFamily;
function setup(store) {
(0, crypto_assets_1.setCryptoAssetsStore)(store);
(0, index_3.setCryptoAssetsStore)((0, crypto_assets_1.getCryptoAssetsStore)());
}
exports.setup = setup;
function wrapAccountBridge(bridge) {
return {
...bridge,
getTransactionStatus: async (...args) => {
const blockchainTransactionStatus = await bridge.getTransactionStatus(...args);
const account = args[0];
if (!(0, index_1.isCheckSanctionedAddressEnabled)(account.currency)) {
return blockchainTransactionStatus;
}
const commonTransactionStatus = await commonGetTransactionStatus(...args);
return mergeResults(blockchainTransactionStatus, commonTransactionStatus);
},
};
}
function mergeResults(blockchainTransactionStatus, commonTransactionStatus) {
const errors = { ...blockchainTransactionStatus.errors, ...commonTransactionStatus.errors };
const warnings = { ...blockchainTransactionStatus.warnings, ...commonTransactionStatus.warnings };
return { ...blockchainTransactionStatus, errors, warnings };
}
async function commonGetTransactionStatus(account, transaction) {
const errors = {};
const warnings = {};
let isRecipientSanctioned = false;
if (transaction.recipient && transaction.recipient !== "") {
isRecipientSanctioned = await (0, index_1.isAddressSanctioned)(account.currency, transaction.recipient);
if (isRecipientSanctioned) {
errors.recipient = new errors_2.AddressesSanctionedError("AddressesSanctionedError", {
addresses: [transaction.recipient],
});
}
}
const isSenderSanctioned = await (0, index_1.isAddressSanctioned)(account.currency, account.freshAddress);
if (isSenderSanctioned) {
errors.sender = new errors_2.AddressesSanctionedError("AddressesSanctionedError", {
addresses: [account.freshAddress],
});
}
return { errors, warnings };
}
//# sourceMappingURL=impl.js.map