[Internal] Add check for tags field (array) (#5960)

* Add check for tags field (array)

* Lint fix

* Using reduce() instead of forEach() for checking all values

Co-authored-by: Catenocrypt <catenocrypt@users.noreply.github.com>
This commit is contained in:
Adam R 2021-03-18 00:27:21 +01:00 committed by GitHub
parent 5adea7d775
commit 63c1dcaf1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 15 deletions

View File

@ -4,23 +4,14 @@
"source_code": "https://github.com/trustwallet/",
"white_paper": "",
"description": "Utility token to increase adoption of cryptocurrency.",
"socials": [
{
"name": "Twitter",
"url": "https://twitter.com/trustwalletapp",
"handle": "TrustWalletApp"
},
{
"name": "Reddit",
"url": "https://reddit.com/r/trustapp",
"handle": "TrustApp"
}
],
"explorer": "https://bscscan.com/token/0x4B0F1812e5Df2A09796481Ff14017e6005508003",
"research": "https://research.binance.com/en/projects/trustwallet",
"type": "BEP20",
"symbol": "TWT",
"decimals": 18,
"status": "active",
"id": "0x4B0F1812e5Df2A09796481Ff14017e6005508003"
"id": "0x4B0F1812e5Df2A09796481Ff14017e6005508003",
"tags": [
"dao"
]
}

View File

@ -10,6 +10,7 @@ import { isValidJSON, readJsonFile, writeJsonFile } from "../generic/json";
import { ActionInterface, CheckStepInterface } from "../generic/interface";
import { CoinType } from "@trustwallet/wallet-core";
import { isValidStatusValue } from "../generic/status-values";
import { isValidTagValues } from "../generic/tag-values";
import * as bluebird from "bluebird";
const requiredKeys = ["name", "type", "symbol", "decimals", "description", "website", "explorer", "status", "id"];
@ -67,7 +68,14 @@ function isAssetInfoValid(info: unknown, path: string, address: string, chain: s
// status
if (!isValidStatusValue(info['status'])) {
return [`Invalid value for status field, '${info['status']}'`, "", fixedInfo]
return [`Invalid value for status field, '${info['status']}'`, "", fixedInfo];
}
// tags
if (info['tags']) {
if (!isValidTagValues(info['tags'])) {
return [`Invalid tags, '${info['tags']}'`, "", fixedInfo];
}
}
const isKeys2CorrectType =

View File

@ -0,0 +1,19 @@
import { readJsonFile } from "../generic/json";
const tags: any = readJsonFile("script/tags.json") as any;
export function isValidTagValue(value: string): boolean {
//console.log(`isValidTagValue ${value}`);
if (!value) {
return false;
}
if (!(value in tags)) {
return false;
}
//console.log(`TAG ${tags[value]['name']}`);
return true;
}
export function isValidTagValues(values: string[]): boolean {
return values.reduce((accum: boolean, value: string) => accum && isValidTagValue(value), true);
}

34
script/tags.json Normal file
View File

@ -0,0 +1,34 @@
{
"stablecoin": {
"name": "Stablecoin",
"description": "Tokens that are fixed to an external asset, e.g. the US dollar."
},
"wrapped": {
"name": "Wrapped",
"description": "Tokens that are wrapped or peg representation of digital assets. Excluded stablecoins"
},
"synthetics": {
"name": "Synthetics",
"description": "Synthetic assets created to track the value of another asset"
},
"nft": {
"name": "NFT",
"description": "Non-fungible tokens or tokens associated with the NFT ecosystem."
},
"dao": {
"name": "DAO",
"description": "Tokens that govern decentralized autonomous organizations (DAOs)."
},
"defi": {
"name": "DeFi",
"description": null
},
"creator": {
"name": "Creator",
"description": "Tokens by creators."
},
"staking": {
"name": "Staking",
"description": "Tokens that are used for staking to receive rewards."
}
}

View File

@ -22,6 +22,7 @@ import {
} from "../script/generic/types";
import { findImagesToFetch } from "../script/blockchain/binance";
import { isValidStatusValue } from "../script/generic/status-values";
import { isValidTagValue, isValidTagValues } from "../script/generic/tag-values";
describe("Test eth-address helpers", () => {
test(`Test isChecksum`, () => {
@ -136,7 +137,7 @@ describe("Test blockchain binance", () => {
});
});
describe("Test status-values", () => {
describe("Test status, tag values", () => {
test(`Test status-values`, () => {
expect(isValidStatusValue("active")).toEqual(true);
expect(isValidStatusValue("abandoned")).toEqual(true);
@ -144,4 +145,17 @@ describe("Test status-values", () => {
expect(isValidStatusValue("ACTIVE")).toEqual(false);
expect(isValidStatusValue("")).toEqual(false);
});
test(`Test tag-values`, () => {
expect(isValidTagValue("defi")).toEqual(true);
expect(isValidTagValue("dao")).toEqual(true);
expect(isValidStatusValue("invalidvalue")).toEqual(false);
expect(isValidStatusValue("DAO")).toEqual(false);
expect(isValidStatusValue("")).toEqual(false);
expect(isValidTagValues(["defi"])).toEqual(true);
expect(isValidTagValues(["dao"])).toEqual(true);
expect(isValidTagValues(["defi", "dao"])).toEqual(true);
expect(isValidTagValues(["invalid"])).toEqual(false);
expect(isValidTagValues(["defi", "invalid"])).toEqual(false);
});
});