Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make wasm32-* compatible #1

Merged
merged 4 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ jobs:
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
run: |
cargo test --verbose
cargo test --verbose --all-targets --all-features
18 changes: 15 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,24 @@ multibase = { version = "1.0", git = "https://github.com/cryptidtech/rust-multib
multicodec = { version = "1.0", git = "https://github.com/cryptidtech/rust-multicodec.git" }
multitrait = { version = "1.0", git = "https://github.com/cryptidtech/multitrait.git" }
multiutil = { version = "1.0", git = "https://github.com/cryptidtech/multiutil.git" }
serde = { version = "1.0", default-features = false, features = ["alloc", "derive"], optional = true }
ssh-key = { version = "0.6", features = ["crypto"] }
ssh-encoding = "0.2"
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"] }

[dev-dependencies]
hex = "0.4"
serde_test = "1.0"
Expand Down
41 changes: 36 additions & 5 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,10 +127,39 @@ pub enum SharesError {
#[derive(Clone, Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ConversionsError {
/// Ssh signature conversion error
#[error(transparent)]
SshSig(#[from] ssh_key::Error),
/// Ssh label error
/// 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)
}
}
8 changes: 4 additions & 4 deletions src/ms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ pub const SIG_CODECS: [Codec; 4] = [
// Codec::Es384Msig,
// Codec::Es521Msig,
// Codec::Rs256Msig,
Codec::Es256KMsig//,
//Codec::LamportMsig,
Codec::Es256KMsig, //,
//Codec::LamportMsig,
];

/// the list of signature share codecs supported
pub const SIG_SHARE_CODECS: [Codec; 2] = [
Codec::Bls12381G1ShareMsig,
Codec::Bls12381G2ShareMsig//,
//Codec::LamportShareMsig,
Codec::Bls12381G2ShareMsig, //,
//Codec::LamportShareMsig,
];

/// the multisig sigil
Expand Down
16 changes: 8 additions & 8 deletions src/views/bls12381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,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 @@ -399,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 @@ -419,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 @@ -439,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
2 changes: 1 addition & 1 deletion src/views/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<'a> ConvView for View<'a> {
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()))?,
)
}
}
6 changes: 3 additions & 3 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 @@ -60,10 +60,10 @@ impl<'a> ConvView for View<'a> {
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()))?)
}
}