Skip to content

Commit 20804ab

Browse files
committed
shim: fix submit key loading
This changeset fixes loading of the submit key. The problem was that if the key length was equal 32 then the function will return an error. Also, I don't think it's a good idea to accept input that is not 32 bytes long, so I changes it to strict equallity check. Test added.
1 parent d34af0d commit 20804ab

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

Cargo.lock

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

sugondat-shim/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ subxt = { version = "0.32.1" }
2323
subxt-signer = {version = "0.32.1", features = ["subxt"] }
2424
sha2 = "0.10.8"
2525
hex = "0.4.3"
26+
27+
[dev-dependencies]
28+
temp-dir = "0.1.11"

sugondat-shim/src/key.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ pub use subxt_signer::sr25519::Keypair;
1313
pub fn load<P: AsRef<Path>>(path: P) -> anyhow::Result<Keypair> {
1414
let raw = hex::decode(std::fs::read(path)?)?;
1515
let mut seed: Seed = Seed::default();
16-
if raw.len() <= seed.len() {
17-
anyhow::bail!("Keyfile length invalid")
16+
if raw.len() != seed.len() {
17+
anyhow::bail!(
18+
"Keyfile length invalid, expected {} bytes, got {} bytes",
19+
seed.len(),
20+
raw.len()
21+
);
1822
}
1923
seed.copy_from_slice(&raw[..]);
2024
Ok(Keypair::from_seed(seed)?)
@@ -24,3 +28,21 @@ pub fn load<P: AsRef<Path>>(path: P) -> anyhow::Result<Keypair> {
2428
pub fn alice() -> Keypair {
2529
subxt_signer::sr25519::dev::alice()
2630
}
31+
32+
#[test]
33+
fn load_alice_key() {
34+
use std::fs;
35+
use temp_dir::TempDir;
36+
// `subkey inspect //Alice`:
37+
const ALICE_SECRET_SEED_HEX: &str =
38+
"e5be9a5092b81bca64be81d212e7f2f9eba183bb7a90954f7b76361f6edb5c0a";
39+
let dir = TempDir::new().unwrap();
40+
let alice_key = dir.child("alice.key");
41+
fs::write(&alice_key, ALICE_SECRET_SEED_HEX).unwrap();
42+
let actual_alice_pubk = load(&alice_key).unwrap().public_key();
43+
let expected_alice_pubk = alice().public_key();
44+
assert_eq!(
45+
hex::encode(actual_alice_pubk.as_ref()),
46+
hex::encode(expected_alice_pubk.as_ref()),
47+
);
48+
}

0 commit comments

Comments
 (0)