Skip to content

Commit 29bf00e

Browse files
authored
Fix and test sr25519 signing in nostd (#1872)
* 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
1 parent 94f4e7f commit 29bf00e

File tree

12 files changed

+271
-128
lines changed

12 files changed

+271
-128
lines changed

.github/workflows/rust.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ jobs:
291291
- name: Install cargo-nextest
292292
run: cargo install cargo-nextest
293293

294+
- name: Run subxt-signer no-std tests
295+
uses: actions-rs/[email protected]
296+
with:
297+
command: test
298+
working-directory: signer/tests/no-std
299+
294300
- name: Run tests
295301
uses: actions-rs/[email protected]
296302
with:
@@ -416,7 +422,7 @@ jobs:
416422
run: |
417423
wasm-pack test --headless --firefox
418424
wasm-pack test --headless --chrome
419-
working-directory: signer/wasm-tests
425+
working-directory: signer/tests/wasm
420426

421427
- if: "failure()"
422428
uses: "andymckay/cancel-action@a955d435292c0d409d104b57d8e78435a93a6ef1" # v0.5

signer/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ hmac = { workspace = true }
6666
zeroize = { workspace = true }
6767
bip39 = { workspace = true }
6868
bip32 = { workspace = true, features = ["alloc", "secp256k1"], optional = true }
69-
schnorrkel = { workspace = true, optional = true }
69+
schnorrkel = { workspace = true, optional = true, features = ["getrandom"] }
7070
secp256k1 = { workspace = true, optional = true, features = [
7171
"alloc",
7272
"recovery",

signer/src/sr25519.rs

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
// see LICENSE for license details.
44

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

File renamed without changes.

signer/tests/no-std/Cargo.toml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "nostd-tests"
3+
version = "0.1.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[dev-dependencies]
8+
9+
# This crate is not a part of the workspace, to ensure that no features
10+
# are enabled for it at the workspace level; which conflict with this test.
11+
subxt-signer = { path = "../../", default-features = false, features = [
12+
"sr25519",
13+
"ecdsa",
14+
"unstable-eth",
15+
] }
16+
17+
# this shouldn't be needed, it's in workspace.exclude, but still
18+
# I get the complaint unless I add it...
19+
[workspace]

signer/tests/no-std/tests/no_std.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#![no_std]
2+
3+
use subxt_signer::{ecdsa, eth, sr25519};
4+
5+
// Run the tests by calling:
6+
//
7+
// ```text
8+
// cargo test
9+
// ```
10+
//
11+
// These are independent of any other package to ensure that nothing
12+
// else enabled the same feature flag that subxt-signer needs to work ok
13+
// (subxt seems to, for instance).
14+
15+
#[test]
16+
fn sr25519_signing_works() {
17+
let alice = sr25519::dev::alice();
18+
19+
// There's some non-determinism in the signing, so this ensures that
20+
// the rand stuff is configured properly to run ok in wasm.
21+
let signature = alice.sign(b"Hello there");
22+
assert!(sr25519::verify(
23+
&signature,
24+
b"Hello there",
25+
&alice.public_key()
26+
));
27+
}
28+
29+
#[test]
30+
fn ecdsa_signing_works() {
31+
let alice = ecdsa::dev::alice();
32+
33+
// There's some non-determinism in the signing, so this ensures that
34+
// the rand stuff is configured properly to run ok in wasm.
35+
let signature = alice.sign(b"Hello there");
36+
assert!(ecdsa::verify(
37+
&signature,
38+
b"Hello there",
39+
&alice.public_key()
40+
));
41+
}
42+
43+
#[test]
44+
fn eth_signing_works() {
45+
let alice = eth::dev::alith();
46+
47+
// There's some non-determinism in the signing, so this ensures that
48+
// the rand stuff is configured properly to run ok in wasm.
49+
let signature = alice.sign(b"Hello there");
50+
assert!(eth::verify(&signature, b"Hello there", &alice.public_key()));
51+
}

signer/tests/wasm/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
Cargo.lock

signer/wasm-tests/Cargo.toml renamed to signer/tests/wasm/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ console_error_panic_hook = "0.1.7"
1313
# enable the "web" feature here but don't want it enabled as part
1414
# of workspace builds. Also disable the "subxt" feature here because
1515
# we want to ensure it works in isolation of that.
16-
subxt-signer = { path = "..", default-features = false, features = [
16+
subxt-signer = { path = "../../", default-features = false, features = [
1717
"web",
1818
"sr25519",
1919
"ecdsa",

signer/wasm-tests/tests/wasm.rs renamed to signer/tests/wasm/tests/wasm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
88
// Run the tests by calling:
99
//
1010
// ```text
11-
// wasm-pack test --firefox --headless`
11+
// wasm-pack test --firefox --headless
1212
// ```
1313
//
1414
// These are independent of any other package to ensure that nothing

0 commit comments

Comments
 (0)