-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticity.ts
121 lines (114 loc) · 3.21 KB
/
authenticity.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { BloockBridge } from "../bridge/bridge";
import { ConfigData } from "../bridge/proto/config";
import { EcdsaKeyPair, KeyPair } from "../entity/key";
import { NewConfigData } from "../config/config";
import { Signature } from "../entity/authenticity";
import { Record } from "../entity/record";
import {
GetSignaturesRequest,
SignRequest,
VerifyRequest
} from "../bridge/proto/authenticity";
import { Signer } from "../entity/authenticity/signer";
import { GenerateLocalKeyRequest } from "../bridge/proto/keys";
import { KeyType } from "../bridge/proto/keys_entities";
/**
* Represents a client for interacting with the [Bloock Authenticity service](https://dashboard.bloock.com/login).
*/
export class AuthenticityClient {
private bridge: BloockBridge;
private configData: ConfigData;
/**
* Creates a new instance of the AuthenticityClient with default configuration.
* @param configData
*/
constructor(configData?: ConfigData) {
this.bridge = new BloockBridge();
this.configData = NewConfigData(configData);
}
/**
* Generates ECDSA key pair for signing records.
* @returns {Promise<KeyPair>} An object containing both the public and the private key
* @deprecated Will be deleted in future versions. Use KeyClient.newLocalKey function instead.
*/
public async generateEcdsaKeyPair(): Promise<KeyPair> {
let request = GenerateLocalKeyRequest.fromPartial({
configData: this.configData,
keyType: KeyType.EcP256k
});
return this.bridge
.getKey()
.GenerateLocalKey(request)
.then(res => {
if (res.error) {
throw res.error;
}
return EcdsaKeyPair.fromProto(res);
});
}
/**
* Signs a Bloock record using the specified signer.
* @param record
* @param signer
* @returns
*/
public async sign(record: Record, signer: Signer): Promise<Signature> {
return this.bridge
.getAuthenticity()
.Sign(
SignRequest.fromPartial({
configData: this.configData,
record: record.toProto(),
signer: signer.toProto()
})
)
.then(res => {
if (res.error) {
throw res.error;
}
return Signature.fromProto(res.signature!);
});
}
/**
* Verifies the authenticity of a Bloock record.
* @param record
* @returns
*/
public async verify(record: Record): Promise<boolean> {
return this.bridge
.getAuthenticity()
.Verify(
VerifyRequest.fromPartial({
configData: this.configData,
record: record.toProto()
})
)
.then(res => {
if (res.error) {
throw res.error;
}
return res.valid;
});
}
/**
* Gets the signatures associated with a Bloock record.
* @param record
* @returns
*/
async getSignatures(record: Record): Promise<Signature[]> {
return this.bridge
.getAuthenticity()
.GetSignatures(
GetSignaturesRequest.fromPartial({
configData: this.configData,
record: record.toProto()
})
)
.then(res => {
if (res.error) {
throw res.error;
}
return res.signatures.map(x => Signature.fromProto(x));
});
}
}