Skip to content

Commit

Permalink
ssh-key default-features false for wasm32-* targets
Browse files Browse the repository at this point in the history
  • Loading branch information
DougAnderson444 committed Aug 25, 2024
1 parent e43014e commit 5260e72
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 31 deletions.
12 changes: 9 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ license = "Apache-2.0"

[features]
default = ["serde"]
ssh = ["ssh-key", "ssh-encoding"]

[dependencies]
blsful = { version = "2.5", git = "https://github.com/mikelodder7/blsful.git" }
Expand All @@ -23,12 +22,19 @@ serde = { version = "1.0", default-features = false, features = [
"alloc",
"derive",
], optional = true }
ssh-encoding = { version = "0.2" }
thiserror = "1.0"
unsigned-varint = { version = "0.8", features = ["std"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
ssh-key = { version = "0.6", default-features = false, features = [
"alloc",
"ecdsa",
"ed25519",
] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ssh-key = { version = "0.6", features = ["crypto"], optional = true }
ssh-encoding = { version = "0.2", optional = true }
ssh-key = { version = "0.6", features = ["crypto"] }

[dev-dependencies]
hex = "0.4"
Expand Down
43 changes: 36 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

// SPDX-License-Idnetifier: Apache-2.0
/// Errors created by this library
#[derive(Clone, Debug, thiserror::Error)]
Expand Down Expand Up @@ -125,12 +127,39 @@ pub enum SharesError {
#[derive(Clone, Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ConversionsError {
/// Ssh signature conversion error
#[cfg(feature = "ssh")]
#[error(transparent)]
SshSig(#[from] ssh_key::Error),
/// Ssh label error
#[cfg(feature = "ssh")]
/// Ssh conversion error
#[error(transparent)]
SshSigLabel(#[from] ssh_encoding::LabelError),
Ssh(#[from] SshError),
}

/// SSH Errors
#[derive(Clone, Debug)]
pub enum SshError {
/// SSH Sig
Sig(ssh_key::Error),
/// SSH Sig label
SigLabel(ssh_encoding::LabelError),
}

impl Display for SshError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SshError::Sig(e) => write!(f, "SSH Sig error: {}", e),
SshError::SigLabel(e) => write!(f, "SSH Sig label error: {}", e),
}
}
}

impl std::error::Error for SshError {}

impl From<ssh_key::Error> for SshError {
fn from(e: ssh_key::Error) -> Self {
SshError::Sig(e)
}
}

impl From<ssh_encoding::LabelError> for SshError {
fn from(e: ssh_encoding::LabelError) -> Self {
SshError::SigLabel(e)
}
}
5 changes: 0 additions & 5 deletions src/ms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ impl Builder {
}

/// create new multisig from ssh Signature
#[cfg(feature = "ssh")]
pub fn new_from_ssh_signature(sig: &ssh_key::Signature) -> Result<Self, Error> {
let mut attributes = BTreeMap::new();
use ssh_key::Algorithm::*;
Expand Down Expand Up @@ -603,7 +602,6 @@ mod tests {
assert_eq!(ms1, ms3);
}

#[cfg(feature = "ssh")]
#[test]
fn test_eddsa_ssh_roundtrip() {
let ms1 = Builder::new(Codec::EddsaMsig)
Expand All @@ -619,7 +617,6 @@ mod tests {
assert_eq!(ms1, ms2);
}

#[cfg(feature = "ssh")]
#[test]
fn test_es256k_ssh_roundtrip() {
let ms1 = Builder::new(Codec::Es256KMsig)
Expand All @@ -635,7 +632,6 @@ mod tests {
assert_eq!(ms1, ms2);
}

#[cfg(feature = "ssh")]
#[test]
fn test_bls_signature_ssh_roundtrip() {
let sk = blsful::Bls12381G1::new_secret_key();
Expand All @@ -662,7 +658,6 @@ mod tests {
assert_eq!(ms1, ms2);
}

#[cfg(feature = "ssh")]
#[test]
fn test_bls_signature_combine_ssh_roundtrip() {
let sk = blsful::Bls12381G2::new_secret_key();
Expand Down
1 change: 0 additions & 1 deletion src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub trait DataView {
/// trait for converting Multisigs to other formats
pub trait ConvView {
/// convert the Multisig to an SSH signature
#[cfg(feature = "ssh")]
fn to_ssh_signature(&self) -> Result<ssh_key::Signature, Error>;
}

Expand Down
17 changes: 8 additions & 9 deletions src/views/bls12381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ impl<'a> DataView for View<'a> {

impl<'a> ConvView for View<'a> {
/// convert to SSH signature format
#[cfg(feature = "ssh")]
fn to_ssh_signature(&self) -> Result<ssh_key::Signature, Error> {
// get the signature data
let dv = self.ms.data_view()?;
Expand All @@ -387,11 +386,11 @@ impl<'a> ConvView for View<'a> {
Ok(ssh_key::Signature::new(
ssh_key::Algorithm::Other(
ssh_key::AlgorithmName::new(ALGORITHM_NAME_G1)
.map_err(|e| ConversionsError::SshSigLabel(e))?,
.map_err(|e| ConversionsError::Ssh(e.into()))?,
),
sig_data,
)
.map_err(|e| ConversionsError::SshSig(e))?)
.map_err(|e| ConversionsError::Ssh(e.into()))?)
}
Codec::Bls12381G2Msig => {
// create the combined sig tuple
Expand All @@ -400,11 +399,11 @@ impl<'a> ConvView for View<'a> {
Ok(ssh_key::Signature::new(
ssh_key::Algorithm::Other(
ssh_key::AlgorithmName::new(ALGORITHM_NAME_G2)
.map_err(|e| ConversionsError::SshSigLabel(e))?,
.map_err(|e| ConversionsError::Ssh(e.into()))?,
),
sig_data,
)
.map_err(|e| ConversionsError::SshSig(e))?)
.map_err(|e| ConversionsError::Ssh(e.into()))?)
}
Codec::Bls12381G1ShareMsig => {
// get the threshold attributes
Expand All @@ -420,11 +419,11 @@ impl<'a> ConvView for View<'a> {
Ok(ssh_key::Signature::new(
ssh_key::Algorithm::Other(
ssh_key::AlgorithmName::new(ALGORITHM_NAME_G1_SHARE)
.map_err(|e| ConversionsError::SshSigLabel(e))?,
.map_err(|e| ConversionsError::Ssh(e.into()))?,
),
sig_data,
)
.map_err(|e| ConversionsError::SshSig(e))?)
.map_err(|e| ConversionsError::Ssh(e.into()))?)
}
Codec::Bls12381G2ShareMsig => {
// get the threshold attributes
Expand All @@ -440,11 +439,11 @@ impl<'a> ConvView for View<'a> {
Ok(ssh_key::Signature::new(
ssh_key::Algorithm::Other(
ssh_key::AlgorithmName::new(ALGORITHM_NAME_G2_SHARE)
.map_err(|e| ConversionsError::SshSigLabel(e))?,
.map_err(|e| ConversionsError::Ssh(e.into()))?,
),
sig_data,
)
.map_err(|e| ConversionsError::SshSig(e))?)
.map_err(|e| ConversionsError::Ssh(e.into()))?)
}
_ => Err(Error::UnsupportedAlgorithm(self.ms.codec.to_string())),
}
Expand Down
3 changes: 1 addition & 2 deletions src/views/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@ impl<'a> DataView for View<'a> {

impl<'a> ConvView for View<'a> {
/// convert to SSH signature format
#[cfg(feature = "ssh")]
fn to_ssh_signature(&self) -> Result<ssh_key::Signature, Error> {
// get the signature data
let dv = self.ms.data_view()?;
let sig_bytes = dv.sig_bytes()?;
Ok(
ssh_key::Signature::new(ssh_key::Algorithm::Ed25519, sig_bytes)
.map_err(|e| ConversionsError::SshSig(e))?,
.map_err(|e| ConversionsError::Ssh(e.into()))?,
)
}
}
7 changes: 3 additions & 4 deletions src/views/secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
use multicodec::Codec;

/// the name used to identify these signatures in non-Multikey formats
pub const ALGORITHM_NAME: &'static str = "secp256k1@multisig";
pub const ALGORITHM_NAME: &str = "secp256k1@multisig";

pub(crate) struct View<'a> {
ms: &'a Multisig,
Expand Down Expand Up @@ -53,18 +53,17 @@ impl<'a> DataView for View<'a> {

impl<'a> ConvView for View<'a> {
/// convert to SSH signature format
#[cfg(feature = "ssh")]
fn to_ssh_signature(&self) -> Result<ssh_key::Signature, Error> {
// get the signature data
let dv = self.ms.data_view()?;
let sig_bytes = dv.sig_bytes()?;
Ok(ssh_key::Signature::new(
ssh_key::Algorithm::Other(
ssh_key::AlgorithmName::new(ALGORITHM_NAME)
.map_err(|e| ConversionsError::SshSigLabel(e))?,
.map_err(|e| ConversionsError::Ssh(e.into()))?,
),
sig_bytes,
)
.map_err(|e| ConversionsError::SshSig(e))?)
.map_err(|e| ConversionsError::Ssh(e.into()))?)
}
}

0 comments on commit 5260e72

Please sign in to comment.