-
Notifications
You must be signed in to change notification settings - Fork 2
/
url-utils.js
31 lines (28 loc) · 852 Bytes
/
url-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// @ts-ignore
import { varint } from '@synonymdev/slashtags-common';
import { base32 } from 'multiformats/bases/base32';
import b4a from 'b4a';
const URL_PREFIX = 'slashtag:';
/**
* Convert a public key to a DID
*
* @param {Buffer} publicKey
* @param {'ES256K' | 'EdDSA'} [type = 'ES256K']
* @returns {string}
*/
export function formatDidUri(publicKey, type) {
const codec = type === 'ES256K' ? 0xe7 : 0xed;
return URL_PREFIX + base32.encode(varint.prepend(codec, publicKey));
}
/**
* Get the public key of the Hypercore from the did uri
*
* @param {string} didURI
*/
export function parseDidUri(didURI) {
const id = didURI.split(':').pop() || '';
const multiHash = base32.decode(id);
const codec = multiHash.slice(1)[0];
const key = b4a.from(multiHash.slice(2));
return { key, type: codec === 0xe7 ? 'ES256K' : 'EdDSA' };
}