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

fix onchain metadata decoding error #54

Merged
merged 1 commit into from
Oct 24, 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
6 changes: 4 additions & 2 deletions example/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ avt
})
.catch(console.log);

avt
.getHeader(ensName)
try {
avt
.getHeader(ensName, { jsdomWindow: jsdom })
.then(header => {
console.log('header: ', header);
})
.catch(console.log);
} catch {}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.2",
"version": "1.0.3",
"license": "MIT",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand Down
3 changes: 2 additions & 1 deletion src/specs/erc1155.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export default class ERC1155 {
'base64'
).toString();
}
return JSON.parse(_resolvedUri);
const metadata = JSON.parse(_resolvedUri);
return { ...metadata, is_owner: isOwner };
}

const marketplaceKey = getMarketplaceAPIKey(resolvedURI, options);
Expand Down
2 changes: 1 addition & 1 deletion src/specs/erc721.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class ERC721 {
'base64'
).toString();
}
const metadata = JSON.parse(decodeURI(_resolvedUri));
const metadata = JSON.parse(_resolvedUri);
return { ...metadata, is_owner: isOwner };
}
const response = await fetch(
Expand Down
11 changes: 6 additions & 5 deletions src/utils/resolveURI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
const networkRegex = /(?<protocol>ipfs:\/|ipns:\/|ar:\/)?(?<root>\/)?(?<subpath>ipfs\/|ipns\/)?(?<target>[\w\-.]+)(?<subtarget>\/.*)?/;
const base64Regex = /^data:([a-zA-Z\-/+]*);base64,([^"].*)/;
const dataURIRegex = /^data:([a-zA-Z\-/+]*)?(;[a-zA-Z0-9].*?)?(,)/;
const JSON_MIMETYPE = 'data:application/json;';

function _getImageMimeType(uri: string) {
const base64Data = uri.replace(base64Regex, '$2');
Expand Down Expand Up @@ -49,11 +50,11 @@
}

const [header, str] = uri.split('base64,');

const mimeType = _getImageMimeType(uri);

if (!mimeType || !header.includes(mimeType)) {
return false;
if (header != JSON_MIMETYPE) {

Check warning on line 53 in src/utils/resolveURI.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Expected '!==' and instead saw '!='

Check warning on line 53 in src/utils/resolveURI.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and windows-latest

Expected '!==' and instead saw '!='

Check warning on line 53 in src/utils/resolveURI.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and macOS-latest

Expected '!==' and instead saw '!='

Check warning on line 53 in src/utils/resolveURI.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 20.x and ubuntu-latest

Expected '!==' and instead saw '!='

Check warning on line 53 in src/utils/resolveURI.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 20.x and windows-latest

Expected '!==' and instead saw '!='

Check warning on line 53 in src/utils/resolveURI.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 20.x and macOS-latest

Expected '!==' and instead saw '!='
const mimeType = _getImageMimeType(uri);
if (!mimeType || !header.includes(mimeType)) {
return false;
}
}

// length must be multiple of 4
Expand Down
Loading