UNPKG

@ledgerhq/coin-stellar

Version:
60 lines 2.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isAddressValid = exports.isAccountMultiSign = exports.getAssetCodeIssuer = exports.getAmountValue = exports.getAssetIdFromAsset = exports.STELLAR_BURN_ADDRESS = void 0; const bignumber_js_1 = require("bignumber.js"); const stellar_sdk_1 = require("@stellar/stellar-sdk"); const helpers_1 = require("@ledgerhq/coin-framework/account/helpers"); const network_1 = require("../network"); exports.STELLAR_BURN_ADDRESS = "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF"; const getAssetIdFromAsset = (asset) => `${asset.asset_code}:${asset.asset_issuer}`; exports.getAssetIdFromAsset = getAssetIdFromAsset; function getAmountValue(account, transaction, fees) { // Asset if (transaction.subAccountId) { const asset = (0, helpers_1.findSubAccountById)(account, transaction.subAccountId); if (!asset) { throw new Error(`Sub-account with ID ${transaction.subAccountId} not found`); } return transaction.useAllAmount ? new bignumber_js_1.BigNumber(asset.spendableBalance.toString()) : transaction.amount; } // Native return transaction.useAllAmount && transaction.networkInfo ? bignumber_js_1.BigNumber.max(new bignumber_js_1.BigNumber(account.spendableBalance.toString()).minus(fees), 0) : transaction.amount; } exports.getAmountValue = getAmountValue; function getAssetCodeIssuer(tr) { if (tr.subAccountId) { const assetString = tr.subAccountId.split("+")[1]; return assetString.split(":"); } return [tr.assetReference || "", tr.assetOwner || ""]; } exports.getAssetCodeIssuer = getAssetCodeIssuer; async function isAccountMultiSign(account) { const signers = await (0, network_1.fetchSigners)(account); return signers.length > 1; } exports.isAccountMultiSign = isAccountMultiSign; /** * Returns true if address is valid, false if it's invalid (can't parse or wrong checksum) * * @param {*} address */ function isAddressValid(address) { if (!address) return false; // FIXME Workaround for burn address, see https://ledgerhq.atlassian.net/browse/LIVE-4014 if (address === exports.STELLAR_BURN_ADDRESS) return false; try { return stellar_sdk_1.StrKey.isValidEd25519PublicKey(address) || stellar_sdk_1.StrKey.isValidMed25519PublicKey(address); } catch { return false; } } exports.isAddressValid = isAddressValid; //# sourceMappingURL=utils.js.map