示例#1
0
def delete_immutable_data( data_hash, txid, privkey ):
   """
   Given the hash of the data, the private key of the user,
   and the txid that deleted the data's hash from the blockchain,
   delete the data from all immutable data stores.
   """

   global storage_handlers

   # sanity check
   if not keys.is_singlesig(privkey):
       log.error("Only single-signature data private keys are supported")
       return False

   data_hash = str(data_hash)
   txid = str(txid)
   sigb64 = sign_raw_data( data_hash + txid, privkey )

   for handler in storage_handlers:

      if not hasattr( handler, "delete_immutable_handler" ):
         continue

      try:

         handler.delete_immutable_handler( data_hash, txid, sigb64 )
      except Exception, e:

         log.exception( e )
         return False
示例#2
0
def delete_mutable_data(fq_data_id, privatekey, only_use=None):
    """
   Given the data ID and private key of a user,
   go and delete the associated mutable data.
   """

    global storage_handlers

    only_use = [] if only_use is None else only_use

    # sanity check
    if not keys.is_singlesig(privatekey):
        log.error("Only single-signature data private keys are supported")
        return False

    fq_data_id = str(fq_data_id)
    assert is_fq_data_id(fq_data_id) or is_name_valid(
        fq_data_id
    ), "Data ID must be fully qualified or must be a valid blockchain ID (got %s)" % fq_data_id

    sigb64 = sign_raw_data(fq_data_id, privatekey)

    # remove data
    for handler in storage_handlers:

        if not hasattr(handler, "delete_mutable_handler"):
            continue

        if len(only_use) > 0 and handler.__name__ in only_use:
            log.debug("Skip storage driver %s" % handler.__name__)
            continue

        try:

            handler.delete_mutable_handler(fq_data_id, sigb64)
        except Exception, e:

            log.exception(e)
            return False
示例#3
0
def put_mutable_data( fq_data_id, data_json, privatekey, required=None, use_only=None ):
   """
   Given the unserialized data, store it into our mutable data stores.
   Do so in a best-effort way.  This method only fails if all storage providers fail.

   @fq_data_id is the fully-qualified data id.  It must be prefixed with the username,
   to avoid collisions in shared mutable storage.

   Return True on success
   Return False on error
   """

   global storage_handlers 

   required = [] if required is None else required
   use_only = [] if use_only is None else use_only 

   # sanity check: only support single-sig private keys 
   if not keys.is_singlesig(privatekey):
       log.error("Only single-signature data private keys are supported")
       return False

   fq_data_id = str(fq_data_id)
   assert is_fq_data_id( fq_data_id ) or is_name_valid(fq_data_id), "Data ID must be fully qualified or must be a valid blockchain ID (got %s)" % fq_data_id
   assert privatekey is not None

   fqu = None
   if is_fq_data_id(fq_data_id):
       fqu = fq_data_id.split(":")[0]
   else:
       fqu = fq_data_id

   serialized_data = serialize_mutable_data( data_json, privatekey )
   successes = 0

   log.debug("put_mutable_data(%s), required=%s" % (fq_data_id, ",".join(required)))

   for handler in storage_handlers:

      if not hasattr( handler, "put_mutable_handler" ):
          if handler.__name__ in required:
              log.debug("Failed to replicate with required storage provider '%s'" % handler.__name__)
              return None
          else:
              continue

      if len(use_only) > 0 and handler.__name__ not in use_only:
          log.debug("Skipping storage driver '%s'" % handler.__name__)
          continue

      rc = False

      try:
         log.debug("Try '%s'" % handler.__name__)
         rc = handler.put_mutable_handler( fq_data_id, serialized_data, fqu=fqu )
      except Exception, e:
         log.exception( e )
         if handler.__name__ in required:
             log.debug("Failed to replicate with required storage provider '%s'" % handler.__name__)
             return None 
         else:
             continue

      if not rc:
         if handler.__name__ in required:
             log.debug("Failed to replicate with required storage provider '%s'" % handler.__name__)
             return None 
         else:
             log.debug("Failed to replicate with '%s'" % handler.__name__)
             continue

      else:
         successes += 1