UNPKG

@ledgerhq/coin-tezos

Version:
99 lines 4.39 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.sync = exports.getAccountShape = void 0; const invariant_1 = __importDefault(require("invariant")); const bignumber_js_1 = require("bignumber.js"); const jsHelpers_1 = require("@ledgerhq/coin-framework/bridge/jsHelpers"); const index_1 = require("@ledgerhq/coin-framework/account/index"); const logic_1 = require("./logic"); const tzkt_1 = __importStar(require("../network/tzkt")); const getAccountShape = async ({ initialAccount, rest, currency, derivationMode, }) => { const publicKey = (0, logic_1.reconciliatePublicKey)(rest?.publicKey, initialAccount); (0, invariant_1.default)((0, logic_1.isStringHex)(publicKey), `Invalid public key (${publicKey}). Please reimport your Tezos accounts`); const hex = Buffer.from(publicKey, "hex"); const address = (0, logic_1.encodeAddress)(hex); const accountId = (0, index_1.encodeAccountId)({ type: "js", version: "2", currencyId: currency.id, xpubOrAddress: publicKey, derivationMode, }); const initialStableOperations = (initialAccount && initialAccount.id === accountId ? initialAccount.operations : []); // fetch transactions, incrementally if possible const mostRecentStableOperation = initialStableOperations[0]; const lastId = initialAccount && (0, index_1.areAllOperationsLoaded)(initialAccount) && mostRecentStableOperation ? mostRecentStableOperation.extra.id || undefined : undefined; const apiAccountPromise = tzkt_1.default.getAccountByAddress(address); const blocksCountPromise = tzkt_1.default.getBlockCount(); const [apiAccount, blockHeight] = await Promise.all([apiAccountPromise, blocksCountPromise]); if (apiAccount.type === "empty") { return { id: accountId, xpub: publicKey, freshAddress: address, blockHeight, lastSyncDate: new Date(), tezosResources: { revealed: false, counter: 0, }, }; } const fullySupported = apiAccount.type === "user"; const apiOperations = fullySupported ? await (0, tzkt_1.fetchAllTransactions)(address, lastId) : []; const { revealed, counter } = apiAccount; const tezosResources = { revealed, counter, }; const balance = new bignumber_js_1.BigNumber(apiAccount.balance); const subAccounts = []; const newOps = apiOperations .map((0, logic_1.txToOp)({ address, accountId })) .filter(Boolean); // force cast because `filter(Boolean)` remove undefined and null value const operations = (0, jsHelpers_1.mergeOps)(initialStableOperations, newOps); const accountShape = { id: accountId, xpub: publicKey, freshAddress: address, operations, balance, subAccounts, spendableBalance: balance, blockHeight, lastSyncDate: new Date(), tezosResources, }; return accountShape; }; exports.getAccountShape = getAccountShape; exports.sync = (0, jsHelpers_1.makeSync)({ getAccountShape: exports.getAccountShape }); //# sourceMappingURL=synchronization.js.map