Visualize WIF with ability to import to WIF store

This commit is contained in:
scgbckbone 2026-03-05 19:40:04 +01:00 committed by Peter D. Gray
parent 00beed7b94
commit 5e3f7a9321
No known key found for this signature in database
GPG Key ID: A2DCD558C2BE5D7C
3 changed files with 91 additions and 8 deletions

View File

@ -1000,6 +1000,7 @@ class QRScannerInteraction:
elif what == "wif":
data, = vals
wif_str, key_pair, compressed, testnet = data
from wif import ux_visualize_wif
await ux_visualize_wif(wif_str, key_pair, compressed, testnet)
elif what == "vmsg":
@ -1140,14 +1141,6 @@ async def ux_visualize_bip21(proto, addr, args):
from ownership import OWNERSHIP
await OWNERSHIP.search_ux(addr, args)
async def ux_visualize_wif(wif_str, kp, compressed, testnet):
# TODO: remove until we support signing w/ WIF keys IMHO
from ux import ux_show_story
msg = wif_str + "\n\n"
msg += "chain: %s\n\n" % ("XTN" if testnet else "BTC")
msg += "private key hex:\n" + b2a_hex(kp.privkey()).decode() + "\n\n"
msg += "public key sec:\n" + b2a_hex(kp.pubkey().to_bytes(not compressed)).decode() + "\n\n"
await ux_show_story(msg, title="WIF")
async def qr_msg_sign_done(signature, address, text):
from ux import ux_show_story

View File

@ -44,6 +44,30 @@ def iter_wif_store_addresses(chain, addr_fmt):
yield i, chain.address(node, addr_fmt)
async def ux_visualize_wif(wif_str, kp, compressed, testnet):
ch_str = ("XTN" if testnet else "BTC")
sk = b2a_hex(kp.privkey()).decode()
pk = b2a_hex(kp.pubkey().to_bytes(not compressed)).decode()
msg = "%s\n\nchain: %s\n\nPrivkey:\n%s\n\nPubkey:\n%s" % (wif_str, ch_str, sk, pk)
esc = ""
if compressed and (testnet == (chains.current_chain().ctype != "BTC")):
# we only support compressed in WIF store
msg += "\n\nPress (1) to import to WIF Store."
esc += "1"
ch = await ux_show_story(msg, title="WIF", escape=esc)
if ch == "1":
saved = settings.get("wifs", [])
if (pk, sk) in saved:
await ux_show_story("Already saved in WIF Store.", title="Failure")
return
saved.append((pk, sk))
settings.set('wifs', saved)
settings.save()
await ux_show_story("Saved to WIF Store.", title="Success")
class WIFStore(MenuSystem):
MAX_ITEMS = 30

View File

@ -738,4 +738,70 @@ def test_wif_store_signing_with_master(fake_txn, start_sign, end_sign, cap_story
end_sign(finalize=True)
@pytest.mark.parametrize("wif", [
"KwYP78wzyiuShCqppuh1JZQCnKtFdAaY6HcDhRmhDy21vGSiF37N", # mainnet compressed
"5JwcuSWKH4PqV1mU8JSK9BBUkLjuAUS3MFHfP1w1qy9HjnXpavk", # mainnet uncompressed
"91cLPdroy4CtRYxWBXxgggqNnZrTz2CoJrLDkjDjcnkMP74gX5S", # testnet uncompressed
"cUR6JLQCmdPPt3op4jEYmFhjHpWC2AoZaWmZqoDaBQYMXN4QeKuc", # testnet compressed
])
@pytest.mark.parametrize("testnet", [True, False])
def test_visualize_wif(wif, testnet, is_q1, goto_home, need_keypress, use_testnet, use_mainnet,
scan_a_qr, cap_story, settings_remove, press_select):
if not is_q1:
raise pytest.skip("need scanner")
settings_remove("wifs")
if testnet:
use_testnet()
else:
use_mainnet()
goto_home()
need_keypress(KEY_QR)
scan_a_qr(wif)
time.sleep(1)
title, story = cap_story()
split_story = story.split("\n\n")
pubkey = split_story[3].split("\n")[-1]
if wif[0] in "59":
# uncompressed
assert pubkey[0:2] == "04"
assert len(pubkey) == 130
else:
# compressed
assert pubkey[0:2] in ["02", "03"]
assert len(pubkey) == 66
if testnet:
# we are on testnet, mainnet keys are not importable
if wif[0] in "K59":
assert "Press (1) to import to WIF Store" not in story
return
else:
# we are on mainnet, testnet keys are not importable
if wif[0] in "c59":
assert "Press (1) to import to WIF Store" not in story
return
assert "Press (1) to import to WIF Store" in story
need_keypress("1")
time.sleep(.1)
title, story = cap_story()
assert title == "Success"
assert "Saved to WIF Store" in story
press_select()
# try import same wif
goto_home()
need_keypress(KEY_QR)
scan_a_qr(wif)
time.sleep(1)
need_keypress("1")
time.sleep(.1)
title, story = cap_story()
assert title == "Failure"
assert "Already saved in WIF Store" in story
press_select()
# EOF