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
impl NEP2
Sourcepub fn encrypt(password: &str, key_pair: &KeyPair) -> Result<String, Nep2Error>
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 withkey_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");
Sourcepub fn encrypt_with_params(
password: &str,
key_pair: &KeyPair,
params: Params,
) -> Result<String, Nep2Error>
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 withkey_pair
- The KeyPair containing the private key to encryptparams
- 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");
Sourcepub fn decrypt(password: &str, nep2: &str) -> Result<KeyPair, Nep2Error>
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 encryptionnep2
- 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");
Sourcepub fn decrypt_with_params(
password: &str,
nep2: &str,
params: Params,
) -> Result<KeyPair, Nep2Error>
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 encryptionnep2
- The NEP2-formatted string containing the encrypted keyparams
- 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");
Sourcepub fn encrypt_for_test_vector(
password: &str,
key_pair: &KeyPair,
) -> Result<String, Nep2Error>
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 withkey_pair
- The KeyPair containing the private key to encrypt
§Returns
A NEP2-formatted string containing the encrypted key, or an error if encryption fails
Sourcepub fn decrypt_for_test_vector(
password: &str,
nep2: &str,
) -> Result<KeyPair, Nep2Error>
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 encryptionnep2
- The NEP2-formatted string containing the encrypted key
§Returns
The decrypted KeyPair, or an error if decryption fails
Sourcepub fn encrypt_test_vector() -> Result<String, Nep2Error>
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
Sourcepub fn decrypt_test_vector(
password: &str,
nep2: &str,
) -> Result<KeyPair, Nep2Error>
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> 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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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 more