Skip to content

Commit

Permalink
fix(legacy): node id
Browse files Browse the repository at this point in the history
  • Loading branch information
uonr committed Dec 21, 2024
1 parent 47efb01 commit 0299bd0
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions apps/legacy/src/utils/id.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { v1 as uuidV1 } from 'uuid';

const getNodeId = (): number[] => {
const getNodeId = (): Uint8Array => {
const key = 'client-node-id';
const serializedId = localStorage.getItem(key);
if (serializedId) {
try {
return JSON.parse(serializedId) as number[];
const parsed = JSON.parse(serializedId);
if (Array.isArray(parsed) && parsed.length === 6 && parsed.every((x) => typeof x === 'number')) {
return new Uint8Array(parsed);
} else {
throw new Error('Invalid node ID');
}
} catch (e) {
localStorage.removeItem(key);
return getNodeId();
Expand All @@ -15,7 +20,7 @@ const getNodeId = (): number[] => {
window.crypto.getRandomValues(bytes);
const nodeId = Array.from(bytes);
localStorage.setItem(key, JSON.stringify(nodeId));
return nodeId;
return bytes;
}
};

Expand Down

0 comments on commit 0299bd0

Please sign in to comment.