diff --git a/packages/passport/sdk/src/utils/string.test.ts b/packages/passport/sdk/src/utils/string.test.ts index 7bf836251b..2139a13fe2 100644 --- a/packages/passport/sdk/src/utils/string.test.ts +++ b/packages/passport/sdk/src/utils/string.test.ts @@ -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); }); }); }); diff --git a/packages/passport/sdk/src/utils/string.ts b/packages/passport/sdk/src/utils/string.ts index 499f2e109e..15a15acf48 100644 --- a/packages/passport/sdk/src/utils/string.ts +++ b/packages/passport/sdk/src/utils/string.ts @@ -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); diff --git a/packages/passport/sdk/src/zkEvm/personalSign.ts b/packages/passport/sdk/src/zkEvm/personalSign.ts index 53e90f9d96..369ad2b742 100644 --- a/packages/passport/sdk/src/zkEvm/personalSign.ts +++ b/packages/passport/sdk/src/zkEvm/personalSign.ts @@ -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);