Skip to content
Snippets Groups Projects

Draft: Added support for the different SecretFormat within the Vault

Closed Nicolas80 requested to merge Nicolas80/gcli-v2s:vault-support-for-all-secret-format into master
Files
13
+ 93
0
@@ -82,3 +82,96 @@ where
.into()
}
}
// Unit tests
#[cfg(test)]
mod tests {
use super::*;
/// Test which verifies that it's possible to derive a key coming from a cesium v1 id & password
///
/// Using subkey command with **ed25519** scheme to show we can derive a key from a seed
/// and to retrieve expected values.
///
/// ##### Without derivation (using seed from the test)
/// ```
/// subkey inspect --scheme ed25519
/// URI:
/// Secret Key URI `0x2101d2bc68de9ad149c06293bfe489c8608de576c88927aa5439a81be17aae84` is account:
/// Network ID: substrate
/// Secret seed: 0x2101d2bc68de9ad149c06293bfe489c8608de576c88927aa5439a81be17aae84
/// Public key (hex): 0x697f6bd16ddebf142384e503fd3f3efc39fe5c7be7c693bd98d982403bb6eb74
/// Account ID: 0x697f6bd16ddebf142384e503fd3f3efc39fe5c7be7c693bd98d982403bb6eb74
/// Public key (SS58): 5ET2jhgJFoNQUpgfdSkdwftK8DKWdqZ1FKm5GKWdPfMWhPr4
/// SS58 Address: 5ET2jhgJFoNQUpgfdSkdwftK8DKWdqZ1FKm5GKWdPfMWhPr4
/// ```
///
/// ##### With derivation '//0' (using seed from the test)
/// ```
/// subkey inspect --scheme ed25519
/// URI:
/// Secret Key URI `0x2101d2bc68de9ad149c06293bfe489c8608de576c88927aa5439a81be17aae84//0` is account:
/// Network ID: substrate
/// Secret seed: 0x916e95359a49c82e3d84269b90551433352c433eb9bb270fb8cb86e8a6c9ec85
/// Public key (hex): 0x1658ce32f039dff26d83b5282f611cfed8c71296a311417ef737db4e016194de
/// Account ID: 0x1658ce32f039dff26d83b5282f611cfed8c71296a311417ef737db4e016194de
/// Public key (SS58): 5Ca1HrNxQ4hiekd92Z99fzhfdSAqPy2rUkLBmwLsgLCjeSQf
/// SS58 Address: 5Ca1HrNxQ4hiekd92Z99fzhfdSAqPy2rUkLBmwLsgLCjeSQf
/// ```
#[test]
fn test_cesium_v1_key_derivation() {
let cesium_id = "test_cesium_id".to_string();
let cesium_pwd = "test_cesium_pwd".to_string();
let cesium_keypair = pair_from_cesium(cesium_id, cesium_pwd);
println!(
"Nacl keypair: pkey:'0x{}' skey:'0x{}'",
hex::encode(cesium_keypair.pkey),
hex::encode(cesium_keypair.skey)
);
let cesium_v1_pubkey = bs58::encode(cesium_keypair.pkey).into_string();
println!("Cesium v1 Pubkey: '{}'", cesium_v1_pubkey);
let cesium_v1_address: AccountId = cesium_keypair.pkey.into();
println!("SS58 Address: '{}'", cesium_v1_address);
//ed25519 seed **seems** to be the first 32 bytes of the secret key from nacl keypair
let mut seed: [u8; 32] = [0; 32];
seed.copy_from_slice(&cesium_keypair.skey[0..32]);
println!();
println!("ed25519 seed: '0x{}'", hex::encode(seed));
let ed25519_pair = sp_core::ed25519::Pair::from_seed(&seed);
println!();
println!(
"ed25519 keypair: public:'0x{}' raw_vec:'0x{}'",
hex::encode(ed25519_pair.public().0),
hex::encode(ed25519_pair.to_raw_vec().as_slice())
);
let ed25519_address: AccountId = ed25519_pair.public().into();
println!("ed25519 keypair: public SS58 Address:'{}'", ed25519_address);
assert_eq!(cesium_v1_address, ed25519_address);
// Tested derivation manually with `subkey` command using adapted suri: `0x2101d2bc68de9ad149c06293bfe489c8608de576c88927aa5439a81be17aae84//0`
let expected_ss58_address_derivation_0 =
"5Ca1HrNxQ4hiekd92Z99fzhfdSAqPy2rUkLBmwLsgLCjeSQf".to_string();
let (derived_ed25519_pair, _seed) = ed25519_pair
.derive(Some(sp_core::DeriveJunction::hard(0)).into_iter(), None)
.unwrap();
println!();
println!(
"ed25519 derived keypair: public:'0x{}' raw_vec:'0x{}'",
hex::encode(derived_ed25519_pair.public().0),
hex::encode(derived_ed25519_pair.to_raw_vec().as_slice())
);
let account_id32 = subxt::utils::AccountId32::from(derived_ed25519_pair.public());
println!(
"ed25519 derived keypair: public SS58 Address:'{}'",
account_id32
);
assert_eq!(expected_ss58_address_derivation_0, account_id32.to_string());
}
}
Loading