Skip to content

Commit

Permalink
Merge pull request #385 from ERC725Alliance/fix/minimal-updates
Browse files Browse the repository at this point in the history
fix: Fix LF JSON parsing issue and add some exports (no upgrades or other changes)
  • Loading branch information
richtera authored Feb 23, 2024
2 parents e46dfd8 + aa68bf1 commit 1796043
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ docs/html
.vscode/*
.nyc_output
coverage
*.tgz
*.tgz
.DS_Store
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, <br/>use only for testing or experimentation purposes.<br/>
Expand Down
27 changes: 21 additions & 6 deletions src/lib/getDataFromExternalSources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1796043

Please sign in to comment.