Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/stakwork/sphinx-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Evanfeenstra committed Dec 18, 2023
2 parents f18f6e4 + 197a835 commit 3df56a0
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 16 deletions.
5 changes: 3 additions & 2 deletions glyph/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ impl Controller {
ControlMessage::Nonce => (),
_ => {
// nonce must be higher each time
if msg_nonce.1 <= self.2 {
// keep sanity and don't increment by more than 20 at a time
if msg_nonce.1 <= self.2 || msg_nonce.1 > self.2 + 20 {
return Err(anyhow::anyhow!("invalid nonce"));
}
self.2 += 1;
self.2 = msg_nonce.1;
store.set_nonce(self.2)?;
}
}
Expand Down
10 changes: 8 additions & 2 deletions glyph/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ fn test_controlmessage_serde() {
url: "https://www.sphinx.chat/signer/ota".to_string(),
sha256_hash: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
.to_string(),
message_sig: "H119Q3ZihfysLmcMuOHdSqLqLwmEOOFHF96+16rFkEYEc3dXH8xW1lSM0Fi4ZjZ8XAMSMwltQWJ5pDblAhEZVoc=".to_string(),
}),
ControlMessage::QueryAll,
] {
Expand Down Expand Up @@ -300,6 +301,7 @@ fn test_controlresponse_serde() {
url: "https://www.sphinx.chat/signer/ota".to_string(),
sha256_hash: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
.to_string(),
message_sig: "H119Q3ZihfysLmcMuOHdSqLqLwmEOOFHF96+16rFkEYEc3dXH8xW1lSM0Fi4ZjZ8XAMSMwltQWJ5pDblAhEZVoc=".to_string(),
}),
ControlResponse::AllCurrent(All {
policy: Policy {
Expand Down Expand Up @@ -573,22 +575,25 @@ fn serialize_otaparams(
object: &OtaParams,
) -> Result<()> {
rmp::serialize_field_name(buff, field_name)?;
rmp::serialize_map_len(buff, 3u32)?;
rmp::serialize_map_len(buff, 4u32)?;
rmp::serialize_uint(buff, Some("version"), object.version)?;
rmp::serialize_string(buff, Some("url"), &object.url)?;
rmp::serialize_string(buff, Some("sha256_hash"), &object.sha256_hash)?;
rmp::serialize_string(buff, Some("message_sig"), &object.message_sig)?;
Ok(())
}

fn deserialize_otaparams(bytes: &mut Bytes) -> Result<OtaParams> {
rmp::deserialize_map_len(bytes, 3u32)?;
rmp::deserialize_map_len(bytes, 4u32)?;
let version = rmp::deserialize_uint(bytes, Some("version"))?;
let url = rmp::deserialize_string(bytes, Some("url"))?;
let sha256_hash = rmp::deserialize_string(bytes, Some("sha256_hash"))?;
let message_sig = rmp::deserialize_string(bytes, Some("message_sig"))?;
Ok(OtaParams {
version,
url,
sha256_hash,
message_sig,
})
}

Expand All @@ -598,6 +603,7 @@ fn test_otaparams_serde() {
version: u64::MAX,
url: "https://www.sphinx.chat/signer/ota".to_string(),
sha256_hash: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855".to_string(),
message_sig: "H119Q3ZihfysLmcMuOHdSqLqLwmEOOFHF96+16rFkEYEc3dXH8xW1lSM0Fi4ZjZ8XAMSMwltQWJ5pDblAhEZVoc=".to_string(),
};

//serialize
Expand Down
4 changes: 4 additions & 0 deletions glyph/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ pub struct OtaParams {
pub url: String,
// sha256 hash as integrity check of binary file
pub sha256_hash: String,
// A base64 encoded bitcoin::sign_message::MessageSignature on the sha256_hash string
// Should satisfy bitcoin::sign_message::is_signed_by_address
// Get the message hash from bitcoin::sign_message::signed_msg_hash
pub message_sig: String,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
Expand Down
6 changes: 3 additions & 3 deletions lss-connector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ anyhow = { version = "1", default-features = false }
secp256k1 = { version = "0.24.0", default-features = false, features = ["bitcoin_hashes"] }
# broker libs
# vls
lightning-storage-server = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "19447b1e5d2956aa72aeca4229da7a96c5eaf208", optional = true }
vls-frontend = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "19447b1e5d2956aa72aeca4229da7a96c5eaf208", optional = true }
vls-protocol-signer = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "19447b1e5d2956aa72aeca4229da7a96c5eaf208", default-features = false, features = ["secp-lowmemory", "tracker_size_workaround"] }
lightning-storage-server = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "300495658a1903a45a081771fdf2fe6883b057a7", optional = true }
vls-frontend = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "300495658a1903a45a081771fdf2fe6883b057a7", optional = true }
vls-protocol-signer = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "300495658a1903a45a081771fdf2fe6883b057a7", default-features = false, features = ["secp-lowmemory", "tracker_size_workaround"] }
# local
# lightning-storage-server = { path = "../../vls/lightning-storage-server", optional = true }
# vls-frontend = { path = "../../vls/vls-frontend", optional = true }
Expand Down
7 changes: 4 additions & 3 deletions signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ std = ["vls-persist/std", "vls-protocol-signer/std", "vls-protocol/std", "lss-co
broker-test = ["lss-connector/broker"]
no-std = ["vls-protocol-signer/no-std", "vls-persist/no-std", "lss-connector/no-std", "sphinx-glyph/no-std"]
no-native = ["lss-connector/no-native"]
lowmemory = []

[dependencies]
sphinx-glyph = { path = '../glyph', default-features = false }
Expand All @@ -32,9 +33,9 @@ fsdb = { git = "https://github.com/Evanfeenstra/fsdb.git", rev = "7d0db454133bf3
# fsdb = { path = "../../fsdb", optional = true }
thiserror = "1.0.44"
# vls
vls-protocol-signer = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "19447b1e5d2956aa72aeca4229da7a96c5eaf208", default-features = false, features = ["secp-lowmemory", "tracker_size_workaround"] }
vls-protocol = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "19447b1e5d2956aa72aeca4229da7a96c5eaf208", default-features = false }
vls-persist = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "19447b1e5d2956aa72aeca4229da7a96c5eaf208", default-features = false, features = ["kvv"], optional = true }
vls-protocol-signer = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "300495658a1903a45a081771fdf2fe6883b057a7", default-features = false, features = ["secp-lowmemory", "tracker_size_workaround"] }
vls-protocol = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "300495658a1903a45a081771fdf2fe6883b057a7", default-features = false }
vls-persist = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "300495658a1903a45a081771fdf2fe6883b057a7", default-features = false, features = ["kvv"], optional = true }
# local
# vls-protocol-signer = { path = "../../vls/vls-protocol-signer", default-features = false, features = ["secp-lowmemory", "tracker_size_workaround"] }
# vls-protocol = { path = "../../vls/vls-protocol", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions signer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod approver;
pub mod derive;
#[cfg(not(feature = "lowmemory"))]
pub mod mobile;
pub mod parser;
pub mod root;
Expand Down
28 changes: 22 additions & 6 deletions signer/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ use lss_connector::{LssSigner, Response as LssResponse, SignerMutations};
use thiserror::Error;
use vls_protocol::model::PubKey;
use vls_protocol::msgs::{self, read_serial_request_header, write_serial_response_header, Message};
#[cfg(feature = "lowmemory")]
use vls_protocol::serde_bolt::NonContiguousOctets;
use vls_protocol_signer::handler::{Handler, RootHandler, RootHandlerBuilder};
use vls_protocol_signer::lightning_signer;

#[cfg(feature = "lowmemory")]
pub type MsgBytes = NonContiguousOctets<1024>;
#[cfg(not(feature = "lowmemory"))]
pub type MsgBytes = Vec<u8>;

#[derive(Error, Debug)]
pub enum VlsHandlerError {
#[error("failed read_serial_request_header: {0}")]
Expand Down Expand Up @@ -134,17 +141,19 @@ pub fn policy_interval(int: Interval) -> VelocityControlIntervalType {
// returns the VLS return msg and the muts
fn handle_inner(
root_handler: &RootHandler,
bytes: Vec<u8>,
#[cfg(feature = "lowmemory")] mut bytes: MsgBytes,
#[cfg(not(feature = "lowmemory"))] bytes: MsgBytes,
expected_sequence: Option<u16>,
do_log: bool,
) -> Result<(Vec<u8>, Mutations, u16, String), VlsHandlerError> {
// println!("Signer is handling these bytes: {:?}", bytes);
let mut cursor = Cursor::new(bytes);
#[cfg(not(feature = "lowmemory"))]
let mut bytes = Cursor::new(bytes);
let msgs::SerialRequestHeader {
sequence,
peer_id,
dbid,
} = read_serial_request_header(&mut cursor)
} = read_serial_request_header(&mut bytes)
.map_err(|e| VlsHandlerError::HeaderRead(format!("{:?}", e)))?;
log::info!("=> handler sequence: {}", sequence);
if let Some(expected) = expected_sequence {
Expand All @@ -154,7 +163,7 @@ fn handle_inner(
}

let message =
msgs::read(&mut cursor).map_err(|e| VlsHandlerError::MsgRead(format!("{:?}", e)))?;
msgs::read(&mut bytes).map_err(|e| VlsHandlerError::MsgRead(format!("{:?}", e)))?;

if let Message::HsmdInit(ref m) = message {
if ChainHash::using_genesis_block(root_handler.node().network()).as_bytes()
Expand Down Expand Up @@ -198,7 +207,7 @@ fn handle_inner(

pub fn handle(
root_handler: &RootHandler,
bytes: Vec<u8>,
bytes: MsgBytes,
expected_sequence: Option<u16>,
do_log: bool,
) -> Result<(Vec<u8>, u16), VlsHandlerError> {
Expand All @@ -211,7 +220,7 @@ pub fn handle(
pub fn handle_with_lss(
root_handler: &RootHandler,
lss_signer: &LssSigner,
bytes: Vec<u8>,
bytes: MsgBytes,
expected_sequence: Option<u16>,
do_log: bool,
) -> Result<(Vec<u8>, Vec<u8>, u16, String, Option<[u8; 32]>), VlsHandlerError> {
Expand Down Expand Up @@ -353,6 +362,13 @@ fn vls_cmd(msg: &Message) -> String {
Message::SignHtlcTxMingleReply(_) => "SignHtlcTxMingleReply",
Message::BlockChunk(_) => "BlockChunk",
Message::BlockChunkReply(_) => "BlockChunkReply",
Message::SignerError(_) => "SignerError",
Message::CheckOutpoint(_) => "CheckOutpoint",
Message::CheckOutpointReply(_) => "CheckOutpointReply",
Message::LockOutpoint(_) => "LockOutpoint",
Message::LockOutpointReply(_) => "LockOutpointReply",
Message::ForgetChannel(_) => "ForgetChannel",
Message::ForgetChannelReply(_) => "ForgetChannelReply",
};
m.to_string()
}

0 comments on commit 3df56a0

Please sign in to comment.