UNPKG

@ledgerhq/coin-hedera

Version:
142 lines 5.6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getOperationsForAccount = exports.getAccountTransactions = exports.getAccountsForPublicKey = void 0; const sdk_1 = require("@hashgraph/sdk"); const network_1 = __importDefault(require("@ledgerhq/live-network/network")); const bignumber_js_1 = __importDefault(require("bignumber.js")); const live_env_1 = require("@ledgerhq/live-env"); const operation_1 = require("@ledgerhq/coin-framework/operation"); const network_2 = require("./network"); const utils_1 = require("../bridge/utils"); const getMirrorApiUrl = () => (0, live_env_1.getEnv)("API_HEDERA_MIRROR"); const fetch = (path) => { return (0, network_1.default)({ method: "GET", url: `${getMirrorApiUrl()}${path}`, }); }; async function getAccountsForPublicKey(publicKey) { let r; try { r = await fetch(`/api/v1/accounts?account.publicKey=${publicKey}&balance=false`); } catch (e) { if (e.name === "LedgerAPI4xx") return []; throw e; } const rawAccounts = r.data.accounts; const accounts = []; for (const raw of rawAccounts) { const accountBalance = await (0, network_2.getAccountBalance)(raw.account); accounts.push({ accountId: sdk_1.AccountId.fromString(raw.account), balance: accountBalance.balance, }); } return accounts; } exports.getAccountsForPublicKey = getAccountsForPublicKey; async function getAccountTransactions(address, since) { const transactions = []; const params = new URLSearchParams({ "account.id": address, order: "desc", limit: "100", }); if (since) { params.append("timestamp", `gt:${since}`); } let nextUrl = `/api/v1/transactions?${params.toString()}`; // WARNING: don't break the loop when `transactions` array is empty but `links.next` is present // the mirror node API enforces a 60-day max time range per query, even if `timestamp` param is set // see: https://hedera.com/blog/changes-to-the-hedera-operated-mirror-node while (nextUrl) { const res = await fetch(nextUrl); const newTransactions = res.data.transactions; transactions.push(...newTransactions); nextUrl = res.data.links.next; } return transactions; } exports.getAccountTransactions = getAccountTransactions; async function getOperationsForAccount(ledgerAccountId, address, latestOperationTimestamp) { const rawOperations = await getAccountTransactions(address, latestOperationTimestamp); const operations = []; for (const raw of rawOperations) { const { consensus_timestamp, transaction_id } = raw; const timestamp = new Date(parseInt(consensus_timestamp.split(".")[0], 10) * 1000); const senders = []; const recipients = []; const fee = new bignumber_js_1.default(raw.charged_tx_fee); let value = new bignumber_js_1.default(0); let type = "NONE"; for (let i = raw.transfers.length - 1; i >= 0; i--) { const transfer = raw.transfers[i]; const amount = new bignumber_js_1.default(transfer.amount); const account = sdk_1.AccountId.fromString(transfer.account); if (transfer.account === address) { if (amount.isNegative()) { value = amount.abs(); type = "OUT"; } else { value = amount; type = "IN"; } } if (amount.isNegative()) { senders.push(transfer.account); } else { if (account.shard.eq(0) && account.realm.eq(0)) { if (account.num.lt(100)) { // account is a node, only add to list if we have none if (recipients.length === 0) { recipients.push(transfer.account); } } else if (account.num.lt(1000)) { // account is a system account that is not a node // do NOT add } else { recipients.push(transfer.account); } } else { recipients.push(transfer.account); } } } // NOTE: earlier addresses are the "fee" addresses recipients.reverse(); senders.reverse(); const hash = (0, utils_1.base64ToUrlSafeBase64)(raw.transaction_hash); operations.push({ value, date: timestamp, // NOTE: there are no "blocks" in hedera // Set a value just so that it's considered confirmed according to isConfirmedOperation blockHeight: 5, blockHash: null, extra: { consensusTimestamp: consensus_timestamp, transactionId: transaction_id, }, fee, hash, recipients, senders, accountId: ledgerAccountId, id: (0, operation_1.encodeOperationId)(ledgerAccountId, hash, type), type, }); } return operations; } exports.getOperationsForAccount = getOperationsForAccount; //# sourceMappingURL=mirror.js.map