@ledgerhq/coin-hedera
Version:
Ledger Hedera Coin integration
146 lines • 9.16 kB
TypeScript
import { Timestamp, Transaction as HederaSDKTransaction, TransactionId } from "@hashgraph/sdk";
import type { AssetInfo, TransactionIntent } from "@ledgerhq/coin-module-framework/api/types";
import type { ExplorerView, TokenCurrency } from "@ledgerhq/types-cryptoassets";
import type { AccountLike, Operation as LiveOperation } from "@ledgerhq/types-live";
import BigNumber from "bignumber.js";
import { type HederaCoinConfig } from "../config";
import { HEDERA_DELEGATION_STATUS, HEDERA_OPERATION_TYPES } from "../constants";
import type { EnrichedERC20Transfer, HederaAccount, HederaMemo, HederaMirrorTransaction, HederaOperationExtra, HederaTxData, HederaValidator, MergedTransaction, OperationDetailsExtraField, Transaction, TransactionStaking, TransactionStatus, TransactionTokenAssociate, SyntheticBlock } from "../types";
export declare const serializeSignature: (signature: Uint8Array) => string;
export declare const deserializeSignature: (signature: string) => Buffer<ArrayBuffer>;
export declare const serializeTransaction: (tx: HederaSDKTransaction) => string;
export declare const deserializeTransaction: (tx: string) => HederaSDKTransaction;
export declare const getOperationValue: ({ asset, operation, }: {
asset: AssetInfo;
operation: LiveOperation<HederaOperationExtra>;
}) => bigint;
/**
* Extract the transaction initiator account from a Hedera transaction_id.
*
* Hedera transaction IDs follow the format `0.0.ACCOUNT-TIMESTAMP-NONCE`.
* The first segment (before the first `-`) is the Hedera native account ID of
* the transaction initiator.
*
* This always returns a Hedera-native account ID (e.g. `0.0.12345`), never
* an EVM address, since the Mirror Node transaction_id always uses the native format.
*
* In most cases, the transaction initiator is also the fee payer, but not always: see `extractFeesPayer`.
*/
export declare function extractInitiator(transactionId: string): string;
/**
* Extract the fee payer account for a Hedera mirror transaction.
*
* In most cases, the transaction initiator is also the fee payer, but not always: failed
* transactions can be charged to the initiator, or not. The most reliable signal for
* determining the fee payer is to analyze the actual balance changes (transfers) and the
* charged transaction fee.
*
* This helper implements a best-effort heuristic:
* - it first inspects the transfers and charged fee to infer a unique fee payer when possible;
* - if it cannot unambiguously identify a single payer from balance changes, it falls back to
* the transaction initiator derived from the transaction id.
*/
export declare function extractFeesPayer(transaction: Pick<HederaMirrorTransaction, "transaction_id" | "transfers" | "charged_tx_fee">): string;
export declare const getHederaTransactionBodyBytes: (tx: HederaSDKTransaction) => Uint8Array<ArrayBufferLike>;
export declare const mapIntentToSDKOperation: (txIntent: TransactionIntent) => HEDERA_OPERATION_TYPES;
export declare const getMemoFromBase64: (memoBase64: string | undefined) => string | null;
export declare function base64ToUrlSafeBase64(data: string): string;
export declare const getTransactionExplorer: (explorerView: ExplorerView | null | undefined, operation: LiveOperation) => string | undefined;
export declare const isTokenAssociateTransaction: (tx: Transaction | null | undefined) => tx is TransactionTokenAssociate;
export declare const isAutoTokenAssociationEnabled: (account: AccountLike) => boolean;
export declare const isTokenAssociationRequired: (account: AccountLike, token: TokenCurrency | null | undefined) => boolean;
export declare const isValidExtra: (extra: unknown) => extra is HederaOperationExtra;
export declare const getOperationDetailsExtraFields: (extra: HederaOperationExtra) => OperationDetailsExtraField[];
export declare const sendRecipientCanNext: (status: TransactionStatus) => boolean;
export declare const getChecksum: (accountId: string) => string | null;
export declare function getBlockHash(blockHeight: number): string;
/**
* Calculates a synthetic block height based on Hedera consensus timestamp.
*
* @param consensusTimestamp - Hedera consensus timestamp
* @param blockWindowSeconds - Duration of one synthetic block in seconds (default: 10)
* @returns Deterministic synthetic block (height and hash)
*/
export declare function getSyntheticBlock(consensusTimestamp: string, blockWindowSeconds?: number): SyntheticBlock;
/**
* Calculates the date range based on a synthetic block height.
*
* @param blockHeight - The synthetic block height
* @param blockWindowSeconds - Duration of one synthetic block in seconds (default: 10)
* @returns block date range
*/
export declare function getDateRangeFromBlockHeight(blockHeight: number, blockWindowSeconds?: number): {
start: Date;
end: Date;
};
export declare const formatTransactionId: (transactionId: TransactionId) => string;
/**
* Converts EVM address in hexadecimal format to its corresponding Hedera account ID.
* Only long-zero addresses can be mathematically converted back to account IDs.
* Non-long-zero addresses support would require mirror node call and is not needed for now
* Uses shard 0 and realm 0 by default for the conversion.
* If the conversion fails, it returns null.
*
* @param evmAddress - EVM address in hexadecimal format (should start with '0x')
* @param shard - Optional shard ID (defaults to 0)
* @param realm - Optional realm ID (defaults to 0)
* @returns Hedera account ID in the format `shard.realm.num` or null if conversion fails
*/
export declare const fromEVMAddress: (evmAddress: string, shard?: number, realm?: number) => string | null;
export declare const extractCompanyFromNodeDescription: (description: string) => string;
export declare const sortValidators: (validators: HederaValidator[]) => HederaValidator[];
export declare const filterValidatorBySearchTerm: (validator: HederaValidator, search: string) => boolean;
export declare const getValidatorFromAccount: (account: HederaAccount) => HederaValidator | null;
export declare const getDefaultValidator: (validators: HederaValidator[]) => HederaValidator | null;
export declare const getDelegationStatus: (validator: HederaValidator | null) => HEDERA_DELEGATION_STATUS;
export declare const isStakingTransaction: (tx: Transaction | null | undefined) => tx is TransactionStaking;
export declare const hasSpecificIntentData: <Type extends "staking" | "erc20">(txIntent: TransactionIntent<HederaMemo, HederaTxData>, expectedType: Type) => txIntent is Extract<TransactionIntent<HederaMemo, HederaTxData>, {
data: {
type: Type;
};
}>;
export declare const calculateAPY: (rewardRateStart: number) => number;
export declare const toEntityId: ({ num, shard, realm, }: {
num: number;
shard?: number;
realm?: number;
}) => string;
/**
* Merges transactions from Mirror Node and Hgraph ERC20 transfers into a unified, sorted list.
*
* This function handles the complexity of combining two data sources that may have different indexing speeds:
* - Mirror Node transactions (native HBAR transfers, HTS tokens, contract calls, etc.)
* - Hgraph ERC20 transfers (indexed separately, may lag behind Mirror Node)
*
* Key behaviors:
* 1. Merges both sources into a single array with type discrimination
* 2. Sorts by consensus timestamp (nanosecond precision) in specified order
* 3. Detects and handles ERC20 indexing delays by filtering out transactions after Hgraph's latest timestamp
* 4. Applies pagination limit and generates next cursor for pagination
*
* @param mirrorTransactions - Transactions from Mirror Node API
* @param enrichedERC20Transfers - enriched ERC20 transfers from Hgraph API
* @param order - Sort order: "asc" or "desc"
* @param limit - Maximum number of transactions to return (only applied when fetchAllPages is false)
* @param latestHgraphIndexedTimestampNs - Latest consensus timestamp indexed by Hgraph (used for delay detection)
* @param fetchAllPages - If true, returns all transactions; if false, applies limit
* @returns Object containing merged transactions and next cursor that can be used for pagination
*/
export declare const mergeTransactionsFromDifferentSources: ({ mirrorTransactions, enrichedERC20Transfers, order, limit, latestHgraphIndexedTimestampNs, fetchAllPages, }: {
mirrorTransactions: HederaMirrorTransaction[];
enrichedERC20Transfers: EnrichedERC20Transfer[];
order: "asc" | "desc";
limit: number;
latestHgraphIndexedTimestampNs: BigNumber;
fetchAllPages: boolean;
}) => {
merged: MergedTransaction[];
nextCursor: string | null;
};
export declare function millisToSeconds(millis: number | BigNumber): BigNumber;
export declare function secondsToNanos(seconds: number | BigNumber): BigNumber;
export declare function nanosToSeconds(nanos: number | BigNumber): BigNumber;
export declare function toTimestamp(consensusTimestamp: string): Timestamp;
export declare function createStakingRewardOperationHash(hash: string): string;
export declare function resolveConfig(configOrCurrencyId: HederaCoinConfig | string): HederaCoinConfig;
//# sourceMappingURL=utils.d.ts.map