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..348173d8 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -74,7 +74,12 @@ export {
};
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..49fb121b 100644
--- a/src/lib/getDataFromExternalSources.ts
+++ b/src/lib/getDataFromExternalSources.ts
@@ -101,25 +101,40 @@ 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)) {
return { ...dataEntry, value };
}
+ if (isDataAuthentic(receivedData, urlDataWithHash.verification)) {
+ return { ...dataEntry, value };
+ }
throw new Error('result did not correctly validate');
}
} catch {