async def hsm_status(self, h=None): # refresh HSM status b4 = STATUS.hsm.get('active', False) try: b4_nlc = STATUS.hsm.get('next_local_code') h = h or (await self.send_recv(CCProtocolPacker.hsm_status())) STATUS.hsm = h = json_loads(h) STATUS.notify_watchers() except MissingColdcard: h = {} if h.get('next_local_code') and STATUS.psbt_hash: if b4_nlc != h.next_local_code: STATUS.local_code = calc_local_pincode( a2b_hex(STATUS.psbt_hash), h.next_local_code) else: # won't be required STATUS.local_code = None # has it just transitioned into HSM mode? if STATUS.connected and STATUS.hsm.active and not b4: await self.activated_hsm() return STATUS.hsm
async def hsm_status(self, h=None): # refresh HSM status b4 = STATUS.hsm.get('active', False) try: b4_nlc = STATUS.hsm.get('next_local_code') h = h or (await self.send_recv(CCProtocolPacker.hsm_status())) STATUS.hsm = h = json_loads(h) STATUS.notify_watchers() except MissingColdcard: h = {} if h.get('next_local_code') and STATUS.psbt_hash: if b4_nlc != h.next_local_code: STATUS.local_code = calc_local_pincode( a2b_hex(STATUS.psbt_hash), h.next_local_code) else: # won't be required STATUS.local_code = None if ('summary' in h) and h.summary and not BP.get( 'priv_over_ux') and not BP.get('summary'): logging.info("Captured CC's summary of the policy") BP['summary'] = h.summary BP.save() # has it just transitioned into HSM mode? if STATUS.connected and STATUS.hsm.active and not b4: await self.activated_hsm() return STATUS.hsm
async def run(self): # connect to, and maintain a connection to a single Coldcard logging.info("Connecting to Coldcard.") while 1: try: if not self.serial and os.path.exists(settings.SIMULATOR_SOCK): # if simulator is running, just use it. sn = settings.SIMULATOR_SOCK else: sn = self.serial d = ColdcardDevice(sn=sn) logging.info(f"Found Coldcard {d.serial}.") await asyncio.get_running_loop().run_in_executor( executor, d.check_mitm) async with self.lock: self.dev = d except: logging.error("Cannot connect to Coldcard (will retry)", exc_info=0) await asyncio.sleep(settings.RECONNECT_DELAY) continue # stay connected, and check we are working periodically logging.info(f"Connected to Coldcard {self.dev.serial}.") STATUS.connected = True # read static info about coldcard STATUS.xfp = xfp2str(self.dev.master_fingerprint) STATUS.serial_number = self.dev.serial STATUS.is_testnet = (self.dev.master_xpub[0] == 't') STATUS.hsm = {} STATUS.reset_pending_auth() STATUS.notify_watchers() await self.hsm_status() while 1: await asyncio.sleep(settings.PING_RATE) try: # use long timeout here, even tho simple command, because the CC may # we working on something else right now (thinking). h = await self.send_recv(CCProtocolPacker.hsm_status(), timeout=20000) logging.info("ping ok") await self.hsm_status(h) except MissingColdcard: self._conn_broken() break except: logging.error("Ping failed", exc_info=1)
def hsm_status(): """ Get current status of HSM feature. Is it running, what is the policy (summary only). """ with get_device() as dev: dev.check_mitm() resp = dev.send_recv(CCProtocolPacker.hsm_status()) o = json.loads(resp) click.echo(pformat(o))
def hsm_status(): ''' Get current status of HSM feature. Is it running, what is the policy (summary only). ''' dev = ColdcardDevice(sn=force_serial) dev.check_mitm() resp = dev.send_recv(CCProtocolPacker.hsm_status()) o = json.loads(resp) click.echo(pformat(o))
def user_auth(psbt_file, next_code=None): """ Generate the 6-digit code needed for a specific PSBT file to authorize it's signing on the Coldcard in HSM mode. """ if not next_code: with get_device() as dev: dev.check_mitm() resp = dev.send_recv(CCProtocolPacker.hsm_status()) o = json.loads(resp) assert o['active'], "Coldcard not in HSM mode" next_code = o['next_local_code'] psbt_hash = sha256(psbt_file.read()).digest() rv = calc_local_pincode(psbt_hash, next_code) print("Local authorization code is:\n\n\t%s\n" % rv)