Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion __tests__/cbor.tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { hex } from 'buffer-tag';

import { cborDecode, cborEncode, DataItem } from '../src/cbor';
import { cborDecode, cborEncode, DataItem, getCborEncodeDecodeOptions, setCborEncodeDecodeOptions } from '../src/cbor';

describe('cbor', () => {
it('should properly decode a nested map', () => {
Expand All @@ -16,6 +16,18 @@ describe('cbor', () => {
const decoded = cborDecode(encoded);
const reEncode = cborEncode(decoded);
expect(reEncode.toString('hex')).toBe(encoded.toString('hex'));
expect(encoded[3].toString(16)).toBe('b9'); // Large Map
});

it('should properly encoded and decoded maps using variableMapSize=true', () => {
const options = getCborEncodeDecodeOptions();
options.variableMapSize = true;
setCborEncodeDecodeOptions(options);
const encoded = cborEncode(DataItem.fromData({ foo: 'baz' }));
const decoded = cborDecode(encoded);
const reEncode = cborEncode(decoded);
expect(reEncode.toString('hex')).toBe(encoded.toString('hex'));
expect(encoded[3].toString(16)).toBe('a1'); // Map with one item
});

it('should properly encoded and decoded with arrays', () => {
Expand Down
10 changes: 9 additions & 1 deletion src/cbor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class DateOnly extends Date {
}
}

const encoderDefaults: Options = {
let encoderDefaults: Options = {
tagUint8Array: false,
useRecords: false,
mapsAsObjects: false,
Expand All @@ -59,6 +59,14 @@ addExtension({
decode: (isoStringDate: any): Object => new DateOnly(isoStringDate),
});

export const getCborEncodeDecodeOptions = () : Options => {
return encoderDefaults;
};

export const setCborEncodeDecodeOptions = (options: Options) : void => {
encoderDefaults = options;
};

export const cborDecode = (
input: Buffer | Uint8Array,
options: Options = encoderDefaults,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export { DeviceSignedDocument } from './mdoc/model/DeviceSignedDocument';
export { DeviceResponse } from './mdoc/model/DeviceResponse';
export { MDLError, MDLParseError } from './mdoc/errors';
export { VerificationAssessmentId } from './mdoc/checkCallback';
export { getCborEncodeDecodeOptions, setCborEncodeDecodeOptions } from './cbor';