@chainsafe/ssz
Version:
Simple Serialize
39 lines • 2.1 kB
JavaScript
/**
* An SSZ type provides the following operations:
* - Serialization from/to bytes to either a value or a tree
* - Merkelization to compute the hashTreeRoot of both a value and a tree
* - Proof creation from trees
* - Create a View and a ViewDU instance from a tree
* - Manipulate views
*/
export class Type {
/** INTERNAL METHOD: Merkleize value to tree */
value_toTree(value) {
// TODO: Un-performant path but useful for prototyping. Overwrite in Type if performance is important
const uint8Array = new Uint8Array(this.value_serializedSize(value));
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);
}
/** INTERNAL METHOD: Un-merkleize tree to value */
tree_toValue(node) {
// TODO: Un-performant path but useful for prototyping. Overwrite in Type if performance is important
const uint8Array = new Uint8Array(this.tree_serializedSize(node));
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);
}
/** Serialize a value to binary data */
serialize(value) {
const uint8Array = new Uint8Array(this.value_serializedSize(value));
const dataView = new DataView(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength);
this.value_serializeToBytes({ uint8Array, dataView }, 0, value);
return uint8Array;
}
/** Deserialize binary data to value */
deserialize(uint8Array, opts) {
const dataView = new DataView(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength);
return this.value_deserializeFromBytes({ uint8Array, dataView }, 0, uint8Array.length, opts?.reuseBytes);
}
}
//# sourceMappingURL=abstract.js.map