Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PASS1-67: Change unit to sats in settings #46

Merged
merged 5 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2072,3 +2078,23 @@ async def remove_user_firmware_pubkey(*a):
center=True,
center_vertically=True)
clear_cached_pubkey()

async def show_testnet_warning(*a):
FoundationKen marked this conversation as resolved.
Show resolved Hide resolved
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):
FoundationKen marked this conversation as resolved.
Show resolved Hide resolved
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)
FoundationKen marked this conversation as resolved.
Show resolved Hide resolved
if new_chain != old_chain:
FoundationKen marked this conversation as resolved.
Show resolved Hide resolved
# Let the user know that using Testnet is potentially dangerous
await show_testnet_warning()
32 changes: 22 additions & 10 deletions ports/stm32/boards/Passport/modules/chains.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,30 @@ def hash_message(cls, msg=None, msg_len=0):

@classmethod
def render_value(cls, val, unpad=False):
# convert nValue from a transaction into human form.
# convert nValue from a transaction into either BTC or sats
# - always be precise
# - return (string, units label)
if unpad:
if (val % 1E8):
# precise but unpadded
txt = ('%d.%08d' % (val // 1E8, val % 1E8)).rstrip('0')
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', 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:
# round BTC amount, show no decimal
txt = '%d' % (val // 1E8)
# all the zeros
txt = '%d.%08d' % (val // 1E8, val % 1E8)
else:
# all the zeros
txt = '%d.%08d' % (val // 1E8, val % 1E8)
return txt, cls.ctype
label = cls.ctype_sats
txt = ('%d' % (val))
return txt, label

@classmethod
def render_address(cls, script):
Expand Down Expand Up @@ -193,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 @@ -215,6 +226,7 @@ class BitcoinMain(ChainsBase):

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

Expand Down
24 changes: 23 additions & 1 deletion 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 @@ -102,4 +102,26 @@ def set_chain(idx, text):

return which, [t for _,t in ch], set_chain

def units_chooser():
import chains
from constants import UNIT_TYPE_BTC, UNIT_TYPE_SATS

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

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

try:
which = val.index(units)
except ValueError:
which = 1
FoundationKen marked this conversation as resolved.
Show resolved Hide resolved

def set_units(idx, text):
settings.set('units', val[idx])

return which, ch, set_units

# EOF
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 @@ -40,5 +40,9 @@

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

# Unit types for labeling conversions
UNIT_TYPE_BTC = 0
UNIT_TYPE_SATS = 1

# Maximum amount of characters in a text entry screen
MAX_MESSAGE_LEN = 64
5 changes: 3 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,8 @@ 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),
FoundationKen marked this conversation as resolved.
Show resolved Hide resolved
MenuItem('Units', chooser=units_chooser),
FoundationKen marked this conversation as resolved.
Show resolved Hide resolved
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)
FoundationKen marked this conversation as resolved.
Show resolved Hide resolved
settings.set('words', (self.mode == 'words'))

def register(self, item):
Expand Down