1 | import { IOHelper, JSONHelper, WitnessJSON, WitnessModel } from '@neo-one/client-common-esnext-esm';
|
2 | import { Equals, EquatableKey } from './Equatable';
|
3 | import {
|
4 | DeserializeWireBaseOptions,
|
5 | DeserializeWireOptions,
|
6 | SerializableJSON,
|
7 | SerializeJSONContext,
|
8 | } from './Serializable';
|
9 | import { BinaryReader, utils } from './utils';
|
10 |
|
11 | export interface WitnessAdd {
|
12 | readonly verification: Buffer;
|
13 | readonly invocation: Buffer;
|
14 | }
|
15 |
|
16 | export class Witness extends WitnessModel implements SerializableJSON<WitnessJSON>, EquatableKey {
|
17 | public static deserializeWireBase({ reader }: DeserializeWireBaseOptions): Witness {
|
18 | const invocation = reader.readVarBytesLE(utils.USHORT_MAX_NUMBER_PLUS_ONE);
|
19 | const verification = reader.readVarBytesLE(utils.USHORT_MAX_NUMBER_PLUS_ONE);
|
20 |
|
21 | return new this({ invocation, verification });
|
22 | }
|
23 |
|
24 | public static deserializeWire(options: DeserializeWireOptions): Witness {
|
25 | return this.deserializeWireBase({
|
26 | context: options.context,
|
27 | reader: new BinaryReader(options.buffer),
|
28 | });
|
29 | }
|
30 |
|
31 | public static fromModel(witness: WitnessModel): Witness {
|
32 | return new this({ invocation: witness.invocation, verification: witness.verification });
|
33 | }
|
34 |
|
35 | public readonly equals: Equals = utils.equals(
|
36 | Witness,
|
37 | this,
|
38 | (other) => this.invocation.equals(other.invocation) && this.verification.equals(other.verification),
|
39 | );
|
40 | public readonly toKeyString = utils.toKeyString(
|
41 | Witness,
|
42 | () => `${this.invocation.toString('hex')}:${this.verification.toString('hex')}`,
|
43 | );
|
44 | private readonly sizeInternal = utils.lazy(
|
45 | () => IOHelper.sizeOfVarBytesLE(this.invocation) + IOHelper.sizeOfVarBytesLE(this.verification),
|
46 | );
|
47 |
|
48 | public get size(): number {
|
49 | return this.sizeInternal();
|
50 | }
|
51 |
|
52 | public serializeJSON(_context: SerializeJSONContext): WitnessJSON {
|
53 | return {
|
54 | invocation: JSONHelper.writeBuffer(this.invocation),
|
55 | verification: JSONHelper.writeBuffer(this.verification),
|
56 | };
|
57 | }
|
58 | }
|