-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathutils.js
More file actions
486 lines (425 loc) · 14.5 KB
/
utils.js
File metadata and controls
486 lines (425 loc) · 14.5 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import {
Bech32, fromBase64, fromHex, toHex,
} from '@cosmjs/encoding'
import { sha256, stringToPath } from '@cosmjs/crypto'
// ledger
import TransportWebBLE from '@ledgerhq/hw-transport-web-ble'
import TransportWebUSB from '@ledgerhq/hw-transport-webusb'
import CosmosApp from 'ledger-cosmos-js'
import { LedgerSigner } from '@cosmjs/ledger-amino'
import dayjs from 'dayjs'
import duration from 'dayjs/plugin/duration'
import relativeTime from 'dayjs/plugin/relativeTime'
import utc from 'dayjs/plugin/utc'
import RIPEMD160 from 'ripemd160'
import localeData from 'dayjs/plugin/localeData'
import { $themeColors } from '@themeConfig'
// import { SigningStargateClient } from '@cosmjs/stargate'
import PingWalletClient from './data/signing'
dayjs.extend(localeData)
dayjs.extend(duration)
dayjs.extend(relativeTime)
dayjs.extend(utc)
export function getLocalObject(name) {
const text = localStorage.getItem(name)
if (text) {
return JSON.parse(text)
}
return null
}
export function getLocalChains() {
return getLocalObject('chains')
}
export function getLocalAccounts() {
return getLocalObject('accounts')
}
export function getLocalTxHistory() {
return getLocalObject('txHistory')
}
export function setLocalTxHistory(tx) {
const newTx = tx
const txs = getLocalTxHistory()
if (txs) {
txs.push(newTx)
return localStorage.setItem('txHistory', JSON.stringify(txs))
}
return localStorage.setItem('txHistory', JSON.stringify([newTx]))
}
export async function connectLedger(transport = 'usb') {
const trans = await transport === 'usb' ? TransportWebUSB.create() : TransportWebBLE.create()
return new CosmosApp(trans)
}
export function operatorAddressToAccount(operAddress) {
const { prefix, data } = Bech32.decode(operAddress)
if (prefix === 'iva') { // handle special cases
return Bech32.encode('iaa', data)
}
if (prefix === 'crocncl') { // handle special cases
return Bech32.encode('cro', data)
}
return Bech32.encode(prefix.replace('valoper', ''), data)
}
// TODO, not tested
export function pubkeyToAccountAddress(pubkey, prefix) {
return Bech32.encode(prefix, pubkey, 40)
}
export function addressDecode(address) {
return Bech32.decode(address)
}
export function addressEnCode(prefix, pubkey) {
return Bech32.encode(prefix, pubkey)
}
export function getUserCurrency() {
const currency = localStorage.getItem('currency')
return currency || 'usd'
}
export function setUserCurrency(currency) {
localStorage.setItem('currency', currency)
}
export function chartColors() {
const colors = [
'#8A2BE2', '#9ACD32', '#000080', '#008080', '#DC143C',
'#7FFFD4', '#B8860B', '#EEE8AA', '#FFFAFA', '#FDF5E6',
'#C0C0C0', '#E6E6FA', '#FFFAF0', '#2E8B57', '#DCDCDC',
'#FF1493', '#4682B4', '#191970', '#FF8C00', '#FFFFE0',
'#696969', '#FFFACD', '#DEB887', '#4169E1', '#9932CC',
'#B0C4DE', '#556B2F', '#FFE4E1', '#F5FFFA', '#8FBC8F',
'#B22222', '#90EE90', '#FFFF00', '#4B0082', '#DB7093',
'#F8F8FF', '#006400', '#6610f2', '#FFA500', '#7FFF00',
'#87CEFA', '#5F9EA0', '#483D8B', '#CD5C5C', '#ADFF2F',
'#2F4F4F', '#00FF7F', '#FFF5EE', '#F4A460', '#808000',
'#000000', '#00FA9A', '#000000', '#EE82EE', '#F5DEB3',
'#0000FF', '#BA55D3', '#FFF0F5', '#F5F5DC', '#0000CD',
'#FFD700', '#708090', '#6B8E23', '#800000', '#7B68EE',
'#FFA07A', '#800080', '#B0E0E6', '#00FFFF', '#00BFFF',
'#7CFC00', '#778899', '#FF7F50', '#E0FFFF', '#6495ED',
'#008B8B', '#DDA0DD', '#CD853F', '#FFFFF0', '#98FB98',
'#9400D3', '#D2691E', '#FF0000', '#008000', '#00008B',
'#C71585', '#FFB6C1', '#8B4513', '#20c997', '#FAEBD7',
'#E9967A', '#FFEFD5', '#FFE4C4', '#D8BFD8', '#A52A2A',
'#8B0000', '#32CD32', '#BDB76B', '#FF0000', '#DAA520',
'#800000', '#9370DB', '#F08080', '#FAF0E6', '#FF6347',
'#FF4500', '#FFFF00', '#808080', '#00CED1', '#FFC0CB',
'#FF00FF', '#F0FFFF', '#A9A9A9', '#F0E68C', '#1E90FF',
'#FFDAB9', '#228B22', '#F0FFF0', '#66CDAA', '#ADD8E6',
'#DA70D6', '#A0522D', '#FFE4B5', '#48D1CC', '#D2B48C',
'#FFEBCD', '#8B008B', '#3CB371', '#87CEEB', '#6A5ACD',
'#FFDEAD', '#FF69B4', '#BC8F8F', '#D3D3D3', '#00FF00',
'#FAFAD2', '#AFEEEE', '#40E0D0', '#FFF8DC', '#20B2AA',
'#00FFFF', '#FA8072', '#F0F8FF']
return Object.values($themeColors).concat(colors)
}
export function extractAccountNumberAndSequence(ret) {
let account = ret.value
if (ret.value && ret.value.base_vesting_account) { // vesting account
account = ret.value.base_vesting_account?.base_account
} else if (ret.value && ret.value.base_account) { // evmos based account
account = ret.value.base_account
}
const accountNumber = account.account_number
const sequence = account?.sequence || 0
return {
accountNumber,
sequence,
}
}
export function getUserCurrencySign() {
let s = ''
switch (getUserCurrency()) {
case 'cny':
case 'jpy':
s = '¥'
break
case 'krw':
s = '₩'
break
case 'eur':
s = '€'
break
default:
s = '$'
}
return s
}
export function consensusPubkeyToHexAddress(consensusPubkey) {
let raw = null
if (typeof consensusPubkey === 'object') {
if (consensusPubkey.type === 'tendermint/PubKeySecp256k1') {
raw = new RIPEMD160().update(Buffer.from(sha256(fromBase64(consensusPubkey.value)))).digest('hex').toUpperCase()
return raw
}
raw = sha256(fromBase64(consensusPubkey.value))
} else {
raw = sha256(fromHex(toHex(Bech32.decode(consensusPubkey).data).toUpperCase().replace('1624DE6420', '')))
}
const address = toHex(raw).slice(0, 40).toUpperCase()
return address
}
function toSignAddress(addr) {
const { data } = addressDecode(addr)
return addressEnCode('cosmos', data)
}
function getHdPath(address) {
let hdPath = "m/44'/118/0'/0/0"
Object.values(getLocalAccounts()).forEach(item => {
const curr = item.address.find(i => i.addr === address)
if (curr && curr.hdpath) {
hdPath = curr.hdpath
}
})
// return [44, 118, 0, 0, 0]
// m/0'/1/2'/2/1000000000
return stringToPath(hdPath)
}
export async function sign(device, chainId, signerAddress, messages, fee, memo, signerData, signOpts) {
let transport
let signer
switch (device) {
case 'ledgerBle':
transport = await TransportWebBLE.create()
signer = new LedgerSigner(transport, { hdPaths: [getHdPath(signerAddress)] })
break
case 'ledgerUSB':
transport = await TransportWebUSB.create()
signer = new LedgerSigner(transport, { hdPaths: [getHdPath(signerAddress)] })
break
case 'pingKMS':
if (!window.PingSigner) {
throw new Error('Please install Ping KMS extension')
}
signer = window.PingSigner
break
case 'keplr':
default:
if (!window.getOfflineSigner || !window.keplr) {
throw new Error('Please install keplr extension')
}
await window.keplr.enable(chainId)
// signer = window.getOfflineSigner(chainId)
signer = window.getOfflineSignerOnlyAmino(chainId)
}
// if (signer) return signAmino(signer, signerAddress, messages, fee, memo, signerData)
// Ensure the address has some tokens to spend
const client = await PingWalletClient.offline(signer)
// const client = await SigningStargateClient.offline(signer)
return client.signAmino2(device.startsWith('ledger') ? toSignAddress(signerAddress) : signerAddress, messages, fee, memo, signerData, signOpts)
// return signDirect(signer, signerAddress, messages, fee, memo, signerData)
}
export async function getLedgerAddress(transport = 'blu', hdPath = "m/44'/118/0'/0/0") {
const trans = transport === 'usb' ? await TransportWebUSB.create() : await TransportWebBLE.create()
const signer = new LedgerSigner(trans, { hdPaths: [stringToPath(hdPath)] })
return signer.getAccounts()
}
export function toDuration(value) {
return dayjs.duration(value).humanize()
}
// unit(y M d h m s ms)
export function timeIn(time, amount, unit = 's') {
const input = dayjs(time).add(amount, unit)
return dayjs().unix() > input.unix()
}
export function toDay(time, format = 'long') {
if (format === 'long') {
return dayjs(time).format('YYYY-MM-DD HH:mm')
}
if (format === 'date') {
return dayjs(time).format('YYYY-MM-DD')
}
if (format === 'time') {
return dayjs(time).format('HH:mm:ss')
}
if (format === 'from') {
return dayjs(time).fromNow()
}
if (format === 'to') {
return dayjs(time).toNow()
}
return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
}
export function percent(num) {
return parseFloat((num * 100).toFixed(2))
}
export function abbr(string, length = 6, suffix = '...') {
if (string && string.length > length) {
return `${string.substring(0, length)}${suffix}`
}
return string
}
export function abbrRight(string, length = 6, suffix = '...') {
if (string && string.length > length) {
return `${string.substring(string.length - length)}${suffix}`
}
return string
}
export function abbrMessage(msg) {
if (Array.isArray(msg)) {
const sum = msg.map(x => abbrMessage(x)).reduce((s, c) => {
const sh = s
if (sh[c]) {
sh[c] += 1
} else {
sh[c] = 1
}
return sh
}, {})
const output = []
Object.keys(sum).forEach(k => {
output.push(sum[k] > 1 ? `${k}×${sum[k]}` : k)
})
return output.join(', ')
}
if (msg.typeUrl) {
return msg.typeUrl.substring(msg.typeUrl.lastIndexOf('.') + 1).replace('Msg', '')
}
return msg.type.substring(msg.type.lastIndexOf('/') + 1).replace('Msg', '')
}
export function abbrAddress(address, length = 10) {
return address.substring(0, length).concat('...', address.substring(address.length - length))
}
export function isStringArray(value) {
let is = false
if (Array.isArray(value)) {
is = value.findIndex(x => typeof x === 'string') > -1
}
return is
}
export function isToken(value) {
let is = false
if (Array.isArray(value)) {
is = value.findIndex(x => Object.keys(x).includes('denom')) > -1
}
return is
}
export function findNativeConfigToken(balances, chain) {
if (!Array.isArray(balances)) return undefined
if (!Array.isArray(chain?.assets)) return balances
const configAssetDenoms = chain.assets.map(i => i.base)
// prioritise config tokens in token selection dropdown if available.
const token = balances.find(i => !i.denom.startsWith('ibc') && configAssetDenoms.includes(i.denom))
if (token) return token
// if no token in config, return any native (non-ibc) token
return balances.find(i => !i.denom.startsWith('ibc'))
}
export function formatTokenDenom(tokenDenom) {
if (tokenDenom && tokenDenom.code === undefined) {
let denom = tokenDenom.denom_trace ? tokenDenom.denom_trace.base_denom : tokenDenom
const config = Object.values(getLocalChains())
config.forEach(x => {
if (x.assets) {
const asset = x.assets.find(a => (a.base === denom))
if (asset) denom = asset.symbol
}
})
return denom.startsWith('ibc') ? `IBC...${denom.substring(denom.length - 3)}` : denom.toUpperCase()
}
return ''
}
export function getUnitAmount(amount, tokenDenom) {
const denom = tokenDenom.denom_trace ? tokenDenom.denom_trace.base_denom : tokenDenom
let exp = 6
const config = Object.values(getLocalChains())
config.forEach(x => {
if (x.assets) {
const asset = x.assets.find(a => (a.base === denom))
if (asset) exp = asset.exponent
}
})
// eslint-disable-next-line no-undef
return String(BigInt(Number(amount) * (10 ** exp)))
}
export function numberWithCommas(x) {
const parts = x.toString().split('.')
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
return parts.join('.')
}
export function formatTokenAmount(tokenAmount, fraction = 2, tokenDenom = 'uatom', format = true) {
const denom = tokenDenom.denom_trace ? tokenDenom.denom_trace.base_denom : tokenDenom
let amount = 0
let exp = 6
const config = Object.values(getLocalChains())
config.forEach(x => {
if (x.assets) {
const asset = x.assets.find(a => (a.base === denom))
if (asset) exp = asset.exponent
}
})
amount = Number(Number(tokenAmount)) / (10 ** exp)
if (amount > 10) {
if (format) { return numberWithCommas(parseFloat(amount.toFixed(fraction))) }
return parseFloat(amount.toFixed(fraction))
}
return parseFloat(amount.toFixed(fraction))
}
export function isTestnet() {
return (window.location.hostname.startsWith('testnet')
|| window.location.search.indexOf('testnet') > -1)
}
export function formatToken(token, IBCDenom = {}, decimals = 2, withDenom = true) {
if (token) {
if (withDenom) {
return `${formatTokenAmount(token.amount, decimals, token.denom)} ${formatTokenDenom(IBCDenom[token.denom] || token.denom)}`
}
return formatTokenAmount(token.amount, decimals, token.denom)
}
return token
}
const COUNT_ABBRS = ['', 'K', 'M', 'B', 't', 'q', 's', 'S', 'o', 'n', 'd', 'U', 'D', 'T', 'Qt', 'Qd', 'Sd', 'St']
export function formatNumber(count, withAbbr = false, decimals = 2) {
const i = count === 0 ? count : Math.floor(Math.log(count) / Math.log(1000))
let result = parseFloat((count / (1000 ** i)).toFixed(decimals))
if (withAbbr && COUNT_ABBRS[i]) {
result += `${COUNT_ABBRS[i]}`
}
return result
}
export function tokenFormatter(tokens, denoms = {}) {
if (Array.isArray(tokens)) {
return tokens.map(t => formatToken(t, denoms, 2)).join(', ')
}
return formatToken(tokens, denoms, 2)
}
export function getCachedValidators(chainName) {
const locals = localStorage.getItem(`validators-${chainName}`)
return locals
}
export function isHexAddress(v) {
const re = /^[A-Z\d]{40}$/
return re.test(v)
}
export function getStakingValidatorByHex(chainName, hex) {
const locals = localStorage.getItem(`validators-${chainName}`)
if (locals) {
const val = JSON.parse(locals).find(x => consensusPubkeyToHexAddress(x.consensus_pubkey) === hex)
if (val) {
return val.description.moniker
}
}
return abbr(hex)
}
export function getStakingValidatorByAccount(chainName, addr) {
const locals = localStorage.getItem(`validators-${chainName}`)
if (locals) {
const val = JSON.parse(locals).find(x => operatorAddressToAccount(x.operator_address) === addr)
if (val) {
return val.description.moniker
}
}
return addr
}
export function getStakingValidatorOperator(chainName, addr, length = -1) {
const locals = localStorage.getItem(`validators-${chainName}`)
if (locals) {
const val = JSON.parse(locals).find(x => x.operator_address === addr)
if (val) {
return val.description.moniker
}
}
if (length > 0) {
return addr.substring(addr.length - length)
}
return addr
}
export * from 'compare-versions'
export * from './data'
export class Data {
}