Background
I am currently working on a gadget for substrate that runs another software embedded within the node itself. This is similar to the beefy gadget. However, the software I'm using was originally designed to run as a standalone binary, rather than as substrate-specific software. The gadget serves as a thin wrapper around it and communicates with substrate using subxt over RPC.
Question
In order for this software to function properly and be able to sign/send transactions to the substrate node, it requires access to the SURI/Private Key. However, there doesn't seem to be a clear way to extract the raw private key seed from the keystore itself. The gadget has access to the LocalKeystore
, which is passed to it as a parameter when starting it in the service.rs
file.
Code I have tried
I have attempted the following code:
use sp_application_crypto::{key_types, sr25519, ByteArray, CryptoTypePublicPair, Pair};
fn get_sr25519_pair(local_keystore: Option<Arc<LocalKeystore>>) -> Option<sr25519::AppPair> {
let local_key_store = local_keystore.expect("failed to get local keystore");
let sr25519_public = local_key_store
.keys(key_types::ACCOUNT)
.expect("failed to get keys")
.into_iter()
.find_map(|CryptoTypePublicPair(id, public_key)| {
if id == sr25519::CRYPTO_ID {
sr25519::AppPublic::from_slice(&public_key).ok()
} else {
None
}
})
.expect("failed to get sr25519 public key");
let pair = local_key_store
.key_pair::<sr25519::AppPair>(&sr25519_public)
.expect("failed to get sr25519 pair");
tracing::warn!("Pair Found {}", pair.is_some());
pair
}
The output of this code is Pair Found false
, which means I was able to obtain the public key, but it fails to find its corresponding key pair. It's worth noting that the LocalKeystore::key_pair
method requires P: AppPair
, so I cannot simply use sr25519::Pair
here. This is inconvenient because the key is inserted into the keystore with sr25519::Pair
, not sr25519::AppPair
.
Could you please guide me on how to obtain the private key/seed of the main account (key type is acco
) from the keystore?