pub struct Wallet {
pub name: String,
pub version: String,
pub scrypt_params: ScryptParamsDef,
pub accounts: HashMap<H160, Account>,
pub extra: Option<HashMap<String, String>>,
/* private fields */
}
Fields§
§name: String
§version: String
§scrypt_params: ScryptParamsDef
§accounts: HashMap<H160, Account>
§extra: Option<HashMap<String, String>>
Additional wallet metadata stored as key-value pairs
Implementations§
Source§impl Wallet
impl Wallet
Sourcepub const DEFAULT_WALLET_NAME: &'static str = "NeoWallet"
pub const DEFAULT_WALLET_NAME: &'static str = "NeoWallet"
The default wallet name.
Sourcepub const CURRENT_VERSION: &'static str = "1.0"
pub const CURRENT_VERSION: &'static str = "1.0"
The current wallet version.
Sourcepub fn to_nep6(&self) -> Result<Nep6Wallet, WalletError>
pub fn to_nep6(&self) -> Result<Nep6Wallet, WalletError>
Converts the wallet to a NEP6Wallet format.
Sourcepub fn from_nep6(nep6: Nep6Wallet) -> Result<Self, WalletError>
pub fn from_nep6(nep6: Nep6Wallet) -> Result<Self, WalletError>
Creates a wallet from a NEP6Wallet format.
pub fn from_account(account: &Account) -> Result<Wallet, WalletError>
Sourcepub fn from_accounts(accounts: Vec<Account>) -> Result<Wallet, WalletError>
pub fn from_accounts(accounts: Vec<Account>) -> Result<Wallet, WalletError>
Adds the given accounts to this wallet, if it doesn’t contain an account with the same script hash (address).
§Parameters
accounts
- The accounts to add
§Returns
Returns the mutable wallet reference if the accounts were successfully added, or a WalletError
if an account is already contained in another wallet.
§Errors
Returns a WalletError::IllegalArgument
error if an account is already contained in another wallet.
§Example
use neo3::prelude::*;
use neo3::neo_protocol::AccountTrait;
let account1 = protocol::Account::create().unwrap();
let account2 = protocol::Account::create().unwrap();
let mut wallet = wallets::Wallet::from_accounts(vec![account1, account2]).unwrap();
pub fn save_to_file(&self, path: PathBuf) -> Result<(), WalletError>
pub fn get_account(&self, script_hash: &H160) -> Option<&Account>
pub fn remove_account(&mut self, script_hash: &H160) -> bool
pub fn encrypt_accounts(&mut self, password: &str)
Sourcepub fn encrypt_accounts_parallel(&mut self, password: &str)
pub fn encrypt_accounts_parallel(&mut self, password: &str)
Encrypts all accounts in the wallet using parallel processing.
This method provides significant performance improvements when dealing with wallets containing many accounts by leveraging Rayon’s parallel iteration. The encryption of each account is independent and CPU-intensive (due to scrypt key derivation), making it ideal for parallelization.
§Arguments
password
- The password to use for encrypting all accounts
§Performance Notes
- Uses Rayon’s work-stealing thread pool for optimal CPU utilization
- Each account encryption is processed in parallel
- Thread pool size automatically adjusts to available CPU cores
- Performance gains scale with the number of accounts and CPU cores
§Example
// For wallets with many accounts, use parallel encryption
wallet.encrypt_accounts_parallel("strong_password");
Sourcepub fn encrypt_accounts_parallel_with_threads(
&mut self,
password: &str,
num_threads: usize,
)
pub fn encrypt_accounts_parallel_with_threads( &mut self, password: &str, num_threads: usize, )
Encrypts accounts in parallel with custom thread pool configuration.
This method allows fine-tuning of the parallel encryption process by configuring the number of threads used. This can be useful in scenarios where you want to limit CPU usage or optimize for specific hardware.
§Arguments
password
- The password to use for encrypting all accountsnum_threads
- The number of threads to use for parallel processing
§Example
// Use 4 threads for encryption
wallet.encrypt_accounts_parallel_with_threads("strong_password", 4);
Sourcepub fn encrypt_accounts_batch_parallel(
&mut self,
password: &str,
batch_size: usize,
)
pub fn encrypt_accounts_batch_parallel( &mut self, password: &str, batch_size: usize, )
Encrypts accounts in parallel using batch processing.
This method processes accounts in batches, which can be more efficient for very large wallets by reducing overhead and improving cache locality. It uses a different approach than the standard parallel method by collecting account data first to avoid mutable borrow conflicts.
§Arguments
password
- The password to use for encrypting all accountsbatch_size
- The number of accounts to process in each batch
§Example
// Process accounts in batches of 50
wallet.encrypt_accounts_batch_parallel("strong_password", 50);
Sourcepub fn create(path: &PathBuf, password: &str) -> Result<Self, WalletError>
👎Deprecated since 0.1.0: Please use create_wallet
instead
pub fn create(path: &PathBuf, password: &str) -> Result<Self, WalletError>
create_wallet
insteadCreates a new wallet and saves it to the specified path
This method has been renamed to create_wallet
for clarity.
Please use create_wallet
instead.
§Arguments
path
- The file path where the wallet will be savedpassword
- The password to encrypt the wallet
§Returns
A Result
containing the new wallet or a WalletError
Sourcepub fn open(path: &PathBuf, password: &str) -> Result<Self, WalletError>
👎Deprecated since 0.1.0: Please use open_wallet
instead
pub fn open(path: &PathBuf, password: &str) -> Result<Self, WalletError>
open_wallet
insteadSourcepub fn get_accounts(&self) -> Vec<&Account>
pub fn get_accounts(&self) -> Vec<&Account>
Returns all accounts in the wallet
Sourcepub fn create_account(&mut self) -> Result<&Account, WalletError>
pub fn create_account(&mut self) -> Result<&Account, WalletError>
Creates a new account in the wallet
Sourcepub fn import_private_key(&mut self, wif: &str) -> Result<&Account, WalletError>
pub fn import_private_key(&mut self, wif: &str) -> Result<&Account, WalletError>
Imports a private key in WIF format
Sourcepub fn verify_password(&self, password: &str) -> bool
pub fn verify_password(&self, password: &str) -> bool
Verifies if the provided password is correct by attempting to decrypt any encrypted account
This function checks if the provided password can successfully decrypt at least one of the encrypted private keys in the wallet. If at least one account can be decrypted, the password is considered valid.
Returns true if the password is correct, false otherwise.
Sourcepub fn change_password(
&mut self,
current_password: &str,
new_password: &str,
) -> Result<(), WalletError>
pub fn change_password( &mut self, current_password: &str, new_password: &str, ) -> Result<(), WalletError>
Changes the wallet password
Sourcepub fn change_password_parallel(
&mut self,
current_password: &str,
new_password: &str,
) -> Result<(), WalletError>
pub fn change_password_parallel( &mut self, current_password: &str, new_password: &str, ) -> Result<(), WalletError>
Changes the wallet password using parallel processing.
This method provides better performance for wallets with many accounts by parallelizing both the decryption and re-encryption processes.
§Arguments
current_password
- The current password of the walletnew_password
- The new password to set
§Returns
A Result
indicating success or containing a WalletError
on failure
§Example
wallet.change_password_parallel("old_password", "new_password").unwrap();
Sourcepub async fn get_unclaimed_gas<P>(
&self,
rpc_client: &P,
) -> Result<UnclaimedGas, WalletError>
pub async fn get_unclaimed_gas<P>( &self, rpc_client: &P, ) -> Result<UnclaimedGas, WalletError>
Gets the unclaimed GAS for all accounts in the wallet
Source§impl Wallet
impl Wallet
Sourcepub async fn sign_message<S: Send + Sync + AsRef<[u8]>>(
&self,
message: S,
) -> Result<Secp256r1Signature, WalletError>
pub async fn sign_message<S: Send + Sync + AsRef<[u8]>>( &self, message: S, ) -> Result<Secp256r1Signature, WalletError>
Signs a given message using the default account’s private key.
This method computes the SHA-256 hash of the input message and then signs it using the ECDSA Secp256r1 algorithm. It’s primarily used for generating signatures that can prove ownership of an address or for other cryptographic verifications.
§Parameters
message
: The message to be signed. This can be any data that implementsAsRef<[u8]>
, allowing for flexibility in the type of data that can be signed.
§Returns
A Result
that, on success, contains the Secp256r1Signature
of the message. On failure,
it returns a WalletError
, which could indicate issues like a missing key pair.
§Example
async fn example() -> Result<(), Box<dyn std::error::Error>> {
let message = "Hello, world!";
let signature = wallet.sign_message(message).await?;
println!("Signed message: {:?}", signature);
Sourcepub async fn get_witness<'a, P: JsonRpcProvider + 'static>(
&self,
tx: &Transaction<'a, P>,
) -> Result<Witness, WalletError>
pub async fn get_witness<'a, P: JsonRpcProvider + 'static>( &self, tx: &Transaction<'a, P>, ) -> Result<Witness, WalletError>
Generates a witness for a transaction using the default account’s key pair.
This method is used to attach a signature to a transaction, proving that the transaction was authorized by the owner of the default account. It’s an essential step in transaction validation for blockchain systems.
§Parameters
tx
: A reference to the transaction that needs a witness.
§Returns
A Result
that, on success, contains the Witness
for the given transaction.
On failure, it returns a WalletError
, which could be due to issues like a missing
key pair.
§Example
async fn example() -> Result<(), Box<dyn std::error::Error>> {
let witness = wallet.get_witness(&tx).await?;
println!("Witness: {:?}", witness);
Sourcepub async fn sign_transaction<'a, P>(
&self,
tx_builder: &'a mut TransactionBuilder<'a, P>,
account_address: &str,
password: &str,
) -> Result<Transaction<'a, P>, WalletError>where
P: JsonRpcProvider + 'static,
pub async fn sign_transaction<'a, P>(
&self,
tx_builder: &'a mut TransactionBuilder<'a, P>,
account_address: &str,
password: &str,
) -> Result<Transaction<'a, P>, WalletError>where
P: JsonRpcProvider + 'static,
Signs a transaction using the specified account.
§Arguments
tx_builder
- The transaction builder containing the transaction to signaccount_address
- The address of the account to use for signingpassword
- The password to decrypt the account’s private key if needed
§Returns
A Result
containing the signed transaction or a WalletError
Sourcepub fn create_wallet(
path: &PathBuf,
password: &str,
) -> Result<Self, WalletError>
pub fn create_wallet( path: &PathBuf, password: &str, ) -> Result<Self, WalletError>
Sourcepub fn open_wallet(path: &PathBuf, password: &str) -> Result<Self, WalletError>
pub fn open_wallet(path: &PathBuf, password: &str) -> Result<Self, WalletError>
Sourcepub fn get_all_accounts(&self) -> Vec<&Account>
pub fn get_all_accounts(&self) -> Vec<&Account>
Sourcepub fn create_new_account(&mut self) -> Result<&Account, WalletError>
pub fn create_new_account(&mut self) -> Result<&Account, WalletError>
Sourcepub fn import_from_wif(
&mut self,
private_key: &str,
) -> Result<&Account, WalletError>
pub fn import_from_wif( &mut self, private_key: &str, ) -> Result<&Account, WalletError>
Sourcepub async fn get_unclaimed_gas_as_float<P>(
&self,
rpc_client: &RpcClient<P>,
) -> Result<f64, WalletError>where
P: JsonRpcProvider + 'static,
pub async fn get_unclaimed_gas_as_float<P>(
&self,
rpc_client: &RpcClient<P>,
) -> Result<f64, WalletError>where
P: JsonRpcProvider + 'static,
Sourcepub fn with_network(self, network: u32) -> Self
pub fn with_network(self, network: u32) -> Self
This method configures the wallet to operate within a specific blockchain network by setting the network magic (ID), which is essential for correctly signing transactions.
§Parameters
network
: The network ID to set for the wallet.
§Returns
The modified Wallet
instance with the new network ID set.
§Example
let mut wallet = wallets::Wallet::new();
wallet = wallet.with_network(0x334F454E); // MainNet magic number
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Wallet
impl<'de> Deserialize<'de> for Wallet
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl WalletTrait for Wallet
impl WalletTrait for Wallet
Source§fn scrypt_params(&self) -> &ScryptParamsDef
fn scrypt_params(&self) -> &ScryptParamsDef
Source§fn default_account(&self) -> &Account
fn default_account(&self) -> &Account
Source§fn set_version(&mut self, version: String)
fn set_version(&mut self, version: String)
Source§fn set_scrypt_params(&mut self, params: ScryptParamsDef)
fn set_scrypt_params(&mut self, params: ScryptParamsDef)
Source§fn set_default_account(&mut self, default_account: H160)
fn set_default_account(&mut self, default_account: H160)
Source§fn add_account(&mut self, account: Self::Account)
fn add_account(&mut self, account: Self::Account)
Auto Trait Implementations§
impl Freeze for Wallet
impl RefUnwindSafe for Wallet
impl Send for Wallet
impl Sync for Wallet
impl Unpin for Wallet
impl UnwindSafe for Wallet
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,
§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