From 86224004271dd57df93dc428919fd7e2fb879353 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Fri, 31 Jan 2025 11:01:10 +1100 Subject: [PATCH] feat: Add support for test networks --- .gitignore | 2 +- account.py | 62 ++++++++++++++++++++++++++++++------------ main.py | 15 ++++++---- plugin.py | 3 +- plugins/automations.py | 14 +++++----- plugins/batching.py | 11 ++++---- plugins/renewal.py | 13 ++++----- plugins/testing.py | 53 ++++++++++++++++++++++++++++++++++++ 8 files changed, 127 insertions(+), 46 deletions(-) create mode 100644 plugins/testing.py diff --git a/.gitignore b/.gitignore index 62c7f1b..5789dc0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ .env - +.env* __pycache__/ templates/assets/css/styles.min.css diff --git a/account.py b/account.py index bebd95e..26c6d75 100644 --- a/account.py +++ b/account.py @@ -10,17 +10,37 @@ import time dotenv.load_dotenv() -APIKEY = os.getenv("hsd_api") -ip = os.getenv("hsd_ip") -if ip is None: - ip = "localhost" +HSD_API = os.getenv("hsd_api") +HSD_IP = os.getenv("hsd_ip") +if HSD_IP is None: + HSD_IP = "localhost" + +HSD_NETWORK = os.getenv("hsd_network") +HSD_WALLET_PORT = 12039 +HSD_NODE_PORT = 12037 + +if not HSD_NETWORK: + HSD_NETWORK = "main" +else: + HSD_NETWORK = HSD_NETWORK.lower() + +if HSD_NETWORK == "simnet": + HSD_WALLET_PORT = 15039 + HSD_NODE_PORT = 15037 +elif HSD_NETWORK == "testnet": + HSD_WALLET_PORT = 13039 + HSD_NODE_PORT = 13037 +elif HSD_NETWORK == "regtest": + HSD_WALLET_PORT = 14039 + HSD_NODE_PORT = 14037 + show_expired = os.getenv("show_expired") if show_expired is None: show_expired = False -hsd = api.hsd(APIKEY,ip) -hsw = api.hsw(APIKEY,ip) +hsd = api.hsd(HSD_API,HSD_IP,HSD_NODE_PORT) +hsw = api.hsw(HSD_API,HSD_IP,HSD_WALLET_PORT) cacheTime = 3600 @@ -84,7 +104,7 @@ def createWallet(account: str, password: str): } # Create the account # Python wrapper doesn't support this yet - response = requests.put(f"http://x:{APIKEY}@{ip}:12039/wallet/{account}") + response = requests.put(f"http://x:{HSD_API}@{HSD_IP}:{HSD_WALLET_PORT}/wallet/{account}") if response.status_code != 200: return { "error": { @@ -98,7 +118,7 @@ def createWallet(account: str, password: str): # Encrypt the wallet (python wrapper doesn't support this yet) - response = requests.post(f"http://x:{APIKEY}@{ip}:12039/wallet/{account}/passphrase", + response = requests.post(f"http://x:{HSD_API}@{HSD_IP}:{HSD_WALLET_PORT}/wallet/{account}/passphrase", json={"passphrase": password}) return { @@ -121,7 +141,7 @@ def importWallet(account: str, password: str,seed: str): "mnemonic": seed, } - response = requests.put(f"http://x:{APIKEY}@{ip}:12039/wallet/{account}",json=data) + response = requests.put(f"http://x:{HSD_API}@{HSD_IP}:{HSD_WALLET_PORT}/wallet/{account}",json=data) if response.status_code != 200: return { "error": { @@ -219,9 +239,9 @@ def getPendingTX(account: str): def getDomains(account,own=True): if own: - response = requests.get(f"http://x:{APIKEY}@{ip}:12039/wallet/{account}/name?own=true") + response = requests.get(f"http://x:{HSD_API}@{HSD_IP}:{HSD_WALLET_PORT}/wallet/{account}/name?own=true") else: - response = requests.get(f"http://x:{APIKEY}@{ip}:12039/wallet/{account}/name") + response = requests.get(f"http://x:{HSD_API}@{HSD_IP}:{HSD_WALLET_PORT}/wallet/{account}/name") info = response.json() if show_expired: @@ -304,9 +324,9 @@ def getTransactions(account,page=1,limit=100): lastTX = getTXFromPage(account,page-1,limit) if lastTX: - response = requests.get(f'http://x:{APIKEY}@{ip}:12039/wallet/{account}/tx/history?reverse=true&limit={limit}&after={lastTX}') + response = requests.get(f'http://x:{HSD_API}@{HSD_IP}:{HSD_WALLET_PORT}/wallet/{account}/tx/history?reverse=true&limit={limit}&after={lastTX}') elif page == 1: - response = requests.get(f'http://x:{APIKEY}@{ip}:12039/wallet/{account}/tx/history?reverse=true&limit={limit}') + response = requests.get(f'http://x:{HSD_API}@{HSD_IP}:{HSD_WALLET_PORT}/wallet/{account}/tx/history?reverse=true&limit={limit}') else: return [] @@ -344,7 +364,7 @@ def check_address(address: str, allow_name: bool = True, return_address: bool = return check_hip2(address[1:]) # Check if the address is a valid HNS address - response = requests.post(f"http://x:{APIKEY}@{ip}:12037",json={ + response = requests.post(f"http://x:{HSD_API}@{HSD_IP}:{HSD_NODE_PORT}",json={ "method": "validateaddress", "params": [address] }).json() @@ -393,7 +413,7 @@ def send(account,address,amount): response = hsw.rpc_walletPassphrase(password,10) # Unlock the account - # response = requests.post(f"http://x:{APIKEY}@{ip}:12039/wallet/{account_name}/unlock", + # response = requests.post(f"http://x:{APIKEY}@{ip}:{HSD_WALLET_PORT}/wallet/{account_name}/unlock", # json={"passphrase": password,"timeout": 10}) if response['error'] is not None: return { @@ -449,6 +469,9 @@ def getDNS(domain: str): return { "error": "No DNS records" } + if response['result'] == None: + return [] + if 'records' not in response['result']: return [] return response['result']['records'] @@ -549,6 +572,9 @@ def getRevealTX(reveal): hash = prevout['hash'] index = prevout['index'] tx = hsd.getTxByHash(hash) + if 'inputs' not in tx: + # Check if registered + return None return tx['inputs'][index]['prevout']['hash'] @@ -592,7 +618,7 @@ def revealAll(account): return # Try to send the batch of all renew, reveal and redeem actions - return requests.post(f"http://x:{APIKEY}@{ip}:12039",json={"method": "sendbatch","params": [[["REVEAL"]]]}).json() + return requests.post(f"http://x:{HSD_API}@{HSD_IP}:{HSD_WALLET_PORT}",json={"method": "sendbatch","params": [[["REVEAL"]]]}).json() except Exception as e: return { "error": { @@ -817,7 +843,7 @@ def sendBatch(account, batch): "message": response['error']['message'] } } - response = requests.post(f"http://x:{APIKEY}@{ip}:12039",json={ + response = requests.post(f"http://x:{HSD_API}@{HSD_IP}:{HSD_WALLET_PORT}",json={ "method": "sendbatch", "params": [batch] }).json() @@ -877,7 +903,7 @@ def zapTXs(account): } try: - response = requests.post(f"http://x:{APIKEY}@{ip}:12039/wallet/{account_name}/zap", + response = requests.post(f"http://x:{HSD_API}@{HSD_IP}:{HSD_WALLET_PORT}/wallet/{account_name}/zap", json={"age": age, "account": "default" }) diff --git a/main.py b/main.py index 9ece496..82620c0 100644 --- a/main.py +++ b/main.py @@ -860,18 +860,23 @@ def auction(domain): return redirect("/") domainInfo = account_module.getDomain(search_term) + error = request.args.get("error") + if error == None: + error = "" if 'error' in domainInfo: return render_template("auction.html", account=account,sync=account_module.getNodeSync(), wallet_status=account_module.getWalletStatus(), - search_term=search_term, domain=domainInfo['error']) + search_term=search_term, domain=domainInfo['error'], + error=error) if domainInfo['info'] is None: next_action = f'Open Auction' return render_template("auction.html", account=account, sync=account_module.getNodeSync(), wallet_status=account_module.getWalletStatus(), search_term=search_term,domain=search_term,next_action=next_action, - state="AVAILABLE", next="Open Auction") + state="AVAILABLE", next="Open Auction", + error=error) state = domainInfo['info']['state'] next_action = '' @@ -966,7 +971,7 @@ def bid(domain): blind = float(blind) if bid+blind == 0: - return redirect("/auction/" + domain+ "?message=Invalid bid amount") + return redirect("/auction/" + domain+ "?error=Invalid bid amount") # Show confirm page @@ -1018,7 +1023,7 @@ def bid_confirm(domain): float(blind)) print(response) if 'error' in response: - return redirect("/auction/" + domain + "?message=" + response['error']['message']) + return redirect("/auction/" + domain + "?error=" + response['error']['message']) return redirect("/success?tx=" + response['hash']) @@ -1037,7 +1042,7 @@ def open_auction(domain): if 'error' in response: if response['error'] != None: - return redirect("/auction/" + domain + "?message=" + response['error']['message']) + return redirect("/auction/" + domain + "?error=" + response['error']['message']) print(response) return redirect("/success?tx=" + response['hash']) diff --git a/plugin.py b/plugin.py index 7baf594..bc2dd9c 100644 --- a/plugin.py +++ b/plugin.py @@ -36,8 +36,7 @@ def listPlugins(update=False): if os.system(f"git clone {importurl} customPlugins/{importPath}") != 0: continue elif update: - if os.system(f"cd customPlugins/{importPath} && git pull") != 0: - continue + os.system(f"cd customPlugins/{importPath} && git pull") # Import plugins from customPlugins/ for file in os.listdir(f"customPlugins/{importPath}"): diff --git a/plugins/automations.py b/plugins/automations.py index 5fb17ec..b062015 100644 --- a/plugins/automations.py +++ b/plugins/automations.py @@ -5,10 +5,10 @@ import threading import os import time -APIKEY = os.environ.get("hsd_api") -ip = os.getenv("hsd_ip") -if ip is None: - ip = "localhost" +KEY = account.HSD_API +IP = account.HSD_IP +PORT = account.HSD_WALLET_PORT + if not os.path.exists("user_data"): os.mkdir("user_data") @@ -148,9 +148,9 @@ def automations_background(authentication): if response['error'] is not None: return # Try to send the batch of all renew, reveal and redeem actions - requests.post(f"http://x:{APIKEY}@{ip}:12039",json={"method": "sendbatch","params": [[["RENEW"]]]}) - requests.post(f"http://x:{APIKEY}@{ip}:12039",json={"method": "sendbatch","params": [[["REVEAL"]]]}) - requests.post(f"http://x:{APIKEY}@{ip}:12039",json={"method": "sendbatch","params": [[["REDEEM"]]]}) + requests.post(f"http://x:{KEY}@{IP}:{PORT}",json={"method": "sendbatch","params": [[["RENEW"]]]}) + requests.post(f"http://x:{KEY}@{IP}:{PORT}",json={"method": "sendbatch","params": [[["REVEAL"]]]}) + requests.post(f"http://x:{KEY}@{IP}:{PORT}",json={"method": "sendbatch","params": [[["REDEEM"]]]}) except Exception as e: print(e) diff --git a/plugins/batching.py b/plugins/batching.py index e1770b2..d5121c5 100644 --- a/plugins/batching.py +++ b/plugins/batching.py @@ -500,16 +500,15 @@ def advancedChangeLookahead(params, authentication): lookahead = int(lookahead) wallet = authentication.split(":")[0] password = ":".join(authentication.split(":")[1:]) - APIKEY = os.getenv("hsd_api") - ip = os.getenv("hsd_ip") - if ip is None: - ip = "localhost" + KEY = account.HSD_API + IP = account.HSD_IP + PORT = account.HSD_WALLET_PORT # Unlock wallet - response = requests.post(f"http://x:{APIKEY}@{ip}:12039/wallet/{wallet}/unlock", + response = requests.post(f"http://x:{KEY}@{IP}:{PORT}/wallet/{wallet}/unlock", json={"passphrase": password, "timeout": 10}) - response = requests.patch(f"http://x:{APIKEY}@{ip}:12039/wallet/{wallet}/account/default", + response = requests.patch(f"http://x:{KEY}@{IP}:{PORT}/wallet/{wallet}/account/default", json={"lookahead": lookahead}) diff --git a/plugins/renewal.py b/plugins/renewal.py index 9a9f850..9819e8b 100644 --- a/plugins/renewal.py +++ b/plugins/renewal.py @@ -50,12 +50,11 @@ def main(params, authentication): batches.append(names[i:i+100]) # Unlock wallet - api_key = os.getenv("hsd_api") - ip = os.getenv("hsd_ip") - if api_key is None: - print("API key not set") - return {"status": "API key not set", "transaction": "None"} - response = requests.post(f'http://x:{api_key}@{ip}:12039/wallet/{wallet}/unlock', + KEY = account.HSD_API + IP = account.HSD_IP + PORT = account.HSD_WALLET_PORT + + response = requests.post(f'http://x:{KEY}@{IP}:{PORT}/wallet/{wallet}/unlock', json={'passphrase': password, 'timeout': 600}) if response.status_code != 200: print("Failed to unlock wallet") @@ -74,7 +73,7 @@ def main(params, authentication): batchTX = "[" + ", ".join(batch) + "]" responseContent = f'{{"method": "sendbatch","params":[ {batchTX} ]}}' - response = requests.post(f'http://x:{api_key}@{ip}:12039', data=responseContent) + response = requests.post(f'http://x:{KEY}@{IP}:{PORT}', data=responseContent) if response.status_code != 200: print("Failed to create batch",flush=True) print(f'Status code: {response.status_code}',flush=True) diff --git a/plugins/testing.py b/plugins/testing.py new file mode 100644 index 0000000..ab87751 --- /dev/null +++ b/plugins/testing.py @@ -0,0 +1,53 @@ +import json +import account +import requests + +# Plugin Data +info = { + "name": "Testing tools", + "description": "Testing tools", + "version": "1.0", + "author": "Nathan.Woodburn/" +} + +# Functions +functions = { + "generate":{ + "name": "Generate blocks", + "type": "default", + "description": "Generate blocks to your wallet", + "params": { + "numblocks": { + "name":"Number of blocks to generate", + "type":"number" + }, + "address": { + "name":"Address to generate to", + "type":"text" + } + }, + "returns": { + "status": + { + "name": "Status of the function", + "type": "text" + } + } + } +} + +def generate(params, authentication): + # hsd-cli rpc generatetoaddress $numblocks $address + number = params["numblocks"] + address = params["address"] + if number == "" or int(number) < 1: + number = 1 + + if address == "": + wallet = authentication.split(":")[0] + address = account.getAddress(wallet) + + print(f"Generating {number} blocks to {address}") + blocks = account.hsd.rpc_generateToAddress(address,number) + return {"status": f"Successfully generated {number} blocks to {address}"} + \ No newline at end of file