Skip to content
Open
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
15 changes: 10 additions & 5 deletions src/keri/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Version {
/**
* Denotes version 1.0 of a protocol.
*/
export const Vrsn_1_0 = new Version();
export const Vrsn_1_0 = Object.freeze(new Version());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export const Vrsn_1_0 = Object.freeze(new Version());
export const Vrsn_1_0 = Object.freeze(new Version(major: 1, minor: 0));

Since we use this as Version 1.0 global probably better to be explicit.


/**
* Types of KERI and ACDC events.
Expand Down Expand Up @@ -197,7 +197,7 @@ export function deversify(
let kind;
let size;
let proto;
const version = Vrsn_1_0;
let version;

// we need to identify how to match the buffers pattern ,like we do regex matching for strings
const re = new RegExp(VEREX);
Expand All @@ -206,13 +206,18 @@ export function deversify(
const match = re.exec(versionString);

if (match) {
[proto, version.major, version.minor, kind, size] = [
// version digits are hex, as versify writes them
[proto, version, kind, size] = [
match[1],
+match[2],
+match[3],
new Version(parseInt(match[2], 16), parseInt(match[3], 16)),
match[4],
match[5],
];
if (version.major > 1) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should be version.major != 1 as your regex allows for version 0.0.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (version.major > 1) {
if (version.major != 1) {

throw new Error(
`Incompatible version ${version.major}.${version.minor} with version string = ${versionString}`
);
}
if (!Object.values(Serials).includes(kind as Serials)) {
throw new Error(`Invalid serialization kind = ${kind}`);
}
Expand Down
6 changes: 4 additions & 2 deletions src/keri/core/serder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ export function sizeify(
}

const [proto, knd, version] = deversify(ked['v'] as string);
if (version != Vrsn_1_0) {
throw new Error(`unsupported version ${version.toString()}`);
if (version.major !== Vrsn_1_0.major || version.minor !== Vrsn_1_0.minor) {
throw new Error(
`unsupported version ${version.major}.${version.minor}`
);
}

if (kind == undefined) {
Expand Down
42 changes: 40 additions & 2 deletions test/core/serder.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { deversify, Ilks, Serials, Version } from '../../src/keri/core/core.ts';
import {
deversify,
Ilks,
Serials,
Version,
Vrsn_1_0,
} from '../../src/keri/core/core.ts';
import { assert, describe, it } from 'vitest';
import { Salter, Tier } from '../../src/keri/core/salter.ts';
import { MtrDex } from '../../src/keri/core/matter.ts';
import { Diger } from '../../src/keri/core/diger.ts';
import { Serder } from '../../src/keri/core/serder.ts';
import { Serder, sizeify } from '../../src/keri/core/serder.ts';
import libsodium from 'libsodium-wrappers-sumo';
import { Prefixer } from '../../src/keri/core/prefixer.ts';

Expand All @@ -14,6 +20,38 @@ describe('deversify', () => {
assert.deepStrictEqual(version, new Version(1, 0));
assert.equal(size, '00011c');
});

it('should read version digits as hex', async () => {
const [, , version] = deversify('KERI1aJSON00011c_');
assert.deepStrictEqual(version, new Version(1, 10));
});

it('should not alter Vrsn_1_0', async () => {
const [, , version] = deversify('KERI11JSON00011c_');
assert.deepStrictEqual(version, new Version(1, 1));
assert.notStrictEqual(version, Vrsn_1_0);
assert.deepStrictEqual(Vrsn_1_0, new Version(1, 0));
});

it('should reject a major version above 1, as keripy does', async () => {
assert.throws(
() => deversify('KERI20JSON00011c_'),
'Incompatible version 2.0'
);
});

it('should keep Vrsn_1_0 frozen', async () => {
assert.isFrozen(Vrsn_1_0);
});
});

describe('sizeify', () => {
it('should reject a version other than 1.0', async () => {
assert.throws(
() => sizeify({ v: 'KERI11JSON000000_', t: Ilks.icp }),
'unsupported version 1.1'
);
});
});

describe('Serder', () => {
Expand Down
Loading