Skip to content

Commit

Permalink
rename: HEXADECIMAL_PREFIX to HEX_PREFIX
Browse files Browse the repository at this point in the history
  • Loading branch information
nicole-obrien committed Sep 1, 2023
1 parent 66fae1c commit 2c9fb3d
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 39 deletions.
4 changes: 2 additions & 2 deletions packages/desktop/components/popups/AddProposalPopup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { localize } from '@core/i18n'
import { updateActiveAccountPersistedData } from '@core/profile/actions'
import { activeAccounts } from '@core/profile/stores'
import { HEXADECIMAL_PREFIX } from '@core/utils'
import { HEX_PREFIX } from '@core/utils'
import { truncateString } from '@core/utils/string'
import { closePopup, openPopup, PopupId } from '@desktop/auxiliary/popup'
import type { Auth } from '@iota/wallet'
Expand Down Expand Up @@ -102,7 +102,7 @@
}
async function validateEventId(checkIfAlreadyRegistered: boolean): Promise<void> {
const startsWith0x = eventId?.substring(0, 2) === HEXADECIMAL_PREFIX
const startsWith0x = eventId?.substring(0, 2) === HEX_PREFIX
if (!startsWith0x) {
eventIdError = localize('error.eventId.doesNotStartWith0x')
return Promise.reject(eventIdError)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { ERC20_TOKEN_ADDRESS_LENGTH } from '@core/layer-2'
import { getErc20TokenMetadata } from '@core/layer-2/utils'
import { NetworkId, network } from '@core/network'
import { HEXADECIMAL_PREFIX, HEXADECIMAL_REGEXP } from '@core/utils'
import { HEX_PREFIX, HEXADECIMAL_REGEXP } from '@core/utils'
import { closePopup } from '@desktop/auxiliary/popup'
import { showNotification } from '@auxiliary/notification'
Expand Down Expand Up @@ -57,7 +57,7 @@
}
function validateTokenAddress(): string {
const hasHexPrefix = tokenAddress?.startsWith(HEXADECIMAL_PREFIX)
const hasHexPrefix = tokenAddress?.startsWith(HEX_PREFIX)
const isValidHex = HEXADECIMAL_REGEXP.test(tokenAddress)
if (!hasHexPrefix || !isValidHex) {
return localize('error.erc20Token.invalidAddressFormat')
Expand Down
8 changes: 4 additions & 4 deletions packages/desktop/lib/electron/utils/ledger.utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IEvmTransactionSignature } from '@core/layer-2/interfaces'
import { HEXADECIMAL_PREFIX } from '@core/utils'
import { HEX_PREFIX } from '@core/utils'
import AppEth from '@ledgerhq/hw-app-eth'
import TransportNodeHid from '@ledgerhq/hw-transport-node-hid'
import { listen } from '@ledgerhq/logs'
Expand Down Expand Up @@ -37,9 +37,9 @@ export async function signTransactionData(
const appEth = new AppEth(transport)
const signature = await appEth.signTransaction(bip32Path, transactionHex, null)
return {
r: HEXADECIMAL_PREFIX + signature.r,
v: HEXADECIMAL_PREFIX + signature.v,
s: HEXADECIMAL_PREFIX + signature.s,
r: HEX_PREFIX + signature.r,
v: HEX_PREFIX + signature.v,
s: HEX_PREFIX + signature.s,
}
} catch (error) {
return {
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/src/components/inputs/NftInput.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { localize } from '@core/i18n'
import { getNftByIdFromAllAccountNfts } from '@core/nfts/actions'
import { ownedNfts } from '@core/nfts/stores'
import { HEXADECIMAL_PREFIX } from '@core/utils'
import { HEX_PREFIX } from '@core/utils'
import { IOption, Modal, NftImageOrIconBox, SelectorInput } from '@ui'
export let nftId: string = ''
Expand All @@ -27,7 +27,7 @@
if (!nftId) {
error = localize('error.send.nftRequired')
return Promise.reject(error)
} else if (!nftId.startsWith(HEXADECIMAL_PREFIX)) {
} else if (!nftId.startsWith(HEX_PREFIX)) {
error = localize('error.send.nftNotInHex')
return Promise.reject(error)
} else if (!isNftInPossession()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HEXADECIMAL_PREFIX } from '@core/utils'
import { HEX_PREFIX } from '@core/utils'
import type { SendFlowParameters, TokenSendFlowParameters } from '@core/wallet/types'
import BigInteger from 'big-integer'
import { SpecialStream } from '../classes'
Expand Down Expand Up @@ -28,5 +28,5 @@ export async function getLayer2MetadataForTransfer(sendFlowParameters: SendFlowP
const allowance = encodeAssetAllowance(sendFlowParameters)
metadataStream.writeBytes('allowance', allowance.length, allowance)

return HEXADECIMAL_PREFIX + metadataStream.finalHex()
return HEX_PREFIX + metadataStream.finalHex()
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IAccountState } from '@core/account'
import { prepareEvmTransaction } from '@core/layer-2/utils'
import { EvmChainId, getEvmTransactionOptions } from '@core/network'
import { HEXADECIMAL_PREFIX } from '@core/utils'
import { HEX_PREFIX } from '@core/utils'
import { removeLeadingZeros } from '@core/utils/buffer'
import { Transaction, TxData } from '@ethereumjs/tx'
import { ECDSASignature, fromRpcSig } from '@ethereumjs/util'
Expand All @@ -13,7 +13,7 @@ export async function signEvmTransactionWithStronghold(
chainId: EvmChainId,
account: IAccountState
): Promise<string> {
const unsignedTransactionMessageHex = HEXADECIMAL_PREFIX + prepareEvmTransaction(txData, chainId)
const unsignedTransactionMessageHex = HEX_PREFIX + prepareEvmTransaction(txData, chainId)
const transaction = Transaction.fromTxData(txData, getEvmTransactionOptions(chainId))

const { signature } = await account.signSecp256k1Ecdsa(unsignedTransactionMessageHex, bip44Path)
Expand Down Expand Up @@ -44,7 +44,7 @@ function createSignedTransaction(

function getHexEncodedTransaction(transaction: Transaction): string {
const serializedTransaction = transaction.serialize()
const hexEncodedTransaction = HEXADECIMAL_PREFIX + serializedTransaction.toString('hex')
const hexEncodedTransaction = HEX_PREFIX + serializedTransaction.toString('hex')
return hexEncodedTransaction
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HEXADECIMAL_PREFIX } from '@core/utils'
import { HEX_PREFIX } from '@core/utils'
import { WEI_PER_GLOW } from '../constants'

export function getEvmTransactionValueFromAmount(amount: number | string): string {
return HEXADECIMAL_PREFIX + BigInt(BigInt(amount) * WEI_PER_GLOW).toString(16)
return HEX_PREFIX + BigInt(BigInt(amount) * WEI_PER_GLOW).toString(16)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ReadSpecialStream } from '@core/layer-2/classes'
import { Allowance } from '@core/layer-2/enums'
import { NFT_ID_BYTE_LENGTH } from '@core/nfts/constants'
import { TOKEN_ID_BYTE_LENGTH } from '@core/token/constants'
import { Converter, HEXADECIMAL_PREFIX } from '@core/utils'
import { Converter, HEX_PREFIX } from '@core/utils'
import { CONTRACT_FUNCTIONS, TARGET_CONTRACTS } from '../constants'
import { ILayer2AssetAllowance, ILayer2TransferAllowanceMetadata } from '../interfaces'

Expand All @@ -16,7 +16,7 @@ export function parseLayer2MetadataForTransfer(metadata: Uint8Array): ILayer2Tra
const gasLimit = readStream.readUIntNSpecialEncoding('gasLimit', 3)

const smartContractParameters = parseSmartContractParameters(readStream)
const ethereumAddress = HEXADECIMAL_PREFIX + smartContractParameters['a'].substring(4)
const ethereumAddress = HEX_PREFIX + smartContractParameters['a'].substring(4)

const allowance = parseAssetAllowance(readStream)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EvmChainId, getEvmTransactionOptions } from '@core/network'
import { HEXADECIMAL_PREFIX } from '@core/utils'
import { HEX_PREFIX } from '@core/utils'
import { RLP } from '@ethereumjs/rlp'
import { Transaction } from '@ethereumjs/tx'
import { bufArrToArr } from '@ethereumjs/util'
Expand All @@ -17,5 +17,5 @@ export function prepareEvmTransaction(
const transaction = Transaction.fromTxData(preparedTransactionData, getEvmTransactionOptions(chainId))
const transactionBuffer = isSigned ? transaction.raw() : transaction.getMessageToSign(false)
const transactionHex = Buffer.from(RLP.encode(bufArrToArr(transactionBuffer))).toString('hex')
return isSigned ? HEXADECIMAL_PREFIX + transactionHex : transactionHex
return isSigned ? HEX_PREFIX + transactionHex : transactionHex
}
4 changes: 2 additions & 2 deletions packages/shared/src/lib/core/token/utils/validateTokenId.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { InvalidTokenIdError } from '@auxiliary/deep-link'
import { DEFAULT_COIN_TYPE } from '@core/network'
import { HEXADECIMAL_PREFIX } from '@core/utils'
import { HEX_PREFIX } from '@core/utils'

export function validateTokenId(id: string): void {
const isHex = id.startsWith(HEXADECIMAL_PREFIX)
const isHex = id.startsWith(HEX_PREFIX)
if (isHex && !/^(0x08)?[0-9a-f]{64}?(?:0[1-9]|[1-5][0-9]|6[0-4])?0{8}$/i.test(id)) {
throw new InvalidTokenIdError()
} else if (!isHex && !Object.values(DEFAULT_COIN_TYPE).includes(Number(id))) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const HEXADECIMAL_PREFIX = '0x'
export const HEX_PREFIX = '0x'

export const HEXADECIMAL_REGEXP = /(0x)?[0-9a-fA-F]+$/i
14 changes: 7 additions & 7 deletions packages/shared/src/lib/core/utils/convert.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-bitwise */

import { HEXADECIMAL_PREFIX, MILLISECONDS_PER_SECOND } from './constants'
import { HEX_PREFIX, MILLISECONDS_PER_SECOND } from './constants'
import { isValidDate } from './date'
import { Base64 } from './encode'
import { clamp } from './math'
Expand Down Expand Up @@ -30,7 +30,7 @@ export function convertUnixTimestampToDate(timestamp: number): Date {
export function convertUInt16NumberToLittleEndianHex(num: number, withHexPrefix = true): string {
const littleEndianNumber = ((num & 0xff) << 8) | ((num >> 8) & 0xff)
const hex = ('0000' + littleEndianNumber.toString(16).toUpperCase()).slice(-4)
return withHexPrefix ? HEXADECIMAL_PREFIX + hex : hex
return withHexPrefix ? HEX_PREFIX + hex : hex
}

export function convertBytesToHexString(bytes: number[], withHexPrefix = true): string {
Expand All @@ -43,7 +43,7 @@ export function convertBytesToHexString(bytes: number[], withHexPrefix = true):
}

const hex = bytes.map((byte) => ('0' + (byte & 0xff).toString(16)).slice(-2)).join('')
return withHexPrefix ? HEXADECIMAL_PREFIX + hex : hex
return withHexPrefix ? HEX_PREFIX + hex : hex
}

/**
Expand Down Expand Up @@ -205,7 +205,7 @@ export class Converter {
}
}
}
return prefix ? HEXADECIMAL_PREFIX + hex : hex
return prefix ? HEX_PREFIX + hex : hex
}

/**
Expand All @@ -215,7 +215,7 @@ export class Converter {
* @returns The array.
*/
public static hexToBytes(hex: string, reverse?: boolean): Uint8Array {
if (hex.startsWith(HEXADECIMAL_PREFIX)) {
if (hex.startsWith(HEX_PREFIX)) {
hex = hex.substring(2)
}

Expand Down Expand Up @@ -263,11 +263,11 @@ export class Converter {
}

public static decimalToHex(number: number, prefix = true): string {
return prefix ? HEXADECIMAL_PREFIX + number.toString(16) : number.toString(16)
return prefix ? HEX_PREFIX + number.toString(16) : number.toString(16)
}

public static bigIntToHex(bigInt: bigint, prefix = true): string {
return prefix ? HEXADECIMAL_PREFIX + bigInt.toString(16) : bigInt.toString(16)
return prefix ? HEX_PREFIX + bigInt.toString(16) : bigInt.toString(16)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HEXADECIMAL_PREFIX } from '@core/utils/constants'
import { HEX_PREFIX } from '@core/utils/constants'

export function isValidHexAddress(text: string): boolean {
const isHex = text.startsWith(HEXADECIMAL_PREFIX)
const isHex = text.startsWith(HEX_PREFIX)
return isHex && /^(0x)?[0-9a-f]{64}/i.test(text)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { localize } from '@core/i18n'
import { Keccak } from 'sha3'
import { HEXADECIMAL_PREFIX } from '../../constants'
import { HEX_PREFIX } from '../../constants'
import { KECCAK_HASH_SIZE } from '../constants'

export function validateEthereumAddress(address: string): void {
// 1. Check prefix
if (!/^(0x)*/i.test(address)) {
throw new Error(localize('error.send.wrongAddressPrefix', { prefix: HEXADECIMAL_PREFIX }))
throw new Error(localize('error.send.wrongAddressPrefix', { prefix: HEX_PREFIX }))
// 2. Check hex format
} else if (!/^(0x)?[0-9a-f]*/i.test(address)) {
throw new Error(localize('error.send.wrongAddressFormat'))
Expand All @@ -21,7 +21,7 @@ export function validateEthereumAddress(address: string): void {

// Check if EIP-55 mixed-case checksum address encoding is followed (https://eips.ethereum.org/EIPS/eip-55)
function validateEthereumAddressChecksum(address: string): void {
address = address.replace(HEXADECIMAL_PREFIX, '')
address = address.replace(HEX_PREFIX, '')
const keccak256 = new Keccak(KECCAK_HASH_SIZE)
const addressHash = keccak256.update(address.toLowerCase()).digest('hex')
for (let i = 0; i < address.length; i++) {
Expand Down
6 changes: 3 additions & 3 deletions packages/shared/src/lib/core/utils/format.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { formatDate } from '@core/i18n'

import { HEXADECIMAL_PREFIX, HEXADECIMAL_REGEXP } from './constants'
import { HEX_PREFIX, HEXADECIMAL_REGEXP } from './constants'

export function formatHexString(
hex: string,
Expand All @@ -11,11 +11,11 @@ export function formatHexString(
return '00'
}

hex = hex.replace(HEXADECIMAL_PREFIX, '')
hex = hex.replace(HEX_PREFIX, '')

let result = ''
if (shouldIncludePrefix) {
result += HEXADECIMAL_PREFIX
result += HEX_PREFIX
}

result += shouldBeUppercase ? hex.toUpperCase() : hex.toLowerCase()
Expand Down

0 comments on commit 2c9fb3d

Please sign in to comment.