Skip to content

Commit

Permalink
Namecoin: Add support for IMPORT DNS records
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyRand committed Dec 12, 2019
1 parent 9bde8bc commit 2a131be
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
19 changes: 19 additions & 0 deletions electrum_nmc/electrum/gui/qt/configure_dns_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def __init__(self, value, parent):
self.ui.btnSSHFPCreate.clicked.connect(self.create_sshfp_record)
self.ui.btnTXTCreate.clicked.connect(self.create_txt_record)
self.ui.btnSRVCreate.clicked.connect(self.create_srv_record)
self.ui.btnIMPORTCreate.clicked.connect(self.create_import_record)

self.ui.dialogButtons.accepted.connect(self.accept)
self.ui.dialogButtons.rejected.connect(self.reject)
Expand Down Expand Up @@ -299,6 +300,21 @@ def create_srv_record(self):
self.ui.editSRVPort.setText("")
self.ui.editSRVHost.setText("")

def create_import_record(self):
model = self.ui.listDNSRecords.model()
idx = model.rowCount()

domain = self.get_selected_domain()
imported_name = self.ui.editIMPORTName.text()
imported_subdomain = self.ui.editIMPORTSubdomain.text()

record = [domain, "import", [imported_name, imported_subdomain]]

self.insert_record(idx, record)

self.ui.editIMPORTName.setText("")
self.ui.editIMPORTSubdomain.setText("")

def has_freenet_record(self, domain):
for record in self.get_records():
record_domain, record_type, data = record
Expand Down Expand Up @@ -368,6 +384,9 @@ def insert_record(self, idx, record):
elif record_type == "srv":
formatted_record_type = "SRV"
formatted_data = json.dumps(data)
elif record_type == "import":
formatted_record_type = "IMPORT"
formatted_data = json.dumps(data)
else:
raise Exception("Unknown record type")

Expand Down
80 changes: 80 additions & 0 deletions electrum_nmc/electrum/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,12 @@ def get_domain_records(domain, value):
if value["srv"] == []:
del value["srv"]

if "import" in value:
new_records, value["import"] = get_domain_records_import(domain, value["import"])
records.extend(new_records)
if value["import"] == []:
del value["import"]

if "map" in value:
new_records, value["map"] = get_domain_records_map(domain, value["map"])
records.extend(new_records)
Expand Down Expand Up @@ -946,6 +952,54 @@ def get_domain_records_srv_single(domain, value):

return [domain, "srv", value], None

def get_domain_records_import(domain, value):
# Convert string to array (only 1 IMPORT record exists)
if type(value) == str:
value = [value]

# Must be array
if type(value) != list:
return [], value

# Parse each array item
records = []
remaining = []
for raw_address in value:
single_record, single_remaining = get_domain_records_import_single(domain, raw_address)
if single_record is not None:
records.append(single_record)
if single_remaining is not None:
remaining.append(single_remaining)

return records, remaining

def get_domain_records_import_single(domain, value):
# Convert string to array
if type(value) == str:
value = [value]

# Must be array
if type(value) != list:
return None, value

# Name must be present
if len(value) < 1:
return None, value

# Name must be a string
if type(value[0]) != str:
return None, value

# Don't process IMPORT records with unknown fields
if len(value) > 2:
return None, value

# Add Subdomain Selector if missing
if len(value) < 2:
value.append("")

return [domain, "import", value], None

def get_domain_records_map(domain, value):
# Must be dict
if type(value) != dict:
Expand Down Expand Up @@ -1024,6 +1078,8 @@ def add_domain_record(base_domain, value, record):
add_domain_record_txt(subdomain_value, data)
elif record_type == "srv":
add_domain_record_srv(subdomain_value, data)
elif record_type == "import":
add_domain_record_import(subdomain_value, data)

def add_domain_record_map(value, map_labels):
if len(map_labels) == 0:
Expand Down Expand Up @@ -1195,6 +1251,30 @@ def add_domain_record_srv(value, data):
# Add the record
value["srv"].append(data)

def add_domain_record_import(value, data):
# Make sure the field exists
if "import" not in value:
value["import"] = []

# Make sure the field is an array
if type(value["import"]) == str:
value["import"] = [value["import"]]

# Minimize empty Subdomain Selector if possible
if type(data) == list and len(data) == 2 and data[1] == "":
data = data[0]

# Minimize missing Subdomain Selector if possible
if type(data) == list and len(data) == 1:
data = data[0]

# Add the record
value["import"].append(data)

# Minimize to string form if possible
if len(value["import"]) == 1 and type(value["import"][0]) == str:
value["import"] = value["import"][0]


import binascii
from datetime import datetime, timedelta
Expand Down

0 comments on commit 2a131be

Please sign in to comment.