-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
238 lines (202 loc) · 7.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Node imports
import fs from 'fs'
import fsPromises from 'fs/promises'
import os from 'os'
import path from 'path'
// NPM imports
import { Qyu } from 'qyu'
// Local module imports
import util from './util.cjs'
import imagemin from 'imagemin'
import imageminPngquant from 'imagemin-pngquant'
function getOutputTokenPath(stagingDir, inputTokenFilePath) {
const tokenFilename = path.parse(inputTokenFilePath).base
return path.join(stagingDir, tokenFilename)
}
function stageTokenFile(stagingDir, inputTokenFilePath) {
fs.copyFileSync(inputTokenFilePath, getOutputTokenPath(stagingDir, inputTokenFilePath))
}
async function stageMainnetTokenFile(stagingDir, inputTokenFilePath) {
// Read in the JSON file located at inputTokenFilePath
const tokenList = JSON.parse(fs.readFileSync(inputTokenFilePath, 'utf-8'))
// Ex.
// {
// "0xBBc2AE13b23d715c30720F079fcd9B4a74093505": {
// "name": "Ethernity Chain Token",
// "logo": "ERN.png",
// "erc20": true,
// "symbol": "ERN",
// "decimals": 18
// },
// ...
// }
const outputTokenList = await util.generateMainnetTokenList(tokenList)
// Write the output token list to the staging directory
fs.writeFileSync(getOutputTokenPath(stagingDir, inputTokenFilePath), JSON.stringify(outputTokenList, null, 2))
}
function stagePackageJson(stagingDir) {
const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf-8'))
packageJson.name = 'brave-wallet-lists'
packageJson.scripts = {}
packageJson.devDependencies = {}
packageJson.dependencies = {}
packageJson.engines = {}
fs.writeFileSync(path.join(stagingDir, 'package.json'), JSON.stringify(packageJson, null, 2));
}
async function stageChainListJson(stagingDir) {
const srcPath = path.join('data', 'chainlist', 'chainlist.json')
const dstPath = path.join(stagingDir, 'chainlist.json')
fsPromises.copyFile(srcPath, dstPath)
}
function stageManifest(stagingDir) {
const manifestPath = path.join('data', 'manifest.json');
const outputManifestPath = path.join(stagingDir, 'manifest.json');
fs.copyFileSync(manifestPath, outputManifestPath)
}
async function stageEVMTokenImages(stagingDir, inputTokenFilePath, addExtraTokens = false) {
const outputTokenFilePath = getOutputTokenPath(stagingDir, inputTokenFilePath);
const baseSrcTokenPath = path.dirname(inputTokenFilePath)
// Copy images and convert them to png plus resize to 200x200 if needed
const imagesSrcPath = path.join(baseSrcTokenPath, "images")
const imagesDstPath = path.join(stagingDir, "imagesUncompressed")
const files = fs.readdirSync(imagesSrcPath)
if (!fs.existsSync(imagesDstPath)){
fs.mkdirSync(imagesDstPath)
}
for (var i = 0; i < files.length; i++) {
var file = files[i]
var fileTo = file.substr(0, file.lastIndexOf(".")) + ".png"
var fromPath = path.join(imagesSrcPath, file)
var toPath = path.join(imagesDstPath, fileTo)
if (fs.existsSync(toPath)) {
try {
fs.unlinkSync(toPath)
} catch (e) {
console.log(`Failed to remove: ${toPath}`)
}
}
try {
await util.saveToPNGResize(fromPath, toPath, false)
} catch (e) {
console.log(`Failed to resize: ${fromPath}`)
}
}
util.contractReplaceSvgToPng(outputTokenFilePath)
// We can remove this later if we migrate the tokens to
// the evm-contract-map.json.
// We can't do this yet because we need
// to supporpt old builds that are using the token file.
// This can be done after April 2022.
if (addExtraTokens) {
util.contractAddExtraAssetIcons(outputTokenFilePath, imagesDstPath)
}
}
async function compressPng(imagesDstPath, stagingDir) {
console.log('compressing png images...')
if (!fs.existsSync(stagingDir + '/images')) {
fs.mkdirSync(stagingDir + '/images')
}
await imagemin([imagesDstPath + "/*.png"], {
destination: stagingDir + '/images',
plugins: [
imageminPngquant({
quality: [0.1, 0.3]
})
]
})
}
async function stageTokenListsLogo(stagingDir, token) {
const { address, logoURI } = token
// NFTs do not have the logoURI field.
if (!logoURI) {
return ''
}
const isRemoteURL = logoURI.startsWith('https://') || logoURI.startsWith('http://')
if (!isRemoteURL) {
return logoURI
}
const extension = path.extname(logoURI.split('?')[0])
const sourceFile = `${address}${extension}`
const destFile = `${address}.png`
const sourceFilePath = path.join(os.tmpdir(), sourceFile)
try {
await util.download(logoURI, sourceFile)
} catch (err) {
console.log(err)
return ''
}
// Add a delay to prevent coingecko asset CDN from forcing a JS security
// challenge.
await new Promise(r => setTimeout(r, 800))
try {
await util.saveToPNGResize(
sourceFilePath,
path.join(stagingDir, 'imagesUncompressed', destFile),
false,
)
} catch {
return ''
}
return destFile
}
async function stageTokenListsTokens(stagingDir, tokens, isEVM = true) {
// Use an asynchronous job queue to throttle downloads.
const q = new Qyu({concurrency: 2})
q(tokens, async (token, idx) => {
tokens[idx].logoURI = await stageTokenListsLogo(stagingDir, token)
})
await q.whenEmpty()
return tokens
.reduce((acc, token) => {
const result = {
name: token.name,
logo: token.logoURI,
erc20: isEVM,
symbol: token.symbol,
decimals: token.decimals,
chainId: `0x${token.chainId.toString(16)}`
}
if (token.extensions && token.extensions.coingeckoId) {
result.coingeckoId = token.extensions.coingeckoId
}
return {
...acc,
[token.address]: result
}
}, {})
}
async function stageSPLTokens(stagingDir) {
const tokenMap = JSON.parse(fs.readFileSync(path.join('data', 'solana', 'tokenlist.json')).toString())
const splTokensArray = tokenMap.tokens
const splTokens = await stageTokenListsTokens(stagingDir, splTokensArray, false)
const splTokensPath = path.join(stagingDir, 'solana-contract-map.json')
fs.writeFileSync(splTokensPath, JSON.stringify(splTokens, null, 2))
}
async function stageTokenPackage() {
const stagingDir = 'build'
if (!fs.existsSync(stagingDir)) {
fs.mkdirSync(stagingDir)
}
const imagesDstPath = path.join(stagingDir, "imagesUncompressed")
if (!fs.existsSync(imagesDstPath)){
fs.mkdirSync(imagesDstPath)
}
// Add MetaMask tokens for contract-map.json
const metamaskTokenPath = path.join('node_modules', '@metamask', 'contract-metadata', 'contract-map.json');
await stageMainnetTokenFile(stagingDir, metamaskTokenPath)
await stageEVMTokenImages(stagingDir, metamaskTokenPath, true)
// Add Brave specific tokens in evm-contract-map.json
const braveTokenPath = path.join('data', 'evm-contract-map', 'evm-contract-map.json');
stageTokenFile(stagingDir, braveTokenPath)
await stageEVMTokenImages(stagingDir, braveTokenPath)
// Add Solana (SPL) tokens in solana-contract-map.json.
await stageSPLTokens(stagingDir)
await compressPng(imagesDstPath, stagingDir)
fs.rmSync(imagesDstPath, { recursive: true, force: true });
// Add chainlist.json.
await stageChainListJson(stagingDir)
stagePackageJson(stagingDir)
stageManifest(stagingDir)
}
util.installErrorHandlers();
stageTokenPackage()