Skip to content

Commit 727a112

Browse files
authored
Minor BCMR improvements (Electron-Cash#3196)
* Add chipnet support for BCMR resolution via Paytaca indexer * Update BCMR string trancation to match spec
1 parent 5bb448d commit 727a112

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

electroncash/networks.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class AbstractNet:
4747
BASE_UNITS = {'BCH': 8, 'mBCH': 5, 'bits': 2}
4848
DEFAULT_UNIT = "BCH"
4949
RPA_START_HEIGHT = 0
50+
PAYTACA_HOST = ""
5051

5152

5253
class MainNet(AbstractNet):
@@ -62,6 +63,7 @@ class MainNet(AbstractNet):
6263
DEFAULT_PORTS = {'t': '50001', 's': '50002'}
6364
DEFAULT_SERVERS = _read_json_dict('servers.json') # DO NOT MODIFY IN CLIENT CODE
6465
TITLE = 'Electron Cash'
66+
PAYTACA_HOST = "bcmr.paytaca.com"
6567

6668
# Bitcoin Cash fork block specification
6769
BITCOIN_CASH_FORK_BLOCK_HEIGHT = 478559
@@ -158,6 +160,7 @@ class TestNet4(TestNet):
158160

159161
class ChipNet(TestNet4):
160162
TITLE = 'Electron Cash Chipnet'
163+
PAYTACA_HOST = "bcmr-chipnet.paytaca.com"
161164
HEADERS_URL = "http://bitcoincash.com/files/chipnet_headers" # Unused
162165
DEFAULT_SERVERS = _read_json_dict('servers_chipnet.json') # DO NOT MODIFY IN CLIENT CODE
163166
DEFAULT_PORTS = {'t': '64001', 's': '64002'}

electroncash/token_meta.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from abc import ABCMeta, abstractmethod
1616
from typing import Any, Dict, List, Optional, Tuple, Union
1717

18-
from electroncash import address, token, util
18+
from electroncash import address, networks, token, util
1919
from electroncash.simple_config import SimpleConfig
2020
from electroncash.transaction import Transaction
2121

@@ -431,10 +431,16 @@ def sanitize(self):
431431
self.decimals = int(self.decimals)
432432
except (ValueError, TypeError):
433433
pass
434-
self.decimals = min(max(0, self.decimals), 19) if isinstance(self.decimals, int) else 0
435-
self.name = self.name[:30] if isinstance(self.name, str) else ""
436-
self.description = self.description[:160] if isinstance(self.description, str) else ""
437-
self.symbol = self.symbol[:8] if isinstance(self.symbol, str) else ""
434+
# BCMR spec: "An integer between 0 and 18 (inclusive)"
435+
self.decimals = min(max(0, self.decimals), 18) if isinstance(self.decimals, int) else 0
436+
# BCMR schema: "names should be hidden beyond ... 20 characters until revealed by the user"
437+
# We allow up to 2 x 20 = 40 characters
438+
self.name = self.name[:40] if isinstance(self.name, str) else ""
439+
# BCMR spec: "descriptions should be hidden beyond ... 140 characters until revealed by the user"
440+
# We allow up to 2 x 140 = 280 characters
441+
self.description = self.description[:280] if isinstance(self.description, str) else ""
442+
# BCMR spec: "this standard recommends that... clients accommodate up to 26 characters for full symbols"
443+
self.symbol = self.symbol[:26] if isinstance(self.symbol, str) else ""
438444

439445

440446
def _rewrite_if_ipfs(u: str) -> str:
@@ -471,16 +477,17 @@ def _try_to_dl_icon(icon_url: str, *, timeout=30) -> Optional[Tuple[bytes, str]]
471477
return icon, icon_ext
472478

473479

474-
PAYTACA_HOST = "bcmr.paytaca.com"
475-
476480

477481
def _try_to_dl_from_paytaca_indexer(token_id_hex, timeout=30, *, skip_icon=False,
478482
nft_hex=None) -> Optional[DownloadedMetaData]:
479483
"""Download metadata from the paytaca indexer"""
484+
host = networks.net.PAYTACA_HOST
485+
if not host:
486+
return None
480487
if not nft_hex:
481-
url = f"https://{PAYTACA_HOST}/api/tokens/{token_id_hex}/"
488+
url = f"https://{host}/api/tokens/{token_id_hex}/"
482489
else:
483-
url = f"https://{PAYTACA_HOST}/api/tokens/{token_id_hex}/{nft_hex}"
490+
url = f"https://{host}/api/tokens/{token_id_hex}/{nft_hex}"
484491
r = requests.get(url, timeout=timeout, allow_redirects=True)
485492
if not r.ok:
486493
util.print_error(f"Got error requesting url {url}: {r.status_code} {r.reason}")
@@ -636,7 +643,7 @@ def try_to_download_metadata(wallet, token_id_hex, timeout=30, *, skip_icon=Fals
636643
md = _try_to_dl_from_paytaca_indexer(token_id_hex, timeout=timeout, skip_icon=skip_icon,
637644
nft_hex=nft_hex)
638645
if md is not None:
639-
util.print_error(f"Success in downloading token metadata from {PAYTACA_HOST} for:"
646+
util.print_error(f"Success in downloading token metadata from {networks.net.PAYTACA_HOST} for:"
640647
f" {token_id_hex} ({md.name})")
641648
return md
642649

0 commit comments

Comments
 (0)