add method for resolving DIDs to names, and getting DID info for a given name

This commit is contained in:
Jude Nelson
2018-02-11 18:58:07 -05:00
parent 378c4cbe98
commit d1eec47ac8

View File

@@ -34,6 +34,7 @@ from ..config import *
from ..operations import *
from ..hashing import *
from ..scripts import get_namespace_from_name
from ..util import parse_DID
import virtualchain
from db import *
@@ -720,6 +721,62 @@ class BlockstackDB( virtualchain.StateEngine ):
return name_rec
def get_name_DID_info(self, name, lastblock=None):
"""
Given a name, find its DID (decentralized identifier) information.
Returns {'address': ..., 'index': ...}
"""
if lastblock is None:
lastblock = self.lastblock
cur = self.db.cursor()
name_rec = namedb_get_name(cur, name, lastblock, include_expired=True, include_history=False)
if name_rec is None:
return None
did_info = namedb_get_name_DID_info(cur, name)
if did_info is None:
return None
return did_info
def get_DID_name(self, did):
"""
Given a DID, get the name
Return None if not found, or if the name was revoked
Raise if the DID is invalid
"""
did = str(did)
did_info = None
try:
did_info = parse_DID(did)
assert did_info['name_type'] == 'name'
except Exception as e:
if BLOCKSTACK_DEBUG:
log.exception(e)
raise ValueError("Invalid DID: {}".format(did))
cur = self.db.cursor()
historic_name_info = namedb_get_historic_names_by_address(cur, did_info['address'], offset=did_info['index'], count=1)
if historic_name_info is None:
# no such name
return None
name = historic_name_info[0]['name']
name_rec = self.get_name(name, include_expired=True, include_history=False)
if name_rec is None:
# dead
return None
if name_rec['revoked']:
# revoked
return None
return name_rec
def get_name_at( self, name, block_number, include_expired=False ):
"""
Generate and return the sequence of of states a name record was in