Skip to content

Commit 0f6debc

Browse files
committed
update scrypt allowed range as defined in pjs repo
1 parent 6852bf5 commit 0f6debc

2 files changed

Lines changed: 47 additions & 11 deletions

File tree

signer/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ getrandom = { workspace = true, optional = true }
107107
[dev-dependencies]
108108
proptest = { workspace = true }
109109
hex-literal = { workspace = true }
110-
sp-core = { workspace = true }
111-
sp-keyring = { workspace = true }
110+
sp-core = { workspace = true, features = ["std"]}
111+
sp-keyring = { workspace = true, features = ["std"] }
112112

113113
[package.metadata.cargo-machete]
114114
ignored = ["getrandom"]

signer/src/polkadot_js_compat.rs

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@
44

55
//! A Polkadot-JS account loader.
66
7+
use crate::sr25519;
78
use base64::Engine;
89
use crypto_secretbox::{
910
Key, Nonce, XSalsa20Poly1305,
1011
aead::{Aead, KeyInit},
1112
};
1213
use serde::Deserialize;
13-
14+
use subxt_utils_accountid32::AccountId32;
1415
use thiserror::Error as DeriveError;
1516

16-
use crate::sr25519;
17+
/// Official Polkadot-JS allowed scrypt parameters
18+
/// https://github.com/polkadot-js/common/blob/fe0886be239526e6c559e98d1099815d4b4f4a7f/packages/util-crypto/src/scrypt/defaults.ts#L6
19+
const ALLOWED_PARAMS: &[(u32, u32, u32)] = &[
20+
(1 << 13, 10, 8),
21+
(1 << 14, 5, 8),
22+
(1 << 15, 3, 8),
23+
(1 << 15, 1, 8), // Standard
24+
(1 << 16, 2, 8),
25+
(1 << 17, 1, 8), // High Security
26+
];
1727

1828
/// Given a JSON keypair as exported from Polkadot-JS, this returns an [`sr25519::Keypair`]
1929
pub fn decrypt_json(json: &str, password: &str) -> Result<sr25519::Keypair, Error> {
@@ -75,9 +85,6 @@ struct KeyringPairJson {
7585
address: AccountId32,
7686
}
7787

78-
// Re-export this type which is used above.
79-
pub use subxt_utils_accountid32::AccountId32;
80-
8188
// This can be removed once split_array is stabilized.
8289
fn slice_to_u32(slice: &[u8]) -> u32 {
8390
u32::from_le_bytes(slice.try_into().expect("Slice should be 4 bytes."))
@@ -118,13 +125,21 @@ impl KeyringPairJson {
118125
// protection against carefully-crafted params that can eat up CPU since these are user
119126
// inputs. So we need to get very clever here, but atm we only allow the defaults
120127
// and if no match, bail out.
121-
if n != 32768 || p != 1 || r != 8 {
128+
//
129+
// Check if the combination exists in the allowed list
130+
if !ALLOWED_PARAMS
131+
.iter()
132+
.any(|&(a_n, a_p, a_r)| n == a_n && p == a_p && r == a_r)
133+
{
122134
return Err(Error::UnsupportedScryptParameters { n, p, r });
123135
}
124136

137+
// Calculate the `log_n` (e.g., 32768 -> 15)
138+
let log_n = (n as f64).log2() as u8;
139+
125140
// Hash password.
126-
let scrypt_params =
127-
scrypt::Params::new(15, 8, 1, 32).expect("Provided parameters should be valid.");
141+
let scrypt_params = scrypt::Params::new(log_n, r, p, 32)
142+
.map_err(|_| Error::UnsupportedScryptParameters { n, p, r })?;
128143
let mut key = Key::default();
129144
scrypt::scrypt(password.as_bytes(), salt, &scrypt_params, &mut key)
130145
.expect("Key should be 32 bytes.");
@@ -171,7 +186,28 @@ mod test {
171186
use super::*;
172187

173188
#[test]
174-
fn test_get_keypair_sr25519() {
189+
fn test_get_bob_keypair_sr25519() {
190+
let json = r#"
191+
{
192+
"encoded": "J2FFcPHAY11Pmq/38eqbwfUv9OPitYJs+oYgahBvlagAAAIAAQAAAAgAAAB5o0DwXCWDblsH+9pc++RaBO4fpHBHzUirHFHFE9yS3sDzgAIQjhgvPqJ3ODrMR2gy7vk0VZg1fyirIvmsrfjGbWnOI8YU0joX0tYytroyWaykFKtZJMmE0pNKcJ5dJmDxscbK53Ac+7ld2UdH07yKPXxmPuYNNw3vKx8cg9CdQgifKfzQxHnC+EUpOoHPLwGlHsFEYtIlQtngqd9n",
193+
"encoding": {
194+
"content": ["pkcs8", "sr25519"],
195+
"type": ["scrypt", "xsalsa20-poly1305"],
196+
"version": "3"
197+
},
198+
"address": "5CfWTDh7XxJ2yrayqQ2aJnnZAH5v5XaF1oJFfH5QCpbfP9v8",
199+
"meta": {
200+
"genesisHash": "",
201+
"name": "Bob (Dev)",
202+
"whenCreated": 1768916488918
203+
}
204+
}
205+
"#;
206+
decrypt_json(json, "whoisbob").unwrap();
207+
}
208+
209+
#[test]
210+
fn test_get_alice_keypair_sr25519() {
175211
let json = r#"
176212
{
177213
"encoded": "DumgApKCTqoCty1OZW/8WS+sgo6RdpHhCwAkA2IoDBMAgAAAAQAAAAgAAAB6IG/q24EeVf0JqWqcBd5m2tKq5BlyY84IQ8oamLn9DZe9Ouhgunr7i36J1XxUnTI801axqL/ym1gil0U8440Qvj0lFVKwGuxq38zuifgoj0B3Yru0CI6QKEvQPU5xxj4MpyxdSxP+2PnTzYao0HDH0fulaGvlAYXfqtU89xrx2/z9z7IjSwS3oDFPXRQ9kAdDebtyCVreZ9Otw9v3",

0 commit comments

Comments
 (0)