예제 #1
0
def has_immutable_data( user_zonefile, data_hash ):
   """
   Does the given user have the given immutable data?
   Return True if so
   Return False if not
   """

   data_hash = str(data_hash)
   assert storage.is_valid_hash( data_hash ), "Invalid data hash '%s'" % data_hash

   if not user_zonefile.has_key('txt'):
       return False 

   for txtrec in user_zonefile['txt']:
       h = None
       try:
           h = get_immutable_hash_from_txt( txtrec['txt'] )
           assert storage.is_valid_hash(h)
       except:
           continue 

       if h == data_hash:
           return True 

   return False
예제 #2
0
def remove_immutable_data_zonefile( user_zonefile, data_hash ):
   """
   Remove a data hash from a user's zonefile.
   Return True if removed
   Return False if not present
   """

   data_hash = str(data_hash)
   assert storage.is_valid_hash( data_hash ), "Invalid data hash '%s'" % data_hash

   if not user_zonefile.has_key('txt'):
       return False 

   for txtrec in user_zonefile['txt']:
       h = None
       try:
           h = get_immutable_hash_from_txt( txtrec['txt'] )
           assert storage.is_valid_hash(h)
       except:
           continue 

       if h == data_hash:
           user_zonefile['txt'].remove(txtrec)
           return True

   return False
예제 #3
0
def put_immutable_data_zonefile( user_zonefile, data_id, data_hash, data_url=None ):
   """
   Add a data hash to a user's zonefile.  Make sure it's a valid hash as well.
   Return True on success
   Return False otherwise.
   """
 
   data_hash = str(data_hash)
   assert storage.is_valid_hash( data_hash )

   k = get_immutable_data_hash( user_zonefile, data_id )
   if k is not None and k == data_hash:
       # already there 
       return True

   elif k is not None:
       # name collision 
       return False 

   txtrec = None 
   if data_url is not None:
       txtrec = "%s#%s" % (data_url, data_hash)
   else: 
       txtrec = "#%s" % data_hash

   if not user_zonefile.has_key('txt'):
       user_zonefile['txt'] = []

   user_zonefile["txt"].append({
       "name": data_id,
       "txt": txtrec
   })

   return True
예제 #4
0
def get_immutable_hash_from_txt( txtrec ):
    """
    Given an immutable data txt record,
    get the hash.
    The hash is the suffix that begins with #.
    Return None if invalid or not present
    """
    if '#' not in txtrec:
        return None

    h = txtrec.split('#')[-1]
    if not storage.is_valid_hash( h ):
        return None

    return h
예제 #5
0
def list_immutable_data( user_zonefile ):
    """
    Get the IDs and hashes of all immutable data
    Return [(data ID, hash)]
    """
    ret = []
    if not user_zonefile.has_key('txt'):
        return ret 

    for txtrec in user_zonefile['txt']:
        try:
            d_id = txtrec['name']
            h = get_immutable_hash_from_txt( txtrec['txt'] )
            assert storage.is_valid_hash(h)
            ret.append( (d_id, h) )
        except:
            continue

    return ret
예제 #6
0
def has_immutable_data_id( user_zonefile, data_id ):
   """
   Does the given user have the given immutable data?
   Return True if so
   Return False if not
   """
   if not user_zonefile.has_key('txt'):
       return False 

   for txtrec in user_zonefile['txt']:
       d_id = None 
       try:
           d_id = txtrec['name']
           h = get_immutable_hash_from_txt( txtrec['txt'] )
           assert storage.is_valid_hash(h)
       except AssertionError:
           continue 

       if d_id == data_id:
           return True 

   return False
예제 #7
0
def get_immutable_data_hash( user_zonefile, data_id ):
   """
   Get the hash of an immutable datum by name.
   Return None if there is no match.
   Return the hash if there is one match.
   Return the list of hashes if there are multiple matches.
   """

   if not user_zonefile.has_key('txt'):
       return None 

   ret = None
   for txtrec in user_zonefile['txt']:
       d_id = None 
       h = None
       try:
           d_id = txtrec['name']
           if d_id != data_id:
               continue

           h = get_immutable_hash_from_txt( txtrec['txt'] )
           if h is None:
               log.error("Missing or invalid data hash for '%s'" % d_id)

           assert storage.is_valid_hash(h), "Invalid data hash for '%s' (got '%s' from %s)" % (d_id, h, txtrec['txt'])
       except AssertionError, ae:
           if os.environ.get("BLOCKSTACK_TEST", None) == "1":
               log.exception(ae)

           continue 

       if d_id == data_id:
           if ret is None:
               ret = h
           elif type(ret) != list:
               ret = [ret, h]
           else:
               ret.append(h)