Suite of AT Protocol TypeScript libraries built on web standards
20
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 50 lines 1.4 kB view raw
1import * as bytes from "@atp/bytes"; 2import { BASE58_MULTIBASE_PREFIX, DID_KEY_PREFIX } from "./const.ts"; 3import { plugins } from "./plugins.ts"; 4import { extractMultikey, extractPrefixedBytes, hasPrefix } from "./utils.ts"; 5 6export type ParsedMultikey = { 7 jwtAlg: string; 8 keyBytes: Uint8Array; 9}; 10 11export const parseMultikey = (multikey: string): ParsedMultikey => { 12 const prefixedBytes = extractPrefixedBytes(multikey); 13 const plugin = plugins.find((p) => hasPrefix(prefixedBytes, p.prefix)); 14 if (!plugin) { 15 throw new Error("Unsupported key type"); 16 } 17 const keyBytes = plugin.decompressPubkey( 18 prefixedBytes.slice(plugin.prefix.length), 19 ); 20 return { 21 jwtAlg: plugin.jwtAlg, 22 keyBytes, 23 }; 24}; 25 26export const formatMultikey = ( 27 jwtAlg: string, 28 keyBytes: Uint8Array, 29): string => { 30 const plugin = plugins.find((p) => p.jwtAlg === jwtAlg); 31 if (!plugin) { 32 throw new Error("Unsupported key type"); 33 } 34 const prefixedBytes = bytes.concat([ 35 plugin.prefix, 36 plugin.compressPubkey(keyBytes), 37 ]); 38 return ( 39 BASE58_MULTIBASE_PREFIX + bytes.toString(prefixedBytes, "base58btc") 40 ); 41}; 42 43export const parseDidKey = (did: string): ParsedMultikey => { 44 const multikey = extractMultikey(did); 45 return parseMultikey(multikey); 46}; 47 48export const formatDidKey = (jwtAlg: string, keyBytes: Uint8Array): string => { 49 return DID_KEY_PREFIX + formatMultikey(jwtAlg, keyBytes); 50};