Skip to content

Commit f31cafe

Browse files
committed
Add validation of image
1 parent 9540f38 commit f31cafe

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"lint-test": "eslint \"src/**/*\" && prettier --check \"src/**/*\"",
1212
"lint-fix": "eslint --fix \"src/**/*\" && prettier --write \"src/**/*\"",
1313
"validate:token": "ts-node src/scripts/validate-token.ts",
14+
"validate:image": "ts-node src/scripts/validate-image.ts",
1415
"pre-commit": "lint-staged"
1516
},
1617
"pre-commit": [
@@ -52,6 +53,7 @@
5253
"@keplr-wallet/cosmos": "^0.12.12",
5354
"axios": "^1.4.0",
5455
"curve25519-js": "^0.0.4",
56+
"image-size": "^1.1.1",
5557
"joi": "^17.9.2",
5658
"koa": "^2.14.2",
5759
"koa-router": "^12.0.0",

src/scripts/validate-token.ts

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import FS from "fs";
2+
import sizeOf from "image-size";
23
import { CW20TokenScheme } from "../scheme";
3-
import { getChainBaseMap } from "../utils";
4+
import { getChainBaseMap, validateImageUrl } from "../utils";
45
import Path from "path";
5-
import { Bech32Address } from "@keplr-wallet/cosmos";
6+
import { Bech32Address, ChainIdHelper } from "@keplr-wallet/cosmos";
67
import { fetchTokenMetadata } from "../query";
78
import { sortedJsonByKeyStringify } from "@keplr-wallet/common";
89

@@ -67,6 +68,29 @@ import { sortedJsonByKeyStringify } from "@keplr-wallet/common";
6768
)}), contract: ${validated.value.contractAddress}, chain: ${chain})`
6869
);
6970
}
71+
72+
if (validated.value.imageUrl) {
73+
const chainIdentifier = ChainIdHelper.parse(base.chainId).identifier;
74+
const tokenImageUrl = validateImageUrl(
75+
chainIdentifier,
76+
validated.value.imageUrl
77+
);
78+
79+
const dimensions = sizeOf(
80+
`images/${chainIdentifier}/${tokenImageUrl}`
81+
);
82+
83+
if (dimensions.type === "png") {
84+
const width = dimensions.width ?? 0;
85+
const height = dimensions.height ?? 0;
86+
87+
if (width > 512 || height > 512) {
88+
throw new Error(
89+
`Reduce image size to 512x512 or smaller (expected: 512x512, actual: ${width}x${height})`
90+
);
91+
}
92+
}
93+
}
7094
} else {
7195
throw new Error(`Invalid path: ${path}`);
7296
}

src/utils.ts

+16
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,19 @@ export const getChainBaseMap = (
4949

5050
return map;
5151
};
52+
53+
export const validateImageUrl = (
54+
chainIdentifier: string,
55+
url: string
56+
): string => {
57+
const baseURL = `https://raw.githubusercontent.com/chainapsis/keplr-contract-registry/main/images/${chainIdentifier}/`;
58+
59+
if (!url.startsWith(baseURL)) {
60+
throw new Error(`Invalid image url: ${url}`);
61+
}
62+
if (!(url.endsWith(".png") || url.endsWith(".svg"))) {
63+
throw new Error(`Image formats can only be PNG and SVG.`);
64+
}
65+
66+
return url.replace(baseURL, "");
67+
};

yarn.lock

+21
Original file line numberDiff line numberDiff line change
@@ -3316,6 +3316,17 @@ __metadata:
33163316
languageName: node
33173317
linkType: hard
33183318

3319+
"image-size@npm:^1.1.1":
3320+
version: 1.1.1
3321+
resolution: "image-size@npm:1.1.1"
3322+
dependencies:
3323+
queue: 6.0.2
3324+
bin:
3325+
image-size: bin/image-size.js
3326+
checksum: 23b3a515dded89e7f967d52b885b430d6a5a903da954fce703130bfb6069d738d80e6588efd29acfaf5b6933424a56535aa7bf06867e4ebd0250c2ee51f19a4a
3327+
languageName: node
3328+
linkType: hard
3329+
33193330
"import-fresh@npm:^3.0.0, import-fresh@npm:^3.2.1":
33203331
version: 3.3.0
33213332
resolution: "import-fresh@npm:3.3.0"
@@ -3750,6 +3761,7 @@ __metadata:
37503761
eslint: ^8.34.0
37513762
eslint-config-prettier: ^8.6.0
37523763
eslint-plugin-prettier: ^4.2.1
3764+
image-size: ^1.1.1
37533765
joi: ^17.9.2
37543766
koa: ^2.14.2
37553767
koa-router: ^12.0.0
@@ -4802,6 +4814,15 @@ __metadata:
48024814
languageName: node
48034815
linkType: hard
48044816

4817+
"queue@npm:6.0.2":
4818+
version: 6.0.2
4819+
resolution: "queue@npm:6.0.2"
4820+
dependencies:
4821+
inherits: ~2.0.3
4822+
checksum: ebc23639248e4fe40a789f713c20548e513e053b3dc4924b6cb0ad741e3f264dcff948225c8737834dd4f9ec286dbc06a1a7c13858ea382d9379f4303bcc0916
4823+
languageName: node
4824+
linkType: hard
4825+
48054826
"read-package-json@npm:^2.0.0":
48064827
version: 2.1.2
48074828
resolution: "read-package-json@npm:2.1.2"

0 commit comments

Comments
 (0)