@ledgerhq/coin-hedera
Version:
Ledger Hedera Coin integration
67 lines • 3.01 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildIterateResult = exports.getAccountShape = void 0;
const invariant_1 = __importDefault(require("invariant"));
const derivation_1 = require("@ledgerhq/coin-framework/derivation");
const bignumber_js_1 = require("bignumber.js");
const mirror_1 = require("../api/mirror");
const jsHelpers_1 = require("@ledgerhq/coin-framework/bridge/jsHelpers");
const account_1 = require("@ledgerhq/coin-framework/account");
const network_1 = require("../api/network");
const getAccountShape = async (info) => {
const { currency, derivationMode, address, initialAccount } = info;
(0, invariant_1.default)(address, "an hedera address is expected");
const liveAccountId = (0, account_1.encodeAccountId)({
type: "js",
version: "2",
currencyId: currency.id,
xpubOrAddress: address,
derivationMode,
});
// get current account balance
const accountBalance = await (0, network_1.getAccountBalance)(address);
// grab latest operation's consensus timestamp for incremental sync
const oldOperations = initialAccount?.operations ?? [];
const latestOperationTimestamp = oldOperations[0]
? Math.floor(oldOperations[0].date.getTime() / 1000)
: 0;
// merge new operations w/ previously synced ones
const newOperations = await (0, mirror_1.getOperationsForAccount)(liveAccountId, address, new bignumber_js_1.BigNumber(latestOperationTimestamp).toString());
const operations = (0, jsHelpers_1.mergeOps)(oldOperations, newOperations);
return {
id: liveAccountId,
freshAddress: address,
balance: accountBalance.balance,
spendableBalance: accountBalance.balance,
operations,
// NOTE: there are no "blocks" in hedera
// Set a value just so that operations are considered confirmed according to isConfirmedOperation
blockHeight: 10,
};
};
exports.getAccountShape = getAccountShape;
const buildIterateResult = async ({ result: rootResult }) => {
const accounts = await (0, mirror_1.getAccountsForPublicKey)(rootResult.publicKey);
const addresses = accounts.map(a => a.accountId.toString());
return async ({ currency, derivationMode, index }) => {
const derivationScheme = (0, derivation_1.getDerivationScheme)({
derivationMode,
currency,
});
const freshAddressPath = (0, derivation_1.runDerivationScheme)(derivationScheme, currency, {
account: index,
});
return addresses[index]
? {
address: addresses[index],
publicKey: addresses[index],
path: freshAddressPath,
}
: null;
};
};
exports.buildIterateResult = buildIterateResult;
//# sourceMappingURL=synchronisation.js.map