Skip to content

Commit d186115

Browse files
committed
make rand optional for CosmWasm support
1 parent 6775c85 commit d186115

File tree

11 files changed

+41
-101
lines changed

11 files changed

+41
-101
lines changed

Cargo.lock

Lines changed: 3 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ members = [
1414

1515
# Dependencies we want to keep the same between workspace members
1616
[workspace.dependencies]
17-
ed25519-dalek = { version = "2.1.1", features = ["serde", "rand_core"] }
17+
ed25519-dalek = { version = "2.1.1", default-features = false }
1818
hashbrown = { version = "0.15.2", features = ["serde"] }
1919
lazy_static = "1.4.0"
2020
rand_core = "0.6.4"

stacks-common/Cargo.toml

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ curve25519-dalek = { version = "4.1.3", features = ["serde"] }
3535
ed25519-dalek = { workspace = true }
3636
hashbrown = { workspace = true }
3737
lazy_static = { workspace = true }
38-
rand = { workspace = true }
38+
rand = { workspace = true, optional = true }
3939
ripemd = "0.1.1"
4040
serde = { workspace = true , features = ["derive"] }
4141
serde_derive = { workspace = true }
@@ -45,7 +45,6 @@ slog = { workspace = true }
4545
slog-term = "2.6.0"
4646

4747
# Optional dependencies
48-
rusqlite = { workspace = true, optional = true }
4948
slog-json = { version = "2.3.0", optional = true }
5049

5150
[target.'cfg(unix)'.dependencies]
@@ -57,16 +56,15 @@ winapi = { version = "0.3", features = [
5756
"handleapi",
5857
"synchapi",
5958
"winbase",
60-
] }
61-
62-
[target.'cfg(windows)'.dev-dependencies]
63-
winapi = { version = "0.3", features = ["fileapi", "processenv", "winnt"] }
59+
], optional = true }
6460

6561
[target.'cfg(not(target_family = "wasm"))'.dependencies]
6662
secp256k1 = { version = "0.24.3", features = ["serde", "recovery"] }
63+
rusqlite = { workspace = true, optional = true }
6764

6865
[target.'cfg(target_family = "wasm")'.dependencies]
69-
libsecp256k1 = { version = "0.7.0" }
66+
libsecp256k1 = { version = "0.7.0", default-features = false, features = ["hmac", "static-context"] }
67+
getrandom = { version = "0.2.12", features = ["js"], optional = true }
7068

7169
[target.'cfg(all(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64"), not(any(target_os="windows"))))'.dependencies]
7270
sha2 = { version = "0.10", features = ["asm"] }
@@ -75,23 +73,28 @@ sha2 = { version = "0.10", features = ["asm"] }
7573
sha2 = { version = "0.10" }
7674

7775
[dev-dependencies]
78-
rand_core = { workspace = true }
7976
proptest = "1.6.0"
8077

78+
[target.'cfg(windows)'.dev-dependencies]
79+
winapi = { version = "0.3", features = ["fileapi", "processenv", "winnt"] }
80+
8181
[build-dependencies]
8282
toml = { workspace = true }
8383

8484
[features]
85-
default = ["developer-mode", "ctrlc-handler", "http-parser"]
85+
default = ["developer-mode", "ctrlc-handler", "http-parser", "rand"]
8686
developer-mode = []
87-
# Enables graceful shutdown handling for Ctrl+C (SIGINT) signals on Unix-like systems.
88-
# This pulls in the `nix` dependency.
89-
ctrlc-handler = ["dep:nix"]
87+
# Enables graceful shutdown handling for Ctrl+C (SIGINT) signals.
88+
# This pulls in the `nix` or `winapi` dependency.
89+
ctrlc-handler = ["dep:nix", "dep:winapi"]
9090
# Enables a lightweight, internal HTTP/1.x parser.
9191
http-parser = []
9292
slog_json = ["slog-json"]
93-
rusqlite = ["dep:rusqlite"]
94-
testing = []
93+
rusqlite = ["dep:rusqlite", "rand"]
94+
# Enables the rand module. This flag must be off on deterministic
95+
# platforms such as CosmWasm
96+
rand = ["dep:rand", "dep:getrandom"]
9597
serde = []
98+
testing = ["rand"]
9699
bech32_std = []
97100
bech32_strict = []

stacks-common/src/deps_common/bitcoin/util/mod.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ pub mod hash;
2020

2121
use std::{error, fmt};
2222

23-
use secp256k1;
24-
2523
use crate::deps_common::bitcoin::network;
2624
use crate::deps_common::bitcoin::network::serialize;
2725

@@ -50,8 +48,6 @@ pub trait BitArray {
5048
/// if appropriate.
5149
#[derive(Debug)]
5250
pub enum Error {
53-
/// secp-related error
54-
Secp256k1(secp256k1::Error),
5551
/// Serialization error
5652
Serialize(serialize::Error),
5753
/// Network error
@@ -65,7 +61,6 @@ pub enum Error {
6561
impl fmt::Display for Error {
6662
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6763
match *self {
68-
Error::Secp256k1(ref e) => fmt::Display::fmt(e, f),
6964
Error::Serialize(ref e) => fmt::Display::fmt(e, f),
7065
Error::Network(ref e) => fmt::Display::fmt(e, f),
7166
Error::SpvBadProofOfWork => f.write_str("target correct but not attained"),
@@ -77,28 +72,13 @@ impl fmt::Display for Error {
7772
impl error::Error for Error {
7873
fn cause(&self) -> Option<&dyn error::Error> {
7974
match *self {
80-
Error::Secp256k1(ref e) => Some(e),
8175
Error::Serialize(ref e) => Some(e),
8276
Error::Network(ref e) => Some(e),
8377
Error::SpvBadProofOfWork | Error::SpvBadTarget => None,
8478
}
8579
}
8680
}
8781

88-
#[doc(hidden)]
89-
impl From<secp256k1::Error> for Error {
90-
fn from(e: secp256k1::Error) -> Error {
91-
Error::Secp256k1(e)
92-
}
93-
}
94-
95-
#[doc(hidden)]
96-
impl From<serialize::Error> for Error {
97-
fn from(e: serialize::Error) -> Error {
98-
Error::Serialize(e)
99-
}
100-
}
101-
10282
#[doc(hidden)]
10383
impl From<network::Error> for Error {
10484
fn from(e: network::Error) -> Error {

stacks-common/src/libcommon.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extern crate serde_derive;
1616
#[cfg(all(unix, feature = "ctrlc-handler"))]
1717
extern crate nix;
1818

19-
#[cfg(windows)]
19+
#[cfg(all(windows, feature = "ctrlc-handler"))]
2020
extern crate winapi;
2121

2222
#[macro_use]
@@ -33,8 +33,7 @@ pub mod deps_common {
3333
pub mod bech32;
3434
pub mod bitcoin;
3535

36-
// These are gated by features.
37-
#[cfg(feature = "ctrlc-handler")]
36+
#[cfg(all(not(target_family = "wasm"), feature = "ctrlc-handler"))]
3837
pub mod ctrlc;
3938

4039
#[cfg(feature = "http-parser")]

stacks-common/src/util/chunked_encoding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ mod test {
447447
use std::io;
448448
use std::io::Read;
449449

450-
use rand::RngCore;
450+
use rand::RngCore as _;
451451

452452
use super::*;
453453

stacks-common/src/util/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,5 @@ where
139139
let reader = BufReader::new(file);
140140
serde_json::from_reader::<_, J>(reader).map_err(std::io::Error::from)
141141
}
142+
#[cfg(all(feature = "rusqlite", target_family = "wasm"))]
143+
compile_error!("The `rusqlite` feature is not supported for wasm targets");

stacks-common/src/util/pipe.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,7 @@ mod test {
317317
use std::io::{Read, Write};
318318
use std::{io, thread};
319319

320-
use rand;
321-
use rand::RngCore;
320+
use rand::RngCore as _;
322321

323322
use super::*;
324323

stacks-common/src/util/secp256k1/native.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use ::secp256k1::{
2424
constants as LibSecp256k1Constants, Error as LibSecp256k1Error, Message as LibSecp256k1Message,
2525
PublicKey as LibSecp256k1PublicKey, Secp256k1, SecretKey as LibSecp256k1PrivateKey,
2626
};
27-
use rand::RngCore;
2827
use serde::de::{Deserialize, Error as de_Error};
2928
use serde::Serialize;
3029

@@ -240,7 +239,10 @@ impl PublicKey for Secp256k1PublicKey {
240239
}
241240

242241
impl Secp256k1PrivateKey {
242+
#[cfg(feature = "rand")]
243243
pub fn random() -> Secp256k1PrivateKey {
244+
use rand::RngCore as _;
245+
244246
let mut rng = rand::thread_rng();
245247
loop {
246248
// keep trying to generate valid bytes
@@ -422,6 +424,7 @@ pub fn secp256k1_verify(
422424

423425
#[cfg(test)]
424426
mod tests {
427+
use rand::RngCore as _;
425428
use secp256k1;
426429
use secp256k1::{PublicKey as LibSecp256k1PublicKey, Secp256k1};
427430

stacks-common/src/util/secp256k1/wasm.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use ::libsecp256k1::{
2121
RecoveryId as LibSecp256k1RecoveryId, SecretKey as LibSecp256k1PrivateKey,
2222
Signature as LibSecp256k1Signature,
2323
};
24-
use rand::RngCore;
2524
use serde::de::{Deserialize, Error as de_Error};
2625
use serde::Serialize;
2726

@@ -123,7 +122,10 @@ impl Secp256k1PublicKey {
123122
}
124123

125124
impl Secp256k1PrivateKey {
125+
#[cfg(feature = "rand")]
126126
pub fn new() -> Secp256k1PrivateKey {
127+
use rand::RngCore as _;
128+
127129
let mut rng = rand::thread_rng();
128130
loop {
129131
// keep trying to generate valid bytes

0 commit comments

Comments
 (0)