Skip to content

Commit

Permalink
refactor: replace '0x' with HEXADECIMAL_PREFIX
Browse files Browse the repository at this point in the history
  • Loading branch information
nicole-obrien committed Sep 1, 2023
1 parent 90e89f3 commit 66fae1c
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 28 deletions.
3 changes: 2 additions & 1 deletion packages/desktop/components/popups/AddProposalPopup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +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 { truncateString } from '@core/utils/string'
import { closePopup, openPopup, PopupId } from '@desktop/auxiliary/popup'
import type { Auth } from '@iota/wallet'
Expand Down Expand Up @@ -101,7 +102,7 @@
}
async function validateEventId(checkIfAlreadyRegistered: boolean): Promise<void> {
const startsWith0x = eventId?.substring(0, 2) === '0x'
const startsWith0x = eventId?.substring(0, 2) === HEXADECIMAL_PREFIX
if (!startsWith0x) {
eventIdError = localize('error.eventId.doesNotStartWith0x')
return Promise.reject(eventIdError)
Expand Down
10 changes: 5 additions & 5 deletions packages/desktop/lib/electron/utils/ledger.utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { IEvmTransactionSignature } from '@core/layer-2/interfaces'
import { HEXADECIMAL_PREFIX } from '@core/utils'
import AppEth from '@ledgerhq/hw-app-eth'
import TransportNodeHid from '@ledgerhq/hw-transport-node-hid'
import { listen } from '@ledgerhq/logs'

import { IEvmTransactionSignature } from '@core/layer-2/interfaces'

let transport: TransportNodeHid

export async function openTransport(): Promise<void> {
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: '0x' + signature.r,
v: '0x' + signature.v,
s: '0x' + signature.s,
r: HEXADECIMAL_PREFIX + signature.r,
v: HEXADECIMAL_PREFIX + signature.v,
s: HEXADECIMAL_PREFIX + signature.s,
}
} catch (error) {
return {
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/src/components/inputs/NftInput.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +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 { IOption, Modal, NftImageOrIconBox, SelectorInput } from '@ui'
export let nftId: string = ''
Expand All @@ -26,7 +27,7 @@
if (!nftId) {
error = localize('error.send.nftRequired')
return Promise.reject(error)
} else if (!nftId.startsWith('0x')) {
} else if (!nftId.startsWith(HEXADECIMAL_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,9 +1,10 @@
import { HEXADECIMAL_PREFIX } from '@core/utils'
import type { SendFlowParameters, TokenSendFlowParameters } from '@core/wallet/types'
import BigInteger from 'big-integer'
import { estimateGasForLayer1ToLayer2Transaction } from './estimateGasForLayer1ToLayer2Transaction'
import { SpecialStream } from '../classes'
import { ACCOUNTS_CONTRACT, EXTERNALLY_OWNED_ACCOUNT, GAS_LIMIT_MULTIPLIER, TRANSFER_ALLOWANCE } from '../constants'
import { encodeAddress, encodeAssetAllowance, encodeSmartContractParameters } from '../helpers'
import { estimateGasForLayer1ToLayer2Transaction } from './estimateGasForLayer1ToLayer2Transaction'

export async function getLayer2MetadataForTransfer(sendFlowParameters: SendFlowParameters): Promise<string> {
const metadataStream = new SpecialStream()
Expand All @@ -27,5 +28,5 @@ export async function getLayer2MetadataForTransfer(sendFlowParameters: SendFlowP
const allowance = encodeAssetAllowance(sendFlowParameters)
metadataStream.writeBytes('allowance', allowance.length, allowance)

return '0x' + metadataStream.finalHex()
return HEXADECIMAL_PREFIX + metadataStream.finalHex()
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { TxData } from '@ethereumjs/tx'
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 { removeLeadingZeros } from '@core/utils/buffer'
import { Transaction } from '@ethereumjs/tx'
import { Transaction, TxData } from '@ethereumjs/tx'
import { ECDSASignature, fromRpcSig } from '@ethereumjs/util'
import type { Bip44 } from '@iota/wallet/types'

Expand All @@ -13,7 +13,7 @@ export async function signEvmTransactionWithStronghold(
chainId: EvmChainId,
account: IAccountState
): Promise<string> {
const unsignedTransactionMessageHex = '0x' + prepareEvmTransaction(txData, chainId)
const unsignedTransactionMessageHex = HEXADECIMAL_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 = '0x' + serializedTransaction.toString('hex')
const hexEncodedTransaction = HEXADECIMAL_PREFIX + serializedTransaction.toString('hex')
return hexEncodedTransaction
}

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

export function getEvmTransactionValueFromAmount(amount: number | string): string {
return '0x' + BigInt(BigInt(amount) * WEI_PER_GLOW).toString(16)
return HEXADECIMAL_PREFIX + BigInt(BigInt(amount) * WEI_PER_GLOW).toString(16)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Converter } from '@core/utils'
import { NFT_ID_BYTE_LENGTH } from '@core/nfts/constants'
import { ILayer2AssetAllowance, ILayer2TransferAllowanceMetadata } from '../interfaces'
import { CONTRACT_FUNCTIONS, TARGET_CONTRACTS } from '../constants'
import { Allowance } from '@core/layer-2/enums'
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 { CONTRACT_FUNCTIONS, TARGET_CONTRACTS } from '../constants'
import { ILayer2AssetAllowance, ILayer2TransferAllowanceMetadata } from '../interfaces'

// Function to parse data from the L1 metadata, using the new encoding where the shimmer chainId is 1072
export function parseLayer2MetadataForTransfer(metadata: Uint8Array): ILayer2TransferAllowanceMetadata {
Expand All @@ -16,7 +16,7 @@ export function parseLayer2MetadataForTransfer(metadata: Uint8Array): ILayer2Tra
const gasLimit = readStream.readUIntNSpecialEncoding('gasLimit', 3)

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

const allowance = parseAssetAllowance(readStream)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EvmChainId, getEvmTransactionOptions } from '@core/network'
import { HEXADECIMAL_PREFIX } from '@core/utils'
import { RLP } from '@ethereumjs/rlp'
import { Transaction } from '@ethereumjs/tx'
import { bufArrToArr } from '@ethereumjs/util'
Expand All @@ -16,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 ? '0x' + transactionHex : transactionHex
return isSigned ? HEXADECIMAL_PREFIX + transactionHex : transactionHex
}
3 changes: 2 additions & 1 deletion packages/shared/src/lib/core/token/utils/validateTokenId.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { InvalidTokenIdError } from '@auxiliary/deep-link'
import { DEFAULT_COIN_TYPE } from '@core/network'
import { HEXADECIMAL_PREFIX } from '@core/utils'

export function validateTokenId(id: string): void {
const isHex = id.startsWith('0x')
const isHex = id.startsWith(HEXADECIMAL_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
8 changes: 4 additions & 4 deletions packages/shared/src/lib/core/utils/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export class Converter {
}
}
}
return prefix ? '0x' + hex : hex
return prefix ? HEXADECIMAL_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('0x')) {
if (hex.startsWith(HEXADECIMAL_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 ? '0x' + number.toString(16) : number.toString(16)
return prefix ? HEXADECIMAL_PREFIX + number.toString(16) : number.toString(16)
}

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

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

export function isValidHexAddress(text: string): boolean {
const isHex = text.startsWith('0x')
const isHex = text.startsWith(HEXADECIMAL_PREFIX)
return isHex && /^(0x)?[0-9a-f]{64}/i.test(text)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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('0x', '')
address = address.replace(HEXADECIMAL_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

0 comments on commit 66fae1c

Please sign in to comment.