1

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?

1 Answer 1

1

Note that AppCrypto trait has const ID: KeyTypeId which determines which keys will be filtered when using LocalKeystore::key_pair. Since you're using sp_application_crypto::sr25519::AppPair, which doesn't use key_types::ACCOUNT, LocalKeystore::key_pair can't find your key - assuming your key is inserted to keystore with key_types::ACCOUNT key type.

One solution is to construct appropriate AppPair using app_crypto! like so:

pub mod my_mod {
    use sp_application_crypto::{app_crypto, key_types::ACCOUNT};
    use sp_core::sr25519;

    app_crypto!(sr25519, ACCOUNT);
}

And then use it to get your private-public pair:

let pair = local_key_store.key_pair::<my_mod::Pair>(&sr25519_public);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.