Skip to content

Commit

Permalink
fix: Allow LF or CRLF at the end of a JSON file.
Browse files Browse the repository at this point in the history
  • Loading branch information
richtera committed Feb 20, 2024
1 parent 0b23269 commit 321b786
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/lib/ERC725.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class ERC725 {
}

const defaultConfig = {
ipfsGateway: 'https://cloudflare-ipfs.com/ipfs/',
ipfsGateway: 'https://api.universalprofile.cloud/ipfs/',
gas: DEFAULT_GAS_VALUE,
};

Expand Down
19 changes: 13 additions & 6 deletions src/lib/getDataFromExternalSources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,27 @@ 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],
);
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 = Buffer.from(receivedData).toString();
const value = JSON.parse(json);
if (isDataAuthentic(value, urlDataWithHash.verification)) {
Expand Down

0 comments on commit 321b786

Please sign in to comment.