mirror of
https://github.com/stakwork/sphinx-key.git
synced 2026-01-25 02:14:26 +01:00
nonce utils in parser, key derivation utils to get node id from seed
This commit is contained in:
@@ -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(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user