1import { parseDidKey, parsePublicMultikey } from "@atcute/crypto";
2import { fromBase58Btc } from "@atcute/multibase";
3
4export const detectKeyType = (key: string): string => {
5 try {
6 return parsePublicMultikey(key).type;
7 } catch (e) {
8 try {
9 const bytes = fromBase58Btc(key.startsWith("z") ? key.slice(1) : key);
10 if (bytes.length >= 2) {
11 const type = (bytes[0] << 8) | bytes[1];
12 if (type === 0xed01) {
13 return "ed25519";
14 }
15 }
16 } catch {}
17 return "unknown";
18 }
19};
20
21export const detectDidKeyType = (key: string): string => {
22 try {
23 return parseDidKey(key).type;
24 } catch (e) {
25 if (key.startsWith("did:key:")) {
26 return detectKeyType(key.slice(8));
27 }
28 return "unknown";
29 }
30};