Skip to content

Commit

Permalink
SFT-2988: moved message signing to new menu, improved UI
Browse files Browse the repository at this point in the history
  • Loading branch information
mjg-foundation committed Dec 15, 2023
1 parent eaae5ca commit 72cdf8b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
class SignElectrumMessageFlow(Flow):
def __init__(self):
self.message = None
self.address = None
self.signature = None
super().__init__(initial_state=self.scan_message, name='Sign Electrum Message Flow')

async def scan_message(self):
Expand All @@ -37,28 +39,35 @@ async def validate_message(self):
from pages import ErrorPage
from utils import validate_sign_text

msg = uio.StringIO(self.message)
header = msg.readline()
self.message = self.message[len(header)::]
# msg = uio.StringIO(self.message)
# header = msg.readline()
# self.message = self.message[len(header)::]

# header_elements = header.split(' ')

parts = self.message.split(':', 1)
self.message = parts[1]
header_elements = parts[0].split(' ')

print("header:")
print(header)
print(header_elements)
print("message:")
print(self.message)

header_elements = header.split(' ')

if header_elements[0] != 'signmessage':
await ErrorPage('Not a valid message to sign').show()
self.set_result(False)
return

if header_elements[2] != 'ascii:\n':
if header_elements[2] != 'ascii':
await ErrorPage('Unsupported message type').show()
self.set_result(False)
return

(self.subpath, error) = validate_sign_text(self.message, header_elements[1], space_limit=False)
(self.subpath, error) = validate_sign_text(self.message,
header_elements[1],
space_limit=False,
check_whitespace=False)

if error:
await ErrorPage(error).show()
Expand All @@ -72,39 +81,49 @@ async def validate_message(self):
async def show_message(self):
from pages import LongTextPage, QuestionPage
import microns
from wallets.utils import get_addr_type_from_deriv
from public_constants import AF_CLASSIC, AF_P2WPKH
import stash
from utils import stylize_address

result = await LongTextPage(centered=True,
text=self.message,
card_header={'title': 'Message'}).show()

self.address

if not result:
self.back()
self.set_result(False)
return

result = await QuestionPage(text='Sign message with path {}?'.format(self.subpath),
self.addr_format = get_addr_type_from_deriv(self.subpath)

if self.addr_format is None:
self.addr_format = AF_CLASSIC

print('addr_format: {}, AF_P2WPKH: {}, AF_CLASSIC: {}'.format(self.addr_format, AF_P2WPKH, AF_CLASSIC))

with stash.SensitiveValues() as sv:
node = sv.derive_path(self.subpath)
self.address = sv.chain.address(node, self.addr_format)

self.address = stylize_address(self.address)

result = await QuestionPage(text='Sign message with this address?\n\n{}'.format(self.address),
right_micron=microns.Sign).show()

if not result:
self.set_result(False)
return

self.goto(self.do_sign)

async def do_sign(self):
from wallets.utils import get_addr_type_from_deriv
from public_constants import AF_CLASSIC, AF_P2WPKH

addr_format = get_addr_type_from_deriv(self.subpath)

if addr_format is None:
addr_format = AF_CLASSIC

print('addr_format: {}, AF_P2WPKH: {}, AF_CLASSIC: {}'.format(addr_format, AF_P2WPKH, AF_CLASSIC))

(signature, address, error) = await spinner_task('Signing Message', sign_text_file_task,
args=[self.message, self.subpath, addr_format])
args=[self.message, self.subpath, self.addr_format])

if error is None:
self.signature = signature
self.address = address
self.goto(self.show_signed)
else:
# TODO: Refactor this to a simpler, common error handler page?
Expand All @@ -117,13 +136,11 @@ async def show_signed(self):
import microns
from utils import bytes_to_hex_str

caption = 'Signed by {}'.format(self.address)
qr_data = bytes_to_hex_str(self.signature)
print(qr_data)
print(type(qr_data))

result = await ShowQRPage(qr_data=qr_data,
right_micron=microns.Checkmark,
caption=caption).show()
right_micron=microns.Checkmark).show()

self.set_result(result)
20 changes: 14 additions & 6 deletions ports/stm32/boards/Passport/modules/menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,39 @@
def manage_account_menu():
from flows import (RenameAccountFlow,
DeleteAccountFlow,
ConnectWalletFlow,
AddressExplorerFlow)
ConnectWalletFlow)
from pages import AccountDetailsPage

return [
{'icon': 'ICON_FOLDER', 'label': 'Account Details', 'page': AccountDetailsPage},
{'icon': 'ICON_INFO', 'label': 'Rename Account', 'flow': RenameAccountFlow},
{'icon': 'ICON_CONNECT', 'label': 'Connect Wallet', 'flow': ConnectWalletFlow,
'statusbar': {'title': 'CONNECT'}},
{'icon': 'ICON_CANCEL', 'label': 'Delete Account', 'flow': DeleteAccountFlow},
]


def account_tools():
from flows import VerifyAddressFlow, SignElectrumMessageFlow, AddressExplorerFlow

return [
{'icon': 'ICON_VERIFY_ADDRESS', 'label': 'Verify Address', 'flow': VerifyAddressFlow},
{'icon': 'ICON_SCAN_QR', 'label': 'Sign a message', 'flow': SignElectrumMessageFlow,
'statusbar': {'title': 'SIGN MESSAGE'}},
{'icon': 'ICON_VERIFY_ADDRESS', 'label': 'Explore Addresses', 'flow': AddressExplorerFlow,
'statusbar': {'title': 'LIST ADDRESSES'}},
{'icon': 'ICON_CANCEL', 'label': 'Delete Account', 'flow': DeleteAccountFlow},
]


def account_menu():
from flows import VerifyAddressFlow, SignPsbtQRFlow, SignPsbtMicroSDFlow, SignElectrumMessageFlow
from flows import SignPsbtQRFlow, SignPsbtMicroSDFlow

return [
{'icon': 'ICON_SCAN_QR', 'label': 'Sign with QR Code', 'flow': SignPsbtQRFlow,
'statusbar': {'title': 'SIGN'}},
{'icon': 'ICON_MICROSD', 'label': 'Sign with microSD', 'flow': SignPsbtMicroSDFlow,
'statusbar': {'title': 'SIGN'}},
{'icon': 'ICON_VERIFY_ADDRESS', 'label': 'Verify Address', 'flow': VerifyAddressFlow},
{'icon': 'ICON_SCAN_QR', 'label': 'Sign a message', 'flow': SignElectrumMessageFlow},
{'icon': 'ICON_FOLDER', 'label': 'Account Tools', 'submenu': account_tools},
{'icon': 'ICON_FOLDER', 'label': 'Manage Account', 'submenu': manage_account_menu},
]

Expand Down
11 changes: 6 additions & 5 deletions ports/stm32/boards/Passport/modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,13 +1278,14 @@ def is_passphrase_active():
MSG_MAX_SPACES = 4


def validate_sign_text(text, subpath, space_limit=True):
def validate_sign_text(text, subpath, space_limit=True, check_whitespace=True):
# Check for leading or trailing whitespace
if text[0] == ' ':
return (subpath, 'File contains leading whitespace.')
if check_whitespace:
if text[0] == ' ':
return (subpath, 'File contains leading whitespace.')

if text[-1] == ' ':
(subpath, 'File contains trailing whitespace.')
if text[-1] == ' ':
return (subpath, 'File contains trailing whitespace.')

# Ensure characters are in range and not too many spaces
run = 0
Expand Down

0 comments on commit 72cdf8b

Please sign in to comment.