def CreateAndRegisterEnclave(config):
    global enclave
    global txn_dependencies

    # if we are using the eservice then there is nothing to register since
    # the eservice has already registered the enclave
    if use_eservice:
        try:
            eservice_url = random.choice(
                config['Service']['EnclaveServiceURLs'])
            logger.info('use enclave service at %s', eservice_url)
            enclave = eservice_helper.EnclaveServiceClient(eservice_url)
            return enclave
        except Exception as e:
            logger.error('failed to contact enclave service; %s', str(e))
            sys.exit(-1)

    enclave_config = config.get('EnclaveModule')
    ledger_config = config.get('Sawtooth')

    try:
        enclave_helper.initialize_enclave(enclave_config)
        enclave = enclave_helper.Enclave.create_new_enclave()
    except Exception as e:
        logger.error('failed to initialize the enclave; %s', str(e))
        sys.exit(-1)

    try:
        if use_ledger:
            txnid = enclave.register_enclave(ledger_config)
            txn_dependencies.append(txnid)

            logger.info('enclave registration successful')

            enclave.verify_registration(ledger_config)
            logger.info('verified enclave registration')
        else:
            logger.debug('no ledger config; skipping enclave registration')
    except Exception as e:
        logger.error('failed to register the enclave; %s', str(e))
        ErrorShutdown()

    return enclave
def CreateAndRegisterEnclave(config):
    global enclave
    global txn_dependencies

    if use_eservice:
        eservice_url = config.get('eservice-url')
        logger.info('use enclave service at %s', eservice_url)
        enclave = eservice_helper.EnclaveServiceClient(eservice_url)
        return enclave

    enclave_config = config.get('EnclaveModule')
    ledger_config = config.get('Sawtooth')

    try:
        enclave_helper.initialize_enclave(enclave_config,
                                          enclave_type="intkey")
        enclave = enclave_helper.Enclave.create_new_enclave()
    except Exception as e:
        logger.error('failed to initialize the enclave; %s', str(e))
        sys.exit(-1)

    try:
        if use_ledger:
            txnid = enclave.register_enclave(ledger_config)
            txn_dependencies.append(txnid)

            logger.info('enclave registration successful')

            enclave.verify_registration(ledger_config)
            logger.info('verified enclave registration')
        else:
            logger.info('no ledger config; skipping enclave registration')
    except Exception as e:
        logger.error('failed to register the enclave; %s', str(e))
        sys.exit(-1)

    return enclave