Skip to content

Commit

Permalink
Fix and test sr25519 signing in nostd (#1872)
Browse files Browse the repository at this point in the history
* Fix and test sr25519 signing in nostd

* Remove sr25519 signing test on nostd for thumbabi target

* Don't use sr25519 feature in nostd tests

* Fix nits, remove WASM deps from nostd test, improve comments

* Change copypasted comment

* fmt

* Update CI to account for signer tests
  • Loading branch information
jsdw authored Jan 30, 2025
1 parent 94f4e7f commit 29bf00e
Show file tree
Hide file tree
Showing 12 changed files with 271 additions and 128 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ jobs:
- name: Install cargo-nextest
run: cargo install cargo-nextest

- name: Run subxt-signer no-std tests
uses: actions-rs/[email protected]
with:
command: test
working-directory: signer/tests/no-std

- name: Run tests
uses: actions-rs/[email protected]
with:
Expand Down Expand Up @@ -416,7 +422,7 @@ jobs:
run: |
wasm-pack test --headless --firefox
wasm-pack test --headless --chrome
working-directory: signer/wasm-tests
working-directory: signer/tests/wasm

- if: "failure()"
uses: "andymckay/cancel-action@a955d435292c0d409d104b57d8e78435a93a6ef1" # v0.5
Expand Down
2 changes: 1 addition & 1 deletion signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ hmac = { workspace = true }
zeroize = { workspace = true }
bip39 = { workspace = true }
bip32 = { workspace = true, features = ["alloc", "secp256k1"], optional = true }
schnorrkel = { workspace = true, optional = true }
schnorrkel = { workspace = true, optional = true, features = ["getrandom"] }
secp256k1 = { workspace = true, optional = true, features = [
"alloc",
"recovery",
Expand Down
4 changes: 4 additions & 0 deletions signer/src/sr25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// see LICENSE for license details.

//! An sr25519 keypair implementation.
//!
//! **Note:** This implementation requires the `getrandom` dependency to obtain randomness,
//! and will not compile on targets that it does not support. See the supported `getrandom`
//! targets here: <https://docs.rs/getrandom/latest/getrandom/#supported-targets>.
use core::str::FromStr;

Expand Down
File renamed without changes.
19 changes: 19 additions & 0 deletions signer/tests/no-std/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "nostd-tests"
version = "0.1.0"
edition = "2021"
publish = false

[dev-dependencies]

# This crate is not a part of the workspace, to ensure that no features
# are enabled for it at the workspace level; which conflict with this test.
subxt-signer = { path = "../../", default-features = false, features = [
"sr25519",
"ecdsa",
"unstable-eth",
] }

# this shouldn't be needed, it's in workspace.exclude, but still
# I get the complaint unless I add it...
[workspace]
51 changes: 51 additions & 0 deletions signer/tests/no-std/tests/no_std.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#![no_std]

use subxt_signer::{ecdsa, eth, sr25519};

// Run the tests by calling:
//
// ```text
// cargo test
// ```
//
// These are independent of any other package to ensure that nothing
// else enabled the same feature flag that subxt-signer needs to work ok
// (subxt seems to, for instance).

#[test]
fn sr25519_signing_works() {
let alice = sr25519::dev::alice();

// There's some non-determinism in the signing, so this ensures that
// the rand stuff is configured properly to run ok in wasm.
let signature = alice.sign(b"Hello there");
assert!(sr25519::verify(
&signature,
b"Hello there",
&alice.public_key()
));
}

#[test]
fn ecdsa_signing_works() {
let alice = ecdsa::dev::alice();

// There's some non-determinism in the signing, so this ensures that
// the rand stuff is configured properly to run ok in wasm.
let signature = alice.sign(b"Hello there");
assert!(ecdsa::verify(
&signature,
b"Hello there",
&alice.public_key()
));
}

#[test]
fn eth_signing_works() {
let alice = eth::dev::alith();

// There's some non-determinism in the signing, so this ensures that
// the rand stuff is configured properly to run ok in wasm.
let signature = alice.sign(b"Hello there");
assert!(eth::verify(&signature, b"Hello there", &alice.public_key()));
}
2 changes: 2 additions & 0 deletions signer/tests/wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ console_error_panic_hook = "0.1.7"
# enable the "web" feature here but don't want it enabled as part
# of workspace builds. Also disable the "subxt" feature here because
# we want to ensure it works in isolation of that.
subxt-signer = { path = "..", default-features = false, features = [
subxt-signer = { path = "../../", default-features = false, features = [
"web",
"sr25519",
"ecdsa",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
// Run the tests by calling:
//
// ```text
// wasm-pack test --firefox --headless`
// wasm-pack test --firefox --headless
// ```
//
// These are independent of any other package to ensure that nothing
Expand Down
Loading

0 comments on commit 29bf00e

Please sign in to comment.