From 17458d623f214f026307184357bcf7ff75fad7ed Mon Sep 17 00:00:00 2001 From: Andreas Richter <708186+richtera@users.noreply.github.com> Date: Fri, 23 Feb 2024 09:50:59 -0500 Subject: [PATCH 1/3] fix: Re-do PR with minimal changes --- .gitignore | 3 ++- package-lock.json | 4 ++-- src/index.ts | 8 ++++++-- src/lib/getDataFromExternalSources.ts | 24 ++++++++++++++++++------ 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 267e82ff..522238a6 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ docs/html .vscode/* .nyc_output coverage -*.tgz \ No newline at end of file +*.tgz +.DS_Store diff --git a/package-lock.json b/package-lock.json index ae367a79..d734fda1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@erc725/erc725.js", - "version": "0.17.2", + "version": "0.21.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@erc725/erc725.js", - "version": "0.17.2", + "version": "0.21.3", "license": "Apache-2.0", "dependencies": { "add": "^2.0.6", diff --git a/src/index.ts b/src/index.ts index d797aa50..fb4f9357 100644 --- a/src/index.ts +++ b/src/index.ts @@ -73,8 +73,12 @@ export { ERC725JSONSchemaValueType, }; -export { ERC725Config, KeyValuePair, ProviderTypes } from './types'; -export { encodeData } from './lib/utils'; +export { encodeData, encodeArrayKey } from './lib/utils'; +export { decodeData } from './lib/decodeData'; +export { encodeKeyName } from './lib/encodeKeyName'; +export { decodeMappingKey } from './lib/decodeMappingKey'; +export { decodeValueType, decodeValueContent } from './lib/encoder'; +export { getDataFromExternalSources } from './lib/getDataFromExternalSources'; /** * This package is currently in early stages of development,
use only for testing or experimentation purposes.
diff --git a/src/lib/getDataFromExternalSources.ts b/src/lib/getDataFromExternalSources.ts index 809d58c6..5d5e4721 100644 --- a/src/lib/getDataFromExternalSources.ts +++ b/src/lib/getDataFromExternalSources.ts @@ -101,20 +101,32 @@ export const getDataFromExternalSources = ( // - check whether those could represent valid JSON data. // - then validate the data as JSON // - then verfiy the data against the verification method - const key = String.fromCharCode( - receivedData[0], - receivedData[receivedData.length - 1], - ); + + // Improved JSON detection. We now check the first and up to the last 3 bytes. + // The 3 bytes can be `]` or '}' with either SPACE, LF or CRLF at the end. + // When editing JSON using a text editor, a lot of time it's pretty printed + // and an empty line added to the end. This is a common pattern. + const capture: number[] = []; + capture.push(receivedData[0]); + if (receivedData.length > 3) { + capture.push(receivedData[receivedData.length - 3]); + } + if (receivedData.length > 2) { + capture.push(receivedData[receivedData.length - 2]); + } + if (receivedData.length > 1) { + capture.push(receivedData[receivedData.length - 1]); + } + const key = String.fromCharCode.apply(null, capture); // Currently not supported even though they could be added and can represent valid JSON. // " " => JSON.stringify("") NOT SUPPORTED as valid JSON // t or f and e => JSON.stringify(true) or JSON.stringify(false) NOT SUPPORTED as valid JSON // 0-9 => JSON.stringify(0) integer or float (note .5 is not legitimate JSON) NOT SUPPORTED as valid JSON // if (/^(\[\]|\{\}|(tf)e|\d\d)$/.test(key)) { - // Check if the beginning or end are // { and } => JSON.stringify({...}) => pretty much 100% of our JSON will be this. // [ and ] => JSON.stringify([...]) - if (/^(\[\]|\{\})$/.test(key)) { + if (/^(\[.*\]|\{.*\})\s*$/.test(key)) { const json = arrToBufArr(receivedData).toString(); const value = JSON.parse(json); if (isDataAuthentic(value, urlDataWithHash.verification)) { From d13be00ba77be924f914cba1283e9c37250ec9bd Mon Sep 17 00:00:00 2001 From: Andreas Richter <708186+richtera@users.noreply.github.com> Date: Fri, 23 Feb 2024 10:01:52 -0500 Subject: [PATCH 2/3] fix: Missing line --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index fb4f9357..348173d8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -73,6 +73,7 @@ export { ERC725JSONSchemaValueType, }; +export { ERC725Config, KeyValuePair, ProviderTypes } from './types'; export { encodeData, encodeArrayKey } from './lib/utils'; export { decodeData } from './lib/decodeData'; export { encodeKeyName } from './lib/encodeKeyName'; From aa68bf132d2e4793b4da784c30ae8f306560da8c Mon Sep 17 00:00:00 2001 From: Andreas Richter <708186+richtera@users.noreply.github.com> Date: Fri, 23 Feb 2024 10:26:29 -0500 Subject: [PATCH 3/3] fix: Add check for pretty printed and non-pretty printed versions of JSON --- src/lib/getDataFromExternalSources.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/lib/getDataFromExternalSources.ts b/src/lib/getDataFromExternalSources.ts index 5d5e4721..49fb121b 100644 --- a/src/lib/getDataFromExternalSources.ts +++ b/src/lib/getDataFromExternalSources.ts @@ -132,6 +132,9 @@ export const getDataFromExternalSources = ( if (isDataAuthentic(value, urlDataWithHash.verification)) { return { ...dataEntry, value }; } + if (isDataAuthentic(receivedData, urlDataWithHash.verification)) { + return { ...dataEntry, value }; + } throw new Error('result did not correctly validate'); } } catch {