Skip to content

Commit

Permalink
Use recent 'bdk' rev, add last_unused endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
notmandatory committed Mar 17, 2021
1 parent 960378a commit f549f5c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bdk = { version = "0.3.0", default-features = false }
bdk = { git = "https://github.com/bitcoindevkit/bdk.git", revision = "c456a25", default-features = false }
bdk-macros = "^0.2"
simple-server = "0.4.0"
log = "0.4.0"
Expand Down
27 changes: 22 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ use bdk::electrum_client::{Client, ElectrumApi, ListUnspentRes, Error};
use structopt::StructOpt;

use crate::config::ConfigOpts;
use bdk::blockchain::{AnyBlockchainConfig, ElectrumBlockchainConfig, AnyBlockchain, ConfigurableBlockchain};
use bdk::blockchain::{AnyBlockchainConfig, ElectrumBlockchainConfig, AnyBlockchain, ConfigurableBlockchain, log_progress};
use std::sync::{Arc, Mutex};
use bdk::wallet::AddressIndex::{New, LastUnused};

fn prepare_home_dir(data_dir: &str) -> PathBuf {
let mut dir = PathBuf::new();
Expand Down Expand Up @@ -132,6 +133,8 @@ fn main() {
tree,
AnyBlockchain::from_config(&electrum_config).unwrap(),
).unwrap();

wallet.sync(log_progress(), None).unwrap();

let wallet_mutex = Arc::new(Mutex::new(wallet));

Expand All @@ -145,10 +148,8 @@ fn main() {

match (request.method(), request.uri().path()) {
(&Method::GET, "/bitcoin/api/new") => {
// curl 127.0.0.1:7878/bitcoin/api/new

let wallet = wallet_mutex.lock().unwrap();
let address = wallet.get_new_address();
let address = wallet.get_address(New);
return match address {
Ok(a) => {
info!("new addr {}", a.to_string());
Expand All @@ -161,6 +162,22 @@ fn main() {
Err(e) => Ok(response.body(e.to_string().as_bytes().to_vec())?)
};
}
(&Method::GET, "/bitcoin/api/last_unused") => {
let wallet = wallet_mutex.lock().unwrap();
wallet.sync(log_progress(), None).unwrap();
let address = wallet.get_address(LastUnused);
return match address {
Ok(a) => {
info!("last unused addr {}", a.to_string());
let value = serde_json::json!({
"network": a.network.to_string(),
"address": a.to_string()
});
Ok(response.body(value.to_string().as_bytes().to_vec())?)
},
Err(e) => Ok(response.body(e.to_string().as_bytes().to_vec())?)
};
}
(&Method::GET, "/bitcoin/api/check") => {
// curl 127.0.0.1:7878/bitcoin/api/check?tb1qm4safqvzu28jvjz5juta7qutfaqst7nsfsumuz:0
let mut query = request.uri().query().unwrap_or("").split(':');
Expand Down Expand Up @@ -197,7 +214,7 @@ fn main() {
}
(&Method::GET, "/bitcoin") => {
let wallet = wallet_mutex.lock().unwrap();
let address = wallet.get_new_address().unwrap();
let address = wallet.get_address(New).unwrap();

return match redirect(address) {
Ok(txt) => {
Expand Down

0 comments on commit f549f5c

Please sign in to comment.