Skip to content

Commit

Permalink
Added warnings for Testnet and made the setting volatile
Browse files Browse the repository at this point in the history
  • Loading branch information
coreylakey committed Aug 19, 2021
1 parent 8714067 commit 4a49a7d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 17 deletions.
28 changes: 27 additions & 1 deletion ports/stm32/boards/Passport/modules/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from utils import (UXStateMachine, imported, pretty_short_delay, xfp2str, to_str,
truncate_string_to_width, set_next_addr, scan_for_address, get_accounts, run_chooser,
make_account_name_num, is_valid_address, save_next_addr, needs_microsd, format_btc_address,
is_all_zero, bytes_to_hex_str, split_to_lines, is_valid_btc_address, do_address_verify)
is_all_zero, bytes_to_hex_str, split_to_lines, is_valid_btc_address, do_address_verify, run_chooser)
from wallets.utils import get_export_mode, get_addr_type_from_address, get_deriv_path_from_addr_type_and_acct
from ux import (the_ux, ux_confirm, ux_enter_pin,
ux_enter_text, ux_scan_qr_code, ux_shutdown,
Expand Down Expand Up @@ -1128,6 +1128,9 @@ async def sign_tx_from_sd(*a):

import stash

# Let the user know that using Testnet is potentially dangerous
await show_testnet_warning()

if stash.bip39_passphrase:
title = '[%s]' % xfp2str(settings.get('xfp'))
else:
Expand Down Expand Up @@ -1323,6 +1326,9 @@ async def magic_scan(menu, label, item):

title = item.arg

# Let the user know that using Testnet is potentially dangerous
await show_testnet_warning()

while True:
system.turbo(True)
data = await ux_scan_qr_code(title)
Expand Down Expand Up @@ -2065,3 +2071,23 @@ async def remove_user_firmware_pubkey(*a):
center=True,
center_vertically=True)
clear_cached_pubkey()

async def show_testnet_warning(*a):
chain = settings.get('chain', 'BTC')
if chain == 'TBTC':
await ux_show_story('Passport is in Testnet mode. Use a separate seed to avoid issues with malicious software wallets.',
title='Warning',
center=True,
center_vertically=True)

async def test_chooser(*a):
from choosers import chain_chooser

old_chain = settings.get('chain', 'BTC')
await run_chooser(chain_chooser, 'Passport', show_checks=True)
new_chain = settings.get('chain', 'BTC')

# Only display the warning if the chain changed (user selected something)
if new_chain != old_chain:
# Let the user know that using Testnet is potentially dangerous
await show_testnet_warning()
15 changes: 9 additions & 6 deletions ports/stm32/boards/Passport/modules/chains.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,29 +150,30 @@ def hash_message(cls, msg=None, msg_len=0):

@classmethod
def render_value(cls, val, unpad=False):
from common import settings

# convert nValue from a transaction into either BTC or sats
# - always be precise
# - return (string, units label)
from common import settings
from constants import UNIT_TYPE_BTC, UNIT_TYPE_SATS

# BTC is the default if not set yet
units = settings.get('units', 'BTC')
if units == 'BTC':
units = settings.get('units', UNIT_TYPE_BTC)
if units == UNIT_TYPE_BTC:
label = cls.ctype
if unpad:
if (val % 1E8):
# precise but unpadded
txt = ('%d.%08d' % (val // 1E8, val % 1E8)).rstrip('0')
else:
# round BTC amount, show no decimal

txt = '%d' % (val // 1E8)
else:
# all the zeros
txt = '%d.%08d' % (val // 1E8, val % 1E8)
else:
label = cls.ctype_sats
txt = ('%d' % (val))
return txt, units
return txt, label

@classmethod
def render_address(cls, script):
Expand Down Expand Up @@ -202,6 +203,7 @@ def render_address(cls, script):
class BitcoinMain(ChainsBase):
# see <https://github.com/bitcoin/bitcoin/blob/master/src/chainparams.cpp#L140>
ctype = 'BTC'
ctype_sats = 'sats'
name = 'Bitcoin'
core_name = 'Bitcoin Core'
menu_name = 'Bitcoin Mainnet'
Expand All @@ -224,6 +226,7 @@ class BitcoinMain(ChainsBase):

class BitcoinTestnet(BitcoinMain):
ctype = 'TBTC'
ctype_sats = 'tsats'
name = 'Bitcoin Testnet'
menu_name = 'Bitcoin Testnet'

Expand Down
16 changes: 9 additions & 7 deletions ports/stm32/boards/Passport/modules/choosers.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def chain_chooser():
def set_chain(idx, text):
val = ch[idx][0]
assert ch[idx][1] == text
settings.set('chain', val)
settings.set_volatile('chain', val)

try:
# update xpub stored in settings
Expand All @@ -103,14 +103,16 @@ def set_chain(idx, text):
return which, [t for _,t in ch], set_chain

def units_chooser():
DEFAULT_UNITS = "BTC"
import chains
from constants import UNIT_TYPE_BTC, UNIT_TYPE_SATS

units = settings.get('units', DEFAULT_UNITS)
chain = chains.current_chain()
units = settings.get('units', UNIT_TYPE_BTC)

ch = ['BTC',
'sats']
val = ['BTC',
'sats']
ch = [chain.ctype,
chain.ctype_sats]
val = [UNIT_TYPE_BTC,
UNIT_TYPE_SATS]

try:
which = val.index(units)
Expand Down
4 changes: 4 additions & 0 deletions ports/stm32/boards/Passport/modules/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@
MAX_MULTISIG_NAME_LEN = 20

DEFAULT_ACCOUNT_ENTRY = {'name': 'Primary', 'acct_num': 0}

# Unit types for labeling conversions
UNIT_TYPE_BTC = 0
UNIT_TYPE_SATS = 1
4 changes: 2 additions & 2 deletions ports/stm32/boards/Passport/modules/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from multisig import make_multisig_menu
from wallets.utils import has_export_mode
from export import view_backup_password
from utils import is_new_wallet_in_progress, get_accounts, is_screenshot_mode_enabled
from utils import is_new_wallet_in_progress, get_accounts, is_screenshot_mode_enabled, run_chooser
from new_wallet import pair_new_wallet
from ie import show_browser

Expand Down Expand Up @@ -70,7 +70,7 @@ def has_pubkey():
MenuItem('MicroSD Settings', menu=SDCardMenu),
MenuItem('View Seed Words', f=view_seed_words, predicate=lambda: settings.get('words', True)),
MenuItem('Developer PubKey', menu=DeveloperPubkeyMenu, menu_title='Developer'),
MenuItem('Testnet', chooser=chain_chooser),
MenuItem('Testnet', f=test_chooser),
MenuItem('Units', chooser=units_chooser),
MenuItem('Erase Passport', f=erase_wallet, arg=True)
]
Expand Down
2 changes: 1 addition & 1 deletion ports/stm32/boards/Passport/modules/stash.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def capture_xpub(self):
settings.set_volatile('xfp', xfp)
settings.set_volatile('xpub', xpub)

settings.set('chain', self.chain.ctype)
settings.set_volatile('chain', self.chain.ctype)
settings.set('words', (self.mode == 'words'))

def register(self, item):
Expand Down

0 comments on commit 4a49a7d

Please sign in to comment.