def ready(self): logger = logging.getLogger(__name__) cfg = init_config() base_api_url_path = PATH_PREFIX_SLASH.strip('/') role = (cfg['Agent']['role'] or '').lower().replace( ' ', '') # will be a dir as a pool name: spaces are evil p = None # the node pool p = NodePool('pool.{}'.format(role), cfg['Pool']['genesis.txn.path']) do(p.open()) assert p.handle cache.set('pool', p) ag = None if role == 'trust-anchor': bootstrap_json = cfg['Agent'] ag = TrustAnchorAgent(p, cfg['Agent']['seed'], 'wallet-{}'.format(role), None, cfg['Agent']['host'], int(cfg['Agent']['port']), base_api_url_path) do(ag.open()) assert ag.did tag_did = ag.did # register trust anchor if need be if not json.loads(do(ag.get_nym(ag.did))): do(ag.send_nym(ag.did, ag.verkey)) if not json.loads(do(ag.get_endpoint(ag.did))): do(ag.send_endpoint()) # send schema if need be, seeding schema cache en passant with open( pjoin(dirname(abspath(__file__)), 'protocol', 'schema-lookup.json'), 'r') as proto: j = proto.read() if not json.loads( do( ag.process_post( json.loads(j % (ag.did, cfg['Schema']['name'], cfg['Schema']['version']))))): with open( pjoin(dirname(abspath(__file__)), 'protocol', 'schema-send.json'), 'r') as proto: j = proto.read() schema = do( ag.process_post( json.loads(j % (ag.did, cfg['Schema']['name'], cfg['Schema']['version'])))) assert schema elif role in ('sri', 'the-org-book', 'bc-registrar'): logging.debug("check {} 1".format(role)) # create agent via factory by role if role == 'sri': ag = SRIAgent(p, cfg['Agent']['seed'], 'wallet-{}'.format(role), None, cfg['Agent']['host'], int(cfg['Agent']['port']), base_api_url_path) elif role == 'the-org-book': ag = OrgBookAgent(p, cfg['Agent']['seed'], 'wallet-{}'.format(role), None, cfg['Agent']['host'], int(cfg['Agent']['port']), PATH_PREFIX_SLASH.strip('/')) elif role == 'bc-registrar': ag = BCRegistrarAgent(p, cfg['Agent']['seed'], 'wallet-{}'.format(role), None, cfg['Agent']['host'], int(cfg['Agent']['port']), base_api_url_path) do(ag.open()) logging.debug("role {}; ag class {}".format( role, ag.__class__.__name__)) trust_anchor_host = cfg['Trust Anchor']['host'] trust_anchor_port = cfg['Trust Anchor']['port'] # trust anchor DID is necessary r = requests.get('http://{}:{}/{}/did'.format( trust_anchor_host, trust_anchor_port, base_api_url_path)) r.raise_for_status() tag_did = r.json() assert tag_did logging.debug("{}; tag_did {}".format(role, tag_did)) # get nym: if not registered; get trust-anchor host & port, post an agent-nym-send form if not json.loads(do(ag.get_nym(ag.did))): with open( pjoin(dirname(abspath(__file__)), 'protocol', 'agent-nym-send.json'), 'r') as proto: j = proto.read() logging.debug("{}; sending {}".format(role, j % (ag.did, ag.verkey))) r = requests.post('http://{}:{}/{}/agent-nym-send'.format( trust_anchor_host, trust_anchor_port, base_api_url_path), json=json.loads(j % (ag.did, ag.verkey))) r.raise_for_status() # get endpoint: if not present, send it if not json.loads(do(ag.get_endpoint(ag.did))): do(ag.send_endpoint()) # Post a schema_lookup, seeding schema cache and obviating need to specify schema in POST messages with open( pjoin(dirname(abspath(__file__)), 'protocol', 'schema-lookup.json'), 'r') as proto: j = proto.read() schema_json = do( ag.process_post( json.loads(j % (tag_did, cfg['Schema']['name'], cfg['Schema']['version'])))) assert json.loads(schema_json) if role in ('bc-registrar', 'sri'): # issuer send claim def do(ag.send_claim_def(schema_json)) logging.debug("\n== check {} 8".format(role)) if role in ('the-org-book', 'sri'): # set master secret from os import getpid # append pid to avoid re-using a master secret on restart of HolderProver agent; indy-sdk library # is shared, so it remembers and forbids it unless we shut down all processes do( ag.create_master_secret(cfg['Agent']['master.secret'] + '.' + str(getpid()))) else: raise ValueError('Unsupported agent role [{}]'.format(role)) assert ag is not None assert ag._schema_cache cache.set('agent', ag) atexit.register(_cleanup)
def go(): logger = logging.getLogger(__name__) cfg = do(mem_cache.get('config')) role = (cfg['Agent']['role'] or '').lower().replace( ' ', '') # will be a dir as a pool name: spaces are evil profile = environ.get('AGENT_PROFILE').lower().replace( ' ', '') # several profiles may share a role logger.debug('Starting agent; profile={}, role={}'.format( profile, role)) pool = NodePool('pool.{}'.format(profile), cfg['Pool']['genesis.txn.path']) do(pool.open()) assert pool.handle do(mem_cache.set('pool', pool)) ag = None if role == 'trust-anchor': bootstrap_json = cfg['Agent'] ag = TrustAnchorAgent( do(Wallet(pool, cfg['Agent']['seed'], profile).create()), BootSequence.agent_config_for(cfg)) do(ag.open()) assert ag.did tag_did = ag.did # register trust anchor if need be if not json.loads(do(ag.get_nym(ag.did))): do(ag.send_nym(ag.did, ag.verkey, ag.wallet.profile)) if not json.loads(do(ag.get_endpoint(ag.did))): do(ag.send_endpoint()) # originate schemata if need be do(BootSequence.originate(ag, cfg)) elif role in ('sri', 'org-book', 'bc-registrar'): # create agent by role if role == 'sri': ag = SRIAgent( do(Wallet(pool, cfg['Agent']['seed'], profile).create()), BootSequence.agent_config_for(cfg)) elif role == 'org-book': ag = OrgBookAgent( do(Wallet(pool, cfg['Agent']['seed'], profile).create()), BootSequence.agent_config_for(cfg)) elif role == 'bc-registrar': ag = BCRegistrarAgent( do(Wallet(pool, cfg['Agent']['seed'], profile).create()), BootSequence.agent_config_for(cfg)) do(ag.open()) logger.debug('profile {}; ag class {}'.format( profile, ag.__class__.__name__)) trust_anchor_base_url = 'http://{}:{}/api/v0'.format( cfg['Trust Anchor']['host'], cfg['Trust Anchor']['port']) # get nym: if not registered; get trust-anchor host & port, post an agent-nym-send form if not json.loads(do(ag.get_nym(ag.did))): try: r = requests.get('{}/did'.format(trust_anchor_base_url)) if not r.ok: logger.error( 'Agent {} nym is not on the ledger, but trust anchor is not responding' .format(profile)) r.raise_for_status() tag_did = r.json() logger.debug('{}; tag_did {}'.format(profile, tag_did)) assert tag_did with open( pjoin(BootSequence.dir_proto, 'agent-nym-send.json'), 'r') as proto: j = proto.read() logger.debug('{}; sending {}'.format( profile, j % (ag.did, ag.verkey))) r = requests.post( '{}/agent-nym-send'.format(trust_anchor_base_url), json=json.loads(j % (ag.did, ag.verkey))) r.raise_for_status() except Exception: raise ServerError( 'Agent {} requires Trust Anchor agent, but it is not responding' .format(profile)) # get endpoint: if not present, send it if not json.loads(do(ag.get_endpoint(ag.did))): do(ag.send_endpoint()) if role in ('bc-registrar', 'sri'): # originate schemata if need be do(BootSequence.originate(ag, cfg)) if role in ('org-book'): # set master secret from os import getpid # append pid to avoid re-using a master secret on restart of HolderProver agent; indy-sdk library # is shared, so it remembers and forbids it unless we shut down all processes do( ag.create_master_secret(cfg['Agent']['master.secret'] + '.' + str(getpid()))) else: raise ServerError( 'Agent profile {} configured for unsupported role {}'.format( profile, role)) assert ag is not None do(mem_cache.set('agent', ag))