Files
libsignal/node/ts/zkgroup/internal/ByteArray.ts
2026-03-16 18:55:17 -07:00

50 lines
1.2 KiB
TypeScript

//
// Copyright 2020-2021 Signal Messenger, LLC.
// SPDX-License-Identifier: AGPL-3.0-only
//
import { LibSignalErrorBase } from '../../Errors.js';
import * as Native from '../../Native.js';
export const UNCHECKED_AND_UNCLONED: unique symbol = Symbol();
export default class ByteArray {
contents: Uint8Array<ArrayBuffer>;
protected constructor(
contents: Uint8Array<ArrayBuffer>,
checkValid:
| ((contents: Uint8Array<ArrayBuffer>) => void)
| typeof UNCHECKED_AND_UNCLONED
) {
if (checkValid === UNCHECKED_AND_UNCLONED) {
this.contents = contents;
} else {
checkValid.call(Native, contents);
this.contents = Uint8Array.from(contents);
}
}
protected static checkLength(
expectedLength: number
): (contents: Uint8Array<ArrayBuffer>) => void {
return (contents) => {
if (contents.length !== expectedLength) {
throw new LibSignalErrorBase(
`Length of array supplied was ${contents.length} expected ${expectedLength}`,
undefined,
this.name
);
}
};
}
public getContents(): Uint8Array<ArrayBuffer> {
return this.contents;
}
public serialize(): Uint8Array<ArrayBuffer> {
return Uint8Array.from(this.contents);
}
}