mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Handle trailing / in block explorer URLs (#8592)
* Strip trailing slashes from variable in block explorer URL template strings
This commit is contained in:
commit
d0fcf665bb
44
test/unit/lib/account-link.test.js
Normal file
44
test/unit/lib/account-link.test.js
Normal 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)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
@ -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}`
|
||||
|
@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user