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
32
33
34
35
36
37
// SPDX-License-Identifier: 0BSD
import { compressPoint } from "https://esm.town/v/vladimyr/libec";
import { bytesToMultibase } from "https://esm.town/v/vladimyr/libmultibase";
import { base64url } from "npm:multiformats/bases/base64";
type JWK = {
kty: "EC";
crv: string;
x: string;
y: string;
[propName: string]: unknown;
} | {
kty: "OKP";
crv: string;
x: string;
[propName: string]: unknown;
};
export function jwkToMultibase(jwk: JWK) {
if (jwk.kty === "OKP") {
const bytes = base64url.baseDecode(jwk.x);
return bytesToMultibase(bytes, jwk.crv);
}
if (jwk.kty === "EC") {
const bytes = compressPoint({
x: base64url.baseDecode(jwk.x),
y: base64url.baseDecode(jwk.y),
});
return bytesToMultibase(bytes, jwk.crv);
}
throw new TypeError("error: unsupported key type");
}
export function jwkToDidKey(jwk: JWK) {
const keyMultibase = jwkToMultibase(jwk);
return `did:key:${keyMultibase}`;
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
April 15, 2024