SHA256
CORE/CONTENT test5593 и адаптация UI
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import { base64ToBytes } from './crypto-utils.js';
|
||||
|
||||
function isByteArrayLike(value) {
|
||||
return Array.isArray(value) || ArrayBuffer.isView(value);
|
||||
}
|
||||
|
||||
function parseKeypairJson64(raw) {
|
||||
const text = String(raw || '').trim();
|
||||
if (!text.startsWith('[')) return null;
|
||||
|
||||
let parsed;
|
||||
try {
|
||||
parsed = JSON.parse(text);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!isByteArrayLike(parsed)) return null;
|
||||
const asArray = Array.from(parsed);
|
||||
if (asArray.length < 32) {
|
||||
throw new Error('Некорректный JSON Ed25519-ключ: ожидалось минимум 32 байта');
|
||||
}
|
||||
|
||||
const out = new Uint8Array(asArray.length);
|
||||
for (let i = 0; i < asArray.length; i += 1) {
|
||||
const n = Number(asArray[i]);
|
||||
if (!Number.isInteger(n) || n < 0 || n > 255) {
|
||||
throw new Error('Некорректный JSON Ed25519-ключ: найдены не-байтовые значения');
|
||||
}
|
||||
out[i] = n;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function extractSeed32FromPkcs8B64(pkcs8B64) {
|
||||
const bytes = base64ToBytes(String(pkcs8B64 || '').trim());
|
||||
if (bytes.length < 32) throw new Error('Некорректный PKCS8 Ed25519-ключ');
|
||||
return bytes.slice(bytes.length - 32);
|
||||
}
|
||||
|
||||
export function extractEd25519Seed32FromStoredValue(storedKey) {
|
||||
const raw = String(storedKey || '').trim();
|
||||
if (!raw) throw new Error('Пустой приватный Ed25519-ключ');
|
||||
|
||||
const jsonBytes = parseKeypairJson64(raw);
|
||||
if (jsonBytes) {
|
||||
return jsonBytes.slice(0, 32);
|
||||
}
|
||||
|
||||
return extractSeed32FromPkcs8B64(raw);
|
||||
}
|
||||
|
||||
// Legacy API name retained for callers outside the wallet subsystem.
|
||||
export const extractClientKey32FromStoredValue = extractEd25519Seed32FromStoredValue;
|
||||
Reference in New Issue
Block a user