Skip to content
Open
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
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ jobs:
python3 -m compileall ../src ../test

tests:
runs-on: ubuntu-latest
# Frozen imports segfault on 26.04, including on master.
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
4 changes: 3 additions & 1 deletion src/apps/bip85.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from gui.decorators import on_release
from gui.screens import Menu, NumericScreen, QRAlert, Alert, Prompt
from gui.screens.mnemonic import MnemonicScreen
from helpers import mnemonic_to_standard_seedqr
import platform

class QRWithSD(QRAlert):
Expand Down Expand Up @@ -112,8 +113,9 @@ async def menu(self, show_screen):
Bip85MnemonicScreen(mnemonic=mnemonic, title=title, note=note)
)
if action == Bip85MnemonicScreen.QR:
seedqr = mnemonic_to_standard_seedqr(mnemonic)
await show_screen(
QRAlert(title=title, message=mnemonic, note=note)
QRAlert("%s as Standard SeedQR" % title, seedqr, note=note)
)
elif action == Bip85MnemonicScreen.SD:
fname = "bip85-%s-mnemonic-%d-%d.txt" % (
Expand Down
12 changes: 12 additions & 0 deletions src/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ def fix_mnemonic(phrase):
return bip39.mnemonic_from_bytes(entropy)


def mnemonic_to_standard_seedqr(mnemonic):
"""Encode a BIP39 mnemonic as a Standard SeedQR digit string."""
return "".join(
"%04d" % bip39.WORDLIST.index(word) for word in mnemonic.split()
)


def mnemonic_to_compact_seedqr(mnemonic):
"""Return BIP39 entropy bytes for a Compact SeedQR."""
return bip39.mnemonic_to_bytes(mnemonic)


def tagged_hash(tag: str, data: bytes) -> bytes:
"""BIP-Schnorr tag-specific key derivation"""
hashtag = hashlib.sha256(tag.encode()).digest()
Expand Down
13 changes: 9 additions & 4 deletions src/keystore/ram.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
from embit import ec, bip39, bip32
from embit.liquid import slip77
from embit.transaction import SIGHASH
from helpers import aead_encrypt, aead_decrypt, tagged_hash
from helpers import (
aead_encrypt,
aead_decrypt,
mnemonic_to_compact_seedqr,
mnemonic_to_standard_seedqr,
tagged_hash,
)
import secp256k1
from gui.screens import Alert, PinScreen, Prompt, Menu, QRAlert
from gui.screens.mnemonic import ExportMnemonicScreen
Expand Down Expand Up @@ -384,11 +390,10 @@ async def show_mnemonic(self):
if v == 255:
return
elif v == 1:
nums = [bip39.WORDLIST.index(w) for w in self.mnemonic.split()]
qr_msg = "".join([("000"+str(n))[-4:] for n in nums])
qr_msg = mnemonic_to_standard_seedqr(self.mnemonic)
msg = qr_msg
elif v == 2:
qr_msg = bip39.mnemonic_to_bytes(self.mnemonic)
qr_msg = mnemonic_to_compact_seedqr(self.mnemonic)
msg = hexlify(qr_msg).decode()
elif v == 3:
qr_msg = self.mnemonic
Expand Down
1 change: 1 addition & 0 deletions test/tests_native/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
from .test_transaction_confirmation import *
from .test_change_security import *
from .test_embed_git_info import *
from .test_seedqr import *
25 changes: 25 additions & 0 deletions test/tests_native/test_seedqr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from unittest import TestCase

from embit import bip39
from helpers import mnemonic_to_compact_seedqr, mnemonic_to_standard_seedqr


class SeedQRTest(TestCase):
def test_standard_seedqr_uses_zero_based_four_digit_indices(self):
mnemonic = "abandon abuse access zoo"
self.assertEqual(mnemonic_to_standard_seedqr(mnemonic), "0000000900102047")

def test_standard_and_compact_seedqr_preserve_bip39_entropy(self):
for word_count in (12, 18, 24):
entropy = bytes(word_count * 4 // 3)
mnemonic = bip39.mnemonic_from_bytes(entropy)
seedqr = mnemonic_to_standard_seedqr(mnemonic)
compact_seedqr = mnemonic_to_compact_seedqr(mnemonic)
decoded = " ".join(
bip39.WORDLIST[int(seedqr[i:i + 4])]
for i in range(0, len(seedqr), 4)
)
self.assertEqual(len(seedqr), word_count * 4)
self.assertEqual(decoded, mnemonic)
self.assertEqual(bip39.mnemonic_to_bytes(decoded), entropy)
self.assertEqual(compact_seedqr, entropy)
Loading