1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00

Handle trailing / in block explorer URLs (#8592)

* Strip trailing slashes from variable in block explorer URL template strings
This commit is contained in:
Erik Marks 2020-05-14 08:13:53 -07:00 committed by GitHub
commit d0fcf665bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 2 deletions

View File

@ -0,0 +1,44 @@
import getAccountLink from '../../../ui/lib/account-link'
import assert from 'assert'
describe('Account link', function () {
describe('getAccountLink', function () {
it('should return the correct block explorer url for an account', function () {
const tests = [
{
expected: 'https://etherscan.io/address/0xabcd',
network: 1,
address: '0xabcd',
},
{
expected: 'https://ropsten.etherscan.io/address/0xdef0',
network: 3,
address: '0xdef0',
rpcPrefs: {},
},
{
// test handling of `blockExplorerUrl` for a custom RPC
expected: 'https://block.explorer/address/0xabcd',
network: 31,
address: '0xabcd',
rpcPrefs: {
blockExplorerUrl: 'https://block.explorer',
},
},
{
// test handling of trailing `/` in `blockExplorerUrl` for a custom RPC
expected: 'https://another.block.explorer/address/0xdef0',
network: 33,
address: '0xdef0',
rpcPrefs: {
blockExplorerUrl: 'https://another.block.explorer/',
},
},
]
tests.forEach(({ expected, address, network, rpcPrefs }) => {
assert.equal(getAccountLink(address, network, rpcPrefs), expected)
})
})
})
})

View File

@ -236,7 +236,7 @@ export function getStatusKey (transaction) {
*/
export function getBlockExplorerUrlForTx (networkId, hash, rpcPrefs = {}) {
if (rpcPrefs.blockExplorerUrl) {
return `${rpcPrefs.blockExplorerUrl}/tx/${hash}`
return `${rpcPrefs.blockExplorerUrl.replace(/\/+$/, '')}/tx/${hash}`
}
const prefix = prefixForNetwork(networkId)
return `https://${prefix}etherscan.io/tx/${hash}`

View File

@ -54,4 +54,44 @@ describe('Transactions utils', function () {
})
})
})
describe('getBlockExplorerUrlForTx', function () {
it('should return the correct block explorer url for a transaction', function () {
const tests = [
{
expected: 'https://etherscan.io/tx/0xabcd',
networkId: 1,
hash: '0xabcd',
},
{
expected: 'https://ropsten.etherscan.io/tx/0xdef0',
networkId: 3,
hash: '0xdef0',
rpcPrefs: {},
},
{
// test handling of `blockExplorerUrl` for a custom RPC
expected: 'https://block.explorer/tx/0xabcd',
networkId: 31,
hash: '0xabcd',
rpcPrefs: {
blockExplorerUrl: 'https://block.explorer',
},
},
{
// test handling of trailing `/` in `blockExplorerUrl` for a custom RPC
expected: 'https://another.block.explorer/tx/0xdef0',
networkId: 33,
hash: '0xdef0',
rpcPrefs: {
blockExplorerUrl: 'https://another.block.explorer/',
},
},
]
tests.forEach(({ expected, networkId, hash, rpcPrefs }) => {
assert.equal(utils.getBlockExplorerUrlForTx(networkId, hash, rpcPrefs), expected)
})
})
})
})

View File

@ -1,6 +1,6 @@
export default function getAccountLink (address, network, rpcPrefs) {
if (rpcPrefs && rpcPrefs.blockExplorerUrl) {
return `${rpcPrefs.blockExplorerUrl}/address/${address}`
return `${rpcPrefs.blockExplorerUrl.replace(/\/+$/, '')}/address/${address}`
}
const net = parseInt(network)