UNPKG

@ledgerhq/coin-hedera

Version:
122 lines 4.79 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.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 getOperationsForAccount(ledgerAccountId, address, latestOperationTimestamp) { const operations = []; let r = await fetch(`/api/v1/transactions?account.id=${address}&timestamp=gt:${latestOperationTimestamp}`); const rawOperations = r.data.transactions; while (r.data.links.next) { r = await fetch(r.data.links.next); const newOperations = r.data.transactions; rawOperations.push(...newOperations); } for (const raw of rawOperations) { const { consensus_timestamp } = 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 }, fee, hash, recipients, senders, accountId: ledgerAccountId, id: (0, operation_1.encodeOperationId)(ledgerAccountId, hash, type), type, }); } return operations; } exports.getOperationsForAccount = getOperationsForAccount; //# sourceMappingURL=mirror.js.map