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 tracing to RS server example. #16

Merged
merged 1 commit into from
Nov 15, 2023
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
2 changes: 2 additions & 0 deletions examples/lightspark-remote-signing-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ edition = "2021"
lightspark = { path = "../../lightspark" }
lightspark-remote-signing = { path = "../../lightspark-remote-signing" }
tokio = "1.32.0"
tracing = { version = "0.1", features = ["log"], default-features = false }
tracing-subscriber = "0.3"
actix-web = "4.4.0"
futures-util = "0.3.28"
hex = "0.4.3"
6 changes: 6 additions & 0 deletions examples/lightspark-remote-signing-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub struct Config {
pub api_client_secret: String,
pub webhook_secret: String,
pub master_seed_hex: String,
pub api_port: u16,
}

impl Config {
Expand All @@ -14,13 +15,18 @@ impl Config {
let api_client_secret = std::env::var("RK_API_CLIENT_SECRET").ok();
let webhook_secret = std::env::var("RK_WEBHOOK_SECRET").ok();
let master_seed_hex = std::env::var("RK_MASTER_SEED_HEX").ok();
let api_port = std::env::var("PORT").ok();

Self {
api_endpoint,
api_client_id: api_client_id.unwrap_or_default(),
api_client_secret: api_client_secret.unwrap_or_default(),
webhook_secret: webhook_secret.unwrap_or_default(),
master_seed_hex: master_seed_hex.unwrap_or_default(),
api_port: api_port
.unwrap_or("8080".to_string())
.parse()
.expect("api port"),
}
}
}
18 changes: 14 additions & 4 deletions examples/lightspark-remote-signing-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ use lightspark_remote_signing::{
signer::{LightsparkSigner, Seed},
validation::PositiveValidator,
};
use tracing::{debug, info, Level};
use tracing_subscriber::FmtSubscriber;

pub mod config;

#[get("/ping")]
async fn ping() -> impl Responder {
println!("ping");
info!("ping");
HttpResponse::NoContent().finish()
}

Expand Down Expand Up @@ -51,26 +53,34 @@ async fn webhook_handler(
.unwrap()
.unwrap();

println!("Response {:?}", response);
debug!("Response {:?}", response);

let result = client
.execute_graphql_request_variable(&response.query, response.variables)
.await;

println!("Graphql response {:?}", result);
debug!("Graphql response {:?}", result);
HttpResponse::NoContent().finish()
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::DEBUG)
.finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");

let config = config::Config::new_from_env();
let port = config.api_port;

info!(config = format!("{:?}", config), "Starting Remote Signer.");
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(config.clone()))
.service(ping)
.service(webhook_handler)
})
.bind(("0.0.0.0", 8080))?
.bind(("0.0.0.0", port))?
.run()
.await
}
2 changes: 1 addition & 1 deletion lightspark-remote-signing/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Handler {
let data = &event.data.as_ref().ok_or(Error::WebhookEventDataMissing)?;
let sub_type: RemoteSigningSubEventType = from_value(data["sub_event_type"].clone())
.map_err(|_| Error::WebhookEventDataMissing)?;
println!("handler for sub_type: {:?}", sub_type.to_string());
info!("handler for sub_type: {:?}", sub_type.to_string());
let event_json =
serde_json::to_string(&event).expect("Serialize event to json should not fail");
if !self.validator.should_sign(event_json) {
Expand Down
13 changes: 12 additions & 1 deletion lightspark-remote-signing/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use bitcoin::hashes::{sha512, Hash, HashEngine, Hmac, HmacEngine};
use bitcoin::secp256k1::ecdh::SharedSecret;
use bitcoin::secp256k1::hashes::sha256;
use bitcoin::secp256k1::{Message, PublicKey, Scalar, Secp256k1, SecretKey};
use log::debug;
use rand_core::{OsRng, RngCore};

const NODE_KEY_PATH: &str = "m/0";
Expand Down Expand Up @@ -164,9 +165,19 @@ impl LightsparkSigner {
mul_tweak: Option<Vec<u8>>,
) -> Result<Vec<u8>, Error> {
let secp = Secp256k1::new();
let signing_key = self.derive_and_tweak_key(derivation_path, add_tweak, mul_tweak)?;
let signing_key =
self.derive_and_tweak_key(derivation_path.clone(), add_tweak, mul_tweak)?;
let msg = Message::from_slice(message.as_slice()).map_err(Error::Secp256k1Error)?;
let signature = secp.sign_ecdsa(&msg, &signing_key);

debug!("Derivation: {}", derivation_path);
debug!("Signing Key: {}", hex::encode(signing_key.as_ref()));
debug!(
"Verification Key: {}",
signing_key.public_key(&secp).to_string()
);
debug!("Message: {}", hex::encode(message.as_slice()));

Ok(signature.serialize_compact().to_vec())
}

Expand Down