Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- `onchain_compose_transaction` keeps the synced watch-only wallet per extended key, script type, network and Electrum URL, so later composes for the same account run an incremental sync that reuses stored transactions instead of a full sync. Every compose still syncs before building PSBTs. Composes with and without a fingerprint share one wallet. Against the staging regtest Electrum with the Trezor emulator account, a compose drops from about 7.5 s to about 3.4 s after the first one.
- Add a `wake` module for the wake push gateway: device key, secret and install id generation, a signed registration (`wake_prepare_registration`, `wake_sign_pubky_proof`, `wake_register`), decryption of legacy (v0) and v1 envelopes from any delivered push (`wake_decrypt_push`), and the device routes for acks, presence, topics and unregistering. Grants let a peer wake the device (`wake_create_grant`, `wake_list_grants`, `wake_revoke_grant`), and `wake_send_peer_wake` sends a peer wake with a grant token another device handed over. The v1 wire format is experimental until the apps adopt it. `register_device` and `test_notification` are unchanged.

## 0.5.16 - 2026-09-14
Expand Down
47 changes: 37 additions & 10 deletions src/modules/onchain/compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ use bdk::wallet::Wallet;
use bdk::FeeRate;

use super::errors::AccountInfoError;
use super::implementation::{
connect_electrum, create_and_sync_wallet, resolve_wallet_setup, run_account_info_blocking,
};
use super::implementation::{resolve_wallet_setup, run_account_info_blocking, WalletSetup};
use super::types::{CoinSelection, ComposeOutput, ComposeParams, ComposeResult};
use super::wallet_cache::synced_database_snapshot;

/// Compose transactions for multiple fee rates, returning one PSBT per rate.
///
/// Creates a BDK wallet from the extended key, syncs via Electrum, then
/// builds a PSBT for each requested fee rate. The PSBTs include BIP32
/// derivation paths (when `fingerprint` is provided) and are ready for
/// signing by any PSBT-aware signer without additional network calls.
/// Syncs a cached BDK wallet for the extended key via Electrum (a full sync
/// on first use, incremental afterwards), then builds a PSBT for each
/// requested fee rate. The PSBTs include BIP32 derivation paths (when
/// `fingerprint` is provided) and are ready for signing by any PSBT-aware
/// signer without additional network calls.
pub async fn compose_transaction(params: ComposeParams) -> Vec<ComposeResult> {
let num_rates = params.fee_rates.len();
if num_rates == 0 {
Expand All @@ -49,6 +49,15 @@ async fn compose_inner(params: ComposeParams) -> Result<Vec<ComposeResult>, Acco
params.wallet.fingerprint.as_deref(),
)?;

// The cached wallet is keyed without the fingerprint, so composes with and
// without one (fee estimates vs. signing) share one synced wallet.
let sync_setup = resolve_wallet_setup(
&params.wallet.extended_key,
params.wallet.network,
params.wallet.account_type,
None,
)?;

validate_outputs(&params.outputs, setup.network)
.map_err(|e| AccountInfoError::WalletError { error_details: e })?;

Expand All @@ -66,8 +75,8 @@ async fn compose_inner(params: ComposeParams) -> Result<Vec<ComposeResult>, Acco
let coin_selection = params.coin_selection;

run_account_info_blocking("compose transaction", move || {
let client = connect_electrum(&electrum_url)?;
let mut wallet = create_and_sync_wallet(&setup, client)?;
let database = synced_database_snapshot(&sync_setup, &electrum_url)?;
let mut wallet = wallet_from_database(&setup, database)?;

let mut results = Vec::with_capacity(fee_rates.len());
for rate in &fee_rates {
Expand All @@ -88,7 +97,25 @@ async fn compose_inner(params: ComposeParams) -> Result<Vec<ComposeResult>, Acco
.await
}

fn build_psbt(
/// Build a wallet over a synced database copy, using the descriptors that
/// carry the key origin (when a fingerprint was given) so the PSBTs include
/// BIP32 derivations.
pub(crate) fn wallet_from_database(
setup: &WalletSetup,
database: MemoryDatabase,
) -> Result<Wallet<MemoryDatabase>, AccountInfoError> {
Wallet::new(
&setup.external_desc,
Some(&setup.internal_desc),
setup.network,
database,
)
.map_err(|e| AccountInfoError::WalletError {
error_details: format!("Failed to create wallet: {}", e),
})
}

pub(crate) fn build_psbt(
wallet: &mut Wallet<MemoryDatabase>,
outputs: &[ComposeOutput],
fee_rate: f32,
Expand Down
1 change: 1 addition & 0 deletions src/modules/onchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod implementation;
mod listener;
mod psbt;
mod types;
mod wallet_cache;

pub use compose::compose_transaction;
pub use errors::{
Expand Down
285 changes: 285 additions & 0 deletions src/modules/onchain/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2851,4 +2851,289 @@ mod tests {
assert_eq!(DEFAULT_GAP_LIMIT, 20);
assert_eq!(crate::get_default_gap_limit(), DEFAULT_GAP_LIMIT);
}

mod compose_wallet_cache {
use crate::modules::onchain::compose::{build_psbt, wallet_from_database};
use crate::modules::onchain::implementation::{create_wallet, resolve_wallet_setup};
use crate::modules::onchain::wallet_cache::{
cached_wallet_count, contains_key, copy_database, insert_empty_for_test,
synced_database_snapshot_with_timeout, WalletCacheKey, MAX_CACHED_WALLETS,
};
use crate::modules::onchain::{
compose_transaction, AccountType, ComposeOutput, ComposeParams, ComposeResult, Network,
WalletParams,
};
use base64::{engine::general_purpose, Engine as _};
use bdk::bitcoin::absolute::LockTime;
use bdk::bitcoin::psbt::PartiallySignedTransaction;
use bdk::bitcoin::{OutPoint, Sequence, Transaction, TxIn, TxOut, Witness};
use bdk::database::{BatchOperations, Database, MemoryDatabase, SyncTime};
use bdk::wallet::AddressIndex;
use bdk::{BlockTime, KeychainKind, LocalUtxo, TransactionDetails};

/// A server that accepts connections and never answers. Each call must
/// fail within the socket timeout and release the wallet's lock, so a
/// second call on the same wallet is not blocked behind the first.
#[test]
fn stalled_server_fails_within_timeout_and_releases_wallet() {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let url = format!("tcp://{}", listener.local_addr().unwrap());
std::thread::spawn(move || {
let mut open = Vec::new();
for stream in listener.incoming() {
open.push(stream);
}
});
let sync_setup = setup(None);

let first = {
let url = url.clone();
std::thread::spawn(move || {
synced_database_snapshot_with_timeout(&setup(None), &url, 1).map(|_| ())
})
};
std::thread::sleep(std::time::Duration::from_millis(200));
let started = std::time::Instant::now();
let second = synced_database_snapshot_with_timeout(&sync_setup, &url, 1);

assert!(first.join().unwrap().is_err());
assert!(second.is_err());
assert!(
started.elapsed() < std::time::Duration::from_secs(10),
"second call waited {:?}",
started.elapsed()
);
}

/// Trezor emulator `all all ... all` seed, m/84'/1'/0'.
const TREZOR_ALL_TPUB: &str = "tpubDCZB6sR48s4T5Cr8qHUYSZEFCQMMHRg8AoVKVmvcAP5bRw7ArDKeoNwKAJujV3xCPkBvXH5ejSgbgyN6kREmF7sMd41NdbuHa8n1DZNxSMg";
const TREZOR_ALL_FINGERPRINT: &str = "5c9e228d";
const REGTEST_ADDR: &str = "bcrt1qw508d6qejxtdg4y5r3zarvary0c5xw7kygt080";

fn setup(
fingerprint: Option<&str>,
) -> crate::modules::onchain::implementation::WalletSetup {
resolve_wallet_setup(
TREZOR_ALL_TPUB,
Some(Network::Regtest),
Some(AccountType::NativeSegwit),
fingerprint,
)
.unwrap()
}

/// A database funded with one confirmed 100k sat UTXO at external index 0,
/// with the internal last index at 3.
fn funded_database() -> MemoryDatabase {
let deriver = create_wallet(&setup(None)).unwrap();
let mut db = MemoryDatabase::new();
for i in 0..10 {
let ext = deriver.get_address(AddressIndex::Peek(i)).unwrap().address;
db.set_script_pubkey(&ext.script_pubkey(), KeychainKind::External, i)
.unwrap();
let int = deriver
.get_internal_address(AddressIndex::Peek(i))
.unwrap()
.address;
db.set_script_pubkey(&int.script_pubkey(), KeychainKind::Internal, i)
.unwrap();
}
let receive = deriver.get_address(AddressIndex::Peek(0)).unwrap().address;
let tx = Transaction {
version: 2,
lock_time: LockTime::ZERO,
input: vec![TxIn {
// Not a coinbase, so the output is spendable right away.
previous_output: OutPoint {
txid: "1111111111111111111111111111111111111111111111111111111111111111"
.parse()
.unwrap(),
vout: 0,
},
script_sig: Default::default(),
sequence: Sequence::MAX,
witness: Witness::new(),
}],
output: vec![TxOut {
value: 100_000,
script_pubkey: receive.script_pubkey(),
}],
};
let block_time = BlockTime {
height: 100,
timestamp: 1_700_000_000,
};
db.set_tx(&TransactionDetails {
transaction: Some(tx.clone()),
txid: tx.txid(),
received: 100_000,
sent: 0,
fee: None,
confirmation_time: Some(block_time.clone()),
})
.unwrap();
db.set_utxo(&LocalUtxo {
outpoint: OutPoint {
txid: tx.txid(),
vout: 0,
},
txout: tx.output[0].clone(),
keychain: KeychainKind::External,
is_spent: false,
})
.unwrap();
db.set_last_index(KeychainKind::External, 0).unwrap();
db.set_last_index(KeychainKind::Internal, 3).unwrap();
db.set_sync_time(SyncTime { block_time }).unwrap();
db
}

#[test]
fn copy_database_keeps_wallet_state() {
let src = funded_database();
let copy = copy_database(&src).unwrap();

assert_eq!(
copy.iter_script_pubkeys(None).unwrap().len(),
src.iter_script_pubkeys(None).unwrap().len()
);
assert_eq!(copy.iter_utxos().unwrap(), src.iter_utxos().unwrap());
assert_eq!(copy.iter_txs(true).unwrap(), src.iter_txs(true).unwrap());
for keychain in [KeychainKind::External, KeychainKind::Internal] {
assert_eq!(
copy.get_last_index(keychain).unwrap(),
src.get_last_index(keychain).unwrap()
);
}
assert_eq!(copy.get_sync_time().unwrap(), src.get_sync_time().unwrap());
}

#[test]
fn psbt_from_copy_has_key_origin_and_leaves_source_index() {
// The cached wallet syncs with origin-less descriptors; signing needs
// PSBTs built with the fingerprinted ones over a copy of its data.
let src = funded_database();
let mut wallet = wallet_from_database(
&setup(Some(TREZOR_ALL_FINGERPRINT)),
copy_database(&src).unwrap(),
)
.unwrap();
let outputs = vec![ComposeOutput::Payment {
address: REGTEST_ADDR.to_string(),
amount_sats: 10_000,
}];

let result = build_psbt(
&mut wallet,
&outputs,
2.0,
bdk::bitcoin::Network::Regtest,
None,
)
.unwrap();
let ComposeResult::Success { psbt, fee, .. } = result else {
panic!("compose failed: {:?}", result);
};
assert!(fee > 0);

let bytes = general_purpose::STANDARD.decode(psbt).unwrap();
let psbt = PartiallySignedTransaction::deserialize(&bytes).unwrap();
let origin = psbt.inputs[0]
.bip32_derivation
.values()
.next()
.expect("input carries a BIP32 derivation");
assert_eq!(origin.0.to_string(), TREZOR_ALL_FINGERPRINT);
assert_eq!(origin.1.to_string(), "m/84'/1'/0'/0/0");

// Change goes to the next internal index and is marked as ours.
let change_paths: Vec<String> = psbt
.outputs
.iter()
.flat_map(|o| o.bip32_derivation.values())
.map(|(_, path)| path.to_string())
.collect();
assert_eq!(change_paths, vec!["m/84'/1'/0'/1/4".to_string()]);

// Building advanced the copy's change index, not the source's.
assert_eq!(src.get_last_index(KeychainKind::Internal).unwrap(), Some(3));
}

#[test]
fn cache_key_ignores_fingerprint_but_not_server() {
let url = "tcp://127.0.0.1:60001";
let without = WalletCacheKey::new(&setup(None), url);
assert_eq!(without, WalletCacheKey::new(&setup(None), url));
assert_ne!(
without,
WalletCacheKey::new(&setup(None), "tcp://127.0.0.1:60002")
);
let other_type = resolve_wallet_setup(
TREZOR_ALL_TPUB,
Some(Network::Regtest),
Some(AccountType::Taproot),
None,
)
.unwrap();
assert_ne!(without, WalletCacheKey::new(&other_type, url));
}

#[test]
fn cache_evicts_least_recently_used() {
let keys: Vec<WalletCacheKey> = (0..=MAX_CACHED_WALLETS)
.map(|i| WalletCacheKey::new(&setup(None), &format!("tcp://evict-test:{}", i)))
.collect();
for key in &keys {
insert_empty_for_test(key);
std::thread::sleep(std::time::Duration::from_millis(2));
}
assert!(cached_wallet_count() <= MAX_CACHED_WALLETS);
assert!(!contains_key(&keys[0]), "oldest entry should be evicted");
assert!(contains_key(&keys[MAX_CACHED_WALLETS]));
}

/// Composes twice against a live regtest Electrum and prints both
/// timings. Set `COMPOSE_CACHE_ELECTRUM_URL` to use another server.
#[tokio::test]
#[ignore]
async fn compose_reuses_synced_wallet() {
let url = std::env::var("COMPOSE_CACHE_ELECTRUM_URL")
.unwrap_or_else(|_| "ssl://electrs.bitkit.stag0.blocktank.to:9999".to_string());
let params = |fingerprint: Option<String>| ComposeParams {
wallet: WalletParams {
extended_key: TREZOR_ALL_TPUB.to_string(),
electrum_url: url.clone(),
fingerprint,
network: Some(Network::Regtest),
account_type: Some(AccountType::NativeSegwit),
},
outputs: vec![ComposeOutput::Payment {
address: REGTEST_ADDR.to_string(),
amount_sats: 10_000,
}],
fee_rates: vec![2.0],
coin_selection: None,
};

let mut fees = vec![];
for (label, fingerprint) in [
("cold, no fingerprint", None),
("warm, no fingerprint", None),
(
"warm, fingerprint",
Some(TREZOR_ALL_FINGERPRINT.to_string()),
),
] {
let start = std::time::Instant::now();
let results = compose_transaction(params(fingerprint)).await;
println!("compose {}: {:?}", label, start.elapsed());
match &results[0] {
ComposeResult::Success { fee, .. } => fees.push(*fee),
ComposeResult::Error { error } => panic!("compose failed: {}", error),
}
}
assert!(fees.windows(2).all(|w| w[0] == w[1]), "fees: {:?}", fees);
assert!(contains_key(&WalletCacheKey::new(&setup(None), &url)));
}
}
}
Loading
Loading