From 41cbe258c061809c51c9ca09157a5fdb26e93920 Mon Sep 17 00:00:00 2001
From: Nathan Woodburn <github@nathan.woodburn.au>
Date: Fri, 7 Mar 2025 14:14:45 +1100
Subject: [PATCH] feat: Add varo connections

---
 hnslinks.py | 91 ++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 80 insertions(+), 11 deletions(-)

diff --git a/hnslinks.py b/hnslinks.py
index 1898a23..405d63a 100644
--- a/hnslinks.py
+++ b/hnslinks.py
@@ -1,12 +1,13 @@
 import json
 import account
 import requests
+import os
 
 
 # Plugin Data
 info = {
     "name": "HNS Links",
-    "description": "This is a plugin to quickly setup a HNS Links site",
+    "description": "This is a plugin to quickly setup a HNS Links site<br>First you need to setup the varo instance and api key<br>Then add the domain to varo, then add it to hns-links",
     "version": "1.0",
     "author": "Nathan.Woodburn/"
 }
@@ -20,7 +21,7 @@ functions = {
         "description": "You need to connect to a varo instance to setup the dns for domains",
         "params": {
             "instance": {
-                "name":"Varo Instance",
+                "name":"Varo Instance (eg. domains.hns.au)",
                 "type":"text"
             },
             "key": {
@@ -36,10 +37,10 @@ functions = {
             }
         }
     },
-    "dns":{
-        "name": "Set Onchain DNS for Domains",
+    "addDomain":{
+        "name": "Add domain to varo",
         "type": "default",
-        "description": "Set onchain DNS for domains before HNS Links can work",
+        "description": "Add a domain to Varo",
         "params": {
             "domain": {
                 "name":"Domain",
@@ -47,11 +48,13 @@ functions = {
             }
         },
         "returns": {
-            "hash": {
-                "name": "Hash of the transaction",
-                "type": "tx"
+            "status": 
+            {
+                "name": "Status of the function",
+                "type": "text"
             }
         }
+    
     }
 }
 
@@ -81,6 +84,72 @@ def login(params, authentication):
 
 
 
-def dns(params,authentication):
-    dns = params["dns"]
-    return {"hash":"f921ffe1bb01884bf515a8079073ee9381cb93a56b486694eda2cce0719f27c0"}
\ No newline at end of file
+def addDomain(params, authentication):
+    # Add a domain to Varo
+    domain = params["domain"]
+
+    if not os.path.exists("user_data/hnslinks.json"):
+        return {"status": "Missing Varo API or instance"}
+
+    with open("user_data/varo.json", "r") as f:
+        auth = json.load(f)
+        if not auth:
+            return {"status": "Missing Varo API or instance"}
+        if 'api' not in auth or 'instance' not in auth:
+            return {"status": "Missing Varo API or instance"}
+        api = auth["api"]
+        instance = auth["instance"]
+    
+    headers = {"Authorization": f"Bearer {api}"}
+    data = {
+        "action": "getZones"
+    }
+    zones = requests.post(f"https://{instance}/api", json=data, headers=headers)
+    if zones.status_code != 200:
+        return {"status": "Error connecting to Varo"}
+    if zones.json()["success"] != True:
+        return {"status": "Error connecting to Varo"}
+    
+    zones = zones.json()["data"]
+    for zone in zones:
+        if zone["name"] == domain:
+            return {"status": "Domain already exists"}
+        
+    # Check domain is owned by user
+    wallet = authentication.split(":")[0]
+    owned = account.getDomains(wallet)
+    # Only keep owned domains ["name"]
+    ownedNames = [domain["name"] for domain in owned]
+    if domain not in ownedNames:
+        return {"status": "Domain not owned by user"}
+    
+    data = {
+        "action": "createZone",
+        "domain": domain
+    }
+    response = requests.post(f"https://{instance}/api", json=data, headers=headers)
+    if response.status_code != 200:
+        return {"status": "Error connecting to Varo"}
+    if response.json()["success"] != True:
+        return {"status": "Error connecting to Varo"}
+    zoneID = response.json()["data"]["zone"]
+    data = {
+        "action": "showZone",
+        "zone": zoneID
+    }
+    response = requests.post(f"https://{instance}/api", json=data, headers=headers)
+    if response.status_code != 200:
+        return {"status": "Error connecting to Varo"}
+    if response.json()["success"] != True:
+        return {"status": "Error connecting to Varo"}
+    zone = response.json()["data"]
+    
+    dns = []
+    for ns in zone['NS']:
+        dns.append({'type': 'NS', 'value': ns})
+    ds = zone['DS']
+    ds = ds.split(' ')
+    dns.append({'type': 'DS', 'keyTag': int(ds[0]), 'algorithm': int(ds[1]), 'digestType': int(ds[2]), 'digest': ds[3]})
+    dns = json.dumps(dns)
+    response = account.setDNS(authentication,domain,dns)
+    return {"status": "Success"}
\ No newline at end of file