You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-desktop/ts/session/messages/outgoing/preconditions.ts

38 lines
864 B
TypeScript

import { isEmpty } from 'lodash';
import { PubKey } from '../../types';
import { PreConditionFailed } from '../../utils/errors';
function checkUin8tArrayOrThrow({
context,
data,
expectedLength,
varName,
}: {
data: Uint8Array;
expectedLength: number;
varName: string;
context: string;
}) {
if (isEmpty(data) || data.length !== expectedLength) {
throw new PreConditionFailed(
`${varName} length should be ${expectedLength} for ctx:"${context}"`
);
}
}
function checkArrayHaveOnly05Pubkeys({
context,
arr,
varName,
}: {
arr: Array<string>;
varName: string;
context: string;
}) {
if (arr.some(v => !PubKey.is05Pubkey(v))) {
throw new PreConditionFailed(`${varName} did not contain only 05 pubkeys for ctx:"${context}"`);
}
}
export const Preconditions = { checkUin8tArrayOrThrow, checkArrayHaveOnly05Pubkeys };