mirror of
https://github.com/stakwork/sphinx-key.git
synced 2026-02-14 20:24:22 +01:00
nonce utils in parser, key derivation utils to get node id from seed
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
pub mod policy;
|
||||
pub mod validator;
|
||||
|
||||
use serde::ser;
|
||||
use std::cmp::min;
|
||||
|
||||
@@ -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<Vec<u8>> {
|
||||
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<Vec<u8>> {
|
||||
Ok(rmp_serde::to_vec(&msg)?)
|
||||
}
|
||||
pub fn parse_msg(&mut self, input: Vec<u8>) -> anyhow::Result<ControlMessage> {
|
||||
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<ControlResponse> {
|
||||
Ok(rmp_serde::from_slice(input)?)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<u8>) -> 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(())
|
||||
}
|
||||
}
|
||||
13
signer/src/derive.rs
Normal file
13
signer/src/derive.rs
Normal file
@@ -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)
|
||||
}
|
||||
@@ -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<u8>,
|
||||
|
||||
Reference in New Issue
Block a user