UNPKG

@ledgerhq/coin-mina

Version:
84 lines 2.52 kB
import { BigNumber } from "bignumber.js"; import { CoinType } from "@ledgerhq/types-cryptoassets"; import { MAX_MEMO_LENGTH, MINA_DECODED_ADDRESS_LENGTH } from "../consts"; import bs58check from "bs58check"; /* * Validate a Mina address. */ export function isValidAddress(address) { try { if (!address.toLowerCase().startsWith("b62")) { return false; } const decodedAddress = Buffer.from(bs58check.decode(address)).toString("hex"); return !!decodedAddress && decodedAddress.length === MINA_DECODED_ADDRESS_LENGTH; } catch { return false; } } export const isValidMemo = (memo) => { return memo.length <= MAX_MEMO_LENGTH; }; // Get the account number from the path export const getAccountNumFromPath = (path) => { const parts = path.split("'/"); if (parts.length === 3) { return; } if (parts[1] !== `${CoinType.MINA}`) { return; } try { const acc = parseInt(parts[2], 10); if (acc >= 0) { return acc; } return; } catch { return; } }; /* * Get the max amount that can be spent, taking into account tx type and pending operations. */ export const getMaxAmount = (a, _t, fees) => { let maxAmount; let pendingDefaultAmount = new BigNumber(0); a.pendingOperations.forEach(({ value }) => { pendingDefaultAmount = pendingDefaultAmount.plus(value); }); maxAmount = a.spendableBalance.minus(pendingDefaultAmount); if (fees) { maxAmount = maxAmount.minus(fees); } if (maxAmount === undefined || maxAmount.lt(0)) { return new BigNumber(0); } return maxAmount; }; export const getTotalSpent = (a, t, fees) => { if (t.useAllAmount) { return a.spendableBalance; } return new BigNumber(t.amount).plus(fees); }; // reEncodeRawSignature takes a raw signature in the form of a 128-character hex string and returns a re-encoded version of it. export function reEncodeRawSignature(rawSignature) { function shuffleBytes(hex) { const bytes = hex.match(/.{2}/g); if (!bytes) { throw "Invalid hex input"; } bytes.reverse(); return bytes.join(""); } if (rawSignature.length !== 128) { throw "Invalid raw signature input"; } const field = rawSignature.substring(0, 64); const scalar = rawSignature.substring(64); return shuffleBytes(field) + shuffleBytes(scalar); } //# sourceMappingURL=index.js.map