@@ -2,6 +2,7 @@ import * as didJwsz6Mkf from '../../../../../core/src/crypto/__tests__/__fixture
22import * as didJwsz6Mkv from '../../../../../core/src/crypto/__tests__/__fixtures__/didJwsz6Mkv'
33import { JsonEncoder } from '../../../../../core/src/utils/JsonEncoder'
44import { JsonTransformer } from '../../../../../core/src/utils/JsonTransformer'
5+ import { TypedArrayEncoder } from '../../../../../core/src/utils/TypedArrayEncoder'
56import { DidCommAttachment , DidCommAttachmentData } from '../DidCommAttachment'
67
78const mockJson = {
@@ -96,6 +97,60 @@ describe('Decorators | DidCommAttachment', () => {
9697 expect ( mockJson . data . json ) . toEqual ( gotData )
9798 } )
9899
100+ describe ( 'getDataAsUint8Array' , ( ) => {
101+ // Bytes chosen so their base64 encoding exercises both `+` and `/` characters
102+ // (the ones that differ between the standard and url-safe alphabets) *and*
103+ // requires `=` padding. This way the url-alphabet / no-padding variants
104+ // below are genuinely different strings from the canonical base64, not
105+ // no-ops.
106+ const payload = new Uint8Array ( [ 0xff , 0xe0 , 0x3f , 0xff , 0xfe ] )
107+ const padded = TypedArrayEncoder . toBase64 ( payload ) // has '+', '/', and '=' padding
108+ const unpadded = padded . replace ( / = + $ / , '' )
109+ const urlPadded = padded . replace ( / \+ / g, '-' ) . replace ( / \/ / g, '_' )
110+ const urlUnpadded = unpadded . replace ( / \+ / g, '-' ) . replace ( / \/ / g, '_' )
111+
112+ test . each ( [
113+ [ 'standard base64 with padding' , padded ] ,
114+ [ 'base64url with padding' , urlPadded ] ,
115+ [ 'base64url without padding' , urlUnpadded ] ,
116+ ] ) ( 'decodes %s' , ( _label , encoded ) => {
117+ const attachment = new DidCommAttachment ( {
118+ id : 'some-uuid' ,
119+ data : new DidCommAttachmentData ( { base64 : encoded } ) ,
120+ } )
121+ expect ( attachment . getDataAsUint8Array ( ) ) . toEqual ( payload )
122+ } )
123+
124+ it ( 'throws when no base64 payload is present' , ( ) => {
125+ const attachment = new DidCommAttachment ( {
126+ id : 'some-uuid' ,
127+ data : new DidCommAttachmentData ( { json : { hello : 'world' } } ) ,
128+ } )
129+ expect ( ( ) => attachment . getDataAsUint8Array ( ) ) . toThrow ( / N o b a s e 6 4 a t t a c h m e n t d a t a f o u n d / )
130+ } )
131+
132+ it ( 'throws a clear error for genuinely invalid input' , ( ) => {
133+ const attachment = new DidCommAttachment ( {
134+ id : 'some-uuid' ,
135+ data : new DidCommAttachmentData ( { base64 : 'this is definitely not base64 $$$' } ) ,
136+ } )
137+ expect ( ( ) => attachment . getDataAsUint8Array ( ) ) . toThrow (
138+ / C o u l d n o t d e c o d e a t t a c h m e n t d a t a a s b a s e 6 4 u r l o r b a s e 6 4 s t r i n g /
139+ )
140+ } )
141+
142+ it ( 'getDataAsJson delegates to getDataAsUint8Array, accepting base64url-without-padding too' , ( ) => {
143+ const json = { hello : 'world' }
144+ const standardPadded = JsonEncoder . toBase64 ( json )
145+ const urlNoPad = standardPadded . replace ( / \+ / g, '-' ) . replace ( / \/ / g, '_' ) . replace ( / = + $ / , '' )
146+ const attachment = new DidCommAttachment ( {
147+ id : 'some-uuid' ,
148+ data : new DidCommAttachmentData ( { base64 : urlNoPad } ) ,
149+ } )
150+ expect ( attachment . getDataAsJson ( ) ) . toEqual ( json )
151+ } )
152+ } )
153+
99154 describe ( 'addJws' , ( ) => {
100155 it ( 'correctly adds the jws to the data' , async ( ) => {
101156 const base64 = JsonEncoder . toBase64 ( didJwsz6Mkf . DATA_JSON )
0 commit comments