示例#1
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
示例#2
0
    def create_transactions(self, wallet, revocation_address,
                            issuing_transaction_cost, split_input_trxs):
        # finish tree
        self.tree.make_tree()

        op_return_value = unhexlify(self.tree.get_merkle_root())

        unspent_outputs = wallet.get_unspent_outputs(self.issuing_address)
        last_output = unspent_outputs[-1]

        txouts = self.build_txouts(issuing_transaction_cost)
        txouts = txouts + [
            trx_utils.create_transaction_output(
                revocation_address, issuing_transaction_cost.min_per_output)
        ]

        tx = trx_utils.create_trx(op_return_value, issuing_transaction_cost,
                                  self.issuing_address, txouts, last_output)

        unsigned_tx_file_name = convert_file_name(
            self.config.unsigned_txs_file_pattern, self.batch_id)
        unsent_tx_file_name = convert_file_name(
            self.config.signed_txs_file_pattern, self.batch_id)
        sent_tx_file_name = convert_file_name(
            self.config.sent_txs_file_pattern, self.batch_id)

        td = TransactionData(uid=self.batch_id,
                             tx=tx,
                             tx_input=last_output,
                             op_return_value=hexlify(op_return_value),
                             unsigned_tx_file_name=unsigned_tx_file_name,
                             signed_tx_file_name=unsent_tx_file_name,
                             sent_tx_file_name=sent_tx_file_name)

        return [td]
示例#3
0
    def create_transactions(self, revocation_address,
                            issuing_transaction_cost):
        """
        Create the batch Bitcoin transaction
        :param revocation_address:
        :param issuing_transaction_cost:
        :return:
        """
        self.tree.make_tree()

        spendables = get_unspent_outputs(self.issuing_address)
        if not spendables:
            error_message = 'No money to spend at address {}'.format(
                self.issuing_address)
            logging.error(error_message)
            raise InsufficientFundsError(error_message)

        last_input = spendables[-1]

        op_return_value = unhexlify(self.tree.get_merkle_root())

        tx_outs = self.build_recipient_tx_outs()
        tx_outs.append(
            trx_utils.create_transaction_output(
                revocation_address, issuing_transaction_cost.min_per_output))

        transaction = trx_utils.create_trx(op_return_value,
                                           issuing_transaction_cost,
                                           self.issuing_address, tx_outs,
                                           last_input)

        unsigned_tx_file_name = convert_file_name(
            self.config.unsigned_txs_file_pattern, self.batch_id)
        unsent_tx_file_name = convert_file_name(
            self.config.signed_txs_file_pattern, self.batch_id)
        sent_tx_file_name = convert_file_name(
            self.config.sent_txs_file_pattern, self.batch_id)

        transaction_data = TransactionData(
            uid=self.batch_id,
            tx=transaction,
            tx_input=last_input,
            op_return_value=hexlify(op_return_value),
            unsigned_tx_file_name=unsigned_tx_file_name,
            signed_tx_file_name=unsent_tx_file_name,
            sent_tx_file_name=sent_tx_file_name)

        return [transaction_data]
示例#4
0
def archive_files(from_pattern, archive_dir, to_pattern, batch_id):
    """
    Archives files matching from_pattern and renames to to_pattern based on uid in a batch folder
    :param from_pattern:
    :param archive_dir
    :param to_pattern:
    :param batch_id:
    :return:
    """
    # place archived files in a timestamped folder
    archive_folder = os.path.join(archive_dir, batch_id)
    if not os.path.isdir(archive_folder):
        os.mkdir(archive_folder)
        os.mkdir(os.path.join(archive_folder, 'unsigned_certs'))
        os.mkdir(os.path.join(archive_folder, 'signed_certs'))
        os.mkdir(os.path.join(archive_folder, 'sent_txs'))
        os.mkdir(os.path.join(archive_folder, 'receipts'))
        os.mkdir(os.path.join(archive_folder, 'blockchain_certificates'))

    archived_file_pattern = os.path.join(archive_folder, to_pattern)
    [
        shutil.move(filename,
                    models.convert_file_name(archived_file_pattern, uid))
        for filename, (uid, ) in glob2.iglob(from_pattern, with_matches=True)
    ]
示例#5
0
    def create_transactions(self, wallet, revocation_address,
                            issuing_transaction_cost, split_input_trxs):

        unspent_outputs = wallet.get_unspent_outputs(self.issuing_address)
        current_tail = -1

        txs = []
        for uid, certificate_metadata in self.certificates_to_issue.items():
            last_output = unspent_outputs[current_tail]

            with open(certificate_metadata.certificate_hash_file_name,
                      'r') as in_file:
                op_return_value = in_file.read()

            # send a transaction to the recipient's public key, and to a
            # revocation address
            txouts = trx_utils.create_recipient_outputs(
                issuing_transaction_cost.min_per_output,
                certificate_metadata.public_key, revocation_address)

            tx = trx_utils.create_trx(unhexlify(op_return_value),
                                      issuing_transaction_cost,
                                      self.issuing_address, txouts,
                                      last_output)

            unsigned_tx_file_name = convert_file_name(
                self.config.unsigned_txs_file_pattern, uid)
            unsent_tx_file_name = convert_file_name(
                self.config.signed_txs_file_pattern, uid)
            sent_tx_file_name = convert_file_name(
                self.config.sent_txs_file_pattern, uid)
            td = TransactionData(uid=certificate_metadata.uid,
                                 tx=tx,
                                 tx_input=last_output,
                                 op_return_value=op_return_value,
                                 unsigned_tx_file_name=unsigned_tx_file_name,
                                 signed_tx_file_name=unsent_tx_file_name,
                                 sent_tx_file_name=sent_tx_file_name)
            txs.append(td)
            if split_input_trxs:
                current_tail -= 1
        return txs
示例#6
0
def archive_files(from_pattern, to_pattern, timestamp):
    """
    Archives files matching from_pattern and renames to to_pattern based on uid
    :param from_pattern:
    :param to_pattern:
    :param timestamp:
    :return:
    """
    # todo: change to move
    [
        shutil.copyfile(
            filename,
            models.convert_file_name(to_pattern, uid) + '-' + timestamp)
        for filename, (uid, ) in glob2.iglob(from_pattern, with_matches=True)
    ]
def archive_files(from_pattern, archive_dir, to_pattern, batch_id):
    """
    Archives files matching from_pattern and renames to to_pattern based on uid in a batch folder
    :param from_pattern:
    :param archive_dir
    :param to_pattern:
    :param batch_id:
    :return:
    """
    # place archived files in a timestamped folder
    archive_folder = os.path.join(archive_dir, batch_id)
    if not os.path.isdir(archive_folder):
        os.mkdir(archive_folder)
        os.mkdir(os.path.join(archive_folder, 'unsigned_certs'))
        os.mkdir(os.path.join(archive_folder, 'signed_certs'))
        os.mkdir(os.path.join(archive_folder, 'sent_txs'))
        os.mkdir(os.path.join(archive_folder, 'receipts'))
        os.mkdir(os.path.join(archive_folder, 'blockchain_certificates'))

    archived_file_pattern = os.path.join(archive_folder, to_pattern)
    [shutil.move(filename, models.convert_file_name(archived_file_pattern, uid))
     for filename, (uid,) in glob2.iglob(from_pattern, with_matches=True)]