wallet: expose an on-demand seed accessor over core + UniFFI #138

Merged
sirius merged 1 commit from onionwire/t_2142490d-wallet-seed-expose-an-on-demand-seed-acc into main 2026-09-20 16:13:28 +00:00
4 changed files with 116 additions and 0 deletions

View file

@ -594,6 +594,17 @@ impl Wire {
.map_err(WireError::new)
}
/// Re-read this wallet's 25-word seed so the UI can offer a "back up seed"
/// action after creation. Dials the wallet child's RPC (`query_key
/// mnemonic`), so it is cheap but not free, and it needs a wallet the child
/// has open. The result is a spend key: call this only from an explicit
/// user action, show it, and do not persist or log it.
///
/// The Kotlin binding is `Wire.walletSeed()`.
pub async fn wallet_seed(&self) -> WResult<String> {
self.node()?.wallet_seed().await.map_err(WireError::new)
}
/// Import a wallet from an existing 25-word seed. This is the only path
/// that takes a user-supplied secret.
pub async fn import_wallet(&self, password: String, seed: String) -> WResult<()> {

View file

@ -1179,6 +1179,18 @@ impl Node {
.map_err(|e| e.to_string())
}
/// Re-read the seed of the wallet file the child has open. `create_wallet`
/// returns it once; after that this is the only path to it. The seed is a
/// spend key, so the caller must only show it on an explicit user action
/// and must not log or store it.
pub async fn wallet_seed(&self) -> Result<String, String> {
let w = self.wallet_snapshot();
if !w.configured() {
return Err(wallet::NOT_CONFIGURED_MSG.into());
}
w.mnemonic().await.map_err(|e| e.to_string())
}
/// Import a wallet from an existing 25-word seed. This is the only path
/// that takes a user-supplied secret.
pub async fn wallet_restore(&self, password: &str, seed: &str) -> Result<(), String> {

View file

@ -788,6 +788,25 @@ impl Wallet {
json_str(&v, "key")
}
/// Re-read the seed of the wallet file the RPC has open (`query_key
/// mnemonic`). After `create_wallet` this is the **only** way to recover a
/// created wallet's seed, so the UI can offer a backup action instead of
/// showing it once. It needs an open, spend-capable wallet file; a
/// watch-only one answers the RPC's `-29` and surfaces as the view-only
/// refusal.
///
/// The returned words are a spend key: never log them, never store them,
/// and never put them in an error string.
pub async fn mnemonic(&self) -> Result<String> {
if !self.configured() {
return Err(Error(NOT_CONFIGURED_MSG.into()));
}
let v = self
.rpc("query_key", serde_json::json!({"key_type": "mnemonic"}))
.await?;
json_str(&v, "key")
}
/// Import an existing wallet from its 25-word seed. This is the only path
/// that takes a user-supplied secret.
pub async fn restore_wallet(&self, filename: &str, password: &str, seed: &str) -> Result<()> {

View file

@ -819,6 +819,80 @@ async fn mock_digest_auth_accepted() {
assert!(got.starts_with('4'), "got {got}");
}
/// The seed is a spend key: `mnemonic` must be one `query_key mnemonic` call
/// and nothing else, so the caller sees exactly what the open wallet answers.
#[tokio::test]
async fn mnemonic_returns_the_words_the_rpc_answers() {
const WORDS: &str = "one two three four five six seven eight nine ten eleven twelve \
thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twentyone \
twentytwo twentythree twentyfour twentyfive";
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
tokio::spawn(serve_rpc_conns(
listener,
Arc::new(|method: &str| match method {
"query_key" => rpc_result(&format!(r#"{{"key":"{WORDS}"}}"#)),
other => panic!("mnemonic must make one query_key call, saw {other}"),
}),
None,
None,
));
let got = recording_wallet(port).mnemonic().await.expect("mnemonic");
assert_eq!(got, WORDS);
}
/// A wallet with no open file answers a JSON-RPC error. It must surface as the
/// wallet's error, and the RPC login from the URL must not ride along with it.
#[tokio::test]
async fn mnemonic_surfaces_an_rpc_error_without_leaking_the_login() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
tokio::spawn(serve_rpc_conns(
listener,
Arc::new(|_: &str| rpc_error(-1, "wallet file is not open")),
None,
None,
));
let err = recording_wallet(port).mnemonic().await.unwrap_err();
let msg = err.to_string();
assert!(msg.contains("wallet file is not open"), "got {err}");
assert!(!msg.contains("secret"), "password leaked: {err}");
assert!(!msg.contains("http://"), "endpoint leaked: {err}");
}
/// The refused-digest refusal names the login in prose ("the login does not
/// match"), which is exactly the copy that must not carry the credential.
#[tokio::test]
async fn mnemonic_refuses_a_rejected_digest_without_leaking_the_login() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
tokio::spawn(serve_rpc_conns_with(
listener,
Arc::new(|_: &str| "HTTP/1.1 401 Unauthorized\r\nContent-Length: 0\r\n\r\n".to_string()),
None,
None,
true,
));
let err = recording_wallet(port).mnemonic().await.unwrap_err();
let msg = err.to_string();
assert!(msg.contains("digest rejected"), "got {err}");
assert!(!msg.contains("secret"), "password leaked: {err}");
}
/// No endpoint, no dial: the refusal is the configured-guard copy the rest of
/// the wallet uses, not a connection error.
#[test]
fn mnemonic_on_an_unconfigured_wallet_is_a_refusal_not_a_dial() {
let w = Wallet::disabled();
let err = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(w.mnemonic())
.unwrap_err();
assert_eq!(err.to_string(), NOT_CONFIGURED_MSG);
}
/// Answers the Digest challenge, then holds the reply for `delay`. Lets a test
/// prove which call classes get which budget.
async fn serve_delayed(listener: TcpListener, delay: std::time::Duration) {