-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRootTrustBase.ts
More file actions
277 lines (251 loc) · 8.33 KB
/
Copy pathRootTrustBase.ts
File metadata and controls
277 lines (251 loc) · 8.33 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import { InvalidJsonStructureError } from '../../serialization/json/InvalidJsonStructureError.js';
import { JsonError } from '../../serialization/json/JsonError.js';
import { HexConverter } from '../../util/HexConverter.js';
import { dedent } from '../../util/StringUtils.js';
import { NetworkId } from '../NetworkId.js';
/**
* JSON representation of a root trust base node information.
*/
interface INodeInfoJson {
/**
* Node id.
*/
readonly nodeId: string;
/**
* Signing key.
*/
readonly sigKey: string;
/**
* Staked amount.
*/
readonly stake: string;
}
/**
* Root trust base node information.
*/
export class RootTrustBaseNodeInfo {
/**
* RootTrustBaseNodeInfo constructor.
* @param nodeId node id.
* @param _signingKey signing key.
* @param stakedAmount staked amount.
*/
private constructor(
public readonly nodeId: string,
private readonly _signingKey: Uint8Array,
public readonly stakedAmount: bigint,
) {
this._signingKey = new Uint8Array(_signingKey);
}
/**
* Get the signing key.
*/
public get signingKey(): Uint8Array {
return new Uint8Array(this._signingKey);
}
/**
* Create a RootTrustBaseNodeInfo from JSON representation.
* @param input JSON representation.
* @return RootTrustBaseNodeInfo instance.
*/
public static fromJSON(input: unknown): RootTrustBaseNodeInfo {
if (!RootTrustBaseNodeInfo.isJSON(input)) {
throw new InvalidJsonStructureError();
}
const stake = BigInt(input.stake);
if (stake <= 0n) {
throw new JsonError('Each trust base root node must have positive stake.');
}
return new RootTrustBaseNodeInfo(input.nodeId, HexConverter.decode(input.sigKey), stake);
}
/**
* Check whether the input is a JSON representation of RootTrustBaseNodeInfo.
* @param input input to check.
* @return true if the input is a JSON representation of RootTrustBaseNodeInfo.
*/
public static isJSON(input: unknown): input is INodeInfoJson {
return typeof input === 'object' && input !== null && 'nodeId' in input && 'sigKey' in input && 'stake' in input;
}
/**
* @returns {string} String representation of the node info.
*/
public toString(): string {
return dedent`
RootTrustBaseNodeInfo:
NodeId: ${this.nodeId},
SigningKey: ${HexConverter.encode(this._signingKey)},
Stake: ${this.stakedAmount}`;
}
}
/**
* JSON shape of a {@link RootTrustBase}.
*/
interface IRootTrustBaseJson {
readonly changeRecordHash: string | null;
readonly epoch: string;
readonly epochStartRound: string;
readonly networkId: number;
readonly previousEntryHash: string | null;
readonly quorumThreshold: string;
readonly rootNodes: INodeInfoJson[];
readonly signatures: { [key: string]: string };
readonly stateHash: string;
readonly version: string;
}
/**
* Root trust base information.
*/
export class RootTrustBase {
private static readonly VERSION = 1n;
private constructor(
public readonly networkId: NetworkId,
public readonly epoch: bigint,
public readonly epochStartRound: bigint,
public readonly _rootNodes: Map<string, RootTrustBaseNodeInfo>,
public readonly quorumThreshold: bigint,
public readonly _stateHash: Uint8Array,
public readonly _changeRecordHash: Uint8Array | null,
public readonly _previousEntryHash: Uint8Array | null,
private readonly _signatures: Map<string, Uint8Array>,
) {
this._rootNodes = new Map(_rootNodes);
this._stateHash = new Uint8Array(_stateHash);
this._changeRecordHash = _changeRecordHash ? new Uint8Array(_changeRecordHash) : null;
this._previousEntryHash = _previousEntryHash ? new Uint8Array(_previousEntryHash) : null;
this._signatures = new Map(
Array.from(_signatures.entries()).map((_signature) => [_signature[0], new Uint8Array(_signature[1])]),
);
}
/**
* Get the change record hash.
*/
public get changeRecordHash(): Uint8Array | null {
return this._changeRecordHash ? new Uint8Array(this._changeRecordHash) : null;
}
/**
* Get the previous entry hash.
*/
public get previousEntryHash(): Uint8Array | null {
return this._previousEntryHash ? new Uint8Array(this._previousEntryHash) : null;
}
/**
* Get the root nodes.
*/
public get rootNodes(): Map<string, RootTrustBaseNodeInfo> {
return new Map(this._rootNodes);
}
/**
* Get the signatures.
*/
public get signatures(): Map<string, Uint8Array> {
return new Map(
Array.from(this._signatures.entries()).map((_signature) => [_signature[0], new Uint8Array(_signature[1])]),
);
}
/**
* Get the state hash.
*/
public get stateHash(): Uint8Array {
return new Uint8Array(this._stateHash);
}
/**
* @returns {bigint} Wire-format version of the trust base.
*/
public get version(): bigint {
return RootTrustBase.VERSION;
}
/**
* Create a root trust base from JSON string.
*
* @param input JSON string
* @return root trust base
*/
public static fromJSON(input: unknown): RootTrustBase {
if (!RootTrustBase.isJSON(input)) {
throw new InvalidJsonStructureError();
}
const version = BigInt(input.version);
if (version !== RootTrustBase.VERSION) {
throw new JsonError(`Unsupported RootTrustBase version: ${version}`);
}
const rootNodes = new Map<string, RootTrustBaseNodeInfo>();
const signingKeys = new Set<string>();
for (const node of input.rootNodes) {
const info = RootTrustBaseNodeInfo.fromJSON(node);
if (rootNodes.has(info.nodeId)) {
throw new JsonError(`Duplicate trust base node id: ${info.nodeId}`);
}
const signingKey = HexConverter.encode(info.signingKey);
if (signingKeys.has(signingKey)) {
throw new JsonError('Duplicate trust base signing key.');
}
signingKeys.add(signingKey);
rootNodes.set(info.nodeId, info);
}
if (rootNodes.size === 0) {
throw new JsonError('Trust base must contain at least one root node.');
}
const quorumThreshold = BigInt(input.quorumThreshold);
if (quorumThreshold <= 0n || quorumThreshold > BigInt(rootNodes.size)) {
throw new JsonError('Trust base quorum threshold must be between 1 and the root node count.');
}
return new RootTrustBase(
NetworkId.fromId(input.networkId),
BigInt(input.epoch),
BigInt(input.epochStartRound),
rootNodes,
quorumThreshold,
HexConverter.decode(input.stateHash),
input.changeRecordHash ? HexConverter.decode(input.changeRecordHash) : null,
input.previousEntryHash ? HexConverter.decode(input.previousEntryHash) : null,
new Map(Object.entries(input.signatures).map(([id, signature]) => [id, HexConverter.decode(signature)])),
);
}
/**
* Check whether the input is a JSON representation of RootTrustBase.
* @param input input to check.
* @return true if the input is a JSON representation of RootTrustBase.
*/
public static isJSON(input: unknown): input is IRootTrustBaseJson {
return (
typeof input === 'object' &&
input !== null &&
'version' in input &&
'networkId' in input &&
'epoch' in input &&
'epochStartRound' in input &&
'rootNodes' in input &&
Array.isArray(input.rootNodes) &&
'quorumThreshold' in input &&
'stateHash' in input &&
'changeRecordHash' in input &&
'previousEntryHash' in input &&
'signatures' in input &&
typeof input.signatures == 'object' &&
input.signatures !== null
);
}
/**
* @returns {string} String representation of the trust base.
*/
public toString(): string {
return dedent`
RootTrustBase:
Version: ${this.version},
NetworkId: ${this.networkId.toString()},
Epoch: ${this.epoch},
EpochStartRound: ${this.epochStartRound},
RootNodes: [${Array.from(this._rootNodes.values())
.map((node) => node.toString())
.join(', ')}],
QuorumThreshold: ${this.quorumThreshold},
StateHash: ${HexConverter.encode(this._stateHash)},
ChangeRecordHash: ${this._changeRecordHash ? HexConverter.encode(this._changeRecordHash) : 'null'},
PreviousEntryHash: ${this._previousEntryHash ? HexConverter.encode(this._previousEntryHash) : 'null'},
Signatures: [
${Array.from(this._signatures.entries())
.map(([id, sig]) => `${id}: ${HexConverter.encode(sig)}`)
.join(', ')}
]`;
}
}