What changed, and why it matters
This commit fixes how a 12-word backup phrase (mnemonic) is generated for the SparkL2 wallet. Previously, the code used a method from the coincurve library that may not produce a properly random or correctly-sized seed for a 12-word mnemonic. The fix switches to Python's standard secrets.token_bytes(16), which provides 128 bits of cryptographically secure randomness—the correct amount for a 12-word BIP39 mnemonic. If the old method produced weak or invalid seeds, users' wallet backups could be easier to guess or might fail validation.
Review whether any deployed SparkL2 wallets were initialized with mnemonics generated by the old PrivateKey().secret path. If so, consider advising users to rotate or migrate funds to wallets with freshly generated 12-word mnemonics, since the old seed's security properties depend on coincurve's RNG and 32-byte truncation behavior. Also verify that mnemonic_is_valid accepts the new 16-byte output without issues.
Security signals we found
Replaced non-standard private-key entropy with standard CSPRNG entropy for mnemonic generation
Removed coincurve PrivateKey import, reducing attack surface
Added secrets module import for cryptographically secure random bytes
Corrected entropy length to 16 bytes (128 bits) for 12-word BIP39 mnemonic
Evidence from the diff
In lnbits/wallets/sparkl2.py, the mnemonic generation fallback when SPARK_L2_MNEMONIC is unset changed from mnemonic_from_bytes(PrivateKey().secret) to mnemonic_from_bytes(secrets.token_bytes(16)). PrivateKey().secret from coincurve returns a 32-byte secp256k1 private key, which is twice the entropy expected for a 12-word BIP39 mnemonic and relies on coincurve’s RNG rather than Python’s secrets module. The new code uses secrets.token_bytes(16), yielding exactly 128 bits of entropy suitable for 12-word mnemonics and aligning with embit.bip39 expectations. This reduces dependency surface and removes a potential source of non-standard seed generation.
Changed components
lnbits/wallets/sparkl2.pySparkL2Wallet._maybe_set_sidecar_mnemonic()SPARK_L2_MNEMONIC fallback generationInspect captured patch +2 / −2
diff --git a/lnbits/wallets/sparkl2.py b/lnbits/wallets/sparkl2.py
index e565fa8..cd863b6 100644
--- a/lnbits/wallets/sparkl2.py
+++ b/lnbits/wallets/sparkl2.py
@@ -1,6 +1,7 @@
import asyncio
import hashlib
import json
+import secrets
import uuid
from collections.abc import AsyncGenerator
from pathlib import Path
@@ -8,7 +9,6 @@ from typing import Any, cast
import httpx
from bolt11 import decode as bolt11_decode
-from coincurve.keys import PrivateKey
from embit.bip39 import mnemonic_from_bytes, mnemonic_is_valid
from loguru import logger
@@ -340,7 +340,7 @@ class SparkL2Wallet(Wallet):
return
logger.info("SPARK_L2_MNEMONIC is not set, one will be generated for you.")
- mnemonic = mnemonic_from_bytes(PrivateKey().secret)
+ mnemonic = mnemonic_from_bytes(secrets.token_bytes(16))
await self._set_sidecar_mnemonic(mnemonic)
async def _set_sidecar_mnemonic(self, mnemonic: str):
Why this scored 56/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.