SHA256
86 lines
3.1 KiB
JavaScript
86 lines
3.1 KiB
JavaScript
import { t } from '../i18n/index.js';
|
|
import qrcode from '../vendor-qrcode-generator.js';
|
|
|
|
const TRANSFER_PREFIX = 'shine-key-transfer-v1:';
|
|
const encoder = new TextEncoder();
|
|
const decoder = new TextDecoder();
|
|
|
|
function bytesToBase64Url(bytes) {
|
|
let binary = '';
|
|
bytes.forEach((b) => {
|
|
binary += String.fromCharCode(b);
|
|
});
|
|
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');
|
|
}
|
|
|
|
function base64UrlToBytes(value) {
|
|
const normalized = String(value || '').trim().replace(/-/g, '+').replace(/_/g, '/');
|
|
const padded = normalized + '='.repeat((4 - (normalized.length % 4)) % 4);
|
|
const binary = atob(padded);
|
|
const out = new Uint8Array(binary.length);
|
|
for (let i = 0; i < binary.length; i += 1) out[i] = binary.charCodeAt(i);
|
|
return out;
|
|
}
|
|
|
|
export function keyLabel(id) {
|
|
if (id === 'blockchain') return t('remaining.qr_keys.blockchain_key');
|
|
if (id === 'device' || id === 'client') return t('remaining.qr_keys.access_key');
|
|
return id;
|
|
}
|
|
|
|
export function describeTransferKeys(keys = {}) {
|
|
const out = [];
|
|
if (keys.clientKey) out.push(t('remaining.qr_keys.access_key'));
|
|
if (keys.blockchainKey) out.push(t('remaining.qr_keys.blockchain_key'));
|
|
return out;
|
|
}
|
|
|
|
export function makeKeyTransferText({ login, keys }) {
|
|
const clientKey = String(keys?.clientKey || '').trim();
|
|
const blockchainKey = String(keys?.blockchainKey || '').trim();
|
|
if (!clientKey || !blockchainKey) {
|
|
throw new Error(t('remaining.qr_keys.both_an_access_key_and_blockchain_key_are_required_to_conn'));
|
|
}
|
|
const payload = {
|
|
v: 1,
|
|
type: 'shine-key-transfer',
|
|
login: String(login || '').trim(),
|
|
keys: { clientKey, blockchainKey },
|
|
createdAtMs: Date.now(),
|
|
};
|
|
const json = JSON.stringify(payload);
|
|
return `${TRANSFER_PREFIX}${bytesToBase64Url(encoder.encode(json))}`;
|
|
}
|
|
|
|
export function parseKeyTransferText(text) {
|
|
const raw = String(text || '').trim();
|
|
if (!raw.startsWith(TRANSFER_PREFIX)) {
|
|
throw new Error(t('remaining.qr_keys.this_is_not_a_shine_key_transfer_qr_code'));
|
|
}
|
|
const json = decoder.decode(base64UrlToBytes(raw.slice(TRANSFER_PREFIX.length)));
|
|
const payload = JSON.parse(json);
|
|
if (payload?.v !== 1 || payload?.type !== 'shine-key-transfer') {
|
|
throw new Error(t('remaining.qr_keys.unsupported_qr_code_format'));
|
|
}
|
|
const login = String(payload.login || '').trim();
|
|
if (!login) throw new Error(t('remaining.qr_keys.no_login_found_in_qr_code'));
|
|
const keys = payload.keys && typeof payload.keys === 'object' ? payload.keys : {};
|
|
const clientKey = String(keys.clientKey || '').trim();
|
|
const blockchainKey = String(keys.blockchainKey || '').trim();
|
|
if (!clientKey || !blockchainKey) {
|
|
throw new Error(t('remaining.qr_keys.qr_code_does_not_contain_all_required_keys'));
|
|
}
|
|
return {
|
|
login,
|
|
keys: { clientKey, blockchainKey },
|
|
keyTypes: describeTransferKeys({ clientKey, blockchainKey }),
|
|
};
|
|
}
|
|
|
|
export function renderQrSvg(text, { cellSize = 4, margin = 4 } = {}) {
|
|
const qr = qrcode(0, 'L');
|
|
qr.addData(String(text || ''), 'Byte');
|
|
qr.make();
|
|
return qr.createSvgTag(cellSize, margin);
|
|
}
|