Skip to content

Commit 4ca8c8e

Browse files
CoveMBAniket-Engg
authored andcommitted
Use Textdecoder when decoding code param payload
1 parent 8e7473e commit 4ca8c8e

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

libs/remix-ui/workspace/src/lib/actions/workspace.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -272,20 +272,14 @@ export type UrlParametersType = {
272272
}
273273

274274
/**
275-
* Decode a base64‑encoded string that was produced by
276-
* percent‑escaping UTF‑8 bytes and then encoded with btoa().
275+
* Decode a base64‑encoded string that was produced by TextEncoder with btoa().
277276
*
278277
* @param {string} b64Payload The base64 payload you got from params.code
279-
* @returns {string} The original UTF‑8 string
280278
*/
281-
export const decodePercentEscapedBase64 = (b64Payload: string) => {
282-
const rawByteString = atob(b64Payload);
283-
284-
const percentEscapedString = rawByteString.split('')
285-
.map(c => '%' + c.charCodeAt(0).toString(16).padStart(2, '0'))
286-
.join('')
287-
288-
return decodeURIComponent(percentEscapedString);
279+
export const decodeBase64 = (b64Payload: string) => {
280+
const raw = atob(decodeURIComponent(b64Payload));
281+
const bytes = Uint8Array.from(raw, c => c.charCodeAt(0));
282+
return new TextDecoder().decode(bytes);
289283
}
290284

291285
export const loadWorkspacePreset = async (template: WorkspaceTemplate = 'remixDefault', opts?) => {
@@ -305,7 +299,7 @@ export const loadWorkspacePreset = async (template: WorkspaceTemplate = 'remixDe
305299
const hashed = bytesToHex(hash.keccakFromString(params.code))
306300

307301
path = 'contract-' + hashed.replace('0x', '').substring(0, 10) + (params.language && params.language.toLowerCase() === 'yul' ? '.yul' : '.sol')
308-
content = decodePercentEscapedBase64(params.code)
302+
content = decodeBase64(params.code)
309303
await workspaceProvider.set(path, content)
310304
}
311305
if (params.shareCode) {

0 commit comments

Comments
 (0)