UNPKG

1.76 kBPlain TextView Raw
1import { BinaryWriter, HeaderJSON, InvalidFormatError, IOHelper, UInt256 } from '@neo-one/client-common-esnext-esm';
2import { BlockBase, BlockBaseAdd } from './BlockBase';
3import {
4 DeserializeWireBaseOptions,
5 DeserializeWireOptions,
6 SerializableJSON,
7 SerializableWire,
8 SerializeJSONContext,
9} from './Serializable';
10import { BinaryReader, utils } from './utils';
11
12export type HeaderAdd = BlockBaseAdd;
13export interface HeaderKey {
14 readonly hashOrIndex: UInt256 | number;
15}
16
17export class Header extends BlockBase implements SerializableWire<Header>, SerializableJSON<HeaderJSON> {
18 public static deserializeWireBase(options: DeserializeWireBaseOptions): Header {
19 const { reader } = options;
20 const blockBase = super.deserializeBlockBaseWireBase(options);
21 if (reader.readUInt8() !== 0) {
22 throw new InvalidFormatError();
23 }
24
25 return new this({
26 version: blockBase.version,
27 previousHash: blockBase.previousHash,
28 merkleRoot: blockBase.merkleRoot,
29 timestamp: blockBase.timestamp,
30 index: blockBase.index,
31 consensusData: blockBase.consensusData,
32 nextConsensus: blockBase.nextConsensus,
33 script: blockBase.script,
34 });
35 }
36
37 public static deserializeWire(options: DeserializeWireOptions): Header {
38 return this.deserializeWireBase({
39 context: options.context,
40 reader: new BinaryReader(options.buffer),
41 });
42 }
43
44 protected readonly sizeExclusive: () => number = utils.lazy(() => IOHelper.sizeOfUInt8);
45
46 public serializeWireBase(writer: BinaryWriter): void {
47 super.serializeWireBase(writer);
48 writer.writeUInt8(0);
49 }
50
51 public async serializeJSON(context: SerializeJSONContext): Promise<HeaderJSON> {
52 return super.serializeBlockBaseJSON(context);
53 }
54}