-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInputRecord.ts
More file actions
145 lines (133 loc) · 5.14 KB
/
Copy pathInputRecord.ts
File metadata and controls
145 lines (133 loc) · 5.14 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
import { CborDeserializer } from '../../serialization/cbor/CborDeserializer.js';
import { CborError } from '../../serialization/cbor/CborError.js';
import { CborSerializer } from '../../serialization/cbor/CborSerializer.js';
import { HexConverter } from '../../util/HexConverter.js';
import { dedent } from '../../util/StringUtils.js';
/**
* Input record for UnicityCertificate.
*/
export class InputRecord {
public static readonly CBOR_TAG = 39002n;
private static readonly VERSION = 1n;
public constructor(
public readonly roundNumber: bigint,
public readonly epoch: bigint,
private readonly _previousHash: Uint8Array | null,
private readonly _hash: Uint8Array,
private readonly _summaryValue: Uint8Array,
public readonly timestamp: bigint,
private readonly _blockHash: Uint8Array | null,
public readonly sumOfEarnedFees: bigint,
private readonly _executedTransactionsHash: Uint8Array | null,
) {
this._previousHash = _previousHash ? new Uint8Array(_previousHash) : null;
this._hash = new Uint8Array(_hash);
this._summaryValue = new Uint8Array(_summaryValue);
this._blockHash = _blockHash ? new Uint8Array(_blockHash) : null;
this._executedTransactionsHash = _executedTransactionsHash ? new Uint8Array(_executedTransactionsHash) : null;
}
/**
* Get the block hash.
*/
public get blockHash(): Uint8Array | null {
return this._blockHash ? new Uint8Array(this._blockHash) : null;
}
/**
* Get the executed transactions hash.
*/
public get executedTransactionsHash(): Uint8Array | null {
return this._executedTransactionsHash ? new Uint8Array(this._executedTransactionsHash) : null;
}
/**
* Get the hash.
*/
public get hash(): Uint8Array {
return new Uint8Array(this._hash);
}
/**
* Get the previous hash.
*/
public get previousHash(): Uint8Array | null {
return this._previousHash ? new Uint8Array(this._previousHash) : null;
}
/**
* Get the summary value.
*/
public get summaryValue(): Uint8Array {
return new Uint8Array(this._summaryValue);
}
/**
* @returns {bigint} Wire-format version of this input record.
*/
public get version(): bigint {
return InputRecord.VERSION;
}
/**
* Create InputRecord from CBOR bytes.
*
* @param bytes CBOR bytes
* @return input record
*/
public static fromCBOR(bytes: Uint8Array): InputRecord {
const tag = CborDeserializer.decodeTag(bytes);
if (tag.tag !== InputRecord.CBOR_TAG) {
throw new CborError(`Invalid CBOR tag for InputRecord: ${tag.tag}`);
}
const data = CborDeserializer.decodeArray(tag.data, 10);
const version = CborDeserializer.decodeUnsignedInteger(data[0]);
if (version !== InputRecord.VERSION) {
throw new CborError(`Unsupported InputRecord version: ${version}`);
}
return new InputRecord(
CborDeserializer.decodeUnsignedInteger(data[1]),
CborDeserializer.decodeUnsignedInteger(data[2]),
CborDeserializer.decodeNullable(data[3], CborDeserializer.decodeByteString),
CborDeserializer.decodeByteString(data[4]),
CborDeserializer.decodeByteString(data[5]),
CborDeserializer.decodeUnsignedInteger(data[6]),
CborDeserializer.decodeNullable(data[7], CborDeserializer.decodeByteString),
CborDeserializer.decodeUnsignedInteger(data[8]),
CborDeserializer.decodeNullable(data[9], CborDeserializer.decodeByteString),
);
}
/**
* Convert InputRecord to CBOR bytes.
*
* @return CBOR bytes
*/
public toCBOR(): Uint8Array {
return CborSerializer.encodeTag(
InputRecord.CBOR_TAG,
CborSerializer.encodeArray(
CborSerializer.encodeUnsignedInteger(this.version),
CborSerializer.encodeUnsignedInteger(this.roundNumber),
CborSerializer.encodeUnsignedInteger(this.epoch),
CborSerializer.encodeNullable(this._previousHash, CborSerializer.encodeByteString),
CborSerializer.encodeByteString(this._hash),
CborSerializer.encodeByteString(this._summaryValue),
CborSerializer.encodeUnsignedInteger(this.timestamp),
CborSerializer.encodeNullable(this._blockHash, CborSerializer.encodeByteString),
CborSerializer.encodeUnsignedInteger(this.sumOfEarnedFees),
CborSerializer.encodeNullable(this._executedTransactionsHash, CborSerializer.encodeByteString),
),
);
}
/**
* Returns a string representation of the InputRecord.
* @returns The string representation.
*/
public toString(): string {
return dedent`
Input Record
Version: ${this.version.toString()}
Round Number: ${this.roundNumber.toString()}
Epoch: ${this.epoch.toString()}
Previous Hash: ${this._previousHash ? HexConverter.encode(this._previousHash) : 'null'}
Hash: ${HexConverter.encode(this._hash)}
Summary Value: ${HexConverter.encode(this._summaryValue)}
Timestamp: ${this.timestamp.toString()}
Block Hash: ${this._blockHash ? HexConverter.encode(this._blockHash) : 'null'}
Sum of Earned Fees: ${this.sumOfEarnedFees.toString()}
Executed Transactions Hash: ${this._executedTransactionsHash ? HexConverter.encode(this._executedTransactionsHash) : 'null'}`;
}
}