Skip to content

Commit 5912275

Browse files
committed
Support default chain id of 1 in etherscan uris
1 parent 6731166 commit 5912275

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

ethpm_cli/_utils/etherscan.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any
1+
from typing import Any, Tuple
22
from urllib import parse
33

44
from eth_utils import is_checksum_address
@@ -20,10 +20,10 @@ def is_etherscan_uri(value: Any) -> bool:
2020
if parsed.scheme != "etherscan" or not parsed.netloc:
2121
return False
2222

23-
if ":" not in parsed.netloc:
23+
if parsed.path:
2424
return False
2525

26-
address, chain_id = parsed.netloc.split(":")
26+
address, chain_id = parse_etherscan_uri(value)
2727
if not is_checksum_address(address):
2828
return False
2929

@@ -33,5 +33,14 @@ def is_etherscan_uri(value: Any) -> bool:
3333
return True
3434

3535

36+
def parse_etherscan_uri(uri: str) -> Tuple[str, str]:
37+
parsed = parse.urlparse(uri)
38+
if ":" in parsed.netloc:
39+
address, _, chain_id = parsed.netloc.partition(":")
40+
else:
41+
address, chain_id = (parsed.netloc, "1")
42+
return address, chain_id
43+
44+
3645
def get_etherscan_network(chain_id: str) -> str:
3746
return ETHERSCAN_SUPPORTED_CHAIN_IDS[chain_id]

ethpm_cli/commands/etherscan.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import json
22
import os
33
from typing import Any, Dict, Iterable, Tuple
4-
from urllib import parse
54

65
from eth_typing import URI
76
from eth_utils import to_dict, to_hex, to_int
@@ -10,7 +9,11 @@
109
from ethpm.uri import create_latest_block_uri
1110
import requests
1211

13-
from ethpm_cli._utils.etherscan import get_etherscan_network, is_etherscan_uri
12+
from ethpm_cli._utils.etherscan import (
13+
get_etherscan_network,
14+
is_etherscan_uri,
15+
parse_etherscan_uri,
16+
)
1417
from ethpm_cli.config import get_ipfs_backend, setup_w3
1518
from ethpm_cli.constants import ETHERSCAN_KEY_ENV_VAR
1619
from ethpm_cli.exceptions import ContractNotVerified
@@ -41,7 +44,7 @@ def fetch_uri_contents(
4144
def build_etherscan_manifest(
4245
uri: URI, package_name: str, version: str
4346
) -> Iterable[Tuple[str, Any]]:
44-
address, chain_id = parse.urlparse(uri).netloc.split(":")
47+
address, chain_id = parse_etherscan_uri(uri)
4548
network = get_etherscan_network(chain_id)
4649
body = make_etherscan_request(address, network)
4750
contract_type = body["ContractName"]

tests/core/_utils/test_etherscan.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
@pytest.mark.parametrize(
77
"uri,expected",
88
(
9+
("etherscan://0x6b5DA3cA4286Baa7fBaf64EEEE1834C7d430B729", True),
910
("etherscan://0x6b5DA3cA4286Baa7fBaf64EEEE1834C7d430B729:1", True),
1011
("etherscan://0x6b5DA3cA4286Baa7fBaf64EEEE1834C7d430B729:3", True),
1112
("etherscan://0x6b5DA3cA4286Baa7fBaf64EEEE1834C7d430B729:4", True),
@@ -15,10 +16,10 @@
1516
("etherscan://invalid:1", False),
1617
# non-checksummed
1718
("etherscan://0x6b5da3ca4286baa7fbaf64eeee1834c7d430b729:1", False),
18-
# bad path
19+
# paths are not allowed
1920
("etherscan://0x6b5DA3cA4286Baa7fBaf64EEEE1834C7d430B729/1", False),
21+
("etherscan://0x6b5DA3cA4286Baa7fBaf64EEEE1834C7d430B729/[email protected]", False),
2022
# no chain_id
21-
("etherscan://0x6b5DA3cA4286Baa7fBaf64EEEE1834C7d430B729", False),
2223
("etherscan://0x6b5DA3cA4286Baa7fBaf64EEEE1834C7d430B729:", False),
2324
("etherscan://0x6b5DA3cA4286Baa7fBaf64EEEE1834C7d430B729:10", False),
2425
("://0x6b5DA3cA4286Baa7fBaf64EEEE1834C7d430B729:1", False),

0 commit comments

Comments
 (0)