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

Fix and test sr25519 signing in nostd #1872

Merged
merged 8 commits into from
Jan 30, 2025
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
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"] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps update the README in the signer crate that "sr25519 needs getrandom" which isn't supported on all platforms

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe so because we have web = ["getrandom/js"] to enable the relevant getrandom feature if compiling for the web.

(There's a separate conversation around whether this should be a thing of whether to make downstream consumers enable said feature flag instead, which is what getrandom suggests IIRC)

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
Loading