SHA256
21 lines
1.3 KiB
JavaScript
21 lines
1.3 KiB
JavaScript
/** Точная денежная арифметика для пожертвований SHiNE: без Number. */
|
|
export function parseSolLamports(input) {
|
|
const text = String(input ?? '').trim().replace(',', '.');
|
|
if (!/^(?:0|[1-9]\d*)(?:\.\d{1,9})?$/.test(text)) throw new Error('Введите положительную сумму SOL (не более 9 знаков после запятой)');
|
|
const [whole, fraction = ''] = text.split('.');
|
|
const amount = BigInt(whole) * 1_000_000_000n + BigInt(fraction.padEnd(9, '0') || '0');
|
|
if (amount <= 0n || amount > 0xffff_ffff_ffff_ffffn) throw new Error('Сумма SOL вне допустимого диапазона');
|
|
return amount;
|
|
}
|
|
export function formatLamportsSol(value) {
|
|
const n = BigInt(value);
|
|
const sign = n < 0n ? '-' : '';
|
|
const abs = n < 0n ? -n : n;
|
|
return sign + String(abs / 1_000_000_000n) + (abs % 1_000_000_000n ? '.' + String(abs % 1_000_000_000n).padStart(9, '0').replace(/0+$/, '') : '');
|
|
}
|
|
export function hexHash32(value) {
|
|
const hex = String(value || '').trim().toLowerCase();
|
|
if (!/^[0-9a-f]{64}$/.test(hex)) throw new Error('Некорректный 32-байтовый хеш блока канала');
|
|
return Uint8Array.from({ length: 32 }, (_, i) => parseInt(hex.slice(i * 2, i * 2 + 2), 16));
|
|
}
|