Skip to content

Commit

Permalink
add tests for utils serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
lenkan committed Nov 8, 2023
1 parent c0137c9 commit 244aafa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
6 changes: 1 addition & 5 deletions src/keri/app/credentialing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import { Serder } from '../core/serder';
import { Siger } from '../core/siger';
import { TextDecoder } from 'util';
import { TraitDex } from './habery';
import { Prefixer } from '../core/prefixer';
import { Seqner } from '../core/seqner';
import {
serializeACDCAttachment,
serializeIssExnAttachment,
Expand Down Expand Up @@ -776,9 +774,7 @@ export class Ipex {
i: recp,
};

let prefixer = new Prefixer({ raw: b(acdc.raw) });
let seqner = new Seqner({ sn: acdc.sn });
let acdcAtc = d(serializeACDCAttachment(prefixer, seqner, acdcSaider));
let acdcAtc = d(serializeACDCAttachment(acdc, acdcSaider));
let issAtc = d(serializeIssExnAttachment(anc, issSaider));

let embeds: any = {
Expand Down
28 changes: 4 additions & 24 deletions src/keri/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Seqner } from './seqner';
import { Prefixer } from './prefixer';
import { Saider } from './saider';
import { Serder } from './serder';
import { b } from './core';

export function pad(n: any, width = 3, z = 0) {
return (String(z).repeat(width) + String(n)).slice(String(n).length);
Expand Down Expand Up @@ -116,33 +117,12 @@ export function bytesToInt(ar: Uint8Array): number {
return value;
}

export function serialize(
creder: any,
prefixer: any,
seqner: any,
saider: any
) {
let craw = creder.raw;
let ctr = new Counter({ code: CtrDex.SealSourceTriples, count: 1 }).qb64b;
let prefix = prefixer.qb64b;
let seq = seqner.qb64b;
let said = saider.qb64b;
let newCraw = new Uint8Array(
craw.length + ctr.length + prefix.length + seq.length + said.length
);
newCraw.set(craw);
newCraw.set(ctr, craw.length);
newCraw.set(prefix, craw.length + ctr.length);
newCraw.set(seq, craw.length + ctr.length + prefix.length);
newCraw.set(said, craw.length + ctr.length + prefix.length + seq.length);
return newCraw;
}

export function serializeACDCAttachment(
prefixer: Prefixer,
seqner: Seqner,
acdc: Serder,
saider: Saider
): Uint8Array {
let prefixer = new Prefixer({ raw: b(acdc.raw) });
let seqner = new Seqner({ sn: acdc.sn });
let craw = new Uint8Array();
let ctr = new Counter({ code: CtrDex.SealSourceTriples, count: 1 }).qb64b;
let prefix = prefixer.qb64b;
Expand Down
38 changes: 38 additions & 0 deletions test/core/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Ident, Saider, Serder, Serials, d, versify } from '../../src';
import {
serializeACDCAttachment,
serializeIssExnAttachment,
} from '../../src/keri/core/utils';

describe(serializeIssExnAttachment, () => {
it('serializes iss data', () => {
const [saider, data] = Saider.saidify({
d: '',
v: versify(Ident.KERI, undefined, Serials.JSON, 0),
});

const result = serializeIssExnAttachment(new Serder(data), saider);

expect(d(result)).toEqual(
'-VAS-GAB0AAAAAAAAAAAAAAAAAAAAAAAEKZPmzJqhx76bcC2ftPQgeRirmOd8ZBOtGVqHJrSm7F1'
);
});
});

describe(serializeACDCAttachment, () => {
it('serializes acdc data', () => {
const [saider, data] = Saider.saidify({
d: '',
v: versify(Ident.ACDC, undefined, Serials.JSON, 0),
a: {
LEI: '123',
},
});

const result = serializeACDCAttachment(new Serder(data), saider);

expect(d(result)).toEqual(
'-IABBHsiZCI6IkVORTZzbWw4X1NMZVIzdk9NajRJRExLX2Nn0AAAAAAAAAAAAAAAAAAAAAAAENE6sml8_SLeR3vOMj4IDLK_cgd-A-vtg0Jnu7ozdBjW'
);
});
});

0 comments on commit 244aafa

Please sign in to comment.