Skip to content

Commit f6621cb

Browse files
committed
add custom dummy getdefault
1 parent 97c968c commit f6621cb

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

stacks-common/Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ slog-term = "2.6.0"
4646

4747
# Optional dependencies
4848
slog-json = { version = "2.3.0", optional = true }
49+
getrandom = { version = "0.2", default-features = false, optional = true }
4950

5051
[target.'cfg(unix)'.dependencies]
5152
nix = {version = "0.23", optional = true}
@@ -63,8 +64,7 @@ secp256k1 = { version = "0.24.3", features = ["serde", "recovery"] }
6364
rusqlite = { workspace = true, optional = true }
6465

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

6969
[target.'cfg(all(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64"), not(any(target_os="windows"))))'.dependencies]
7070
sha2 = { version = "0.10", features = ["asm"] }
@@ -93,8 +93,12 @@ slog_json = ["slog-json"]
9393
rusqlite = ["dep:rusqlite", "rand"]
9494
# Enables the rand module. This flag must be off on deterministic
9595
# platforms such as CosmWasm
96-
rand = ["dep:rand", "dep:getrandom"]
96+
rand = ["dep:rand"]
9797
serde = []
9898
testing = ["rand"]
9999
bech32_std = []
100100
bech32_strict = []
101+
102+
# Wasm-specific features for easier configuration
103+
wasm-web = ["rand", "getrandom/js", "libsecp256k1/static-context"]
104+
wasm-deterministic = ["getrandom/custom"]

stacks-common/src/libcommon.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,23 @@ pub mod versions {
110110
include!(concat!(env!("OUT_DIR"), "/versions.rs"));
111111
}
112112

113+
#[cfg(all(feature = "wasm-deterministic", target_family = "wasm"))]
114+
mod getrandom {
115+
use core::num::NonZeroU32;
116+
117+
use getrandom::Error;
118+
119+
pub const NO_RANDOMNESS_ERROR_CODE: u32 = Error::CUSTOM_START;
120+
pub fn always_fail(_buf: &mut [u8]) -> Result<(), Error> {
121+
let code = NonZeroU32::new(NO_RANDOMNESS_ERROR_CODE).unwrap();
122+
Err(Error::from(code))
123+
}
124+
pub use getrandom::register_custom_getrandom;
125+
}
126+
127+
#[cfg(all(feature = "wasm-deterministic", target_family = "wasm"))]
128+
getrandom::register_custom_getrandom!(getrandom::always_fail);
129+
113130
/// This test asserts that the constant above doesn't change.
114131
/// This exists because the constant above is used by Epoch 2.5 instantiation code.
115132
///

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616

1717
use ::libsecp256k1;
1818
pub use ::libsecp256k1::Error;
19+
#[cfg(not(feature = "wasm-deterministic"))]
20+
use ::libsecp256k1::{Error as LibSecp256k1Error, Message as LibSecp256k1Message};
1921
use ::libsecp256k1::{
20-
Error as LibSecp256k1Error, Message as LibSecp256k1Message, PublicKey as LibSecp256k1PublicKey,
21-
RecoveryId as LibSecp256k1RecoveryId, SecretKey as LibSecp256k1PrivateKey,
22-
Signature as LibSecp256k1Signature,
22+
PublicKey as LibSecp256k1PublicKey, RecoveryId as LibSecp256k1RecoveryId,
23+
SecretKey as LibSecp256k1PrivateKey, Signature as LibSecp256k1Signature,
2324
};
2425
use serde::de::{Deserialize, Error as de_Error};
2526
use serde::Serialize;
@@ -101,6 +102,7 @@ impl Secp256k1PublicKey {
101102
Secp256k1PublicKey::from_slice(&data[..]).map_err(|_e| "Invalid public key hex string")
102103
}
103104

105+
#[cfg(not(feature = "wasm-deterministic"))]
104106
pub fn from_private(privk: &Secp256k1PrivateKey) -> Secp256k1PublicKey {
105107
let key = LibSecp256k1PublicKey::from_secret_key(&privk.key);
106108
Secp256k1PublicKey {
@@ -109,6 +111,7 @@ impl Secp256k1PublicKey {
109111
}
110112
}
111113

114+
#[cfg(not(feature = "wasm-deterministic"))]
112115
/// recover message and signature to public key (will be compressed)
113116
pub fn recover_to_pubkey(
114117
msg: &[u8],
@@ -186,6 +189,7 @@ impl Secp256k1PrivateKey {
186189
}
187190
}
188191

192+
#[cfg(not(feature = "wasm-deterministic"))]
189193
pub fn secp256k1_recover(
190194
message_arr: &[u8],
191195
serialized_signature: &[u8],
@@ -197,6 +201,7 @@ pub fn secp256k1_recover(
197201
Ok(recovered_pub_key.serialize_compressed())
198202
}
199203

204+
#[cfg(not(feature = "wasm-deterministic"))]
200205
pub fn secp256k1_verify(
201206
message_arr: &[u8],
202207
serialized_signature: &[u8],
@@ -300,6 +305,12 @@ impl PublicKey for Secp256k1PublicKey {
300305
self.to_bytes()
301306
}
302307

308+
#[cfg(feature = "wasm-deterministic")]
309+
fn verify(&self, _data_hash: &[u8], _sig: &MessageSignature) -> Result<bool, &'static str> {
310+
Err("Not implemented for wasm-deterministic")
311+
}
312+
313+
#[cfg(not(feature = "wasm-deterministic"))]
303314
fn verify(&self, data_hash: &[u8], sig: &MessageSignature) -> Result<bool, &'static str> {
304315
let pub_key = Secp256k1PublicKey::recover_to_pubkey(data_hash, sig)?;
305316
Ok(self.eq(&pub_key))
@@ -315,6 +326,12 @@ impl PrivateKey for Secp256k1PrivateKey {
315326
bits
316327
}
317328

329+
#[cfg(feature = "wasm-deterministic")]
330+
fn sign(&self, _data_hash: &[u8]) -> Result<MessageSignature, &'static str> {
331+
Err("Not implemented for wasm-deterministic")
332+
}
333+
334+
#[cfg(not(feature = "wasm-deterministic"))]
318335
fn sign(&self, data_hash: &[u8]) -> Result<MessageSignature, &'static str> {
319336
let message = LibSecp256k1Message::parse_slice(data_hash)
320337
.map_err(|_e| "Invalid message: failed to decode data hash: must be a 32-byte hash")?;

0 commit comments

Comments
 (0)