Skip to content

Commit

Permalink
fix: use cesrnumber when parsing sn in rotate event (#257)
Browse files Browse the repository at this point in the history
* fix: use cesrnumber when parsing sn in rotate event

* add unit tests
  • Loading branch information
lenkan authored May 9, 2024
1 parent 2b6a2fd commit 014b2bc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/keri/core/eventing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ export function rotate({
throw new Error(`Invalid ilk = ${ilk} for rot or drt.`);
}

const sner = Number(sn);
if (sner < 1) {
throw new Error(`Invalid sn = 0x${sner.toString()} for rot or drt.`);
const sner = new CesrNumber({}, sn);
if (sner.num < 1) {
throw new Error(`Invalid sn = 0x${sner.numh} for rot or drt.`);
}
let _isit: number;

Expand Down Expand Up @@ -199,7 +199,7 @@ export function rotate({
t: _ilk,
d: '',
i: pre,
s: sner.toString(16),
s: sner.numh,
p: dig,
kt:
tholder.num &&
Expand Down
29 changes: 28 additions & 1 deletion test/core/eventing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import libsodium from 'libsodium-wrappers-sumo';
import { Signer } from '../../src/keri/core/signer';
import { strict as assert } from 'assert';
import { MtrDex } from '../../src/keri/core/matter';
import { incept, messagize } from '../../src/keri/core/eventing';
import { incept, messagize, rotate } from '../../src/keri/core/eventing';
import { Saider } from '../../src/keri/core/saider';
import { Diger } from '../../src/keri/core/diger';
import { b, d, Ilks } from '../../src/keri/core/core';
import { Siger } from '../../src/keri/core/siger';
import { randomBytes } from 'crypto';

describe('key event function', () => {
it('incept should create inception events', async () => {
Expand Down Expand Up @@ -190,4 +191,30 @@ describe('key event function', () => {
'BhfNFh5uk-WxvhsL-AABAABB3MJGmBXxSEryNHw3YwZZLRl_6Ws4Me2WFq8PrQ6WlluSOpPqbwXuiG9RvNWZkqeW8A_0VRjokGMVRZ3m-c0I'
);
});

it('Rotate should create rotation event with hex sequence number', async () => {
await libsodium.ready;

const signer0 = new Signer({ transferable: true });
const signer1 = new Signer({ transferable: true });
const keys0 = [signer0.verfer.qb64];
const ndigs = [new Diger({}, signer1.verfer.qb64b).qb64];
const serder = incept({ keys: keys0, ndigs });

function createRotation(sn: number) {
return rotate({
keys: keys0,
pre: serder.ked.i,
ndigs: serder.ked.n,
sn,
isith: 1,
nsith: 1,
}).ked['s'];
}

assert.equal(createRotation(1), '1');
assert.equal(createRotation(10), 'a');
assert.equal(createRotation(14), 'e');
assert.equal(createRotation(255), 'ff');
});
});

0 comments on commit 014b2bc

Please sign in to comment.