policy is passed in at init

This commit is contained in:
Evan Feenstra
2022-08-01 15:00:14 -07:00
parent 62b0cf861d
commit b312ff0078
6 changed files with 28 additions and 12 deletions

View File

@@ -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"] } 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"]} anyhow = {version = "1", features = ["backtrace"]}
log = "0.4" log = "0.4"
serde = { version = "1.0.105", default-features = false, features = ["derive"] }

View File

@@ -1,6 +1,7 @@
mod init; mod init;
mod policy; mod policy;
use init::new_root_handler_with_policy;
use lightning_signer::persist::Persist; use lightning_signer::persist::Persist;
// use lightning_signer::persist::DummyPersister; // use lightning_signer::persist::DummyPersister;
use std::sync::Arc; 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::serde_bolt::WireString;
use vls_protocol_signer::handler::{Handler, RootHandler}; use vls_protocol_signer::handler::{Handler, RootHandler};
pub use policy::Policy;
pub use sphinx_key_parser::MsgDriver; pub use sphinx_key_parser::MsgDriver;
pub use sphinx_key_persister::FsPersister; pub use sphinx_key_persister::FsPersister;
pub use vls_protocol_signer::lightning_signer; pub use vls_protocol_signer::lightning_signer;
@@ -22,7 +24,7 @@ pub struct InitResponse {
pub const ROOT_STORE: &str = "/sdcard/store"; pub const ROOT_STORE: &str = "/sdcard/store";
pub fn init(bytes: Vec<u8>, network: Network) -> anyhow::Result<InitResponse> { pub fn init(bytes: Vec<u8>, network: Network, policy: Policy) -> anyhow::Result<InitResponse> {
// let persister: Arc<dyn Persist> = Arc::new(DummyPersister); // let persister: Arc<dyn Persist> = Arc::new(DummyPersister);
let persister: Arc<dyn Persist> = Arc::new(FsPersister::new(ROOT_STORE)); let persister: Arc<dyn Persist> = Arc::new(FsPersister::new(ROOT_STORE));
let mut md = MsgDriver::new(bytes); let mut md = MsgDriver::new(bytes);
@@ -36,10 +38,9 @@ pub fn init(bytes: Vec<u8>, network: Network) -> anyhow::Result<InitResponse> {
.iter() .iter()
.map(|s| from_wire_string(s)) .map(|s| from_wire_string(s))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
log::info!("allowlist {:?}", allowlist);
let seed = init.dev_seed.as_ref().map(|s| s.0).expect("no seed"); let seed = init.dev_seed.as_ref().map(|s| s.0).expect("no seed");
log::info!("create root handler now"); 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); // let root_handler = RootHandler::new(network, 0, Some(seed), persister, allowlist);
log::info!("root_handler created"); log::info!("root_handler created");
let init_reply = root_handler let init_reply = root_handler
.handle(Message::HsmdInit2(init)) .handle(Message::HsmdInit2(init))

View File

@@ -1,9 +1,11 @@
use lightning_signer::policy::filter::PolicyFilter; use lightning_signer::policy::filter::PolicyFilter;
use lightning_signer::policy::simple_validator::SimplePolicy; use lightning_signer::policy::simple_validator::SimplePolicy;
use lightning_signer::policy::simple_validator::SimpleValidatorFactory; use lightning_signer::policy::simple_validator::SimpleValidatorFactory;
use serde::{Deserialize, Serialize};
use vls_protocol_signer::lightning_signer; use vls_protocol_signer::lightning_signer;
use vls_protocol_signer::lightning_signer::bitcoin::Network; use vls_protocol_signer::lightning_signer::bitcoin::Network;
#[derive(Serialize, Deserialize)]
pub struct Policy { pub struct Policy {
pub max_htlc_value_sat: u64, pub max_htlc_value_sat: u64,
} }

View File

@@ -4,7 +4,7 @@ use crate::core::config::Config;
use sphinx_key_signer::lightning_signer::bitcoin::Network; use sphinx_key_signer::lightning_signer::bitcoin::Network;
use sphinx_key_signer::vls_protocol::model::PubKey; 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 std::sync::mpsc;
use embedded_svc::httpd::Result; use embedded_svc::httpd::Result;
@@ -42,7 +42,8 @@ pub fn make_event_loop(
do_log: bool, do_log: bool,
led_tx: mpsc::Sender<Status>, led_tx: mpsc::Sender<Status>,
seed: [u8; 32], seed: [u8; 32],
config: Config config: Config,
policy: Policy,
) -> Result<()> { ) -> Result<()> {
while let Ok(event) = rx.recv() { while let Ok(event) = rx.recv() {
log::info!("BROKER IP AND PORT: {}", config.broker); log::info!("BROKER IP AND PORT: {}", config.broker);
@@ -69,7 +70,7 @@ pub fn make_event_loop(
let InitResponse { let InitResponse {
root_handler, root_handler,
init_reply: _, 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 // signing loop
let dummy_peer = PubKey([0; 33]); let dummy_peer = PubKey([0; 33]);
while let Ok(event) = rx.recv() { while let Ok(event) = rx.recv() {
@@ -116,6 +117,8 @@ pub fn make_event_loop(
do_log: bool, do_log: bool,
led_tx: mpsc::Sender<Status>, led_tx: mpsc::Sender<Status>,
_seed: [u8; 32], _seed: [u8; 32],
_config: Config,
_policy: Policy,
) -> Result<()> { ) -> Result<()> {
log::info!("About to subscribe to the mpsc channel"); log::info!("About to subscribe to the mpsc channel");
while let Ok(event) = rx.recv() { while let Ok(event) = rx.recv() {

View File

@@ -5,7 +5,7 @@ mod periph;
use crate::core::{config::*, events::*}; use crate::core::{config::*, events::*};
use crate::periph::led::led_control_loop; 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 anyhow::Result;
use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported 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 esp_idf_svc::nvs_storage::EspNvsStorage;
use sphinx_key_signer::lightning_signer::bitcoin::Network; use sphinx_key_signer::lightning_signer::bitcoin::Network;
use sphinx_key_signer::Policy;
#[cfg(not(feature = "pingpong"))] #[cfg(not(feature = "pingpong"))]
const CLIENT_ID: &str = "sphinx-1"; const CLIENT_ID: &str = "sphinx-1";
@@ -85,7 +86,13 @@ fn main() -> Result<()> {
log::info!("Network set to {:?}", network); log::info!("Network set to {:?}", network);
log::info!(">>>>>>>>>>> blocking forever..."); log::info!(">>>>>>>>>>> blocking forever...");
log::info!("{:?}", exist); 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 { } else {
led_tx.send(Status::WifiAccessPoint).unwrap(); led_tx.send(Status::WifiAccessPoint).unwrap();
println!("=============> START SERVER NOW AND WAIT <=============="); println!("=============> START SERVER NOW AND WAIT <==============");

View File

@@ -4,7 +4,7 @@ use sphinx_key_signer::lightning_signer::bitcoin::Network;
use clap::{App, AppSettings, Arg}; use clap::{App, AppSettings, Arg};
use rumqttc::{self, AsyncClient, Event, MqttOptions, Packet, QoS}; use rumqttc::{self, AsyncClient, Event, MqttOptions, Packet, QoS};
use sphinx_key_signer::vls_protocol::model::PubKey; 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::env;
use std::error::Error; use std::error::Error;
use std::str::FromStr; use std::str::FromStr;
@@ -99,10 +99,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
// this may be another kind of message like MQTT ConnAck // this may be another kind of message like MQTT ConnAck
// loop around again and wait for the init // loop around again and wait for the init
if let Some(init_msg_bytes) = incoming_bytes(init_event) { if let Some(init_msg_bytes) = incoming_bytes(init_event) {
let policy = Policy {
max_htlc_value_sat: 16_777_216,
};
let InitResponse { let InitResponse {
root_handler, root_handler,
init_reply, 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"); .expect("failed to init signer");
client client
.publish(PUB_TOPIC, QoS::AtMostOnce, false, init_reply) .publish(PUB_TOPIC, QoS::AtMostOnce, false, init_reply)