-
Notifications
You must be signed in to change notification settings - Fork 0
/
integrity.ts
196 lines (180 loc) · 4.74 KB
/
integrity.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { BloockBridge } from "../bridge/bridge";
import { ConfigData, Network } from "../bridge/proto/config";
import { Proof } from "../entity/integrity";
import { Anchor } from "../entity/integrity/anchor";
import { Record } from "../entity/record";
import { RecordReceipt } from "../entity/integrity";
import { NewConfigData } from "../config/config";
import {
SendRecordsRequest,
GetAnchorRequest,
WaitAnchorRequest,
GetProofRequest,
ValidateRootRequest,
VerifyProofRequest,
VerifyRecordsRequest
} from "../bridge/proto/integrity";
/**
* Provides functionality to interact with the [Bloock Integrity service](https://dashboard.bloock.com/login).
*/
export class IntegrityClient {
private bridge: BloockBridge;
private configData: ConfigData | undefined;
/**
* Creates a new IntegrityClient with default configuration.
* @param configData
*/
constructor(configData?: ConfigData) {
this.bridge = new BloockBridge();
this.configData = NewConfigData(configData);
}
/**
* Sends records to the Bloock Integrity service for certification.
* @param records
* @returns
*/
public async sendRecords(records: Record[]): Promise<RecordReceipt[]> {
const request = SendRecordsRequest.fromPartial({
configData: this.configData,
records: records.map(x => x.toProto())
});
return this.bridge
.getIntegrity()
.SendRecords(request)
.then(res => {
if (res.error) {
throw res.error;
}
return res.records.map(r => RecordReceipt.fromProto(r));
});
}
/**
* Gets an anchor by its ID from the Bloock Integrity service.
* @param anchorId
* @returns
*/
public async getAnchor(anchorId: number): Promise<Anchor> {
const request = GetAnchorRequest.fromPartial({
configData: this.configData,
anchorId: anchorId
});
return this.bridge
.getIntegrity()
.GetAnchor(request)
.then(res => {
if (res.error) {
throw res.error;
}
return Anchor.fromProto(res.anchor!);
});
}
/**
* Waits for the completion of an anchor on the Bloock Integrity service.
* @param anchorId
* @param timeout
* @returns
*/
public async waitAnchor(anchorId: number, timeout?: number): Promise<Anchor> {
const request = WaitAnchorRequest.fromPartial({
configData: this.configData,
anchorId: anchorId,
timeout: timeout !== null ? timeout : 120000
});
return this.bridge
.getIntegrity()
.WaitAnchor(request)
.then(res => {
if (res.error) {
throw res.error;
}
return Anchor.fromProto(res.anchor!);
});
}
/**
* Gets a proof for a set of records from the Bloock Integrity service.
* @param records
* @returns
*/
public async getProof(records: Record[]): Promise<Proof> {
const request = GetProofRequest.fromPartial({
configData: this.configData,
records: records.map(x => x.toProto())
});
return this.bridge
.getIntegrity()
.GetProof(request)
.then(res => {
if (res.error) {
throw res.error;
}
return Proof.fromProto(res.proof!);
});
}
/**
* Validates the integrity of a merkle root proof on blockchain.
* @param root
* @param network
* @returns
*/
public async validateRoot(root: string, network: Network): Promise<number> {
const request = ValidateRootRequest.fromPartial({
configData: this.configData,
root: root,
network: network
});
return this.bridge
.getIntegrity()
.ValidateRoot(request)
.then(res => {
if (res.error) {
throw res.error;
}
return res.timestamp;
});
}
/**
* Verifies the integrity of a proof.
* @param proof
* @returns
*/
public async verifyProof(proof: Proof): Promise<string> {
const request = VerifyProofRequest.fromPartial({
configData: this.configData,
proof: proof.toProto()
});
return this.bridge
.getIntegrity()
.VerifyProof(request)
.then(res => {
if (res.error) {
throw res.error;
}
return res.record!;
});
}
/**
* Verifies the integrity of a set of records.
* @param records
* @param network
* @returns
*/
public async verifyRecords(
records: Record[],
network?: Network
): Promise<number> {
const request = VerifyRecordsRequest.fromPartial({
configData: this.configData,
records: records.map(x => x.toProto()),
network: network
});
return this.bridge
.getIntegrity()
.VerifyRecords(request)
.then(res => {
if (res.error) {
throw res.error;
}
return res.timestamp;
});
}
}