export const ValidDataIndexSchema = Primitives.PositiveIntSchema.pipe(
Schema.brand("ValidDataIndex")
);
export type ValidDataIndex = typeof ValidDataIndexSchema.Type;
export const makeValidDataIndex = (
tree: MerkleTree,
index: Primitives.PositiveInt
): Either.Either<ValidDataIndex, IndexOutOfBoundsError> => {
const leafCount = Chunk.size(tree.leaves);
return index >= leafCount
? Either.left(
new IndexOutOfBoundsError({
message: `Index ${index} out of bounds [0, ${leafCount - 1}]`,
index,
maxIndex: leafCount - 1,
})
)
: Either.right(index as ValidDataIndex);
};
export const ValidDataIndexSchema = Primitives.PositiveIntSchema.pipe(
Schema.brand("ValidDataIndex")
);
export type ValidDataIndex = typeof ValidDataIndexSchema.Type;
export const makeValidDataIndex = (
tree: MerkleTree,
index: Primitives.PositiveInt
): Either.Either<ValidDataIndex, IndexOutOfBoundsError> => {
const leafCount = Chunk.size(tree.leaves);
return index >= leafCount
? Either.left(
new IndexOutOfBoundsError({
message: `Index ${index} out of bounds [0, ${leafCount - 1}]`,
index,
maxIndex: leafCount - 1,
})
)
: Either.right(index as ValidDataIndex);
};