From dbe13ed029016c1b95f63f3a55aa359310a771b9 Mon Sep 17 00:00:00 2001
From: Piotr Roslaniec
Date: Wed, 25 Oct 2023 10:12:46 +0200
Subject: [PATCH] fix(conditions): encode bytes in context params to hex
---
packages/shared/src/utils.ts | 14 +++++++-------
packages/taco/test/conditions/context.test.ts | 15 +++++++++++++++
2 files changed, 22 insertions(+), 7 deletions(-)
diff --git a/packages/shared/src/utils.ts b/packages/shared/src/utils.ts
index 918fc11e0..f9f67bdd5 100644
--- a/packages/shared/src/utils.ts
+++ b/packages/shared/src/utils.ts
@@ -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;
};
@@ -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 = (
a: ReadonlyArray,
diff --git a/packages/taco/test/conditions/context.test.ts b/packages/taco/test/conditions/context.test.ts
index dd9064f50..6a1e85965 100644
--- a/packages/taco/test/conditions/context.test.ts
+++ b/packages/taco/test/conditions/context.test.ts
@@ -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,
@@ -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 = {};
+ 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();