Skip to content

Commit

Permalink
Move check to where buffer is used
Browse files Browse the repository at this point in the history
  • Loading branch information
imx-mikhala committed Jul 1, 2024
1 parent fc2761c commit aac022a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
22 changes: 11 additions & 11 deletions packages/passport/sdk/src/utils/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,36 @@ import { hexToString } from './string';

describe('string', () => {
describe('hexToString', () => {
it('should return hex if it is not a valid hex', () => {
it('should return hex if it is not a valid hex', async () => {
const hex = '0x123';
const hex2 = 'test';

expect(hexToString(hex)).toEqual(hex);
expect(hexToString(hex2)).toEqual(hex2);
expect(await hexToString(hex)).toEqual(hex);
expect(await hexToString(hex2)).toEqual(hex2);
});

it('should return utf8 string if it is a valid utf8', () => {
it('should return utf8 string if it is a valid utf8', async () => {
const hex = '0x68656c6c6f20776f726c64';

expect(hexToString(hex)).toEqual('hello world');
expect(await hexToString(hex)).toEqual('hello world');
});

it('should return utf8 string if it is a valid utf8 with leading zeros', () => {
it('should return utf8 string if it is a valid utf8 with leading zeros', async () => {
const hex = '0x0068656c6c6f20776f726c64'; // 'hello world' with leading zero

expect(hexToString(hex)).toEqual('hello world');
expect(await hexToString(hex)).toEqual('hello world');
});

it('should return empty string if input is an empty string', () => {
it('should return empty string if input is an empty string', async () => {
const hex = '';

expect(hexToString(hex)).toEqual('');
expect(await hexToString(hex)).toEqual('');
});

it('should return hex if it is a valid hex but not a valid utf8', () => {
it('should return hex if it is a valid hex but not a valid utf8', async () => {
const hex = '0x1234567890abcdef'; // valid hex but not a valid utf8

expect(hexToString(hex)).toEqual(hex);
expect(await hexToString(hex)).toEqual(hex);
});
});
});
8 changes: 7 additions & 1 deletion packages/passport/sdk/src/utils/string.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { utils } from 'ethers';

export const hexToString = (hex: string) => {
export const hexToString = async (hex: string) => {
if (!hex) return hex;

try {
if (typeof window !== 'undefined' && !window.Buffer) {
// Use dynamic import to load Buffer
const bufferModule = await import('buffer');
window.Buffer = bufferModule.Buffer;
}

const stripped = utils.stripZeros(utils.arrayify(hex));
const buff = Buffer.from(stripped);
return buff.length === 32 ? hex : utils.toUtf8String(stripped);
Expand Down
8 changes: 1 addition & 7 deletions packages/passport/sdk/src/zkEvm/personalSign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,7 @@ export const personalSign = async ({
throw new JsonRpcError(RpcErrorCode.INVALID_PARAMS, 'personal_sign requires the signer to be the from address');
}

if (typeof window !== 'undefined' && !window.Buffer) {
// Use dynamic import to load Buffer
const bufferModule = await import('buffer');
window.Buffer = bufferModule.Buffer;
}

const payload = hexToString(message);
const payload = await hexToString(message);
const { chainId } = await rpcProvider.detectNetwork();
flow.addEvent('endDetectNetwork');
const chainIdBigNumber = BigNumber.from(chainId);
Expand Down

0 comments on commit aac022a

Please sign in to comment.