-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypter.ts
55 lines (51 loc) · 1.48 KB
/
encrypter.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
import * as proto from "../../bridge/proto/encryption_entities";
import {
LocalKey,
ManagedKey,
ManagedCertificate,
LocalCertificate,
AccessControl
} from "../key";
/**
* Represents an encryption configuration with various key types and access control.
*/
export class Encrypter {
localKey?: LocalKey;
managedKey?: ManagedKey;
managedCertificate?: ManagedCertificate;
localCertificate?: LocalCertificate;
accessControl?: AccessControl;
/**
* Creates a new Encrypter instance with a local encryption key.
* @param key
* @param accessControl
*/
constructor(
key: LocalKey | ManagedKey | ManagedCertificate | LocalCertificate,
accessControl?: AccessControl
) {
if (key instanceof LocalKey) {
this.localKey = key;
} else if (key instanceof ManagedKey) {
this.managedKey = key;
} else if (key instanceof ManagedCertificate) {
this.managedCertificate = key;
} else if (key instanceof LocalCertificate) {
this.localCertificate = key;
} else {
throw new Error("invalid key provided");
}
if (accessControl) {
this.accessControl = accessControl;
}
}
public toProto(): proto.Encrypter {
return proto.Encrypter.fromPartial({
localKey: this.localKey?.toProto(),
managedKey: this.managedKey?.toProto(),
managedCertificate: this.managedCertificate?.toProto(),
localCertificate: this.localCertificate?.toProto(),
accessControl: this.accessControl?.toProto(),
});
}
}