Skip to content

Commit

Permalink
feat(core): add cslUtil.bytewiseEquals
Browse files Browse the repository at this point in the history
  • Loading branch information
mkazlauskas committed Oct 14, 2021
1 parent 069f9cb commit a45849e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/core/src/CSL/util.ts
Expand Up @@ -2,3 +2,12 @@ import { CardanoSerializationLib } from './loadCardanoSerializationLib';

export const MAX_U64 = 18_446_744_073_709_551_615n;
export const maxBigNum = (csl: CardanoSerializationLib) => csl.BigNum.from_str(MAX_U64.toString());

export type CslObject = { to_bytes: () => Uint8Array };
export const bytewiseEquals = (obj1: CslObject, obj2: CslObject) => {
if (obj1 === obj2) return true;
const obj1Bytes = obj1.to_bytes();
const obj2Bytes = obj2.to_bytes();
if (obj1Bytes.length !== obj2Bytes.length) return false;
return obj1Bytes.every((byte, idx) => obj2Bytes[idx] === byte);
};
25 changes: 25 additions & 0 deletions packages/core/test/CSL/util.test.ts
@@ -0,0 +1,25 @@
import { cslUtil } from '../../src';

describe('cslUtil', () => {
describe('bytewiseEquals', () => {
it('same bytes => true', () => {
expect(
cslUtil.bytewiseEquals({ to_bytes: () => new Uint8Array([1, 2]) }, { to_bytes: () => new Uint8Array([1, 2]) })
).toBe(true);
});

describe('false', () => {
it('different lengths => false', () => {
expect(
cslUtil.bytewiseEquals({ to_bytes: () => new Uint8Array([1, 2]) }, { to_bytes: () => new Uint8Array([1]) })
).toBe(false);
});

it('different byte => false', () => {
expect(
cslUtil.bytewiseEquals({ to_bytes: () => new Uint8Array([1, 2]) }, { to_bytes: () => new Uint8Array([1, 3]) })
).toBe(false);
});
});
});
});

0 comments on commit a45849e

Please sign in to comment.