`lwk_boltz`: store cipher key switched from the mnemonic seed to the xpub string without bumping the `LWK-Boltz-Encryption-Key/1.0` tag, silently orphaning persisted swap state

未关闭
#192 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
42/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
rust
领域
backend, databases

调研方向

Start with lwk_boltz/src/store.rs, especially the xpub key derivation and pending-swaps lookup, then read lwk_common/src/store.rs for encrypted-store lookup behavior. Run the reproduction described in the issue and inspect existing store tests. Done means legacy swap records remain readable after upgrading, or a failed decryption is reported clearly instead of appearing as missing data.

由索引模型根据 Issue 内容生成。

描述

Summary

lwk_boltz/src/store.rs (cipher_from_xpub, encrypted_store_from_xpub) derives the Boltz store cipher key from the wallet xpub string instead of the mnemonic seed while keeping the LWK-Boltz-Encryption-Key/1.0 tag, so the scheme version no longer identifies the key input. An application that upgrades with a durable store silently loses access to its existing entries: pending swaps read back as an empty list and swap data as absent instead of raising an error. The previous records stay in the store, neither deleted nor re-encrypted, but are orphaned and unreachable.

Environment

  • Repository: Blockstream/lwk
  • Current default branch: commit f2c64c74d5f9ef1079e842798a323d82e7dfaa89
  • Reproduced at commit a87d89122a0631715602908d5be7ed9643f530a6; still present in the current code at f2c64c74d5f9ef1079e842798a323d82e7dfaa89, at lwk_boltz/src/store.rs:12, :22-24, :30-32 and :109.
  • Toolchain: Rust 1.85.1 (rust:1.85-slim-bookworm); the reproduction below was run with the network disabled and cargo --offline.

Expected behavior

Entries written by the previous revision of the crate should remain readable, or the change should fail loudly: a store entry that exists but cannot be decrypted must not be reported as "not present". If the key derivation is intentionally changed, the tag should be bumped (so the version identifies the key input) and a migration or dual-read path should keep the previously written keys readable — as the project already does for a later, less invasive store-key change.

Actual behavior

After the upgrade, the same wallet's pending swaps read back as an empty list and swap data as absent, although the legacy records are still in the store and still decrypt under the old cipher. Observed output of the run described above:

REPRO mnemonic            = abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about
REPRO xpub_string         = tpubD6NzVbkrYhZ4XYa9MoLt4BiMZ4gkt2faZ4BcmKu2a9te4LDpQmvEz2L2yDERivHxFPnxXXhqDRkUNnQCpZggCyEZLBktV7VaSmwayqMJy1s
REPRO legacy_key_bytes    = daf5e1aa45954600152d4cc35683cbf65f1de83e906ad416f4a37c82502dde93
REPRO current_key_bytes   = 79d1c14e53c4d2007a57f5329aab8e835887ddf7531078f97565f8d0f8f007b2
REPRO legacy_store_key    = 43e06ba7fc8c4729e4aebbd5a8a8f175d7ddc71a85f2ad5c96966ffc06401964356ee0
REPRO current_store_key   = 9623a22948ae961b7aa1db0f7f8054eff7605980437ebbf324c449926b2e913ca71bff
REPRO derive_xpub_matches = true
REPRO get_pending_swaps(upgraded) = []
REPRO get_swap_data(upgraded) = Ok(None)
REPRO get_pending_swaps(current key) = ["swap-NEW-1"]
REPRO legacy record still stored, decrypts with the old cipher = ["swap-OLD-1"]
test store::tests::repro_legacy_pending_swaps_after_cipher_change ... ok

derive_xpub_matches = true confirms the xpub handed to the cipher is byte-identical to what derive_xpub_from_mnemonic produces, i.e. exactly the value the session stores. The two key derivations differ (daf5e1aa… vs 79d1c14e…), the deterministic store keys differ (43e06ba7… vs 9623a229…), and the lookup under the new cipher returns [] / Ok(None) even though the legacy record is still present and decrypts under the old cipher.

Reproduced with:

git clone https://github.com/Blockstream/lwk.git
cd lwk
git checkout a87d89122a0631715602908d5be7ed9643f530a6
cargo test -p lwk_boltz --lib repro_legacy_pending_swaps_after_cipher_change -- --nocapture

REPRO get_pending_swaps(upgraded) = []
REPRO get_swap_data(upgraded) = Ok(None)

Current code at the default branch (verbatim). The key is still derived from the xpub string, and the tag is still the one the mnemonic-seed scheme used:

// lwk_boltz/src/store.rs:12
 pub struct BoltzEncryptionKeyTag = hash_str("LWK-Boltz-Encryption-Key/1.0");
// lwk_boltz/src/store.rs:22-24
pub fn key_bytes_from_xpub(xpub: &Xpub) -> [u8; 32] {
 BoltzEncryptionKeyHash::hash(xpub.to_string().as_bytes()).to_byte_array()
}
// lwk_boltz/src/store.rs:30-32
pub fn encrypted_store_from_xpub(store: Arc<dyn DynStore>, xpub: &Xpub) -> Arc<dyn DynStore> {
 let key_bytes = key_bytes_from_xpub(xpub);
 Arc::new(EncryptedStore::new_with_key_encryption(store, key_bytes))
}
// lwk_boltz/src/store.rs:109
 .map_or_else(|| Ok(Vec::new()), Ok)

The helper was subsequently renamed and rewired to lwk_common::EncryptedStore::new_with_key_encryption, but its input is unchanged: at the analysed commit it was

// lwk_boltz/src/store.rs:35 (analysed commit a87d8912, verbatim)
/// Create a cipher from an xpub for encrypting Boltz store data.
///
/// The encryption key is derived by hashing the xpub string bytes with a tagged hash.
#[allow(deprecated)]
pub fn cipher_from_xpub(xpub: &Xpub) -> Aes256GcmSiv {
 let xpub_string = xpub.to_string();
 let key_bytes = BoltzEncryptionKeyHash::hash(xpub_string.as_bytes()).to_byte_array();
 let key = GenericArray::from_slice(&key_bytes);
 Aes256GcmSiv::new(key)
}

Root cause

The commit intended to derive the store key from the xpub instead of the mnemonic seed, but it kept the tagged-hash tag LWK-Boltz-Encryption-Key/1.0, so the scheme version no longer identifies the key input. Three properties of the store turn that into silent data loss:

  1. The AES-256-GCM-SIV key is a pure function of its input — BoltzEncryptionKeyHash::hash(xpub.to_string().as_bytes()) versus BoltzEncryptionKeyHash::hash(&mnemonic.to_seed("")) — so the same wallet produces two different keys.
  2. Store keys are deterministic AEAD ciphertexts of the plaintext key name: let hash = sha256::Hash::hash(key.as_bytes()); let nonce_bytes: [u8; 12] = hash.as_byte_array()[..12] (lwk_boltz/src/store.rs:47-59). A different AES key therefore produces a different stored key, so the old entry is never found.
  3. A lookup miss is not an error. EncryptedStore::get returns Ok(None) on a miss (lwk_common/src/store.rs:495-506, via effective_key at lwk_common/src/store.rs:473-485), and the pending-swaps accessor ends in .map_or_else(|| Ok(Vec::new()), Ok), converting the miss into an empty list.

The commit was named and wired as the new scheme in the same change — lwk_boltz/src/lib.rs:19 became pub use store::cipher_from_xpub; (the parent had pub use store::cipher_from_mnemonic;) and lwk_boltz/src/lib.rs:285-286 became pub fn cipher(&self) -> Aes256GcmSiv { cipher_from_xpub(&self.xpub) } — but there is no migration, re-encryption or dual-read path: a search for migrat|legacy|backward across lwk_boltz and lwk_common finds nothing relevant. At the analysed commit the store-level documentation even still said the cipher is "derived from the mnemonic", so the only textual update was to the helper's own doc comment.

The mechanism is live on the current default branch: the tag at lwk_boltz/src/store.rs:12 and the xpub-derived key bytes at lwk_boltz/src/store.rs:22-24 are unchanged, the cipher is still built from those bytes at :30-32, and the miss still becomes an empty list at :109.

Suggested fix

Treat the key derivation input as part of the scheme identity and keep previously written entries readable. The smallest safe change for a scheme switch is a dual read: derive the new key from the xpub, but when a lookup under the new key misses, retry the same plaintext key under the legacy mnemonic-derived cipher and, if the entry decrypts, re-encrypt it under the new key (and remove or leave the old entry). The session has the mnemonic available at construction time, so the legacy cipher can be built alongside the new one.

If a migration is out of scope, at minimum bump the tag (for example LWK-Boltz-Encryption-Key/2.0) so the version actually identifies the key input, and make the miss loud for callers that expect existing state — but note this does not by itself recover the orphaned records, and it turns a silent empty list into an error for anyone who upgrades with an existing store.

A regression test in the spirit of the one at lwk_boltz/src/store.rs:312 (test_default_api_url_keeps_existing_store_keys, which already asserts that store entries written before the api-url namespacing change stay readable) should cover this case: write a pending-swaps record with the previous revision's cipher and assert it is still returned after the key-source change.

主要语言
Rust
星标
114
派生
69
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

Blockstream/lwk 的其他 Issue

查看 Blockstream/lwk 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。