From cbb7cb06063ec937a2a1a335951987cd38164775 Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Mon, 17 Jan 2022 19:25:04 +0100 Subject: [PATCH] Use consistent initialization in DataView --- packages/ssz/src/type/abstract.ts | 10 +++++----- packages/ssz/src/type/bitList.ts | 2 +- packages/ssz/src/type/bitVector.ts | 4 ++-- packages/ssz/src/type/byteList.ts | 2 +- packages/ssz/src/type/byteVector.ts | 2 +- packages/ssz/src/type/containerNodeStruct.ts | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/ssz/src/type/abstract.ts b/packages/ssz/src/type/abstract.ts index baea008b..689e2787 100644 --- a/packages/ssz/src/type/abstract.ts +++ b/packages/ssz/src/type/abstract.ts @@ -109,7 +109,7 @@ export abstract class Type { // Un-performant path but useful for testing and prototyping value_toTree(value: V): Node { const uint8Array = new Uint8Array(this.value_serializedSize(value)); - const dataView = new DataView(uint8Array.buffer); + const dataView = new DataView(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength); this.value_serializeToBytes({uint8Array, dataView}, 0, value); return this.tree_deserializeFromBytes({uint8Array, dataView}, 0, uint8Array.length); } @@ -117,7 +117,7 @@ export abstract class Type { // Un-performant path but useful for testing and prototyping tree_toValue(node: Node): V { const uint8Array = new Uint8Array(this.tree_serializedSize(node)); - const dataView = new DataView(uint8Array.buffer); + const dataView = new DataView(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength); this.tree_serializeToBytes({uint8Array, dataView}, 0, node); return this.value_deserializeFromBytes({uint8Array, dataView}, 0, uint8Array.length); } @@ -126,13 +126,13 @@ export abstract class Type { serialize(value: V): Uint8Array { const uint8Array = new Uint8Array(this.value_serializedSize(value)); - const dataView = new DataView(uint8Array.buffer); + const dataView = new DataView(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength); this.value_serializeToBytes({uint8Array, dataView}, 0, value); return uint8Array; } deserialize(uint8Array: Uint8Array): V { - const dataView = new DataView(uint8Array.buffer); + const dataView = new DataView(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength); return this.value_deserializeFromBytes({uint8Array, dataView}, 0, uint8Array.length); } @@ -167,7 +167,7 @@ export abstract class BasicType extends Type { hashTreeRoot(value: V): Uint8Array { const uint8Array = new Uint8Array(32); - const dataView = new DataView(uint8Array.buffer); + const dataView = new DataView(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength); this.value_serializeToBytes({uint8Array, dataView}, 0, value); return uint8Array; } diff --git a/packages/ssz/src/type/bitList.ts b/packages/ssz/src/type/bitList.ts index 2bbe7092..0c6383f0 100644 --- a/packages/ssz/src/type/bitList.ts +++ b/packages/ssz/src/type/bitList.ts @@ -134,7 +134,7 @@ export class BitListType extends CompositeType { commitViewDU(view: Uint8Array): Node { const uint8Array = new Uint8Array(this.value_serializedSize(view)); - const dataView = new DataView(uint8Array.buffer); + const dataView = new DataView(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength); this.value_serializeToBytes({uint8Array, dataView}, 0, view); return this.tree_deserializeFromBytes({uint8Array, dataView}, 0, uint8Array.length); } diff --git a/packages/ssz/src/type/byteVector.ts b/packages/ssz/src/type/byteVector.ts index 52cf20bf..3497a5ef 100644 --- a/packages/ssz/src/type/byteVector.ts +++ b/packages/ssz/src/type/byteVector.ts @@ -53,7 +53,7 @@ export class ByteVectorType extends CompositeType // TODO: Optimize conversion private valueToTree(value: ValueOfFields): Node { const uint8Array = new Uint8Array(this.value_serializedSize(value)); - const dataView = new DataView(uint8Array.buffer); + const dataView = new DataView(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength); this.value_serializeToBytes({uint8Array, dataView}, 0, value); return super.tree_deserializeFromBytes({uint8Array, dataView}, 0, uint8Array.length); }