Skip to content

Commit

Permalink
Add cookie authentication option for rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
rajarshimaitra committed Feb 12, 2022
1 parent 8ba5da7 commit 180fe47
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
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
34 changes: 23 additions & 11 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 @@ -542,15 +543,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 +1484,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 +1622,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 +1639,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 +1742,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 +1818,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 +1881,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 +1957,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

0 comments on commit 180fe47

Please sign in to comment.