@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
157 lines • 7.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.translateContent = translateContent;
exports.receiveOnAccountLogic = receiveOnAccountLogic;
exports.signTransactionLogic = signTransactionLogic;
exports.broadcastTransactionLogic = broadcastTransactionLogic;
exports.completeExchangeLogic = completeExchangeLogic;
exports.signMessageLogic = signMessageLogic;
const types_live_1 = require("@ledgerhq/types-live");
const converters_1 = require("./converters");
const serializers_1 = require("./serializers");
const index_1 = require("../account/index");
const index_2 = require("../bridge/index");
const index_3 = require("../hw/signMessage/index");
function translateContent(content, locale = "en") {
if (!content || typeof content === "string")
return content;
return content[locale] || content.en;
}
function getParentAccount(account, fromAccounts) {
return (0, index_1.isTokenAccount)(account)
? fromAccounts.find(a => a.id === account.parentId)
: undefined;
}
function receiveOnAccountLogic(walletState, { manifest, accounts, tracking }, accountId, uiNavigation) {
tracking.platformReceiveRequested(manifest);
const account = accounts.find(account => account.id === accountId);
if (!account) {
tracking.platformReceiveFail(manifest);
return Promise.reject(new Error("Account required"));
}
const parentAccount = getParentAccount(account, accounts);
const accountAddress = (0, converters_1.accountToPlatformAccount)(walletState, account, parentAccount).address;
return uiNavigation(account, parentAccount, accountAddress);
}
function signTransactionLogic({ manifest, accounts, tracking }, accountId, transaction, uiNavigation) {
tracking.platformSignTransactionRequested(manifest);
if (!transaction) {
tracking.platformSignTransactionFail(manifest);
return Promise.reject(new Error("Transaction required"));
}
const platformTransaction = (0, serializers_1.deserializePlatformTransaction)(transaction);
const account = accounts.find(account => account.id === accountId);
if (!account) {
tracking.platformSignTransactionFail(manifest);
return Promise.reject(new Error("Account required"));
}
const parentAccount = getParentAccount(account, accounts);
const accountFamily = (0, index_1.isTokenAccount)(account)
? parentAccount?.currency.family
: account.currency.family;
const { canEditFees, liveTx, hasFeesProvided } = (0, converters_1.getPlatformTransactionSignFlowInfos)(platformTransaction);
if (accountFamily !== liveTx.family) {
return Promise.reject(new Error(`Account and transaction must be from the same family. Account family: ${accountFamily}, Transaction family: ${liveTx.family}`));
}
return uiNavigation(account, parentAccount, {
canEditFees,
liveTx,
hasFeesProvided,
});
}
function broadcastTransactionLogic({ manifest, accounts, tracking }, accountId, signedTransaction, uiNavigation) {
if (!signedTransaction) {
tracking.platformBroadcastFail(manifest);
return Promise.reject(new Error("Transaction required"));
}
const account = accounts.find(account => account.id === accountId);
if (!account) {
tracking.platformBroadcastFail(manifest);
return Promise.reject(new Error("Account required"));
}
const parentAccount = getParentAccount(account, accounts);
const signedOperation = (0, serializers_1.deserializePlatformSignedTransaction)(signedTransaction, accountId);
return uiNavigation(account, parentAccount, signedOperation);
}
function completeExchangeLogic({ manifest, accounts, tracking }, { provider, fromAccountId, toAccountId, transaction, binaryPayload, signature, feesStrategy, exchangeType, }, uiNavigation) {
tracking.platformCompleteExchangeRequested(manifest);
// Nb get a hold of the actual accounts, and parent accounts
const fromAccount = accounts.find(a => a.id === fromAccountId);
const toAccount = accounts.find(a => a.id === toAccountId);
if (!fromAccount) {
return Promise.reject();
}
if (exchangeType === 0x00 && !toAccount) {
// if we do a swap, a destination account must be provided
return Promise.reject();
}
const fromParentAccount = getParentAccount(fromAccount, accounts);
const toParentAccount = toAccount ? getParentAccount(toAccount, accounts) : undefined;
const exchange = {
fromAccount,
fromParentAccount,
toAccount,
toParentAccount,
fromCurrency: (0, types_live_1.getCurrencyForAccount)(fromAccount),
toCurrency: toAccount ? (0, types_live_1.getCurrencyForAccount)(toAccount) : undefined,
};
const accountBridge = (0, index_2.getAccountBridge)(fromAccount, fromParentAccount);
const mainFromAccount = (0, index_1.getMainAccount)(fromAccount, fromParentAccount);
const mainFromAccountFamily = mainFromAccount.currency.family;
const platformTransaction = (0, serializers_1.deserializePlatformTransaction)(transaction);
const { liveTx: liveTransaction } = (0, converters_1.getPlatformTransactionSignFlowInfos)(platformTransaction);
if (liveTransaction.family !== mainFromAccountFamily) {
return Promise.reject(new Error(`Account and transaction must be from the same family. Account family: ${mainFromAccountFamily}, Transaction family: ${liveTransaction.family}`));
}
/**
* 'subAccountId' is used for ETH and it's ERC-20 tokens.
* This field is ignored for BTC
*/
const subAccountId = fromParentAccount ? fromAccount.id : undefined;
const bridgeTx = accountBridge.createTransaction(mainFromAccount);
/**
* We append the `recipient` to the tx created from `createTransaction`
* to avoid having userGasLimit reset to null for ETH txs
* cf. libs/ledger-live-common/src/families/ethereum/updateTransaction.ts
*/
const tx = accountBridge.updateTransaction({
...bridgeTx,
recipient: liveTransaction.recipient,
}, {
...liveTransaction,
feesStrategy,
subAccountId,
});
return uiNavigation({
provider,
exchange,
transaction: tx,
binaryPayload,
signature,
feesStrategy,
exchangeType,
});
}
function signMessageLogic({ manifest, accounts, tracking }, accountId, message, uiNavigation) {
tracking.platformSignMessageRequested(manifest);
const account = accounts.find(account => account.id === accountId);
if (account === undefined) {
tracking.platformSignMessageFail(manifest);
return Promise.reject(new Error(`account with id "${accountId}" not found`));
}
let formattedMessage;
try {
if ((0, index_1.isAccount)(account)) {
formattedMessage = (0, index_3.prepareMessageToSign)(account, message);
}
else {
throw new Error("account provided should be the main one");
}
}
catch (error) {
tracking.platformSignMessageFail(manifest);
return Promise.reject(error);
}
return uiNavigation(account, formattedMessage);
}
//# sourceMappingURL=logic.js.map