use crate::{crypto::KeyTypeId, ed25519, sr25519};
use std::{
fmt::{Debug, Display},
panic::UnwindSafe,
sync::Arc,
};
pub use sp_externalities::{Externalities, ExternalitiesExt};
pub trait BareCryptoStore: Send + Sync {
fn sr25519_public_keys(&self, id: KeyTypeId) -> Vec<sr25519::Public>;
fn sr25519_generate_new(
&mut self,
id: KeyTypeId,
seed: Option<&str>,
) -> Result<sr25519::Public, String>;
fn sr25519_key_pair(&self, id: KeyTypeId, pub_key: &sr25519::Public) -> Option<sr25519::Pair>;
fn ed25519_public_keys(&self, id: KeyTypeId) -> Vec<ed25519::Public>;
fn ed25519_generate_new(
&mut self,
id: KeyTypeId,
seed: Option<&str>,
) -> Result<ed25519::Public, String>;
fn ed25519_key_pair(&self, id: KeyTypeId, pub_key: &ed25519::Public) -> Option<ed25519::Pair>;
fn insert_unknown(&mut self, _key_type: KeyTypeId, _suri: &str, _public: &[u8]) -> Result<(), ()>;
fn password(&self) -> Option<&str>;
fn has_keys(&self, public_keys: &[(Vec<u8>, KeyTypeId)]) -> bool;
}
pub type BareCryptoStorePtr = Arc<parking_lot::RwLock<dyn BareCryptoStore>>;
sp_externalities::decl_extension! {
pub struct KeystoreExt(BareCryptoStorePtr);
}
pub trait CodeExecutor: Sized + Send + Sync + CallInWasm + Clone + 'static {
type Error: Display + Debug + Send + 'static;
fn call<
E: Externalities,
R: codec::Codec + PartialEq,
NC: FnOnce() -> Result<R, String> + UnwindSafe,
>(
&self,
ext: &mut E,
method: &str,
data: &[u8],
use_native: bool,
native_call: Option<NC>,
) -> (Result<crate::NativeOrEncoded<R>, Self::Error>, bool);
}
pub trait CallInWasm: Send + Sync {
fn call_in_wasm(
&self,
wasm_blob: &[u8],
method: &str,
call_data: &[u8],
ext: &mut dyn Externalities,
) -> Result<Vec<u8>, String>;
}
sp_externalities::decl_extension! {
pub struct CallInWasmExt(Box<dyn CallInWasm>);
}
impl CallInWasmExt {
pub fn new<T: CallInWasm + 'static>(inner: T) -> Self {
Self(Box::new(inner))
}
}