From a77edf6cf4788b7b9c246b6da98c5a6d7e939619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Degros?= <57880064+fdegros@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:48:11 +1100 Subject: [PATCH] Add --backgrounds and --no-backgrounds options The cover page image, the Paper Wallet background and its diagonal cryptocurrency names, the watermark, and the diagonal group labels on the cards were always printed. Make it an option: a `backgrounds` argument of produce_pdf and write_pdfs, defaulting to defaults.BACKGROUNDS (True, so the output is unchanged), selected on the command line with --backgrounds and --no-backgrounds, like --cover and --no-cover. With --no-backgrounds, the PDF is plainer to read and cheaper to print: no background image behind the Paper Wallets and the cover page, and no text across the cards. Add backgrounds_test.py. --- README.org | 6 +++- slip39/backgrounds_test.py | 66 ++++++++++++++++++++++++++++++++++++++ slip39/defaults.py | 3 ++ slip39/layout/pdf.py | 24 ++++++++------ slip39/main.py | 8 ++++- 5 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 slip39/backgrounds_test.py diff --git a/README.org b/README.org index 5685a82..346279a 100644 --- a/README.org +++ b/README.org @@ -438,7 +438,8 @@ slip39 --help 2>&1 | sed 's/^/: /' # (just for output formatting) : [--wallet-hint WALLET_HINT] [--wallet-format WALLET_FORMAT] : [-s SECRET] [-e ENTROPY] [--show] [--no-show] [--bits BITS] : [--using-bip39] [--anonymous] [--passphrase PASSPHRASE] - : [-C CARD] [--no-card] [--paper PAPER] [--cover] [--no-cover] + : [-C CARD] [--no-card] [--paper PAPER] [--backgrounds] + : [--no-backgrounds] [--cover] [--no-cover] : [--identifier IDENTIFIER] [--extendable] [--no-extendable] : [--text] [--watermark WATERMARK] [--double-sided] : [--no-double-sided] [--single-sided] @@ -514,6 +515,9 @@ slip39 --help 2>&1 | sed 's/^/: /' # (just for output formatting) : business) : --no-card Disable PDF SLIP-39 mnemonic card output : --paper PAPER Paper size (default: Letter) + : --backgrounds Print backgrounds, watermarks and group labels + : (default) + : --no-backgrounds Don't print backgrounds, watermarks and group labels : --cover Produce PDF SLIP-39 cover page : --no-cover Disable PDF SLIP-39 cover page : --identifier IDENTIFIER diff --git a/slip39/backgrounds_test.py b/slip39/backgrounds_test.py new file mode 100644 index 0000000..9a8eeaf --- /dev/null +++ b/slip39/backgrounds_test.py @@ -0,0 +1,66 @@ +import importlib +import pytest + +from fpdf import FlexTemplate + +from .defaults import BACKGROUNDS +from .layout import write_pdfs +from .main import main + + +@pytest.fixture +def rendered( monkeypatch ): + """Records the texts and images of every template rendered into a PDF.""" + rendered = [] + render = FlexTemplate.render + + def record( self, *args, **kwds ): + rendered.append( dict( self.texts )) + return render( self, *args, **kwds ) + monkeypatch.setattr( FlexTemplate, 'render', record ) + return rendered + + +def backgrounds_printed( rendered ): + """The things printed because of backgrounds: the cover image, the card's diagonal group labels, the + watermark, and the Paper Wallet background and its diagonal cryptocurrency names.""" + return { + k + for t in rendered + for k in t + if k in ( 'cover-image', 'cover-fade', 'card-watermark', 'wallet-bg' ) or k.startswith(( 'card-g', 'crypto-f', 'crypto-b' )) + } + + +def test_backgrounds( tmp_path, rendered ): + pytest.importorskip( "Crypto.Protocol.KDF" ) # BIP-38 encrypted Paper Wallets require scrypt + for backgrounds in ( False, True, None ): + rendered.clear() + write_pdfs( + names = [ "Test" ], + master_secret = bytes.fromhex( "ff" * 16 ), + cryptocurrency = [ "BTC" ], + filepath = str( tmp_path ), + filename = "backgrounds", + watermark = "Watermark", + wallet_pwd = "password", + **( {} if backgrounds is None else dict( backgrounds=backgrounds )), # None: the default + ) + printed = backgrounds_printed( rendered ) + if backgrounds is None: + backgrounds = BACKGROUNDS + if backgrounds: + assert { 'cover-image', 'cover-fade', 'card-watermark', 'wallet-bg' } <= printed + assert any( k.startswith( 'card-g' ) for k in printed ) + assert any( k.startswith( 'crypto-f' ) for k in printed ) + else: + assert not printed + + +def test_main_backgrounds( monkeypatch ): + """--backgrounds and --no-backgrounds are passed on to write_pdfs.""" + calls = [] + monkeypatch.setattr( importlib.import_module( 'slip39.main' ), 'write_pdfs', lambda **kwds: calls.append( kwds ) or {} ) + for args,expected in (( [], BACKGROUNDS ), ( [ '--backgrounds' ], True ), ( [ '--no-backgrounds' ], False )): + assert main( [ '-q', *args, 'Test' ] ) == 0 + assert calls[-1]['backgrounds'] is expected diff --git a/slip39/defaults.py b/slip39/defaults.py index b6e72b3..3c5ca39 100644 --- a/slip39/defaults.py +++ b/slip39/defaults.py @@ -59,6 +59,9 @@ # Whether we should avoid printing Cryptocurrency addresses, QR codes ANONYMOUS = False +# Whether we should print backgrounds (and watermarks, and the diagonal group labels on cards) +BACKGROUNDS = True + # SLIP-39 Mnemonic Card Sizes CARD = 'business' CARD_SIZES = dict( diff --git a/slip39/layout/pdf.py b/slip39/layout/pdf.py index d4b63d2..aeded81 100644 --- a/slip39/layout/pdf.py +++ b/slip39/layout/pdf.py @@ -39,7 +39,7 @@ from ..util import chunker from ..recovery import recover, produce_bip39 from ..defaults import ( - FONTS, ANONYMOUS, CARD, CARD_SIZES, PAPER, ORIENTATION, PAGE_MARGIN, MM_IN, PT_IN, + FONTS, ANONYMOUS, BACKGROUNDS, CARD, CARD_SIZES, PAPER, ORIENTATION, PAGE_MARGIN, MM_IN, PT_IN, WALLET, WALLET_SIZES, GROUPS, GROUP_THRESHOLD_RATIO, FILENAME_FORMAT, @@ -188,6 +188,7 @@ def produce_pdf( accounts: Sequence[Sequence[Account]], # The crypto account(s); at least 1 of each required using_bip39: bool, # Using BIP-39 wallets Seed generation anonymous: Optional[bool] = None, # Avoid printing crypto addresses, QR codes + backgrounds: bool = BACKGROUNDS, # Print backgrounds, watermarks and group labels card_format: str = CARD, # 'index' or '(,),' paper_format: Any = None, # 'Letter', (x,y) dimensions in mm. orientations: Sequence[str] = None, # available orientations; default portrait, landscape @@ -298,8 +299,9 @@ def produce_pdf( log.debug( f"Cover elements: {json.dumps( cover_elements, indent=4)}" ) tpl_cover = fpdf.FlexTemplate( pdf, cover_elements ) images = os.path.dirname( __file__ ) - tpl_cover['cover-image'] = os.path.join( images, 'SLIP-39.png' ) - tpl_cover['cover-fade'] = os.path.join( images, '1x1-ffffffbf.png' ) + if backgrounds: + tpl_cover['cover-image'] = os.path.join( images, 'SLIP-39.png' ) + tpl_cover['cover-fade'] = os.path.join( images, '1x1-ffffffbf.png' ) slip39_mnems = [] slip39_group = [] @@ -364,9 +366,10 @@ def produce_pdf( if not (ANONYMOUS if anonymous is None else anonymous) and len( accounts[0] ) > 1: f['card-crypto2'] = f"{accounts[0][1].crypto} {accounts[0][1].path}: {accounts[0][1].address}" f['card-qr2'] = io.BytesIO( qr_acct[1].to_string(encoding='unicode').encode('UTF-8')) # get_image() - f[f'card-g{g_n}'] = \ - b[f'card-g{g_n}'] = f"{g_name:7.7}..{mn_n+1}" if len(g_name) > 8 else f"{g_name} {mn_n+1}" - if watermark: + if backgrounds: + f[f'card-g{g_n}'] = \ + b[f'card-g{g_n}'] = f"{g_name:7.7}..{mn_n+1}" if len(g_name) > 8 else f"{g_name} {mn_n+1}" + if backgrounds and watermark: f['card-watermark'] = watermark for n,m in enumerate_mnemonic( mnem ).items(): mn_idx = f"mnem-{n}-big" if (ANONYMOUS if anonymous is None else anonymous) else f"mnem-{n}" @@ -413,6 +416,7 @@ def write_pdfs( cryptocurrency = None, # sequence of [ 'ETH:', ... ] to produce accounts for edit = None, # Adjust crypto paths according to the provided path edit anonymous = None, # Produce cryptocurrency addresses and QR codes + backgrounds = BACKGROUNDS, # Print backgrounds, watermarks and group labels card_format = None, # Eg. "credit"; False outputs no SLIP-39 Mnemonic cards to PDF paper_format = None, # Eg. "Letter", "Legal", (x,y) filename = None, # A file name format w/ {path}, etc. formatters @@ -531,6 +535,7 @@ def write_pdfs( (pdf_paper,pdf_orient),pdf,_ = produce_pdf( *details, anonymous = anonymous, + backgrounds = backgrounds, card_format = card_format or CARD, paper_format = paper_format or PAPER, orientations = ('portrait', ) if wallet_pwd not in (None, False) else None, @@ -599,9 +604,10 @@ def write_pdfs( wall_n += 1 images = os.path.dirname( __file__ ) - wall_tpl['wallet-bg'] = os.path.join( images, 'paper-wallet-background.png' ) - wall_tpl[f"crypto-f{c_n}"] = account.crypto - wall_tpl[f"crypto-b{c_n}"] = account.crypto + if backgrounds: + wall_tpl['wallet-bg'] = os.path.join( images, 'paper-wallet-background.png' ) + wall_tpl[f"crypto-f{c_n}"] = account.crypto + wall_tpl[f"crypto-b{c_n}"] = account.crypto wall_tpl['center'] = os.path.join( images, account.crypto + '.png' ) diff --git a/slip39/main.py b/slip39/main.py index cad8959..0ab90bb 100644 --- a/slip39/main.py +++ b/slip39/main.py @@ -28,7 +28,7 @@ from .util import log_cfg, log_level, input_secure from .layout import write_pdfs from .defaults import ( # noqa: F401 - ANONYMOUS, CARD, CARD_SIZES, PAPER, WALLET, WALLET_SIZES, + ANONYMOUS, BACKGROUNDS, CARD, CARD_SIZES, PAPER, WALLET, WALLET_SIZES, BITS, BITS_DEFAULT, FILENAME_FORMAT, FILENAME_KEYWORDS, @@ -117,6 +117,11 @@ def main( argv=None ): ap.add_argument( '--paper', default=None, help=f"Paper size (default: {PAPER})" ) + ap.add_argument( '--backgrounds', action='store_true', + default=BACKGROUNDS, + help=f"Print backgrounds, watermarks and group labels{' (default)' if BACKGROUNDS else ''}" ) + ap.add_argument( '--no-backgrounds', dest="backgrounds", action='store_false', + help=f"Don't print backgrounds, watermarks and group labels{'' if BACKGROUNDS else ' (default)'}" ) ap.add_argument( '--cover', dest="cover_page", action='store_true', default=True, help="Produce PDF SLIP-39 cover page" ) @@ -266,6 +271,7 @@ def main( argv=None ): wallet_pwd_hint = wallet_pwd_hint, wallet_format = wallet_format, cover_page = args.cover_page, + backgrounds = args.backgrounds, watermark = args.watermark, double_sided = args.double_sided, extendable = args.extendable,