Skip to content

Commit

Permalink
Encode bytes in Context params to hex (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec authored Oct 26, 2023
2 parents 4922a89 + dbe13ed commit 331116f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
14 changes: 7 additions & 7 deletions packages/shared/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export const toBase64 = (bytes: Uint8Array): string =>
export const fromBase64 = (str: string): Uint8Array =>
Uint8Array.from(atob(str), (c) => c.charCodeAt(0));

export const base64ToU8Receiver = (_key: string, value: unknown) => {
if (typeof value === 'string' && value.startsWith('base64:')) {
return fromBase64(value.split('base64:')[1]);
export const hexToU8Receiver = (_key: string, value: unknown) => {
if (typeof value === 'string' && value.startsWith('0x')) {
return fromHexString(value);
}
return value;
};
Expand All @@ -44,22 +44,22 @@ const sortedReplacer = (_key: string, value: unknown) => {
return value;
};

const u8ToBase64Replacer = (_key: string, value: unknown) => {
const u8ToHexReplacer = (_key: string, value: unknown) => {
if (value instanceof Uint8Array) {
return `base64:${toBase64(value)}`;
return `0x${toHexString(value)}`;
}
return value;
};

const sortedSerializingReplacer = (_key: string, value: unknown): unknown => {
const serializedValue = u8ToBase64Replacer(_key, value);
const serializedValue = u8ToHexReplacer(_key, value);
return sortedReplacer(_key, serializedValue);
};

export const toJSON = (obj: unknown) =>
JSON.stringify(obj, sortedSerializingReplacer);

export const fromJSON = (json: string) => JSON.parse(json, base64ToU8Receiver);
export const fromJSON = (json: string) => JSON.parse(json, hexToU8Receiver);

export const zip = <T, Z>(
a: ReadonlyArray<T>,
Expand Down
15 changes: 15 additions & 0 deletions packages/taco/test/conditions/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fakeProvider, fakeSigner } from '@nucypher/test-utils';
import { ethers } from 'ethers';
import { beforeAll, describe, expect, it } from 'vitest';

import { toBytes, toHexString } from '../../src';
import {
ConditionExpression,
ContractCondition,
Expand Down Expand Up @@ -65,6 +66,20 @@ describe('context', () => {
const conditionExpr = new ConditionExpression(contractCondition);
const context = conditionExpr.buildContext(provider, {}, signer);

describe('custom parameters', () => {
it("serializes bytes as hex strings", async () => {
const customParamsWithBytes: Record<string, CustomContextParam> = {};
const customParam = toBytes('hello');
// Uint8Array is not a valid CustomContextParam, override the type:
customParamsWithBytes[customParamKey] = customParam as unknown as string;

const asJson = await context.withCustomParams(customParamsWithBytes).toJson();
const asObj = JSON.parse(asJson);
expect(asObj).toBeDefined();
expect(asObj[customParamKey]).toEqual(`0x${toHexString(customParam)}`);
});
});

describe('return value test', () => {
it('accepts on a custom context parameters', async () => {
const asObj = await context.withCustomParams(customParams).toObj();
Expand Down

0 comments on commit 331116f

Please sign in to comment.