Skip to content

Commit

Permalink
parse wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
RGGH committed Nov 25, 2023
1 parent 7c97b3e commit 4359c69
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/web/routes_login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,14 @@ async fn gen_wallet(Query(params): Query<HashMap<String, String>>) -> Json<Value
)
.unwrap();

let new_wallet = wallet.get_address(AddressIndex::New).unwrap();
// get a new address (this increments revealed derivation index)
println!(
"revealed address: {:?}",
wallet.get_address(AddressIndex::New)
);
println!("revealed address: {:?}", new_wallet.address);

let q: Option<&String> = params.get("q");

Json(json!({
"message": "Hello!",
"message": new_wallet.address,
"q": q,

}))
}
86 changes: 86 additions & 0 deletions src/web/tempCodeRunnerFile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use bdk::bitcoin::Network;
use bdk::database::MemoryDatabase;
use std::collections::HashMap;

use crate::web;
use crate::web::AUTH_TOKEN;
use crate::{Error, Result};
use axum::body::Body;
use axum::extract::Query;
use axum::http::{Response, StatusCode};
use axum::routing::{get, post};
use axum::Router;
use axum::{Extension, Json};
use serde::Deserialize;
use serde_json::{json, Value};

use bdk::keys::{
bip39::{Language, Mnemonic, WordCount},
DerivableKey, ExtendedKey, GeneratableKey, GeneratedKey,
};
use bdk::template::Bip84;
use bdk::wallet::AddressIndex;
use bdk::{miniscript, KeychainKind, Wallet};

pub fn routes() -> Router {
Router::new()
.route("/api/login", post(api_login))
.route("/api/gen_wallet", get(gen_wallet))
}

async fn api_login(payload: Json<LoginPayload>) -> Result<Json<Value>> {
println!("->>{:<12} - api login", "HANDLER");
// Err Message
if payload.username != "demo1" || payload.password != "welcome" {
return Err(Error::LoginFail);
}

// success body
let body = Json(json!(
{"result" : {
"success" : true
}
}
));
Ok(body)
}

#[derive(Debug, Deserialize)]
struct LoginPayload {
username: String,
password: String,
}

// Handler for "/api/wallet" route
async fn gen_wallet(Query(params): Query<HashMap<String, String>>) -> Json<Value> {
let network = Network::Testnet;
let mnemonic: GeneratedKey<_, miniscript::Segwitv0> =
Mnemonic::generate((WordCount::Words12, Language::English)).unwrap();
let mnemonic_words = mnemonic.to_string();
let mnemonic = Mnemonic::parse(&mnemonic_words).unwrap();
// Generate the extended key
let xkey: ExtendedKey = mnemonic.into_extended_key().unwrap();
// Get xprv from the extended key
let xprv = xkey.into_xprv(network).unwrap();
// Create a BDK wallet structure using BIP 84 descriptor ("m/84h/1h/0h/0" and "m/84h/1h/0h/1")
let wallet = Wallet::new(
Bip84(xprv, KeychainKind::External),
Some(Bip84(xprv, KeychainKind::Internal)),
network,
MemoryDatabase::default(),
)
.unwrap();

let new_wallet = wallet.get_address(AddressIndex::New);
// get a new address (this increments revealed derivation index)
println!(
"revealed address: {:?}", new_wallet
);

let q: Option<&String> = params.get("q");

Json(json!({
"message": new_wallet,
"q": q,
}))
}

0 comments on commit 4359c69

Please sign in to comment.