Skip to content

Commit 0311619

Browse files
authored
fix: use variable map size for map encoding (#85)
Signed-off-by: Timo Glastra <[email protected]>
1 parent fd7cc00 commit 0311619

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

.changeset/seven-pears-relate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@animo-id/mdoc": minor
3+
---
4+
5+
fix: use variable map size for map encoding

src/cbor/parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const encoderDefaults: CborOptions = {
1010
useRecords: false,
1111
mapsAsObjects: false,
1212
unwrapTopLevelDataItem: true,
13+
variableMapSize: true,
1314
}
1415

1516
export const cborDecode = <T>(input: Uint8Array, options: CborOptions = encoderDefaults): T => {

tests/cbor.tests.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,27 @@ describe('cbor', () => {
1313
expect(decoded.get('foo')?.data.get('bar')).toBe('baz')
1414
})
1515

16+
it('should properly encoded and decoded maps (length <= 23)', () => {
17+
const length = 23
18+
const obj = Object.fromEntries(Array.from({ length }, (_, i) => [`key${i}`, i]))
19+
const encoded = cborEncode(DataItem.fromData(obj))
20+
const decoded = cborDecode(encoded, { unwrapTopLevelDataItem: false })
21+
const reEncode = cborEncode(decoded)
22+
expect(compareBytes(reEncode, encoded)).toBeTruthy()
23+
expect(encoded[4].toString(16)).toBe((0xa0 + length).toString(16))
24+
})
25+
26+
it('should properly encoded and decoded maps (length >= 23)', () => {
27+
const length = 24
28+
const obj = Object.fromEntries(Array.from({ length }, (_, i) => [`key${i}`, i]))
29+
const encoded = cborEncode(DataItem.fromData(obj))
30+
const decoded = cborDecode(encoded, { unwrapTopLevelDataItem: false })
31+
const reEncode = cborEncode(decoded)
32+
expect(compareBytes(reEncode, encoded)).toBeTruthy()
33+
expect(encoded[4].toString(16)).toBe('b8')
34+
expect(encoded[5].toString(16)).toBe(length.toString(16))
35+
})
36+
1637
it('should properly encoded and decoded maps', () => {
1738
const encoded = cborEncode(DataItem.fromData({ foo: 'baz' }))
1839
const decoded = cborDecode(encoded, { unwrapTopLevelDataItem: false })

tests/models/validity-info.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ValidityInfo } from '../../src/mdoc/models/validity-info'
33
import { hex } from '../../src/utils'
44

55
const cbor =
6-
'b90003667369676e6564c074323032302d31302d30315431333a33303a30325a6976616c696446726f6dc074323032302d31302d30315431333a33303a30325a6a76616c6964556e74696cc074323032312d31302d30315431333a33303a30325a'
6+
'a3667369676e6564c074323032302d31302d30315431333a33303a30325a6976616c696446726f6dc074323032302d31302d30315431333a33303a30325a6a76616c6964556e74696cc074323032312d31302d30315431333a33303a30325a'
77

88
describe('validity info', () => {
99
test('parse', () => {

0 commit comments

Comments
 (0)