-
Notifications
You must be signed in to change notification settings - Fork 0
/
signer.ts
74 lines (68 loc) · 2.04 KB
/
signer.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
73
74
import * as proto from "../../bridge/proto/authenticity_entities";
import * as protoKey from "../../bridge/proto/keys_entities";
import {
AccessControl,
LocalCertificate,
LocalKey,
ManagedCertificate,
ManagedKey
} from "../key";
import { HashAlg } from "./hash_alg";
/**
* Represents a signer with various key types and additional configurations.
*/
export class Signer {
localKey?: LocalKey;
managedKey?: ManagedKey;
managedCertificate?: ManagedCertificate;
localCertificate?: LocalCertificate;
hashAlg?: HashAlg;
accessControl?: AccessControl;
/**
* Creates a Signer instance with a local key, managed key, local certificate or managed certificate.
* @param key
* @param hashAlg
* @param accessControl
*/
constructor(
key: LocalKey | ManagedKey | ManagedCertificate | LocalCertificate | string,
hashAlg?: HashAlg,
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 (hashAlg) {
this.hashAlg = hashAlg;
}
if (accessControl) {
this.accessControl = accessControl;
}
}
public toProto(): proto.Signer {
let hashAlg: proto.HashAlg | undefined;
if (this.hashAlg) {
hashAlg = this.hashAlg && HashAlg.toProto(this.hashAlg);
}
let accessControl: protoKey.AccessControl | undefined;
if (this.accessControl) {
accessControl = this.accessControl && this.accessControl.toProto();
}
return proto.Signer.fromPartial({
localKey: this.localKey?.toProto(),
managedKey: this.managedKey?.toProto(),
managedCertificate: this.managedCertificate?.toProto(),
localCertificate: this.localCertificate?.toProto(),
hashAlg: hashAlg,
accessControl: accessControl,
});
}
}