UNPKG

@ledgerhq/coin-internet_computer

Version:
118 lines 4.93 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAccountShape = void 0; const index_1 = require("@ledgerhq/coin-framework/account/index"); const api_1 = require("../../api"); const flatMap_1 = __importDefault(require("lodash/flatMap")); const bignumber_js_1 = __importDefault(require("bignumber.js")); const consts_1 = require("../../consts"); const operation_1 = require("@ledgerhq/coin-framework/operation"); const utils_1 = require("../../common-logic/utils"); const invariant_1 = __importDefault(require("invariant")); const icpRosetta_1 = require("./icpRosetta"); const getAccountShape = async (info) => { const { currency, derivationMode, rest = {}, initialAccount } = info; const publicKey = reconciliatePublicKey(rest.publicKey, initialAccount); (0, invariant_1.default)(publicKey, "publicKey is required"); // deriving address from public key const address = await (0, icpRosetta_1.deriveAddressFromPubkey)(publicKey); (0, invariant_1.default)(address, "address is required"); const accountId = (0, index_1.encodeAccountId)({ type: "js", version: "2", currencyId: currency.id, xpubOrAddress: publicKey, derivationMode, }); // log("debug", `Generation account shape for ${address}`); const blockHeight = await (0, api_1.fetchBlockHeight)(); const balanceResp = await (0, api_1.fetchBalances)(address); const balance = balanceResp.balances[0]; const txns = await (0, api_1.fetchTxns)(address); const result = { id: accountId, balance: (0, bignumber_js_1.default)(balance.value), spendableBalance: (0, bignumber_js_1.default)(balance.value), operations: (0, flatMap_1.default)(txns.transactions.reverse(), mapTxToOps(accountId, address)), blockHeight: blockHeight.current_block_identifier.index, operationsCount: txns.transactions.length, xpub: publicKey, }; return result; }; exports.getAccountShape = getAccountShape; function reconciliatePublicKey(publicKey, initialAccount) { if (publicKey) return publicKey; if (initialAccount) { const { xpubOrAddress } = (0, index_1.decodeAccountId)(initialAccount.id); return xpubOrAddress; } throw new Error("publicKey wasn't properly restored"); } const mapTxToOps = (accountId, address, fee = consts_1.ICP_FEES) => { return (txInfo) => { const ops = []; const ownerOperation = txInfo.transaction.operations.find(cur => cur.account.address === address); const counterOperation = txInfo.transaction.operations.find(cur => cur.account.address !== address); if (!ownerOperation || !counterOperation) return ops; const timeStamp = txInfo.transaction.metadata.timestamp; const amount = (0, bignumber_js_1.default)(ownerOperation.amount.value); const hash = txInfo.transaction.transaction_identifier.hash; const fromAccount = amount.isPositive() ? counterOperation.account.address : ownerOperation.account.address; const toAccount = amount.isNegative() ? counterOperation.account.address : ownerOperation.account.address; const memo = txInfo.transaction.metadata.memo.toString(); const blockHeight = txInfo.transaction.metadata.block_height; const date = new Date((0, utils_1.normalizeEpochTimestamp)(timeStamp.toString())); const value = amount.abs(); const feeToUse = (0, bignumber_js_1.default)(fee); const isSending = amount.isNegative(); const isReceiving = amount.isPositive(); if (isSending) { ops.push({ id: (0, operation_1.encodeOperationId)(accountId, hash, "OUT"), hash, type: "OUT", value: value.plus(feeToUse), fee: feeToUse, blockHeight, blockHash: null, accountId, senders: [fromAccount], recipients: [toAccount], date, extra: { memo, }, }); } if (isReceiving) { ops.push({ id: (0, operation_1.encodeOperationId)(accountId, hash, "IN"), hash, type: "IN", value, fee: feeToUse, blockHeight, blockHash: null, accountId, senders: [fromAccount], recipients: [toAccount], date, extra: { memo, }, }); } return ops; }; }; //# sourceMappingURL=account.js.map