Skip to content

Commit

Permalink
fix onchain metadata decoding error
Browse files Browse the repository at this point in the history
  • Loading branch information
mdtanrikulu committed Oct 24, 2024
1 parent b206fdb commit a10cf06
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
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 IPNS_SUBPATH = '/ipns/';
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 @@ function _isValidBase64(uri: string) {
}

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

0 comments on commit a10cf06

Please sign in to comment.