Skip to content

Commit

Permalink
fix: hex values with 0x prefix are valid (#87)
Browse files Browse the repository at this point in the history
* fix: hex values with 0x prefix are valid

* feat: two types of hex validation

* fix: unit tests

* fix: 0x is required for evm chains
  • Loading branch information
kraikov authored May 25, 2022
1 parent f49acb0 commit 1115aae
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
23 changes: 12 additions & 11 deletions src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import {
isValidSolanaAddress,
isValidBitcoinCashAddress,
formatBitcoinCashAddress,
isValidHex,
isValidHexWithout0xPrefix,
isValidSolanaTx,
toLowerCaseWithout0x,
with0x,
isValidTerraAddress,
isValidTerraTx,
getRSKChainID
getRSKChainID,
isValidHexWith0xPrefix
} from './common'

const chains: { [key in ChainId]: Chain } = {
Expand All @@ -33,7 +34,7 @@ const chains: { [key in ChainId]: Chain } = {
// TODO: include network types in validation
isValidAddress: (address) => !!validateBitcoinAddress(address),
formatAddress: (address) => address,
isValidTransactionHash: (hash: string) => isValidHex(hash),
isValidTransactionHash: (hash: string) => isValidHexWithout0xPrefix(hash),
formatTransactionHash: (hash: string) => toLowerCaseWithout0x(hash)
},
[ChainId.BitcoinCash]: {
Expand All @@ -51,7 +52,7 @@ const chains: { [key in ChainId]: Chain } = {
// TODO: include network types in validation
isValidAddress: (address) => isValidBitcoinCashAddress(address),
formatAddress: (address) => formatBitcoinCashAddress(address),
isValidTransactionHash: (hash: string) => isValidHex(hash),
isValidTransactionHash: (hash: string) => isValidHexWithout0xPrefix(hash),
formatTransactionHash: (hash: string) => toLowerCaseWithout0x(hash)
},
[ChainId.Ethereum]: {
Expand All @@ -68,7 +69,7 @@ const chains: { [key in ChainId]: Chain } = {
hasTokens: true,
isValidAddress: (hexAddress: string) => isValidAddress(with0x(hexAddress)),
formatAddress: (hexAddress: string) => toChecksumAddress(with0x(hexAddress)),
isValidTransactionHash: (hash: string) => isValidHex(hash),
isValidTransactionHash: (hash: string) => isValidHexWith0xPrefix(hash),
formatTransactionHash: (hash: string) => toLowerCaseWithout0x(hash)
},
[ChainId.Rootstock]: {
Expand All @@ -86,7 +87,7 @@ const chains: { [key in ChainId]: Chain } = {
isValidAddress: (hexAddress: string) => isValidAddress(with0x(hexAddress)),
formatAddress: (hexAddress: string, network?: string) =>
toChecksumAddress(with0x(hexAddress), getRSKChainID(network)),
isValidTransactionHash: (hash: string) => isValidHex(hash),
isValidTransactionHash: (hash: string) => isValidHexWith0xPrefix(hash),
formatTransactionHash: (hash: string) => toLowerCaseWithout0x(hash)
},
[ChainId.BinanceSmartChain]: {
Expand All @@ -103,7 +104,7 @@ const chains: { [key in ChainId]: Chain } = {
hasTokens: true,
isValidAddress: (hexAddress: string) => isValidAddress(with0x(hexAddress)),
formatAddress: (hexAddress: string) => toChecksumAddress(with0x(hexAddress)),
isValidTransactionHash: (hash: string) => isValidHex(hash),
isValidTransactionHash: (hash: string) => isValidHexWith0xPrefix(hash),
formatTransactionHash: (hash: string) => toLowerCaseWithout0x(hash)
},
[ChainId.Near]: {
Expand Down Expand Up @@ -171,7 +172,7 @@ const chains: { [key in ChainId]: Chain } = {
hasTokens: true,
isValidAddress: (hexAddress: string) => isValidAddress(with0x(hexAddress)),
formatAddress: (hexAddress: string) => toChecksumAddress(with0x(hexAddress)),
isValidTransactionHash: (hash: string) => isValidHex(hash),
isValidTransactionHash: (hash: string) => isValidHexWith0xPrefix(hash),
formatTransactionHash: (hash: string) => toLowerCaseWithout0x(hash)
},
[ChainId.Arbitrum]: {
Expand All @@ -188,7 +189,7 @@ const chains: { [key in ChainId]: Chain } = {
hasTokens: true,
isValidAddress: (hexAddress: string) => isValidAddress(with0x(hexAddress)),
formatAddress: (hexAddress: string) => toChecksumAddress(with0x(hexAddress)),
isValidTransactionHash: (hash: string) => isValidHex(hash),
isValidTransactionHash: (hash: string) => isValidHexWith0xPrefix(hash),
formatTransactionHash: (hash: string) => toLowerCaseWithout0x(hash)
},
[ChainId.Fuse]: {
Expand All @@ -205,7 +206,7 @@ const chains: { [key in ChainId]: Chain } = {
hasTokens: true,
isValidAddress: (hexAddress: string) => isValidAddress(with0x(hexAddress)),
formatAddress: (hexAddress: string) => toChecksumAddress(with0x(hexAddress)),
isValidTransactionHash: (hash: string) => isValidHex(hash),
isValidTransactionHash: (hash: string) => isValidHexWith0xPrefix(hash),
formatTransactionHash: (hash: string) => toLowerCaseWithout0x(hash)
},
[ChainId.Avalanche]: {
Expand All @@ -222,7 +223,7 @@ const chains: { [key in ChainId]: Chain } = {
hasTokens: true,
isValidAddress: (hexAddress: string) => isValidAddress(with0x(hexAddress)),
formatAddress: (hexAddress: string) => toChecksumAddress(with0x(hexAddress)),
isValidTransactionHash: (hash: string) => isValidHex(hash),
isValidTransactionHash: (hash: string) => isValidHexWith0xPrefix(hash),
formatTransactionHash: (hash: string) => hash
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import * as cashaddr from 'cashaddrjs'

const BASE58_LENGTH = 32

export const isValidHex = (hash: string) => /^([A-Fa-f0-9]{64})$/.test(hash)
export const isValidHexWith0xPrefix = (hash: string) => /(0x)([A-Fa-f0-9]{64})$/.test(hash)
export const isValidHexWithout0xPrefix = (hash: string) => /^([A-Fa-f0-9]{64})$/.test(hash)
export const toLowerCaseWithout0x = (hash: string) => hash.toLowerCase().replace(/0x/g, '')
export const with0x = (hash: string) => (hash.startsWith('0x') ? hash : '0x' + hash)

Expand Down
5 changes: 4 additions & 1 deletion tests/chains/avalanche.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ describe('Avalanche chain tests', function () {
})

it('Provides correct tx validation', () => {
const validTxHash = 'b4ec649426ba04cd0407d5929288ff3d59d8baca9b810cf4147620bab6dadba3'
const validTxHash = '0xb4ec649426ba04cd0407d5929288ff3d59d8baca9b810cf4147620bab6dadba3'
expect(avalanche.isValidTransactionHash(validTxHash)).to.be.true

const invalidTxHashWithout0x = 'b4ec649426ba04cd0407d5929288ff3d59d8baca9b810cf4147620bab6dadba3'
expect(avalanche.isValidTransactionHash(invalidTxHashWithout0x)).to.be.false

const validTxHashInvalidSigner =
'8pRdygzR8Zk9oMYmqiyzUpqJQnFmaAnMLniexycNLavr_9eed84cfc2ac0068dd8fc10b8b3b71c8d0f74cfd09211e036bdb8561c264747'
expect(avalanche.isValidTransactionHash(validTxHashInvalidSigner)).to.be.false
Expand Down

0 comments on commit 1115aae

Please sign in to comment.