def issue(app_config, certificate_batch_handler, transaction_handler):
    unsigned_certs_dir = app_config.unsigned_certificates_dir
    signed_certs_dir = app_config.signed_certificates_dir
    blockchain_certificates_dir = app_config.blockchain_certificates_dir
    work_dir = app_config.work_dir

    certificates_metadata = helpers.prepare_issuance_batch(unsigned_certs_dir, signed_certs_dir,
                                                           blockchain_certificates_dir, work_dir)
    num_certificates = len(certificates_metadata)
    if num_certificates < 1:
        logging.warning('No certificates to process')
        return None

    logging.info('Processing %d certificates under work path=%s', num_certificates, work_dir)
    certificate_batch_handler.set_certificates_in_batch(certificates_metadata)

    transaction_handler.ensure_balance()

    issuer = Issuer(
        certificate_batch_handler=certificate_batch_handler,
        transaction_handler=transaction_handler,
        max_retry=app_config.max_retry)
    tx_id = issuer.issue(app_config.bitcoin_chain)

    helpers.copy_output(certificates_metadata)

    logging.info('Your Blockchain Certificates are in %s', blockchain_certificates_dir)
    return tx_id
def issue(app_config, certificate_batch_handler, transaction_handler):
    unsigned_certs_dir = app_config.unsigned_certificates_dir
    signed_certs_dir = app_config.signed_certificates_dir
    blockchain_certificates_dir = app_config.blockchain_certificates_dir
    work_dir = app_config.work_dir

    certificates_metadata = helpers.prepare_issuance_batch(
        unsigned_certs_dir, signed_certs_dir, blockchain_certificates_dir,
        work_dir)
    num_certificates = len(certificates_metadata)
    if num_certificates < 1:
        logging.warning('No certificates to process')
        return None

    logging.info('Processing %d certificates under work path=%s',
                 num_certificates, work_dir)
    certificate_batch_handler.set_certificates_in_batch(certificates_metadata)

    transaction_handler.ensure_balance()

    issuer = Issuer(certificate_batch_handler=certificate_batch_handler,
                    transaction_handler=transaction_handler,
                    max_retry=app_config.max_retry)
    tx_id = issuer.issue(app_config.bitcoin_chain)

    helpers.copy_output(certificates_metadata)

    logging.info('Your Blockchain Certificates are in %s',
                 blockchain_certificates_dir)
    return tx_id
예제 #3
0
def update_token_uri(app_config, certificate_batch_handler,
                     transaction_handler, token_id, token_uri):
    issuer = Issuer(certificate_batch_handler=certificate_batch_handler,
                    transaction_handler=transaction_handler,
                    max_retry=app_config.max_retry)

    issuer.update_token_uri(app_config.chain, app_config, token_id, token_uri)
예제 #4
0
    def finish_tx(self, sent_tx_file_name, tx_id):
        Issuer.finish_tx(self, sent_tx_file_name, tx_id)
        # note that certificates are stored in an ordered dictionary, so we will iterate in the same order
        index = 0
        for uid, _ in self.certificates_to_issue.items():
            receipt = self.tree.make_receipt(index, tx_id)

            receipt_file_name = convert_file_name(
                self.config.receipts_file_pattern, uid)
            with open(receipt_file_name, 'w') as out_file:
                out_file.write(json.dumps(receipt))

            signed_cert_file_name = convert_file_name(
                self.config.signed_certs_file_pattern, uid)
            with open(signed_cert_file_name, 'r') as in_file:
                signed_cert = json.load(in_file)

            blockchain_cert = {
                '@context': 'https://w3id.org/blockcerts/v1',
                'type': 'BlockchainCertificate',
                'document': signed_cert,
                'receipt': receipt
            }
            blockchain_cert_file_name = convert_file_name(
                self.config.blockchain_certificates_file_pattern, uid)

            with open(blockchain_cert_file_name, 'w') as out_file:
                out_file.write(json.dumps(blockchain_cert))

            index += 1
예제 #5
0
 def get_cost_for_certificate_batch(self, dust_threshold, recommended_fee_per_transaction, satoshi_per_byte,
                                    allow_transfer):
     num_certificates = len(self.certificates_to_issue)
     num_outputs = Issuer.get_num_outputs(num_certificates)
     return Issuer.get_cost_for_certificate_batch(dust_threshold, recommended_fee_per_transaction,
                                                  satoshi_per_byte, num_outputs, allow_transfer, num_certificates,
                                                  num_certificates)
예제 #6
0
 def __init__(self, netcode, issuing_address, certificates_to_issue,
              connector, signer, batch_metadata, tx_cost_constants):
     Issuer.__init__(self, netcode, issuing_address, certificates_to_issue,
                     connector, signer)
     self.tree = MerkleTree(hash_f=sha256)
     self.batch_id = batch_metadata.batch_id
     self.batch_metadata = batch_metadata
     self.tx_cost_constants = tx_cost_constants
예제 #7
0
 def get_cost_for_certificate_batch(self, dust_threshold, recommended_fee_per_transaction, satoshi_per_byte,
                                    allow_transfer):
     '''
     Per certificate, we pay 2*min_per_output (which is based on dust) + fee. Note assumes 1 input
     per tx. We may also need to pay additional fees for splitting into temp addresses
     '''
     num_certificates = len(self.certificates_to_issue)
     num_outputs = Issuer.get_num_outputs(num_certificates)
     return Issuer.get_cost_for_certificate_batch(dust_threshold, recommended_fee_per_transaction, satoshi_per_byte,
                                                  num_outputs, allow_transfer, 1, 1)
예제 #8
0
 def get_cost_for_certificate_batch(self, allow_transfer):
     """
     Per certificate, we pay 2*min_per_output (which is based on dust) + fee. Note assumes 1 input
     per tx. We may also need to pay additional fees for splitting into temp addresses
     :param allow_transfer:
     :return:
     """
     num_certificates = len(self.certificates_to_issue)
     num_outputs = Issuer.get_num_outputs(num_certificates)
     return Issuer.get_cost_for_certificate_batch(num_outputs,
                                                  allow_transfer)
예제 #9
0
def issue(app_config, certificate_batch_handler, transaction_handler):
    certificate_batch_handler.pre_batch_actions(app_config)

    transaction_handler.ensure_balance()

    issuer = Issuer(certificate_batch_handler=certificate_batch_handler,
                    transaction_handler=transaction_handler,
                    max_retry=app_config.max_retry)
    tx_id = issuer.issue(app_config.chain)

    certificate_batch_handler.post_batch_actions(app_config)
    return tx_id
예제 #10
0
def issue(app_config, certificate_batch_handler, transaction_handler):
    certificate_batch_handler.pre_batch_actions(app_config)

    transaction_handler.ensure_balance()

    issuer = Issuer(certificate_batch_handler=certificate_batch_handler,
                    transaction_handler=transaction_handler,
                    max_retry=app_config.max_retry)
    tx_id = issuer.issue(app_config.chain)
    """"Este metodo copia del workingDir al blockchainCertDir"""
    #certificate_batch_handler.post_batch_actions(app_config)
    return tx_id
예제 #11
0
파일: apis.py 프로젝트: wenwkich/cert-web
    def post(self):
        json_data = request.get_json(force=True)
        # get recipient from the json_data
        fields = {}
        fields["identity"] = json_data['email']
        fields["name"] = json_data['name']
        recipient = Recipient(fields)

        # get the template from db
        template_col = DAO().get_collection('certificate_template')
        template = template_col.find_one({}, {"_id": 0})

        # get deep copy of the template as cert
        cert = deepcopy(template)

        # get the config
        config = Config().get()

        # call instantiate_assertion and instantiate_recipient
        uid = str(uuid.uuid4())
        issued_on = helpers.create_iso8601_tz()
        instantiate_assertion(cert, uid, issued_on)
        instantiate_recipient(cert, recipient,
                              config.additional_per_recipient_fields,
                              config.hash_emails)

        # get the config
        config = Config().get()

        # post it to blockchain
        # first instantiate handlers
        chain = config.chain
        if chain == Chain.ethereum_mainnet or chain == Chain.ethereum_ropsten:
            from cert_issuer.blockchain_handlers import ethereum
            certificate_batch_handler, transaction_handler, connector = ethereum.instantiate_blockchain_handlers(
                config, False)
        else:
            from cert_issuer.blockchain_handlers import bitcoin
            certificate_batch_handler, transaction_handler, connector = bitcoin.instantiate_blockchain_handlers(
                config, False)

        certificate_batch_handler.certificates_to_issue = [cert]
        issuer = Issuer(certificate_batch_handler=certificate_batch_handler,
                        transaction_handler=transaction_handler,
                        max_retry=config.max_retry)
        tx_id = issuer.issue(config.chain)

        issued_cert = certificate_batch_handler.certificates_to_issue[0]
        issued_cert_to_insert = deepcopy(issued_cert)
        cert_col = DAO().get_collection("certificates")
        cert_col.insert_one(issued_cert_to_insert)
        return issued_cert
예제 #12
0
def issue(app_config, certificate_batch_handler, transaction_handler):
    certificate_batch_handler.pre_batch_actions(app_config)

    transaction_handler.ensure_balance()

    issuer = Issuer(
        certificate_batch_handler=certificate_batch_handler,
        transaction_handler=transaction_handler,
        max_retry=app_config.max_retry)
    tx_id = issuer.issue(app_config.chain)

    certificate_batch_handler.post_batch_actions(app_config)
    return tx_id
예제 #13
0
def issue(app_config, certificate_batch_handler, transaction_handler,
          recipient_address, token_uri):
    certificate_batch_handler.pre_batch_actions(app_config)

    transaction_handler.ensure_balance()

    issuer = Issuer(certificate_batch_handler=certificate_batch_handler,
                    transaction_handler=transaction_handler,
                    max_retry=app_config.max_retry)
    (tx_id, token_id) = issuer.issue(app_config.chain, app_config,
                                     recipient_address, token_uri)

    certificate_batch_handler.post_batch_actions(app_config)
    return (tx_id, token_id)
예제 #14
0
def issue(app_config, certificate_batch_handler, transaction_handler,
          recipient_address, uidArray, token_uri):
    # Provide array of uidArrays to generate certificates
    certificate_batch_handler.pre_batch_actions(app_config, uidArray)

    transaction_handler.ensure_balance()

    issuer = Issuer(certificate_batch_handler=certificate_batch_handler,
                    transaction_handler=transaction_handler,
                    max_retry=app_config.max_retry)
    (tx_id, token_id) = issuer.issue(app_config.chain, app_config,
                                     recipient_address, token_uri)

    certificate_batch_handler.post_batch_actions(app_config)
    return (tx_id, token_id)
예제 #15
0
def main(app_config, secure_signer=None):
    unsigned_certs_dir = app_config.unsigned_certificates_dir
    signed_certs_dir = app_config.signed_certificates_dir
    blockchain_certificates_dir = app_config.blockchain_certificates_dir
    work_dir = app_config.work_dir
    v2 = app_config.v2
    issuing_address = app_config.issuing_address
    revocation_address = app_config.revocation_address  # not needed for v2

    certificates, batch_metadata = helpers.prepare_issuance_batch(
        unsigned_certs_dir, signed_certs_dir, blockchain_certificates_dir,
        work_dir)
    logging.info('Processing %d certificates under work path=%s',
                 len(certificates), work_dir)

    logging.info('Signing certificates...')
    if not secure_signer:
        secure_signer = secure_signer_helper.initialize_secure_signer(
            app_config)
    connector = ServiceProviderConnector(app_config.bitcoin_chain)
    tx_constants = TransactionCostConstants(app_config.tx_fee,
                                            app_config.dust_threshold,
                                            app_config.satoshi_per_byte)

    if v2:
        certificate_handler = CertificateV2Handler()
        transaction_handler = TransactionV2Handler(
            tx_cost_constants=tx_constants, issuing_address=issuing_address)
    else:
        certificate_handler = CertificateV1_2Handler()
        transaction_handler = TransactionV1_2Handler(
            tx_cost_constants=tx_constants,
            issuing_address=issuing_address,
            certificates_to_issue=certificates,
            revocation_address=revocation_address)
    certificate_batch_handler = CertificateBatchHandler(
        certificates_to_issue=certificates,
        certificate_handler=certificate_handler)
    issuer = Issuer(issuing_address=issuing_address,
                    connector=connector,
                    secure_signer=secure_signer,
                    certificate_batch_handler=certificate_batch_handler,
                    transaction_handler=transaction_handler)
    transaction_cost = issuer.calculate_cost_for_certificate_batch()
    logging.info('Total cost will be %d satoshis', transaction_cost)

    # ensure the issuing address has sufficient balance
    balance = connector.get_balance(issuing_address)

    if transaction_cost > balance:
        error_message = 'Please add {} satoshis to the address {}'.format(
            transaction_cost - balance, issuing_address)
        logging.error(error_message)
        raise InsufficientFundsError(error_message)

    tx_id = issuer.issue_certificates()

    blockcerts_tmp_dir = os.path.join(work_dir,
                                      helpers.BLOCKCHAIN_CERTIFICATES_DIR)
    if not os.path.exists(blockchain_certificates_dir):
        os.makedirs(blockchain_certificates_dir)
    for item in os.listdir(blockcerts_tmp_dir):
        s = os.path.join(blockcerts_tmp_dir, item)
        d = os.path.join(blockchain_certificates_dir, item)
        shutil.copy2(s, d)

    logging.info('Your Blockchain Certificates are in %s',
                 blockchain_certificates_dir)
    return tx_id
예제 #16
0
 def __init__(self, config, certificates_to_issue):
     Issuer.__init__(self, config, certificates_to_issue)
     self.batch_id = '%024x' % random.randrange(16**24)
     self.tree = MerkleTree(hash_f=sha256)
예제 #17
0
 def __init__(self, config, certificates_to_issue):
     Issuer.__init__(self, config, certificates_to_issue)