UNPKG

@ledgerhq/coin-tezos

Version:
159 lines • 5.65 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.txToOp = void 0; exports.reconciliatePublicKey = reconciliatePublicKey; exports.encodeAddress = encodeAddress; exports.isStringHex = isStringHex; const blake2b_1 = __importDefault(require("blake2b")); const bs58check_1 = __importDefault(require("bs58check")); const bignumber_js_1 = __importDefault(require("bignumber.js")); const account_1 = require("@ledgerhq/coin-framework/account"); const operation_1 = require("@ledgerhq/coin-framework/operation"); const txToOp = ({ address, accountId }) => (tx) => { let type; let maybeValue; let senders = []; let recipients = []; const hasFailed = tx.status ? tx.status !== "applied" : false; switch (tx.type) { case "transaction": { const initiator = tx.initiator?.address; const from = tx.sender?.address; const to = tx.target?.address; if (from !== address && to !== address && initiator !== address) { // failsafe for a case that shouldn't happen. console.warn("found tx is unrelated to account! " + tx.hash); return null; } senders = [from || initiator || ""]; recipients = [to || ""]; if ((from === address && to === address) || // self tx (from !== address && to !== address) // initiator but not in from/to ) { // we just pay fees in that case type = "FEES"; } else { type = to === address ? "IN" : "OUT"; if (!hasFailed) { maybeValue = new bignumber_js_1.default(tx.amount || 0); if (maybeValue.eq(0)) { type = "FEES"; } } } break; } case "delegation": type = tx.newDelegate ? "DELEGATE" : "UNDELEGATE"; senders = [address]; // convention was to use recipient for the new delegation address or "" if undelegation recipients = [tx.newDelegate ? tx.newDelegate.address : ""]; break; case "reveal": type = "REVEAL"; senders = [address]; recipients = [address]; break; case "migration": type = tx.balanceChange < 0 ? "OUT" : "IN"; maybeValue = new bignumber_js_1.default(Math.abs(tx.balanceChange || 0)); senders = [address]; recipients = [address]; break; case "origination": type = "CREATE"; maybeValue = new bignumber_js_1.default(tx.contractBalance || 0); senders = [address]; recipients = [tx.originatedContract?.address || ""]; break; case "activation": type = "IN"; senders = [address]; recipients = [address]; maybeValue = new bignumber_js_1.default(tx.balance || 0); break; // TODO more type of tx default: console.warn("unsupported tx:", tx); return null; } let { hash } = tx; const { id, allocationFee, bakerFee, storageFee, level: blockHeight, block: blockHash, timestamp, } = tx; if (!hash) { // in migration case, there is no hash... hash = ""; } let value = maybeValue || new bignumber_js_1.default(0); if (type === "IN" && value.eq(0)) { return; // not interesting op } let fee = new bignumber_js_1.default(bakerFee || 0); if (!hasFailed) { fee = fee.plus(allocationFee || 0).plus(storageFee || 0); } if (type !== "IN") { value = value.plus(fee); } return { id: (0, operation_1.encodeOperationId)(accountId, hash, type), hash, type, value, fee, senders, recipients, blockHeight, blockHash, accountId, date: new Date(timestamp), extra: { id }, hasFailed, }; }; exports.txToOp = txToOp; function reconciliatePublicKey(publicKey, account) { if (publicKey) return publicKey; if (account) { const { xpubOrAddress } = (0, account_1.decodeAccountId)(account.id); return xpubOrAddress; } throw new Error("publicKey wasn't properly restored"); } function encodeAddress(publicKey) { const curve = 0; const curveData = { pkB58Prefix: Buffer.from([13, 15, 37, 217]), pkhB58Prefix: Buffer.from([6, 161, 159]), compressPublicKey: (publicKey, curve) => { publicKey = publicKey.slice(0); publicKey[0] = curve; return publicKey; }, }; const publicKeyBuf = curveData.compressPublicKey(publicKey, curve); const key = publicKeyBuf.slice(1); const keyHashSize = 20; // eslint-disable-next-line prefer-const const hash = Buffer.alloc(keyHashSize); const blakHash = (0, blake2b_1.default)(keyHashSize); blakHash.update(key); blakHash.digest(hash); const address = bs58check_1.default.encode(Buffer.concat([curveData.pkhB58Prefix, hash])); return address; } function isStringHex(s) { for (let i = 0; i < s.length; i += 2) { const ss = s.slice(i, i + 2); const x = parseInt(ss, 16); if (Number.isNaN(x)) { return false; } } return true; } //# sourceMappingURL=logic.js.map