Skip to content

Commit

Permalink
key-utils: make it no_std with a std default feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Georges Palauqui committed Jan 24, 2025
1 parent fef6142 commit 61e9ada
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
8 changes: 8 additions & 0 deletions roles/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions utils/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions utils/key-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ name = "key-utils-bin"
path = "src/main.rs"

[dependencies]
bs58 = { version ="0.4.0", features = ["check"] }
secp256k1 = { version = "0.28.2", default-features = false, features =["alloc","rand","rand-std"] }
bs58 = { version ="0.4.0", default-features = false, features = ["check", "alloc"] }
secp256k1 = { version = "0.28.2", default-features = false, features =["alloc","rand"] }
serde = { version = "1.0.89", features = ["derive","alloc"], default-features = false }
rand = {version = "0.8.5", default-features = false }
rustversion = "1.0"

[dev-dependencies]
toml = { version = "0.5.6", git = "https://github.com/diondokter/toml-rs", default-features = false, rev = "c4161aa" }

[features]
default = ["std"]
std = ["bs58/std","secp256k1/rand-std", "rand/std", "rand/std_rng"]

[package.metadata.docs.rs]
all-features = true
38 changes: 31 additions & 7 deletions utils/key-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

use alloc::{
string::{String, ToString},
vec::Vec,
};
use bs58::{decode, decode::Error as Bs58DecodeError};
use core::convert::TryFrom;
use core::{convert::TryFrom, fmt::Display, str::FromStr};
use secp256k1::{
schnorr::Signature, Keypair, Message as SecpMessage, Secp256k1, SecretKey, SignOnly,
VerifyOnly, XOnlyPublicKey,
};
use serde::{Deserialize, Serialize};
use std::{fmt::Display, str::FromStr};

#[derive(Debug)]
pub enum Error {
Expand All @@ -17,7 +24,7 @@ pub enum Error {
}

impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {

Check warning on line 27 in utils/key-utils/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

utils/key-utils/src/lib.rs#L27

Added line #L27 was not covered by tests
match self {
Self::Bs58Decode(error) => write!(f, "Base58 code error: {error}"),
Self::Secp256k1(error) => write!(f, "Secp256k1 error: {error}"),
Expand All @@ -30,7 +37,11 @@ impl Display for Error {
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}
#[cfg(not(feature = "std"))]
#[rustversion::since(1.81)]
impl core::error::Error for Error {}

impl From<Bs58DecodeError> for Error {
fn from(e: Bs58DecodeError) -> Self {
Expand Down Expand Up @@ -73,7 +84,7 @@ impl From<Secp256k1SecretKey> for String {
}

impl Display for Secp256k1SecretKey {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {

Check warning on line 87 in utils/key-utils/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

utils/key-utils/src/lib.rs#L87

Added line #L87 was not covered by tests
let bytes = self.0.secret_bytes();
f.write_str(&bs58::encode(bytes).with_check().into_string())
}
Expand Down Expand Up @@ -116,7 +127,7 @@ impl From<Secp256k1PublicKey> for String {
}

impl Display for Secp256k1PublicKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut output = [0_u8; 34];
output[0] = 1;
let bytes = self.0.serialize();
Expand Down Expand Up @@ -157,12 +168,25 @@ impl SignatureService {
}
}

#[cfg(feature = "std")]
pub fn sign(&self, message: Vec<u8>, private_key: SecretKey) -> Signature {
self.sign_with_rng(message, private_key, &mut rand::thread_rng())

Check warning on line 173 in utils/key-utils/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

utils/key-utils/src/lib.rs#L173

Added line #L173 was not covered by tests
}

pub fn sign_with_rng<R: rand::Rng + rand::CryptoRng>(

Check warning on line 176 in utils/key-utils/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

utils/key-utils/src/lib.rs#L176

Added line #L176 was not covered by tests
&self,
message: Vec<u8>,
private_key: SecretKey,
rng: &mut R,
) -> Signature {
let secret_key = private_key;
let kp = Keypair::from_secret_key(&self.secp_sign, &secret_key);

self.secp_sign
.sign_schnorr(&SecpMessage::from_digest_slice(&message).unwrap(), &kp)
self.secp_sign.sign_schnorr_with_rng(
&SecpMessage::from_digest_slice(&message).unwrap(),
&kp,
rng,

Check warning on line 188 in utils/key-utils/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

utils/key-utils/src/lib.rs#L185-L188

Added lines #L185 - L188 were not covered by tests
)
}

pub fn verify(
Expand Down
7 changes: 7 additions & 0 deletions utils/key-utils/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#[cfg(feature = "std")]
use ::key_utils::{Secp256k1PublicKey, Secp256k1SecretKey};
#[cfg(feature = "std")]
use secp256k1::{rand, Keypair, Secp256k1};

#[cfg(feature = "std")]
fn generate_key() -> (Secp256k1SecretKey, Secp256k1PublicKey) {
let secp = Secp256k1::new();
let (secret_key, _) = secp.generate_keypair(&mut rand::thread_rng());
Expand All @@ -15,10 +18,14 @@ fn generate_key() -> (Secp256k1SecretKey, Secp256k1PublicKey) {
}
}

#[cfg(feature = "std")]
fn main() {
let (secret, public) = generate_key();
let secret: String = secret.into();
let public: String = public.into();
println!("Secret Key: {}", secret);
println!("Public Key: {}", public);
}

#[cfg(not(feature = "std"))]
fn main() {}

0 comments on commit 61e9ada

Please sign in to comment.