-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.ts
41 lines (37 loc) · 1.17 KB
/
key.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
import { LocalKey } from "./local_key";
import { ManagedKey } from "./managed_key";
import * as keysEntitiesProto from "../../bridge/proto/keys_entities";
/**
* Represents a key entity that can be either a LocalKey or a ManagedKey.
*/
export class Key {
localKey?: LocalKey;
managedKey?: ManagedKey;
/**
* Constructs a Key object for a given managed or local key object.
* @param key
*/
constructor(key: LocalKey | ManagedKey) {
if (key instanceof LocalKey) {
this.localKey = key;
} else if (key instanceof ManagedKey) {
this.managedKey = key;
} else {
throw new Error("invalid key provided");
}
}
public toProto(): keysEntitiesProto.Key {
let localKey: keysEntitiesProto.LocalKey | undefined;
if (this.localKey) {
localKey = this.localKey.toProto();
}
let managedKey: keysEntitiesProto.ManagedKey | undefined;
if (this.managedKey) {
managedKey = this.managedKey.toProto();
}
return keysEntitiesProto.Key.fromPartial({
localKey: localKey,
managedKey: managedKey,
});
}
}