hnslinks-wallet-plugin/hnslinks.py

198 lines
5.9 KiB
Python

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<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/"
}
# Functions
functions = {
"login":{
"name": "Login to Varo Instance",
"type": "default",
"description": "You need to connect to a varo instance to setup the dns for domains",
"params": {
"instance": {
"name":"Varo Instance (eg. domains.hns.au)",
"type":"text"
},
"key": {
"name":"API Key",
"type":"text"
}
},
"returns": {
"status":
{
"name": "Status",
"type": "text"
}
}
},
"addDomain":{
"name": "Add domain to varo",
"type": "default",
"description": "Add a domain to Varo",
"params": {
"domain": {
"name":"Domain",
"type":"text"
}
},
"returns": {
"status":
{
"name": "Status of the function",
"type": "text"
}
}
},
"hnslinks":{
"name": "Add domain to HNS Links with wallet address",
"type": "default",
"description": "Add a domain to HNS Links",
"params": {
"domain": {
"name":"Domain",
"type":"text"
}
},
"returns": {
"status":
{
"name": "Status of the function",
"type": "text"
}
}
}
}
def login(params, authentication):
instance = params["instance"]
key = params["key"]
# Test Connection
if instance == "":
return {"status":"Please enter a valid instance"}
instance = instance.replace("https://","")
instance = instance.replace("http://","")
if instance[-1] == "/":
instance = instance[:-1]
try:
# Authorization: Bearer <API Key>
response = requests.post("https://" + instance + "/api", json={"action":"getInfo"}, headers={"Authorization": "Bearer " + key})
if response.status_code != 200:
return {"status":"Error connecting to instance"}
except:
return {"status":"Error connecting to instance"}
with open("user_data/hnslinks.json", "w") as f:
json.dump({"instance":instance,"key":key},f)
return {"status":"Connected to " + instance}
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"}
def hnslinks(params, authentication):
# Verify domain is owned by user
domain = params["domain"]
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"}
# Get wallet address
address = account.getAddress(wallet)
if address == "":
return {"status": "Error getting wallet address"}
# Sign message hns-links
response = account.signMessage(authentication,domain,"hns-links")
if "error" in response and response["error"]:
return {"status": f"Error: {response['error']}"}
return {"status": response}