pub struct Keypair { /* private fields */ }Implementations§
Source§impl Keypair
impl Keypair
Sourcepub fn pair_is_some(&self) -> bool
pub fn pair_is_some(&self) -> bool
Returns whether this keypair has an active key pair.
Sourcepub fn new(
ss58_address: Option<String>,
public_key: Option<String>,
private_key: Option<String>,
ss58_format: u8,
seed_hex: Option<Vec<u8>>,
crypto_type: u8,
) -> Result<Self, String>
pub fn new( ss58_address: Option<String>, public_key: Option<String>, private_key: Option<String>, ss58_format: u8, seed_hex: Option<Vec<u8>>, crypto_type: u8, ) -> Result<Self, String>
Creates a new Keypair instance.
Arguments:
ss58_address (Option<String>): Optional SS58-formatted address.
public_key (Option<String>): Optional public key as hex string.
private_key (Option<String>): Optional private key as hex string.
ss58_format (u8): The SS58 format number for address encoding.
seed_hex (Option<Vec<u8>>): Optional seed bytes.
crypto_type (u8): The cryptographic algorithm type. 0 for ED25519, 1 for SR25519.
Returns:
keypair (Keypair): A new Keypair instance.Sourcepub fn generate_mnemonic(n_words: usize) -> Result<String, String>
pub fn generate_mnemonic(n_words: usize) -> Result<String, String>
Generates a new mnemonic phrase.
Arguments:
n_words (usize): The number of words in the mnemonic (e.g., 12, 15, 18, 21, 24).
Returns:
mnemonic (String): The generated mnemonic phrase.Sourcepub fn create_from_mnemonic(
mnemonic: &str,
crypto_type: u8,
) -> Result<Self, String>
pub fn create_from_mnemonic( mnemonic: &str, crypto_type: u8, ) -> Result<Self, String>
Creates a Keypair from a mnemonic phrase.
Arguments:
mnemonic (str): The mnemonic phrase to create the keypair from.
crypto_type (u8): The cryptographic algorithm type. 0 for ED25519, 1 for SR25519.
Returns:
keypair (Keypair): The Keypair created from the mnemonic.Sourcepub fn create_from_seed(seed: Vec<u8>, crypto_type: u8) -> Result<Self, String>
pub fn create_from_seed(seed: Vec<u8>, crypto_type: u8) -> Result<Self, String>
Creates a Keypair from a seed.
Arguments:
seed (Vec<u8>): The seed bytes to create the keypair from.
crypto_type (u8): The cryptographic algorithm type. 0 for ED25519, 1 for SR25519.
Returns:
keypair (Keypair): The Keypair created from the seed.Sourcepub fn create_from_private_key(
private_key: &str,
crypto_type: u8,
) -> Result<Self, String>
pub fn create_from_private_key( private_key: &str, crypto_type: u8, ) -> Result<Self, String>
Creates a Keypair from a private key.
Arguments:
private_key (str): The private key as hex string to create the keypair from.
crypto_type (u8): The cryptographic algorithm type. 0 for ED25519, 1 for SR25519.
Returns:
keypair (Keypair): The Keypair created from the private key.Sourcepub fn create_from_encrypted_json(
json_data: &str,
passphrase: &str,
) -> Result<Keypair, String>
pub fn create_from_encrypted_json( json_data: &str, passphrase: &str, ) -> Result<Keypair, String>
Creates a Keypair from encrypted JSON data.
Arguments:
json_data (str): The encrypted JSON data containing the keypair.
passphrase (str): The passphrase to decrypt the JSON data.
Returns:
keypair (Keypair): The Keypair created from the encrypted JSON.Sourcepub fn create_from_uri(uri: &str, crypto_type: u8) -> Result<Self, String>
pub fn create_from_uri(uri: &str, crypto_type: u8) -> Result<Self, String>
Creates a Keypair from a URI string.
Arguments:
uri (str): The URI string to create the keypair from.
crypto_type (u8): The cryptographic algorithm type. 0 for ED25519, 1 for SR25519.
Returns:
keypair (Keypair): The Keypair created from the URI.Sourcepub fn sign(&self, data: Vec<u8>) -> Result<Vec<u8>, String>
pub fn sign(&self, data: Vec<u8>) -> Result<Vec<u8>, String>
Signs data with the keypair’s private key.
Arguments:
data (Vec<u8>): The data to sign as bytes.
Returns:
signature (Vec<u8>): The signature as bytes.Sourcepub fn verify(&self, data: Vec<u8>, signature: Vec<u8>) -> Result<bool, String>
pub fn verify(&self, data: Vec<u8>, signature: Vec<u8>) -> Result<bool, String>
Verifies a signature against data using the keypair’s public key.
Arguments:
data (Vec<u8>): The data that was signed as bytes.
signature (Vec<u8>): The signature to verify as bytes.
Returns:
verified (bool): ``True`` if the signature is valid, ``False`` otherwise.Sourcepub fn encrypt(&self, message: &[u8]) -> Result<Vec<u8>, String>
pub fn encrypt(&self, message: &[u8]) -> Result<Vec<u8>, String>
Encrypts a message using the recipient’s ED25519 public key (sealed box).
Arguments:
message (&[u8]): The plaintext message to encrypt.
Returns:
ciphertext (Vec<u8>): The encrypted message (48 bytes longer than the input).Sourcepub fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>, String>
pub fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>, String>
Decrypts a sealed box ciphertext using the keypair’s ED25519 private key.
Arguments:
ciphertext (&[u8]): The encrypted message to decrypt.
Returns:
plaintext (Vec<u8>): The decrypted message.Sourcepub fn ss58_address(&self) -> Option<String>
pub fn ss58_address(&self) -> Option<String>
Returns the SS58 address of the keypair.
Sourcepub fn public_key(&self) -> Result<Option<Vec<u8>>, String>
pub fn public_key(&self) -> Result<Option<Vec<u8>>, String>
Returns the public key of the keypair as bytes.
Sourcepub fn ss58_format(&self) -> u8
pub fn ss58_format(&self) -> u8
Returns the SS58 format number.
Sourcepub fn seed_hex(&self) -> Option<Vec<u8>>
pub fn seed_hex(&self) -> Option<Vec<u8>>
Returns the seed bytes used to derive this keypair, if known.
Sourcepub fn crypto_type(&self) -> u8
pub fn crypto_type(&self) -> u8
Returns the cryptographic algorithm type. Derives from the inner pair if present; falls back to the stored field for pub-only keypairs.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Keypair
impl RefUnwindSafe for Keypair
impl Send for Keypair
impl Sync for Keypair
impl Unpin for Keypair
impl UnsafeUnpin for Keypair
impl UnwindSafe for Keypair
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T, Outer> IsWrappedBy<Outer> for T
impl<T, Outer> IsWrappedBy<Outer> for T
Source§impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
Source§fn unchecked_into(self) -> T
fn unchecked_into(self) -> T
unchecked_from.