From 7b70bb758048fb914aeb16beb62520bd9343496f Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Fri, 2 Sep 2022 12:48:09 -0700 Subject: [PATCH] nonce utils in parser, key derivation utils to get node id from seed --- parser/Cargo.toml | 3 ++- parser/src/lib.rs | 1 - parser/src/policy.rs | 40 +++++++++++++++++++++++++++++++++++++--- parser/src/validator.rs | 17 ----------------- signer/src/derive.rs | 13 +++++++++++++ signer/src/lib.rs | 8 ++++---- 6 files changed, 56 insertions(+), 26 deletions(-) delete mode 100644 parser/src/validator.rs create mode 100644 signer/src/derive.rs diff --git a/parser/Cargo.toml b/parser/Cargo.toml index 36425ef..e28735d 100644 --- a/parser/Cargo.toml +++ b/parser/Cargo.toml @@ -6,8 +6,9 @@ edition = "2021" [dependencies] vls-protocol = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git" } serde = { version = "1.0", default-features = false } +rmp-serde = "1.1.0" serde_bolt = { version = "0.2", default-features = false } -sphinx-auther = "0.1.7" +sphinx-auther = "0.1.8" anyhow = "1" [features] diff --git a/parser/src/lib.rs b/parser/src/lib.rs index bf5c555..df7a1a5 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -1,5 +1,4 @@ pub mod policy; -pub mod validator; use serde::ser; use std::cmp::min; diff --git a/parser/src/policy.rs b/parser/src/policy.rs index b930e1a..0f7fec3 100644 --- a/parser/src/policy.rs +++ b/parser/src/policy.rs @@ -1,20 +1,54 @@ use serde::{Deserialize, Serialize}; +use sphinx_auther::nonce; +use sphinx_auther::secp256k1::{PublicKey, SecretKey}; #[derive(Serialize, Deserialize)] pub enum ControlMessage { - Nonce(u64), + Nonce, QueryPolicy, UpdatePolicy(Policy), + Ota(OtaParams), } #[derive(Serialize, Deserialize)] -pub enum ControlMessageResponse { +pub enum ControlResponse { Nonce(u64), - CurrentPolicy(Policy), + PolicyCurrent(Policy), PolicyUpdated(Policy), + OtaConfirm(OtaParams), } #[derive(Serialize, Deserialize)] pub struct Policy { pub sats_per_day: u64, } + +#[derive(Serialize, Deserialize)] +pub struct OtaParams { + pub version: u64, + pub url: String, +} + +// u64 is the nonce. Each signature must have a higher nonce +pub struct Controller(SecretKey, PublicKey, u64); + +impl Controller { + pub fn new(sk: SecretKey, pk: PublicKey, nonce: u64) -> Self { + Self(sk, pk, nonce) + } + pub fn build_msg(&mut self, msg: ControlMessage) -> anyhow::Result> { + let data = rmp_serde::to_vec(&msg)?; + self.2 = self.2 + 1; + Ok(nonce::build_msg(data, &self.0, self.2)?) + } + pub fn build_response(&self, msg: ControlResponse) -> anyhow::Result> { + Ok(rmp_serde::to_vec(&msg)?) + } + pub fn parse_msg(&mut self, input: Vec) -> anyhow::Result { + let msg = nonce::parse_msg(input, &self.1, self.2)?; + Ok(rmp_serde::from_slice(&msg)?) + } + pub fn parse_response(&self, input: &[u8]) -> anyhow::Result { + Ok(rmp_serde::from_slice(input)?) + } +} diff --git a/parser/src/validator.rs b/parser/src/validator.rs deleted file mode 100644 index 26fb9d9..0000000 --- a/parser/src/validator.rs +++ /dev/null @@ -1,17 +0,0 @@ -use sphinx_auther as auther; -use sphinx_auther::secp256k1::{Message, PublicKey, Secp256k1, SecretKey}; -pub struct Validator(PublicKey); - -const SIG_LEN: usize = 65; - -impl Validator { - fn new(pk: PublicKey) -> Self { - Self(pk) - } - fn parse_control_message(&self, mut input: Vec) -> anyhow::Result<()> { - let arr = input.split_at(input.len() - SIG_LEN); - let sig: [u8; SIG_LEN] = arr.1.try_into().unwrap(); - auther::verify_message(arr.0, &sig, &self.0)?; - Ok(()) - } -} diff --git a/signer/src/derive.rs b/signer/src/derive.rs new file mode 100644 index 0000000..bd80903 --- /dev/null +++ b/signer/src/derive.rs @@ -0,0 +1,13 @@ +use vls_core::{ + bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey}, + bitcoin::Network, + signer::derive::{key_derive, KeyDerivationStyle}, +}; +use vls_protocol_signer::lightning_signer as vls_core; + +pub fn node_keys(network: &Network, seed: &[u8]) -> (PublicKey, SecretKey) { + let style = KeyDerivationStyle::Native; + let deriver = key_derive(style, network.clone()); + let ctx = Secp256k1::new(); + deriver.node_keys(seed, &ctx) +} diff --git a/signer/src/lib.rs b/signer/src/lib.rs index e9e9ecc..ace456c 100644 --- a/signer/src/lib.rs +++ b/signer/src/lib.rs @@ -1,3 +1,4 @@ +mod derive; mod randomstartingtime; use lightning_signer::node::NodeServices; @@ -7,19 +8,18 @@ use lightning_signer::policy::simple_validator::{make_simple_policy, SimpleValid use lightning_signer::util::clock::StandardClock; use lightning_signer::util::velocity::{VelocityControlIntervalType, VelocityControlSpec}; use randomstartingtime::RandomStartingTimeFactory; -pub use vls_protocol_signer::lightning_signer; -pub use vls_protocol_signer::vls_protocol; -// use lightning_signer::persist::DummyPersister; use std::sync::Arc; use vls_protocol::model::PubKey; use vls_protocol::msgs::{self, read_serial_request_header, write_serial_response_header, Message}; use vls_protocol::serde_bolt::WireString; use vls_protocol_signer::handler::{Handler, RootHandler}; +pub use vls_protocol_signer::lightning_signer; use vls_protocol_signer::lightning_signer::bitcoin::Network; +pub use vls_protocol_signer::vls_protocol; +pub use derive::node_keys as derive_node_keys; pub use sphinx_key_parser::MsgDriver; pub use sphinx_key_persister::FsPersister; - pub struct InitResponse { pub root_handler: RootHandler, pub init_reply: Vec,