def start_show_p2sh_address(M, N, addr_format, xfp_paths, witdeem_script): # Show P2SH address to user, also returns it. # - first need to find appropriate multisig wallet associated # - they must provide full redeem script, and we will re-verify it and check pubkeys inside it import ustruct from multisig import MultisigWallet, MultisigOutOfSpace try: assert addr_format in SUPPORTED_ADDR_FORMATS assert addr_format & AFC_SCRIPT except: raise AssertionError('Unknown/unsupported addr format') # Search for matching multisig wallet that we must already know about xfps = [i[0] for i in xfp_paths] idx = MultisigWallet.find_match(M, N, xfps) assert idx >= 0, 'Multisig wallet with those fingerprints not found' ms = MultisigWallet.get_by_idx(idx) assert ms assert ms.M == M assert ms.N == N global active_request UserAuthorizedAction.check_busy(ShowAddressBase) active_request = ShowP2SHAddress(ms, addr_format, xfp_paths, witdeem_script) # kill any menu stack, and put our thing at the top abort_and_goto(active_request) # provide the value back to attached desktop return active_request.address
def maybe_enroll_xpub(sf_len=None, config=None, name=None, ux_reset=False): # Offer to import (enroll) a new multisig wallet. Allow reject by user. global active_request from multisig import MultisigWallet UserAuthorizedAction.cleanup() if sf_len: with SFFile(TXN_INPUT_OFFSET, length=sf_len) as fd: config = fd.read(sf_len).decode() # this call will raise on parsing errors, so let them rise up # and be shown on screen/over usb ms = MultisigWallet.from_file(config, name=name) active_request = NewEnrollRequest(ms) if ux_reset: # for USB case, and import from PSBT # kill any menu stack, and put our thing at the top abort_and_goto(active_request) else: # menu item case: add to stack from ux import the_ux the_ux.push(active_request)
def hsm_status_report(): # Return a JSON-able object. Documented and external programs # rely on this output... and yet, don't overshare either. from auth import UserAuthorizedAction from main import hsm_active, settings from hsm_ux import ApproveHSMPolicy rv = dict() rv['active'] = bool(hsm_active) if not hsm_active: rv['policy_available'] = hsm_policy_available() ar = UserAuthorizedAction.active_request if ar and isinstance(ar, ApproveHSMPolicy): # we are waiting for local user to approve entry into HSM mode rv['approval_wait'] = True rv['users'] = Users.list() rv['wallets'] = [ms.name for ms in MultisigWallet.get_all()] rv['chain'] = settings.get('chain', 'BTC') if hsm_active: hsm_active.status_report(rv) return rv
def __init__(self, j, idx): # read json dict provided self.spent_so_far = 0 # for velocity def check_user(u): if not Users.valid_username(u): raise ValueError("Unknown user: %s" % u) return u self.index = idx + 1 self.per_period = pop_int(j, 'per_period', 0, MAX_SATS) self.max_amount = pop_int(j, 'max_amount', 0, MAX_SATS) self.users = pop_list(j, 'users', check_user) self.whitelist = pop_list(j, 'whitelist', cleanup_whitelist_value) self.min_users = pop_int(j, 'min_users', 1, len(self.users)) self.local_conf = pop_bool(j, 'local_conf') self.wallet = pop_string(j, 'wallet', 1, 20) assert sorted(set(self.users)) == sorted(self.users), 'dup users' # usernames need to be correct and already known if self.min_users is None: self.min_users = len(self.users) if self.users else None else: # redundant w/ code in pop_int() above assert 1 <= self.min_users <= len(self.users), "range" # if specified, 'wallet' must be an existing multisig wallet's name if self.wallet and self.wallet != '1': names = [ms.name for ms in MultisigWallet.get_all()] assert self.wallet in names, "unknown MS wallet: " + self.wallet assert_empty_dict(j)
async def render(self): # Choose from a truncated list of index 0 common addresses, remember # the last address the user selected and use it as the default from glob import dis chain = chains.current_chain() dis.fullscreen('Wait...') with stash.SensitiveValues() as sv: # Create list of choices (address_index_0, path, addr_fmt) choices = [] for name, path, addr_fmt in chains.CommonDerivations: if '{coin_type}' in path: path = path.replace('{coin_type}', str(chain.b44_cointype)) if self.account_num != 0 and '{account}' not in path: # skip derivations that are not affected by account number continue deriv = path.format(account=self.account_num, change=0, idx=0) node = sv.derive_path(deriv, register=False) address = chain.address(node, addr_fmt) choices.append( (truncate_address(address), path, addr_fmt) ) dis.progress_bar_show(len(choices) / len(chains.CommonDerivations)) stash.blank_object(node) items = [MenuItem(address, f=self.pick_single, arg=(path, addr_fmt)) for i, (address, path, addr_fmt) in enumerate(choices)] # some other choices if self.account_num == 0: items.append(MenuItem("Account Number", f=self.change_account)) items.append(MenuItem("Custom Path", menu=self.make_custom)) # if they have MS wallets, add those next for ms in MultisigWallet.iter_wallets(): if not ms.addr_fmt: continue items.append(MenuItem(ms.name, f=self.pick_multisig, arg=ms)) else: items.append(MenuItem("Account: %d" % self.account_num, f=self.change_account)) self.goto_idx(settings.get('axi', 0)) # weak self.replace_items(items)
def generate_public_contents(): # Generate public details about wallet. # # simple text format: # key = value # or #comments # but value is JSON from main import settings from public_constants import AF_CLASSIC num_rx = 5 chain = chains.current_chain() with stash.SensitiveValues() as sv: yield ('''\ # Coldcard Wallet Summary File ## For wallet with master key fingerprint: {xfp} Wallet operates on blockchain: {nb} For BIP44, this is coin_type '{ct}', and internally we use symbol {sym} for this blockchain. ## IMPORTANT WARNING Do **not** deposit to any address in this file unless you have a working wallet system that is ready to handle the funds at that address! ## Top-level, 'master' extended public key ('m/'): {xpub} What follows are derived public keys and payment addresses, as may be needed for different systems. '''.format(nb=chain.name, xpub=chain.serialize_public(sv.node), sym=chain.ctype, ct=chain.b44_cointype, xfp=xfp2str(sv.node.my_fingerprint()))) for name, path, addr_fmt in chains.CommonDerivations: if '{coin_type}' in path: path = path.replace('{coin_type}', str(chain.b44_cointype)) if '{' in name: name = name.format(core_name=chain.core_name) show_slip132 = ('Core' not in name) yield ('''## For {name}: {path}\n\n'''.format(name=name, path=path)) yield ( '''First %d receive addresses (account=0, change=0):\n\n''' % num_rx) submaster = None for i in range(num_rx): subpath = path.format(account=0, change=0, idx=i) # find the prefix of the path that is hardneded if "'" in subpath: hard_sub = subpath.rsplit("'", 1)[0] + "'" else: hard_sub = 'm' if hard_sub != submaster: # dump the xpub needed if submaster: yield "\n" node = sv.derive_path(hard_sub, register=False) yield ("%s => %s\n" % (hard_sub, chain.serialize_public(node))) if show_slip132 and addr_fmt != AF_CLASSIC and ( addr_fmt in chain.slip132): yield ( "%s => %s ##SLIP-132##\n" % (hard_sub, chain.serialize_public(node, addr_fmt))) submaster = hard_sub node.blank() del node # show the payment address node = sv.derive_path(subpath, register=False) yield ('%s => %s\n' % (subpath, chain.address(node, addr_fmt))) node.blank() del node yield ('\n\n') from multisig import MultisigWallet if MultisigWallet.exists(): yield '\n# Your Multisig Wallets\n\n' from uio import StringIO for ms in MultisigWallet.get_all(): fp = StringIO() ms.render_export(fp) print("\n---\n", file=fp) yield fp.getvalue() del fp
async def handle(self, cmd, args): # Dispatch incoming message, and provide reply. from main import hsm_active, is_devmode try: cmd = bytes(cmd).decode() except: raise FramingError('decode') if cmd[0].isupper() and (is_simulator() or is_devmode): # special hacky commands to support testing w/ the simulator try: from usb_test_commands import do_usb_command return do_usb_command(cmd, args) except: raise pass if hsm_active: # only a few commands are allowed during HSM mode if cmd not in HSM_WHITELIST: raise HSMDenied if cmd == 'dfu_': # only useful in factory, undocumented. return self.call_after(callgate.enter_dfu) if cmd == 'rebo': import machine return self.call_after(machine.reset) if cmd == 'logo': from utils import clean_shutdown return self.call_after(clean_shutdown) if cmd == 'ping': return b'biny' + args if cmd == 'upld': offset, total_size = unpack_from('<II', args) data = memoryview(args)[4 + 4:] return await self.handle_upload(offset, total_size, data) if cmd == 'dwld': offset, length, fileno = unpack_from('<III', args) return await self.handle_download(offset, length, fileno) if cmd == 'ncry': version, his_pubkey = unpack_from('<I64s', args) return self.handle_crypto_setup(version, his_pubkey) if cmd == 'vers': from version import get_mpy_version, hw_label from callgate import get_bl_version # Returning: date, version(human), bootloader version, full date version # BUT: be ready for additions! rv = list(get_mpy_version()) rv.insert(2, get_bl_version()[0]) rv.append(hw_label) return b'asci' + ('\n'.join(rv)).encode() if cmd == 'sha2': return b'biny' + self.file_checksum.digest() if cmd == 'xpub': assert self.encrypted_req, 'must encrypt' return self.handle_xpub(args) if cmd == 'mitm': assert self.encrypted_req, 'must encrypt' return await self.handle_mitm_check() if cmd == 'smsg': # sign message addr_fmt, len_subpath, len_msg = unpack_from('<III', args) subpath = args[12:12 + len_subpath] msg = args[12 + len_subpath:] assert len(msg) == len_msg, "badlen" from auth import sign_msg sign_msg(msg, subpath, addr_fmt) return None if cmd == 'p2sh': # show P2SH (probably multisig) address on screen (also provides it back) # - must provide redeem script, and list of [xfp+path] from auth import start_show_p2sh_address if hsm_active and not hsm_active.approve_address_share( is_p2sh=True): raise HSMDenied # new multsig goodness, needs mapping from xfp->path and M values addr_fmt, M, N, script_len = unpack_from('<IBBH', args) assert addr_fmt & AFC_SCRIPT assert 1 <= M <= N <= 20 assert 30 <= script_len <= 520 offset = 8 witdeem_script = args[offset:offset + script_len] offset += script_len assert len(witdeem_script) == script_len xfp_paths = [] for i in range(N): ln = args[offset] assert 1 <= ln <= 16, 'badlen' xfp_paths.append(unpack_from('<%dI' % ln, args, offset + 1)) offset += (ln * 4) + 1 assert offset == len(args) return b'asci' + start_show_p2sh_address(M, N, addr_fmt, xfp_paths, witdeem_script) if cmd == 'show': # simple cases, older code: text subpath from auth import start_show_address addr_fmt, = unpack_from('<I', args) assert not (addr_fmt & AFC_SCRIPT) return b'asci' + start_show_address(addr_fmt, subpath=args[4:]) if cmd == 'enrl': # Enroll new xpubkey to be involved in multisigs. # - text config file must already be uploaded file_len, file_sha = unpack_from('<I32s', args) if file_sha != self.file_checksum.digest(): return b'err_Checksum' assert 100 < file_len <= (20 * 200), "badlen" # Start an UX interaction, return immediately here from auth import maybe_enroll_xpub maybe_enroll_xpub(sf_len=file_len, ux_reset=True) return None if cmd == 'msck': # Quick check to test if we have a wallet already installed. from multisig import MultisigWallet M, N, xfp_xor = unpack_from('<3I', args) return int(MultisigWallet.quick_check(M, N, xfp_xor)) if cmd == 'stxn': # sign transaction txn_len, flags, txn_sha = unpack_from('<II32s', args) if txn_sha != self.file_checksum.digest(): return b'err_Checksum' assert 50 < txn_len <= MAX_TXN_LEN, "bad txn len" from auth import sign_transaction sign_transaction(txn_len, (flags & STXN_FLAGS_MASK), txn_sha) return None if cmd == 'stok' or cmd == 'bkok' or cmd == 'smok' or cmd == 'pwok': # Have we finished (whatever) the transaction, # which needed user approval? If so, provide result. from auth import UserAuthorizedAction req = UserAuthorizedAction.active_request if not req: return b'err_No active request' if req.refused: UserAuthorizedAction.cleanup() return b'refu' if req.failed: rv = b'err_' + req.failed.encode() UserAuthorizedAction.cleanup() return rv if not req.result: # STILL waiting on user return None if cmd == 'pwok': # return new root xpub xpub = req.result UserAuthorizedAction.cleanup() return b'asci' + bytes(xpub, 'ascii') elif cmd == 'smok': # signed message done: just give them the signature addr, sig = req.address, req.result UserAuthorizedAction.cleanup() return pack('<4sI', 'smrx', len(addr)) + addr.encode() + sig else: # generic file response resp_len, sha = req.result UserAuthorizedAction.cleanup() return pack('<4sI32s', 'strx', resp_len, sha) if cmd == 'pass': # bip39 passphrase provided, maybe use it if authorized assert self.encrypted_req, 'must encrypt' from auth import start_bip39_passphrase from main import settings assert settings.get('words', True), 'no seed' assert len(args) < 400, 'too long' pw = str(args, 'utf8') assert len(pw) < 100, 'too long' return start_bip39_passphrase(pw) if cmd == 'back': # start backup: asks user, takes long time. from auth import start_remote_backup return start_remote_backup() if cmd == 'blkc': # report which blockchain we are configured for from chains import current_chain chain = current_chain() return b'asci' + chain.ctype if cmd == 'bagi': return self.handle_bag_number(args) if has_fatram: # HSM and user-related features only larger-memory Mk3 if cmd == 'hsms': # HSM mode "start" -- requires user approval if args: file_len, file_sha = unpack_from('<I32s', args) if file_sha != self.file_checksum.digest(): return b'err_Checksum' assert 2 <= file_len <= (200 * 1000), "badlen" else: file_len = 0 # Start an UX interaction but return (mostly) immediately here from hsm_ux import start_hsm_approval await start_hsm_approval(sf_len=file_len, usb_mode=True) return None if cmd == 'hsts': # can always query HSM mode from hsm import hsm_status_report import ujson return b'asci' + ujson.dumps(hsm_status_report()) if cmd == 'gslr': # get the value held in the Storage Locker assert hsm_active, 'need hsm' return b'biny' + hsm_active.fetch_storage_locker() # User Mgmt if cmd == 'nwur': # new user from users import Users auth_mode, ul, sl = unpack_from('<BBB', args) username = bytes(args[3:3 + ul]).decode('ascii') secret = bytes(args[3 + ul:3 + ul + sl]) return b'asci' + Users.create(username, auth_mode, secret).encode('ascii') if cmd == 'rmur': # delete user from users import Users ul, = unpack_from('<B', args) username = bytes(args[1:1 + ul]).decode('ascii') return Users.delete(username) if cmd == 'user': # auth user (HSM mode) from users import Users totp_time, ul, tl = unpack_from('<IBB', args) username = bytes(args[6:6 + ul]).decode('ascii') token = bytes(args[6 + ul:6 + ul + tl]) if hsm_active: # just queues these details, can't be checked until PSBT on-hand hsm_active.usb_auth_user(username, token, totp_time) return None else: # dryrun/testing purposes: validate only, doesn't unlock nothing return b'asci' + Users.auth_okay(username, token, totp_time).encode('ascii') print("USB garbage: %s +[%d]" % (cmd, len(args))) return b'err_Unknown cmd'
async def handle(self, cmd, args): # Dispatch incoming message, and provide reply. try: cmd = bytes(cmd).decode() except: raise FramingError('decode') if cmd == 'dfu_': # only useful in factory, undocumented. return self.call_after(callgate.enter_dfu) if cmd == 'rebo': import machine return self.call_after(machine.reset) if cmd == 'logo': return self.call_after(callgate.show_logout) if cmd == 'ping': return b'biny' + args if cmd == 'upld': offset, total_size = unpack_from('<II', args) data = memoryview(args)[4 + 4:] return await self.handle_upload(offset, total_size, data) if cmd == 'dwld': offset, length, fileno = unpack_from('<III', args) return await self.handle_download(offset, length, fileno) if cmd == 'ncry': version, his_pubkey = unpack_from('<I64s', args) return self.handle_crypto_setup(version, his_pubkey) if cmd == 'vers': from version import get_mpy_version from callgate import get_bl_version # Returning: date, version(human), bootloader version, full date version # BUT: be ready for additions! rv = list(get_mpy_version()) rv.insert(2, get_bl_version()[0]) return b'asci' + ('\n'.join(rv)).encode() if cmd == 'sha2': return b'biny' + self.file_checksum.digest() if cmd == 'xpub': assert self.encrypted_req, 'must encrypt' return self.handle_xpub(str(args, 'ascii')) if cmd == 'mitm': assert self.encrypted_req, 'must encrypt' return await self.handle_mitm_check() if cmd == 'smsg': # sign message addr_fmt, len_subpath, len_msg = unpack_from('<III', args) subpath = args[12:12 + len_subpath] msg = args[12 + len_subpath:] assert len(msg) == len_msg, "badlen" from auth import sign_msg sign_msg(msg, subpath, addr_fmt) return None if cmd == 'p2sh': # show P2SH (probably multisig) address on screen (also provides it back) # - must provide redeem script, and list of [xfp+path] from auth import start_show_p2sh_address # new multsig goodness, needs mapping from xfp->path and M values addr_fmt, M, N, script_len = unpack_from('<IBBH', args) assert addr_fmt & AFC_SCRIPT assert 1 <= M <= N <= 20 assert 30 <= script_len <= 520 offset = 8 witdeem_script = args[offset:offset + script_len] offset += script_len assert len(witdeem_script) == script_len xfp_paths = [] for i in range(N): ln = args[offset] assert 2 <= ln <= 16, 'badlen' xfp_paths.append(unpack_from('<%dI' % ln, args, offset + 1)) offset += (ln * 4) + 1 assert offset == len(args) return b'asci' + start_show_p2sh_address(M, N, addr_fmt, xfp_paths, witdeem_script) if cmd == 'show': # simple cases, older code: text subpath from auth import start_show_address addr_fmt, = unpack_from('<I', args) assert not (addr_fmt & AFC_SCRIPT) return b'asci' + start_show_address(addr_fmt, subpath=args[4:]) if cmd == 'enrl': # Enroll new xpubkey to be involved in multisigs. # - text config file must already be uploaded file_len, file_sha = unpack_from('<I32s', args) if file_sha != self.file_checksum.digest(): return b'err_Checksum' assert 100 < file_len <= (20 * 200), "badlen" # Start an UX interaction, return immediately here from auth import maybe_enroll_xpub maybe_enroll_xpub(sf_len=file_len, ux_reset=True) return None if cmd == 'msck': # Quick check to test if we have a wallet already installed. from multisig import MultisigWallet M, N, xfp_xor = unpack_from('<3I', args) return int(MultisigWallet.quick_check(M, N, xfp_xor)) if cmd == 'stxn': # sign transaction txn_len, finalize, txn_sha = unpack_from('<II32s', args) if txn_sha != self.file_checksum.digest(): return b'err_Checksum' assert 50 < txn_len <= MAX_TXN_LEN, "bad txn len" from auth import sign_transaction sign_transaction(txn_len, bool(finalize)) return None if cmd == 'stok' or cmd == 'bkok' or cmd == 'smok' or cmd == 'pwok' or cmd == 'enok': # Have we finished (whatever) the transaction, # which needed user approval? If so, provide result. from auth import active_request, UserAuthorizedAction if not active_request: return b'err_No active request' if active_request.refused: UserAuthorizedAction.cleanup() return b'refu' if active_request.failed: rv = b'err_' + active_request.failed.encode() UserAuthorizedAction.cleanup() return rv if not active_request.result: # STILL waiting on user return None if cmd == 'pwok' or cmd == 'enok': # return new root xpub xpub = active_request.result UserAuthorizedAction.cleanup() return b'asci' + bytes(xpub, 'ascii') elif cmd == 'smok': # signed message done: just give them the signature addr, sig = active_request.address, active_request.result UserAuthorizedAction.cleanup() return pack('<4sI', 'smrx', len(addr)) + addr.encode() + sig else: # generic file response resp_len, sha = active_request.result UserAuthorizedAction.cleanup() return pack('<4sI32s', 'strx', resp_len, sha) if cmd == 'pass': # bip39 passphrase provided, maybe use it if authorized assert self.encrypted_req, 'must encrypt' from auth import start_bip39_passphrase from main import settings assert settings.get('words', True), 'no seed' assert len(args) < 400, 'too long' pw = str(args, 'utf8') assert len(pw) < 100, 'too long' return start_bip39_passphrase(pw) if cmd == 'back': # start backup: asks user, takes long time. from auth import start_remote_backup return start_remote_backup() if cmd == 'bagi': return self.handle_bag_number(args) if is_simulator() and cmd[0].isupper(): # special hacky commands to support testing w/ the simulator from sim_usb import do_usb_command return do_usb_command(cmd, args) print("USB garbage: %s +[%d]" % (cmd, len(args))) return b'err_Unknown cmd'