Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release: merge to main #388

Merged
merged 7 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
14.17.0
18.19.0
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
Loading