@ledgerhq/coin-internet_computer
Version:
Ledger Internet Computer integration
137 lines • 5.28 kB
JavaScript
;
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 ledger_live_icp_1 = require("@zondax/ledger-live-icp");
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, ledger_live_icp_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 balance = await (0, api_1.fetchBalance)(address);
const txns = await (0, api_1.fetchTxns)(address, BigInt(blockHeight.toString()), initialAccount ? BigInt(initialAccount.blockHeight.toString()) : undefined);
const result = {
id: accountId,
balance,
spendableBalance: balance,
operations: (0, flatMap_1.default)(txns, mapTxToOps(accountId, address)),
blockHeight: blockHeight.toNumber(),
operationsCount: (initialAccount?.operations.length ?? 0) + txns.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 { transaction: txn } = txInfo;
const ops = [];
if (txn.operation === undefined) {
return [];
}
if ("Transfer" in txn.operation === undefined) {
return [];
}
const timeStamp = txn.timestamp[0]?.timestamp_nanos ?? Date.now();
let amount = (0, bignumber_js_1.default)(0);
let fromAccount = "";
let toAccount = "";
let hash = "";
if ("Transfer" in txn.operation) {
amount = (0, bignumber_js_1.default)(txn.operation.Transfer.amount.e8s.toString());
fromAccount = txn.operation.Transfer.from;
toAccount = txn.operation.Transfer.to;
hash = (0, ledger_live_icp_1.hashTransaction)({
from: fromAccount,
to: toAccount,
amount: txn.operation.Transfer.amount.e8s,
fee: txn.operation.Transfer.fee.e8s,
memo: txn.memo,
created_at_time: txn.created_at_time[0]?.timestamp_nanos ?? BigInt(0),
});
}
const blockHeight = Number(txInfo.id);
const blockHash = "";
const memo = txInfo.transaction.memo.toString();
const date = new Date((0, utils_1.normalizeEpochTimestamp)(timeStamp.toString()));
const value = amount.abs();
const feeToUse = (0, bignumber_js_1.default)(fee);
const isSending = address === fromAccount;
const isReceiving = address === toAccount;
let type;
if (isSending) {
type = "OUT";
}
else {
type = "IN";
}
if (isSending) {
ops.push({
id: (0, operation_1.encodeOperationId)(accountId, hash, type),
hash,
type,
value: value.plus(feeToUse),
fee: feeToUse,
blockHeight,
blockHash,
accountId,
senders: [fromAccount],
recipients: [toAccount],
date,
extra: {
memo,
},
});
}
if (isReceiving) {
ops.push({
id: (0, operation_1.encodeOperationId)(accountId, hash, type),
hash,
type,
value,
fee: feeToUse,
blockHeight,
blockHash,
accountId,
senders: [fromAccount],
recipients: [toAccount],
date,
extra: {
memo,
},
});
}
return ops;
};
};
//# sourceMappingURL=account.js.map