Skip to content

Commit

Permalink
Merge #68: Add cookie authentication option for rpc
Browse files Browse the repository at this point in the history
1ab2b0f Fix docs test for WalletOpts (Steve Myers)
a9f14a9 Update CHANGELOG (rajarshimaitra)
180fe47 Add cookie authentication option for rpc (rajarshimaitra)

Pull request description:

  ### Description

  Fixes #67

  This PR adds an optional `cookie` arg to pass on cookie file if user needs to use that instead of basic user:password based authentication.

  bitcoin core will only generate a `.cookie` file if rpc username passwords are not set in `bitcoin.conf`. Thus cookie file doesn't have a default. User should only provide it if they know a cookie file exists, or else "File not found" error is returned.

  ### Checklists

  #### All Submissions:

  * [x] I've signed all my commits
  * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk-cli/blob/master/CONTRIBUTING.md)
  * [x] I ran `cargo fmt` and `cargo clippy` before committing

  #### New Features:

  * [x] I've added tests for the new feature
  * [x] I've added docs for the new feature
  * [x] I've updated `CHANGELOG.md`

ACKs for top commit:
  notmandatory:
    ACK 1ab2b0f

Tree-SHA512: 164f8c65582028efa36035f5a442b7d1678a19ef74196286f29e10dad79cb3495fe1b3ac46207eca55115d6a7744e94ddf333238b1933bc7586df049062b670b
  • Loading branch information
notmandatory committed Feb 12, 2022
2 parents 8ba5da7 + 1ab2b0f commit 52ef313
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
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

0 comments on commit 52ef313

Please sign in to comment.