|
1 | 1 | import { ethers } from "ethers";
|
2 | 2 | import { PUBLIC_NETWORKS } from "../constants";
|
3 |
| -import { createShortHexString } from "./FormattingUtils"; |
| 3 | +import { createShortenedString } from "./FormattingUtils"; |
| 4 | +import { chainIsEvm } from "./NetworkUtils"; |
4 | 5 | import { isDefined } from "./TypeGuards";
|
| 6 | +import bs58 from "bs58"; |
5 | 7 |
|
6 | 8 | /**
|
7 | 9 | * Creates a block explorer link for a transaction or address on a given network.
|
@@ -49,32 +51,37 @@ function constructURL(domain: string, parts: string[]): string {
|
49 | 51 | * @param chainId The chainId to link to.
|
50 | 52 | * @returns A formatted markdown block explorer link to the given transaction hash or address on the given chainId.
|
51 | 53 | */
|
52 |
| -function _createBlockExplorerLinkMarkdown(hex: string, chainId = 1): string | null { |
| 54 | +function _createBlockExplorerLinkMarkdown(addr: string, chainId = 1): string | null { |
53 | 55 | // Attempt to resolve the block explorer domain for the given chainId.
|
54 | 56 | const explorerDomain = resolveBlockExplorerDomain(chainId);
|
55 | 57 | // If the chainId is not supported, return an unsupported link.
|
56 | 58 | if (!isDefined(explorerDomain)) {
|
57 |
| - return `<unsupported chain/hash ${chainId}:${hex}>}`; |
| 59 | + return `<unsupported chain/hash ${chainId}:${addr}>}`; |
58 | 60 | }
|
59 | 61 | // Ensure that the first two characters are "0x". If they are not, append them.
|
60 |
| - if (hex.substring(0, 2) !== "0x") { |
61 |
| - hex = `0x${hex}`; |
62 |
| - } |
63 |
| - // Ensure that the hex string is a valid hexadecimal string. |
64 |
| - if (!ethers.utils.isHexString(hex)) { |
65 |
| - return null; |
| 62 | + if (addr.substring(0, 2) !== "0x" && chainIsEvm(chainId)) { |
| 63 | + addr = `0x${addr}`; |
| 64 | + if (!ethers.utils.isHexString(addr)) { |
| 65 | + return null; |
| 66 | + } |
66 | 67 | }
|
67 | 68 | // Resolve the short URL string.
|
68 |
| - const shortURLString = createShortHexString(hex); |
69 |
| - // Iterate over the two possible hex lengths. |
70 |
| - for (const [length, route] of [ |
71 |
| - [66, "tx"], |
72 |
| - [42, "address"], |
73 |
| - ] as [number, string][]) { |
74 |
| - // If the hex string is the correct length, return the link. |
75 |
| - if (hex.length === length) { |
76 |
| - return `<${constructURL(explorerDomain, [route, hex])} | ${shortURLString}>`; |
| 69 | + const shortURLString = createShortenedString(addr); |
| 70 | + if (chainIsEvm(chainId)) { |
| 71 | + // Iterate over the two possible addr lengths. |
| 72 | + for (const [length, route] of [ |
| 73 | + [66, "tx"], |
| 74 | + [42, "address"], |
| 75 | + ] as [number, string][]) { |
| 76 | + // If the hex string is the correct length, return the link. |
| 77 | + if (addr.length === length) { |
| 78 | + return `<${constructURL(explorerDomain, [route, addr])} | ${shortURLString}>`; |
| 79 | + } |
77 | 80 | }
|
| 81 | + } else { |
| 82 | + const addrLength = bs58.decode(addr).length; |
| 83 | + const route = addrLength === 32 ? "account" : "tx"; |
| 84 | + return `<${constructURL(explorerDomain, [route, addr])} | ${shortURLString}>`; |
78 | 85 | }
|
79 | 86 | return null;
|
80 | 87 | }
|
|
0 commit comments