Skip to main content

Keypair

Struct Keypair 

Source
pub struct Keypair { /* private fields */ }

Implementations§

Source§

impl Keypair

Source

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 (1 for SR25519).
Returns:
    keypair (Keypair): A new Keypair instance.
Source

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.
Source

pub fn create_from_mnemonic(mnemonic: &str) -> Result<Self, String>

Creates a Keypair from a mnemonic phrase.

Arguments:
    mnemonic (str): The mnemonic phrase to create the keypair from.
Returns:
    keypair (Keypair): The Keypair created from the mnemonic.
Source

pub fn create_from_seed(seed: Vec<u8>) -> Result<Self, String>

Creates a Keypair from a seed.

Arguments:
    seed (Vec<u8>): The seed bytes to create the keypair from.
Returns:
    keypair (Keypair): The Keypair created from the seed.
Source

pub fn create_from_private_key(private_key: &str) -> 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.
Returns:
    keypair (Keypair): The Keypair created from the private key.
Source

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.
Source

pub fn create_from_uri(uri: &str) -> Result<Self, String>

Creates a Keypair from a URI string.

Arguments:
    uri (str): The URI string to create the keypair from.
Returns:
    keypair (Keypair): The Keypair created from the URI.
Source

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.
Source

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.
Source

pub fn ss58_address(&self) -> Option<String>

Returns the SS58 address of the keypair.

Source

pub fn public_key(&self) -> Result<Option<Vec<u8>>, String>

Returns the public key of the keypair as bytes.

Source

pub fn ss58_format(&self) -> u8

Returns the SS58 format number.

Source

pub fn seed_hex(&self) -> Option<Vec<u8>>

Source

pub fn crypto_type(&self) -> u8

Returns the cryptographic algorithm type.

Source

pub fn set_crypto_type(&mut self, crypto_type: u8)

Sets the cryptographic algorithm type.

Arguments:
    crypto_type (u8): The cryptographic algorithm type (1 for SR25519).
Source

pub fn mnemonic(&self) -> Option<String>

Returns the mnemonic phrase of the keypair.

Source

pub fn private_key(&self) -> Result<Option<Vec<u8>>, String>

Returns the private key of the keypair as bytes.

Trait Implementations§

Source§

impl Clone for Keypair

Source§

fn clone(&self) -> Keypair

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Keypair

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Keypair

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Keypair

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T, Outer> IsWrappedBy<Outer> for T
where Outer: AsRef<T> + AsMut<T> + From<T>, T: From<Outer>,

Source§

fn from_ref(outer: &Outer) -> &T

Get a reference to the inner from the outer.

Source§

fn from_mut(outer: &mut Outer) -> &mut T

Get a mutable reference to the inner from the outer.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> UncheckedInto<T> for S
where T: UncheckedFrom<S>,

Source§

fn unchecked_into(self) -> T

The counterpart to unchecked_from.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> JsonSchemaMaybe for T

Source§

impl<T> MaybeDebug for T
where T: Debug,

Source§

impl<T> MaybeRefUnwindSafe for T
where T: RefUnwindSafe,