Skip to content

Commit 57dead2

Browse files
CoveMBAniket-Engg
authored andcommitted
Retrive special character that could be present allongside unicode notation in the base64 code params
1 parent aaffedc commit 57dead2

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,23 @@ export type UrlParametersType = {
266266
ghfolder: string
267267
}
268268

269+
/**
270+
* Decode a base64‑encoded string that was produced by
271+
* percent‑escaping UTF‑8 bytes and then encoded with btoa().
272+
*
273+
* @param {string} b64Payload The base64 payload you got from params.code
274+
* @returns {string} The original UTF‑8 string
275+
*/
276+
export const decodePercentEscapedBase64 = (b64Payload: string) => {
277+
const rawByteString = atob(b64Payload);
278+
279+
const percentEscapedString = rawByteString.split('')
280+
.map(c => '%' + c.charCodeAt(0).toString(16).padStart(2, '0'))
281+
.join('')
282+
283+
return decodeURIComponent(percentEscapedString);
284+
}
285+
269286
export const loadWorkspacePreset = async (template: WorkspaceTemplate = 'remixDefault', opts?) => {
270287
const workspaceProvider = plugin.fileProviders.workspace
271288
const electronProvider = plugin.fileProviders.electron
@@ -283,7 +300,7 @@ export const loadWorkspacePreset = async (template: WorkspaceTemplate = 'remixDe
283300
const hashed = bytesToHex(hash.keccakFromString(params.code))
284301

285302
path = 'contract-' + hashed.replace('0x', '').substring(0, 10) + (params.language && params.language.toLowerCase() === 'yul' ? '.yul' : '.sol')
286-
content = atob(decodeURIComponent(params.code))
303+
content = decodePercentEscapedBase64(params.code)
287304
await workspaceProvider.set(path, content)
288305
}
289306
if (params.shareCode) {
@@ -586,7 +603,7 @@ export const uploadFile = async (target, targetFolder: string, cb?: (err: Error,
586603
// the files module. Please ask the user here if they want to overwrite
587604
// a file and then just use `files.add`. The file explorer will
588605
// pick that up via the 'fileAdded' event from the files module.
589-
;[...target.files].forEach(async (file) => {
606+
[...target.files].forEach(async (file) => {
590607
const workspaceProvider = plugin.fileProviders.workspace
591608
const name = targetFolder === '/' ? file.name : `${targetFolder}/${file.name}`
592609

0 commit comments

Comments
 (0)