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

Wallet output descriptor load from and save to SD card #275

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/krux/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,5 @@ def screensaver(self):
anim_frame = 0
bg_color, fg_color = fg_color, bg_color

if self.input.wait_for_press(block=False) is not None:
if self.input.wait_for_button(block=False) is not None:
break
68 changes: 57 additions & 11 deletions src/krux/pages/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
PUBKEY_FILE_EXTENSION,
PSBT_FILE_EXTENSION,
SIGNED_FILE_SUFFIX,
JSON_FILE_EXTENSION,
)

# to start xpub value without the xpub/zpub/ypub prefix
Expand Down Expand Up @@ -214,14 +215,37 @@ def wallet(self):
else:
self.display_wallet(self.ctx.wallet)
wallet_data, qr_format = self.ctx.wallet.wallet_qr()
title = t("Wallet output descriptor")
self.print_standard_qr(
wallet_data, qr_format, t("Wallet output descriptor")
wallet_data, qr_format, title
)

# Try to save the Wallet output descriptor on the SD card
if self.has_sd_card():
from .files_operations import SaveFile

save_page = SaveFile(self.ctx)
save_page.save_file(
self.ctx.wallet.wallet_data,
self.ctx.wallet.label,
self.ctx.wallet.label,
title + ":",
JSON_FILE_EXTENSION,
)
return MENU_CONTINUE

def _load_wallet(self):
wallet_data, qr_format = self.capture_qr_code()
if wallet_data is None:
# Try to read the wallet output descriptor from a file on the SD card
qr_format = FORMAT_NONE
try:
_, wallet_data = self._load_file(JSON_FILE_EXTENSION)
except OSError:
pass

if wallet_data is None:
# Both the camera and the file on SD card failed!
self.flash_text(t("Failed to load output descriptor"), theme.error_color)
return MENU_CONTINUE

Expand Down Expand Up @@ -415,29 +439,51 @@ def display_wallet(self, wallet, include_qr=True):
"""
about = wallet.label + "\n"
if wallet.is_multisig():
xpubs = []
for i, xpub in enumerate(wallet.policy["cosigners"]):
xpubs.append(
str(i + 1)
+ ". "
+ xpub[WALLET_XPUB_START : WALLET_XPUB_START + WALLET_XPUB_DIGITS]
+ ".."
+ xpub[len(xpub) - WALLET_XPUB_DIGITS :]
import binascii

fingerprints = []
for i, key in enumerate(wallet.descriptor.keys):
fingerprints.append(
str(i + 1) + ". " + binascii.hexlify(key.fingerprint).decode()
)
about += "\n".join(xpubs)
about += "\n".join(fingerprints)
else:
about += wallet.key.fingerprint_hex_str()
xpub = wallet.key.xpub()
about += (
xpub[WALLET_XPUB_START : WALLET_XPUB_START + WALLET_XPUB_DIGITS]
"\n"
+ xpub[WALLET_XPUB_START : WALLET_XPUB_START + WALLET_XPUB_DIGITS]
+ ".."
+ xpub[len(xpub) - WALLET_XPUB_DIGITS :]
)

if include_qr:
wallet_data, qr_format = wallet.wallet_qr()
self.display_qr_codes(wallet_data, qr_format, title=about)
else:
self.ctx.display.draw_hcentered_text(about, offset_y=DEFAULT_PADDING)

# If multisig, show loaded wallet again with all XPUB
if wallet.is_multisig():
about = wallet.label + "\n"
xpubs = []
for i, xpub in enumerate(wallet.policy["cosigners"]):
xpubs.append(
str(i + 1)
+ ". "
+ xpub[WALLET_XPUB_START : WALLET_XPUB_START + WALLET_XPUB_DIGITS]
+ ".."
+ xpub[len(xpub) - WALLET_XPUB_DIGITS :]
)
about += "\n".join(xpubs)

if include_qr:
wallet_data, qr_format = wallet.wallet_qr()
self.display_qr_codes(wallet_data, qr_format, title=about)
else:
self.ctx.input.wait_for_button()
self.ctx.display.draw_hcentered_text(about, offset_y=DEFAULT_PADDING)

def print_standard_qr(self, data, qr_format, title="", width=33):
"""Loads printer driver and UI"""
if self.print_qr_prompt():
Expand Down
1 change: 1 addition & 0 deletions src/krux/sd_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

SIGNED_FILE_SUFFIX = "-signed"
PSBT_FILE_EXTENSION = ".psbt"
JSON_FILE_EXTENSION = ".json"
SIGNATURE_FILE_EXTENSION = ".sig"
PUBKEY_FILE_EXTENSION = ".pub"

Expand Down
Loading