From b312ff0078727b5ed1ce7b3d03f1495e9248e191 Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Mon, 1 Aug 2022 15:00:14 -0700 Subject: [PATCH] policy is passed in at init --- signer/Cargo.toml | 2 +- signer/src/lib.rs | 9 +++++---- signer/src/policy.rs | 2 ++ sphinx-key/src/core/events.rs | 9 ++++++--- sphinx-key/src/main.rs | 11 +++++++++-- tester/src/main.rs | 7 +++++-- 6 files changed, 28 insertions(+), 12 deletions(-) diff --git a/signer/Cargo.toml b/signer/Cargo.toml index 2ee8114..7ddcadb 100644 --- a/signer/Cargo.toml +++ b/signer/Cargo.toml @@ -12,5 +12,5 @@ sphinx-key-persister = { path = "../persister" } vls-protocol-signer = { git = "https://gitlab.com/Evanfeenstra/validating-lightning-signer", branch = "sphinx-key-1.1", default-features = false, features = ["std", "secp-lowmemory"] } anyhow = {version = "1", features = ["backtrace"]} log = "0.4" - +serde = { version = "1.0.105", default-features = false, features = ["derive"] } diff --git a/signer/src/lib.rs b/signer/src/lib.rs index ccf3343..e8bc201 100644 --- a/signer/src/lib.rs +++ b/signer/src/lib.rs @@ -1,6 +1,7 @@ mod init; mod policy; +use init::new_root_handler_with_policy; use lightning_signer::persist::Persist; // use lightning_signer::persist::DummyPersister; use std::sync::Arc; @@ -9,6 +10,7 @@ use vls_protocol::msgs::{self, read_serial_request_header, write_serial_response use vls_protocol::serde_bolt::WireString; use vls_protocol_signer::handler::{Handler, RootHandler}; +pub use policy::Policy; pub use sphinx_key_parser::MsgDriver; pub use sphinx_key_persister::FsPersister; pub use vls_protocol_signer::lightning_signer; @@ -22,7 +24,7 @@ pub struct InitResponse { pub const ROOT_STORE: &str = "/sdcard/store"; -pub fn init(bytes: Vec, network: Network) -> anyhow::Result { +pub fn init(bytes: Vec, network: Network, policy: Policy) -> anyhow::Result { // let persister: Arc = Arc::new(DummyPersister); let persister: Arc = Arc::new(FsPersister::new(ROOT_STORE)); let mut md = MsgDriver::new(bytes); @@ -36,10 +38,9 @@ pub fn init(bytes: Vec, network: Network) -> anyhow::Result { .iter() .map(|s| from_wire_string(s)) .collect::>(); - log::info!("allowlist {:?}", allowlist); let seed = init.dev_seed.as_ref().map(|s| s.0).expect("no seed"); - log::info!("create root handler now"); - let root_handler = RootHandler::new(network, 0, Some(seed), persister, allowlist); + let root_handler = new_root_handler_with_policy(network, 0, seed, persister, allowlist, policy); + // let root_handler = RootHandler::new(network, 0, Some(seed), persister, allowlist); log::info!("root_handler created"); let init_reply = root_handler .handle(Message::HsmdInit2(init)) diff --git a/signer/src/policy.rs b/signer/src/policy.rs index d608735..69ed7da 100644 --- a/signer/src/policy.rs +++ b/signer/src/policy.rs @@ -1,9 +1,11 @@ use lightning_signer::policy::filter::PolicyFilter; use lightning_signer::policy::simple_validator::SimplePolicy; use lightning_signer::policy::simple_validator::SimpleValidatorFactory; +use serde::{Deserialize, Serialize}; use vls_protocol_signer::lightning_signer; use vls_protocol_signer::lightning_signer::bitcoin::Network; +#[derive(Serialize, Deserialize)] pub struct Policy { pub max_htlc_value_sat: u64, } diff --git a/sphinx-key/src/core/events.rs b/sphinx-key/src/core/events.rs index 211af82..5bbdcde 100644 --- a/sphinx-key/src/core/events.rs +++ b/sphinx-key/src/core/events.rs @@ -4,7 +4,7 @@ use crate::core::config::Config; use sphinx_key_signer::lightning_signer::bitcoin::Network; use sphinx_key_signer::vls_protocol::model::PubKey; -use sphinx_key_signer::{self, InitResponse}; +use sphinx_key_signer::{self, InitResponse, Policy}; use std::sync::mpsc; use embedded_svc::httpd::Result; @@ -42,7 +42,8 @@ pub fn make_event_loop( do_log: bool, led_tx: mpsc::Sender, seed: [u8; 32], - config: Config + config: Config, + policy: Policy, ) -> Result<()> { while let Ok(event) = rx.recv() { log::info!("BROKER IP AND PORT: {}", config.broker); @@ -69,7 +70,7 @@ pub fn make_event_loop( let InitResponse { root_handler, init_reply: _, - } = sphinx_key_signer::init(init_msg, network).expect("failed to init signer"); + } = sphinx_key_signer::init(init_msg, network, policy).expect("failed to init signer"); // signing loop let dummy_peer = PubKey([0; 33]); while let Ok(event) = rx.recv() { @@ -116,6 +117,8 @@ pub fn make_event_loop( do_log: bool, led_tx: mpsc::Sender, _seed: [u8; 32], + _config: Config, + _policy: Policy, ) -> Result<()> { log::info!("About to subscribe to the mpsc channel"); while let Ok(event) = rx.recv() { diff --git a/sphinx-key/src/main.rs b/sphinx-key/src/main.rs index 9d2a45f..7f3ef30 100644 --- a/sphinx-key/src/main.rs +++ b/sphinx-key/src/main.rs @@ -5,7 +5,7 @@ mod periph; use crate::core::{config::*, events::*}; use crate::periph::led::led_control_loop; -use crate::periph::sd::{mount_sd_card, simple_fs_test}; +use crate::periph::sd::mount_sd_card; use anyhow::Result; use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported @@ -19,6 +19,7 @@ use esp_idf_svc::nvs::*; use esp_idf_svc::nvs_storage::EspNvsStorage; use sphinx_key_signer::lightning_signer::bitcoin::Network; +use sphinx_key_signer::Policy; #[cfg(not(feature = "pingpong"))] const CLIENT_ID: &str = "sphinx-1"; @@ -85,7 +86,13 @@ fn main() -> Result<()> { log::info!("Network set to {:?}", network); log::info!(">>>>>>>>>>> blocking forever..."); log::info!("{:?}", exist); - make_event_loop(mqtt_client, rx, network, do_log, led_tx, exist.seed, exist)?; + + // initial default policy + let policy = Policy { + max_htlc_value_sat: 16_777_216, + }; + + make_event_loop(mqtt_client, rx, network, do_log, led_tx, exist.seed, exist, policy)?; } else { led_tx.send(Status::WifiAccessPoint).unwrap(); println!("=============> START SERVER NOW AND WAIT <=============="); diff --git a/tester/src/main.rs b/tester/src/main.rs index 87a1bff..8913a21 100644 --- a/tester/src/main.rs +++ b/tester/src/main.rs @@ -4,7 +4,7 @@ use sphinx_key_signer::lightning_signer::bitcoin::Network; use clap::{App, AppSettings, Arg}; use rumqttc::{self, AsyncClient, Event, MqttOptions, Packet, QoS}; use sphinx_key_signer::vls_protocol::model::PubKey; -use sphinx_key_signer::{self, InitResponse}; +use sphinx_key_signer::{self, InitResponse, Policy}; use std::env; use std::error::Error; use std::str::FromStr; @@ -99,10 +99,13 @@ async fn main() -> Result<(), Box> { // this may be another kind of message like MQTT ConnAck // loop around again and wait for the init if let Some(init_msg_bytes) = incoming_bytes(init_event) { + let policy = Policy { + max_htlc_value_sat: 16_777_216, + }; let InitResponse { root_handler, init_reply, - } = sphinx_key_signer::init(init_msg_bytes, Network::Regtest) + } = sphinx_key_signer::init(init_msg_bytes, Network::Regtest, policy) .expect("failed to init signer"); client .publish(PUB_TOPIC, QoS::AtMostOnce, false, init_reply)