Skip to content

Commit

Permalink
Fix Base64 serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva committed Apr 18, 2023
1 parent 8ca03ef commit 2756a69
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-grapes-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@metaplex-foundation/umi': patch
---

Fix Base64 serializer
24 changes: 21 additions & 3 deletions packages/umi/src/utils/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,27 @@ export const base58: Serializer<string> = baseX(
* A string serializer that uses base64 encoding.
* @category Serializers
*/
export const base64: Serializer<string> = baseX(
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
);
export const base64: Serializer<string> = {
description: 'base64',
fixedSize: null,
maxSize: null,
serialize(value: string) {
try {
return new Uint8Array(
atob(value)
.split('')
.map((c) => c.charCodeAt(0))
);
} catch (e) {
throw new InvalidBaseStringError(value, 64);
}
},
deserialize(buffer, offset = 0) {
const slice = buffer.slice(offset);
const value = btoa(String.fromCharCode.apply(null, [...slice]));
return [value, buffer.length];
},
};

/**
* A string serializer that uses base16 encoding.
Expand Down
35 changes: 26 additions & 9 deletions packages/umi/test/utils/bytes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,16 @@ test('it can serialize base 64 strings', (t) => {
t.deepEqual(base64.serialize(''), new Uint8Array([]));
t.deepEqual(base64.deserialize(new Uint8Array([])), ['', 0]);

t.deepEqual(base64.serialize('A'), new Uint8Array([0]));
t.deepEqual(base64.deserialize(new Uint8Array([0])), ['A', 1]);
t.deepEqual(base64.serialize('AA'), new Uint8Array([0]));
t.deepEqual(base64.serialize('AA='), new Uint8Array([0]));
t.deepEqual(base64.serialize('AA=='), new Uint8Array([0]));
t.deepEqual(base64.deserialize(new Uint8Array([0])), ['AA==', 1]);

t.deepEqual(base64.serialize('B'), new Uint8Array([1]));
t.deepEqual(base64.deserialize(new Uint8Array([1])), ['B', 1]);
t.deepEqual(base64.serialize('AQ=='), new Uint8Array([1]));
t.deepEqual(base64.deserialize(new Uint8Array([1])), ['AQ==', 1]);

t.deepEqual(base64.serialize('AA'), new Uint8Array([0, 0]));
t.deepEqual(base64.deserialize(new Uint8Array([0, 0])), ['AA', 2]);

t.deepEqual(base64.serialize('q'), new Uint8Array([42]));
t.deepEqual(base64.deserialize(new Uint8Array([42])), ['q', 1]);
t.deepEqual(base64.serialize('Kg'), new Uint8Array([42]));
t.deepEqual(base64.deserialize(new Uint8Array([42])), ['Kg==', 1]);

const sentence = 'TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu';
const bytes = new Uint8Array([
Expand All @@ -144,6 +143,24 @@ test('it can serialize base 64 strings', (t) => {
message: (m) =>
m.includes('Expected a string of base 64, got [INVALID_INPUT].'),
});

t.throws(() => base64.serialize('A'), {
message: (m) => m.includes('Expected a string of base 64, got [A].'),
});

const base64TokenData =
'AShNrkm2joOHhfQnRCzfSbrtDUkUcJSS7PJryR4PPjsnyyIWxL0ESVFoE7QWBowtz2B/iTtUGdb2EEyKbLuN5gEAAAAAAAAAAQAAAGCtpnOhgF7t+dM8By+nG51mKI9Dgb0RtO/6xvPX1w52AgAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
const base16TokenData =
'01284dae49b68e838785f427442cdf49baed0d4914709492ecf26bc91e0f3e3b27cb2216c4bd0449516813b416068c2dcf607f893b5419d6f6104c8a6cbb8de601000000000000000100000060ada673a1805eedf9d33c072fa71b9d66288f4381bd11b4effac6f3d7d70e76020000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000';

t.deepEqual(
base16.deserialize(base64.serialize(base64TokenData))[0],
base16TokenData
);
t.deepEqual(
base64.deserialize(base16.serialize(base16TokenData))[0],
base64TokenData
);
});

test('it can serialize bit arrays', (t) => {
Expand Down

0 comments on commit 2756a69

Please sign in to comment.