The best way to control Claude Code, Codex and more from any computer or phone

main
ts 54 lines 1.74 KB
Raw
1 export const authenticatorDataFromAttestation = (bytes: Uint8Array) => {
2 let offset = 0;
3 const readHead = () => {
4 const initial = bytes[offset++];
5 if (initial === undefined) return undefined;
6 const info = initial & 31;
7 let length = info;
8 if (info >= 24) {
9 if (info > 27) return undefined;
10 const size = 1 << (info - 24);
11 if (offset + size > bytes.length) return undefined;
12 length = 0;
13 for (let index = 0; index < size; index++) length = length * 256 + (bytes[offset++] ?? 0);
14 }
15 return { major: initial >> 5, length };
16 };
17 const skipValue = (): boolean => {
18 const head = readHead();
19 if (!head) return false;
20 switch (head.major) {
21 case 2:
22 case 3:
23 offset += head.length;
24 return offset <= bytes.length;
25 case 4:
26 for (let index = 0; index < head.length; index++) if (!skipValue()) return false;
27 return true;
28 case 5:
29 for (let index = 0; index < head.length * 2; index++) if (!skipValue()) return false;
30 return true;
31 case 6:
32 return skipValue();
33 default:
34 return true;
35 }
36 };
37
38 const map = readHead();
39 if (map?.major !== 5) return undefined;
40 for (let entry = 0; entry < map.length; entry++) {
41 const key = readHead();
42 if (key?.major !== 3 || offset + key.length > bytes.length) return undefined;
43 const name = new TextDecoder().decode(bytes.subarray(offset, offset + key.length));
44 offset += key.length;
45 if (name !== "authData") {
46 if (!skipValue()) return undefined;
47 continue;
48 }
49 const value = readHead();
50 if (value?.major !== 2 || offset + value.length > bytes.length) return undefined;
51 return bytes.slice(offset, offset + value.length);
52 }
53 return undefined;
54 };