Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cookie authentication option for rpc #68

Merged
merged 3 commits into from
Feb 12, 2022
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 @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Replace `wallet bump_fee` command `--send_all` with new `--shrink` option
- Add 'reserve' feature to enable proof of reserve
- If no wallet name is provided, derive one from the descriptor instead of using "main"
- Add optional cookie authentication for rpc backend

## [0.3.0]

Expand Down
12 changes: 9 additions & 3 deletions src/bdk_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,15 @@ where

#[cfg(feature = "rpc")]
let config: AnyBlockchainConfig = {
let auth = Auth::UserPass {
username: wallet_opts.rpc_opts.auth.0.clone(),
password: wallet_opts.rpc_opts.auth.1.clone(),
let auth = if let Some(cookie) = &wallet_opts.rpc_opts.cookie {
Auth::Cookie {
file: cookie.into(),
}
} else {
Auth::UserPass {
username: wallet_opts.rpc_opts.basic_auth.0.clone(),
password: wallet_opts.rpc_opts.basic_auth.1.clone(),
}
};

// Use deterministic wallet name derived from descriptor
Expand Down
37 changes: 25 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,9 @@ use bdk_reserves::reserves::ProofOfReserves;
/// #[cfg(feature = "rpc")]
/// rpc_opts: RpcOpts{
/// address: "127.0.0.1:18443".to_string(),
/// auth: ("user".to_string(), "password".to_string()),
/// basic_auth: ("user".to_string(), "password".to_string()),
/// skip_blocks: None,
/// cookie: None,
/// },
/// #[cfg(feature = "compact_filters")]
/// compactfilter_opts: CompactFilterOpts{
Expand Down Expand Up @@ -425,8 +426,9 @@ let expected_wallet_opts = WalletOpts {
#[cfg(feature = "rpc")]
rpc_opts: RpcOpts{
address: "127.0.0.1:18443".to_string(),
auth: ("user".to_string(), "password".to_string()),
basic_auth: ("user".to_string(), "password".to_string()),
skip_blocks: None,
cookie: None,
},
#[cfg(any(feature="compact_filters", feature="electrum", feature="esplora"))]
proxy_opts: ProxyOpts{
Expand Down Expand Up @@ -542,15 +544,19 @@ pub struct RpcOpts {
)]
pub address: String,

/// Sets the rpc authentication username:password
/// Sets the rpc basic authentication
#[structopt(
name = "USER:PASSWD",
short = "a",
long = "auth",
long = "basic-auth",
parse(try_from_str = parse_proxy_auth),
default_value = "user:password",
)]
pub auth: (String, String),
pub basic_auth: (String, String),

/// Sets an optional cookie authentication
#[structopt(name = "COOKIE", long = "cookie")]
pub cookie: Option<String>,

/// Optionally skip initial `skip_blocks` blocks
#[structopt(name = "SKIP_BLOCKS", short = "s", long = "skip-blocks")]
Expand Down Expand Up @@ -1479,7 +1485,8 @@ mod test {
#[cfg(feature = "rpc")]
rpc_opts: RpcOpts {
address: "127.0.0.1:18443".to_string(),
auth: ("user".to_string(), "password".to_string()),
basic_auth: ("user".to_string(), "password".to_string()),
cookie: None,
skip_blocks: None,
},
},
Expand Down Expand Up @@ -1616,7 +1623,8 @@ mod test {
"--descriptor", "wpkh(xpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/0/*)",
"--change_descriptor", "wpkh(xpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/1/*)",
"--node", "125.67.89.101:56678",
"--auth", "user:password",
"--basic-auth", "user:password",
"--cookie", "/home/user/.bitcoin/regtest/.cookie",
"--skip-blocks", "5",
"get_new_address"];

Expand All @@ -1632,7 +1640,8 @@ mod test {
change_descriptor: Some("wpkh(xpubDEnoLuPdBep9bzw5LoGYpsxUQYheRQ9gcgrJhJEcdKFB9cWQRyYmkCyRoTqeD4tJYiVVgt6A3rN6rWn9RYhR9sBsGxji29LYWHuKKbdb1ev/1/*)".to_string()),
rpc_opts: RpcOpts {
address: "125.67.89.101:56678".to_string(),
auth: ("user".to_string(), "password".to_string()),
basic_auth: ("user".to_string(), "password".to_string()),
cookie: Some("/home/user/.bitcoin/regtest/.cookie".to_string()),
skip_blocks: Some(5),
},
},
Expand Down Expand Up @@ -1734,7 +1743,8 @@ mod test {
#[cfg(feature = "rpc")]
rpc_opts: RpcOpts {
address: "127.0.0.1:18443".to_string(),
auth: ("user".to_string(), "password".to_string()),
basic_auth: ("user".to_string(), "password".to_string()),
cookie: None,
skip_blocks: None,
},
},
Expand Down Expand Up @@ -1809,7 +1819,8 @@ mod test {
#[cfg(feature = "rpc")]
rpc_opts: RpcOpts {
address: "127.0.0.1:18443".to_string(),
auth: ("user".to_string(), "password".to_string()),
basic_auth: ("user".to_string(), "password".to_string()),
cookie: None,
skip_blocks: None,
},
},
Expand Down Expand Up @@ -1871,7 +1882,8 @@ mod test {
#[cfg(feature = "rpc")]
rpc_opts: RpcOpts {
address: "127.0.0.1:18443".to_string(),
auth: ("user".to_string(), "password".to_string()),
basic_auth: ("user".to_string(), "password".to_string()),
cookie: None,
skip_blocks: None,
},
#[cfg(any(feature="compact_filters", feature="electrum", feature="esplora"))]
Expand Down Expand Up @@ -1946,7 +1958,8 @@ mod test {
#[cfg(feature = "rpc")]
rpc_opts: RpcOpts {
address: "127.0.0.1:18443".to_string(),
auth: ("user".to_string(), "password".to_string()),
basic_auth: ("user".to_string(), "password".to_string()),
cookie: None,
skip_blocks: None,
},
},
Expand Down