diff --git a/src/components/CardParameter.vue b/src/components/CardParameter.vue index 6df31437..9a5cd9e7 100644 --- a/src/components/CardParameter.vue +++ b/src/components/CardParameter.vue @@ -15,7 +15,11 @@ function calculateValue(value: any) { if (!value) return; if (value instanceof Uint8Array) { - return formatter.formatDecimalToPercent(fromAscii(value), 1e18); + try { + return formatter.formatDecimalToPercent(fromAscii(value), 1e18); + } catch { + return '-'; + } } if (Array.isArray(value)) { diff --git a/src/libs/address.ts b/src/libs/address.ts index 5f10bbb4..0cd1d420 100644 --- a/src/libs/address.ts +++ b/src/libs/address.ts @@ -61,7 +61,12 @@ export const decodeKey = (value: Any): Key => { export function consensusPubkeyToKey(consensusPubkey?: Any) { if (!consensusPubkey) return; if (consensusPubkey.typeUrl === '/cosmos.crypto.ed25519.PubKey') { - return Ed25519PubKey.decode(consensusPubkey.value).key; + const value = + // fix trim 0a20 at beginning of buffer + consensusPubkey.value.length === 32 + ? new Uint8Array([10, 32, ...consensusPubkey.value]) + : consensusPubkey.value; + return Ed25519PubKey.decode(value).key; } if (consensusPubkey.typeUrl === '/cosmos.crypto.secp256k1.PubKey') { return Secp256k1PubKey.decode(consensusPubkey.value).key; @@ -69,6 +74,7 @@ export function consensusPubkeyToKey(consensusPubkey?: Any) { } export function consensusPubkeyToHexAddress(consensusPubkey?: Any) { + console.log('consensusPubkey', consensusPubkey); const pubkey = consensusPubkeyToKey(consensusPubkey); if (!pubkey) return ''; if (consensusPubkey?.typeUrl === '/cosmos.crypto.ed25519.PubKey') { diff --git a/src/modules/[chain]/staking/index.vue b/src/modules/[chain]/staking/index.vue index 18bbef2c..31eaa678 100644 --- a/src/modules/[chain]/staking/index.vue +++ b/src/modules/[chain]/staking/index.vue @@ -292,12 +292,7 @@ loadAvatars(); - {{ - format.percent( - slashing.slashFractionDoubleSign && - fromAscii(slashing.slashFractionDoubleSign) - ) - }} + {{ format.percent(slashing.slashFractionDoubleSign) }} {{ $t('staking.double_sign_slashing') }} @@ -315,12 +310,7 @@ loadAvatars(); - {{ - format.percent( - slashing.slashFractionDowntime && - fromAscii(slashing.slashFractionDowntime) - ) - }} + {{ format.percent(slashing.slashFractionDowntime) }} {{ $t('staking.downtime_slashing') }} diff --git a/src/modules/[chain]/uptime/index.vue b/src/modules/[chain]/uptime/index.vue index ebef2382..6696040a 100644 --- a/src/modules/[chain]/uptime/index.vue +++ b/src/modules/[chain]/uptime/index.vue @@ -313,10 +313,7 @@ function fetchAllKeyRotation() { :data-tip="`Window size: ${slashingParam.signedBlocksWindow}`" >{{ slashingParam.minSignedPerWindow && - format.percent( - fromAscii(slashingParam.minSignedPerWindow), - 1e18 - ) + format.percent(slashingParam.minSignedPerWindow, 1e18) }} diff --git a/src/stores/useFormatter.ts b/src/stores/useFormatter.ts index 95c83075..2db383a7 100644 --- a/src/stores/useFormatter.ts +++ b/src/stores/useFormatter.ts @@ -8,7 +8,13 @@ import updateLocale from 'dayjs/plugin/updateLocale'; import utc from 'dayjs/plugin/utc'; import localeData from 'dayjs/plugin/localeData'; import { useStakingStore } from './useStakingStore'; -import { fromBase64, fromBech32, fromHex, toHex } from '@cosmjs/encoding'; +import { + fromAscii, + fromBase64, + fromBech32, + fromHex, + toHex, +} from '@cosmjs/encoding'; import { consensusPubkeyToHexAddress, get } from '@/libs'; import { useBankStore } from './useBankStore'; // import type { Coin, DenomTrace } from '@/types'; @@ -347,9 +353,19 @@ export const useFormatter = defineStore('formatter', { if (!rate) return '-'; return this.percent(rate, divideDecimal); }, - percent(decimal?: string | number, divideDecimal?: number) { - let decimalFormat = numeral(decimal); - if (!decimalFormat) return '-'; + percent(decimal?: string | number | Uint8Array, divideDecimal?: number) { + if (!decimal) return '-'; + let decimalFormat; + if (decimal instanceof Uint8Array) { + try { + decimalFormat = numeral(fromAscii(decimal)); + } catch { + return '-'; + } + } else { + decimalFormat = numeral(decimal); + } + if (!decimalFormat || !decimalFormat.value()) return '-'; if (divideDecimal) { decimalFormat = decimalFormat.divide(divideDecimal); }