-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDilithiumKeyPair.ts
72 lines (68 loc) · 2.06 KB
/
DilithiumKeyPair.ts
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const dilithium = require("../util/api");
import { base64url } from "./encoding";
import { suiteTypes } from "./suites";
export class DilithiumKeyPair {
public id: string;
public type: string;
public controller: string;
public publicKeyJwk: any;
public privateKeyJwk: any;
static async generate() {
const api = await dilithium.init();
const privateKeyJwk = await api.generate();
const { x, d, xs, ds, ...publicKeyJwk } = privateKeyJwk;
return DilithiumKeyPair.from({
id: "did:example:123#key-0",
type: "JsonWebKey2020",
controller: "did:example:123",
publicKeyJwk: {
...publicKeyJwk,
// todo: https://github.com/mesur-io/dilithium/issues/5
// x: base64url.encode(Buffer.from(x, "base64")),
// xs: base64url.encode(Buffer.from(xs, "base64")),
x,
xs,
},
privateKeyJwk: {
...publicKeyJwk,
x,
xs,
d,
ds,
// todo: https://github.com/mesur-io/dilithium/issues/5
// x: base64url.encode(Buffer.from(x, "base64")),
// xs: base64url.encode(Buffer.from(xs, "base64")),
// d: base64url.encode(Buffer.from(d, "base64")),
// ds: base64url.encode(Buffer.from(ds, "base64")),
},
});
}
static async from(kp: any) {
return new DilithiumKeyPair(kp);
}
constructor(kp: any) {
this.id = kp.id;
this.type = kp.type;
this.controller = kp.controller;
this.publicKeyJwk = kp.publicKeyJwk;
this.privateKeyJwk = kp.privateKeyJwk;
}
signer(type: "CRYDI3" = "CRYDI3") {
if (!this.privateKeyJwk) {
throw new Error("No private key to sign with.");
}
if (suiteTypes[type]) {
return suiteTypes[type].signer(this.privateKeyJwk);
}
throw new Error("Unsupported suite type " + type);
}
verifier(type: "CRYDI3" = "CRYDI3") {
if (!this.publicKeyJwk) {
throw new Error("No public key to verify with.");
}
if (suiteTypes[type]) {
return suiteTypes[type].verifier(this.publicKeyJwk);
}
throw new Error("Unsupported suite type " + type);
}
}