-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathsecretKey.ts
More file actions
43 lines (34 loc) · 1.15 KB
/
secretKey.ts
File metadata and controls
43 lines (34 loc) · 1.15 KB
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
import crypto from "crypto";
import blst from "@chainsafe/blst";
import {SecretKey as ISecretKey} from "../types.js";
import {PublicKey} from "./publicKey.js";
import {Signature} from "./signature.js";
import {isZeroUint8Array} from "../helpers/utils.js";
import {ZeroSecretKeyError} from "../errors.js";
export class SecretKey implements ISecretKey {
constructor(private readonly value: blst.SecretKey) {}
static fromBytes(bytes: Uint8Array): SecretKey {
if (isZeroUint8Array(bytes)) {
throw new ZeroSecretKeyError();
}
return new SecretKey(blst.SecretKey.fromBytes(bytes));
}
static fromHex(hex: string): SecretKey {
return new SecretKey(blst.SecretKey.fromHex(hex));
}
static fromKeygen(entropy?: Uint8Array): SecretKey {
return new SecretKey(blst.SecretKey.fromKeygen(entropy ?? crypto.getRandomValues(new Uint8Array(32))));
}
sign(message: Uint8Array): Signature {
return new Signature(this.value.sign(message));
}
toPublicKey(): PublicKey {
return new PublicKey(this.value.toPublicKey());
}
toBytes(): Uint8Array {
return this.value.toBytes();
}
toHex(): string {
return this.value.toHex();
}
}