Struct NEP2

Source
pub struct NEP2;
Expand description

NEP2 provides methods for encrypting and decrypting NEO private keys according to the NEP2 standard specification.

This struct implements the core functionality for working with NEP2 encrypted keys, including encryption and decryption operations with configurable security parameters.

Implementations§

Source§

impl NEP2

Source

pub fn encrypt(password: &str, key_pair: &KeyPair) -> Result<String, Nep2Error>

Encrypts a KeyPair with a password using default scrypt parameters.

§Arguments
  • password - The password to encrypt the key with
  • key_pair - The KeyPair containing the private key to encrypt
§Returns

A NEP2-formatted string containing the encrypted key, or an error if encryption fails

§Example
use neo3::prelude::*;
use neo3::neo_crypto::{KeyPair, Secp256r1PrivateKey};
use neo3::neo_protocol::NEP2;
use p256::elliptic_curve::rand_core::OsRng;

// Generate a key pair
let key_pair = KeyPair::from_secret_key(&Secp256r1PrivateKey::random(&mut OsRng));

// Encrypt the key pair
let encrypted = NEP2::encrypt("my-secure-password", &key_pair).expect("Encryption failed");
Source

pub fn encrypt_with_params( password: &str, key_pair: &KeyPair, params: Params, ) -> Result<String, Nep2Error>

Encrypts a KeyPair with a password using custom scrypt parameters.

§Arguments
  • password - The password to encrypt the key with
  • key_pair - The KeyPair containing the private key to encrypt
  • params - Custom scrypt parameters for key derivation
§Returns

A NEP2-formatted string containing the encrypted key, or an error if encryption fails

§Example
use neo3::prelude::*;
use neo3::neo_crypto::{KeyPair, Secp256r1PrivateKey};
use neo3::neo_protocol::NEP2;
use p256::elliptic_curve::rand_core::OsRng;
use scrypt::Params;

// Generate a key pair
let key_pair = KeyPair::from_secret_key(&Secp256r1PrivateKey::random(&mut OsRng));

// Custom scrypt parameters
let params = Params::new(15, 8, 8, 32).unwrap();

// Encrypt with custom parameters
let encrypted = NEP2::encrypt_with_params("my-secure-password", &key_pair, params)
    .expect("Encryption failed");
Source

pub fn decrypt(password: &str, nep2: &str) -> Result<KeyPair, Nep2Error>

Decrypts a NEP2-formatted string to retrieve the original KeyPair using default scrypt parameters.

§Arguments
  • password - The password used for encryption
  • nep2 - The NEP2-formatted string containing the encrypted key
§Returns

The decrypted KeyPair, or an error if decryption fails

§Example
use neo3::prelude::*;
use neo3::neo_crypto::{KeyPair, Secp256r1PrivateKey};
use neo3::neo_protocol::NEP2;
use p256::elliptic_curve::rand_core::OsRng;

// First encrypt a key pair
let key_pair = KeyPair::from_secret_key(&Secp256r1PrivateKey::random(&mut OsRng));
let encrypted = NEP2::encrypt("my-password", &key_pair).expect("Encryption failed");

// Then decrypt it back
let decrypted = NEP2::decrypt("my-password", &encrypted).expect("Decryption failed");
Source

pub fn decrypt_with_params( password: &str, nep2: &str, params: Params, ) -> Result<KeyPair, Nep2Error>

Decrypts a NEP2-formatted string to retrieve the original KeyPair using custom scrypt parameters.

§Arguments
  • password - The password used for encryption
  • nep2 - The NEP2-formatted string containing the encrypted key
  • params - Custom scrypt parameters for key derivation
§Returns

The decrypted KeyPair, or an error if decryption fails

§Example
use neo3::prelude::*;
use neo3::neo_crypto::{KeyPair, Secp256r1PrivateKey};
use neo3::neo_protocol::NEP2;
use p256::elliptic_curve::rand_core::OsRng;
use scrypt::Params;

// First encrypt a key pair with custom parameters
let key_pair = KeyPair::from_secret_key(&Secp256r1PrivateKey::random(&mut OsRng));
let params = Params::new(15, 8, 8, 32).unwrap();
let encrypted = NEP2::encrypt_with_params("my-password", &key_pair, params.clone())
    .expect("Encryption failed");

// Then decrypt with the same parameters
let decrypted = NEP2::decrypt_with_params("my-password", &encrypted, params)
    .expect("Decryption failed");
Source

pub fn encrypt_for_test_vector( password: &str, key_pair: &KeyPair, ) -> Result<String, Nep2Error>

Encrypts a KeyPair for test vector compatibility.

This method uses the parameters from the NEP2 specification test vector. It’s primarily for testing and verification against the standard.

§Arguments
  • password - The password to encrypt the key with
  • key_pair - The KeyPair containing the private key to encrypt
§Returns

A NEP2-formatted string containing the encrypted key, or an error if encryption fails

Source

pub fn decrypt_for_test_vector( password: &str, nep2: &str, ) -> Result<KeyPair, Nep2Error>

Decrypts a NEP2-formatted string for test vector compatibility.

This method uses the parameters from the NEP2 specification test vector. It’s primarily for testing and verification against the standard.

§Arguments
  • password - The password used for encryption
  • nep2 - The NEP2-formatted string containing the encrypted key
§Returns

The decrypted KeyPair, or an error if decryption fails

Source

pub fn encrypt_test_vector() -> Result<String, Nep2Error>

Encrypt a private key using the NEP2 test vector parameters and data.

This is specifically for matching the NEP2 specification test vector. It doesn’t perform actual encryption, but instead uses the exact test vector data. It is not recommended for general use.

§Returns

The NEP2-formatted string that exactly matches the test vector

Source

pub fn decrypt_test_vector( password: &str, nep2: &str, ) -> Result<KeyPair, Nep2Error>

Decrypt the NEP2 test vector string.

This is specifically for the NEP2 specification test vector. It bypasses the address verification to ensure the test vector works.

§Arguments
  • password - The password used for encryption (should be “TestingOneTwoThree” for the test vector)
  • nep2 - The NEP2-formatted string (should be the test vector)
§Returns

The decrypted KeyPair

Auto Trait Implementations§

§

impl Freeze for NEP2

§

impl RefUnwindSafe for NEP2

§

impl Send for NEP2

§

impl Sync for NEP2

§

impl Unpin for NEP2

§

impl UnwindSafe for NEP2

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
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
§

impl<T> ErasedDestructor for T
where T: 'static,