Skip to main content

Account

The account module provides a series of methods related to Mystiko accounts.

Data Structure

An account is an object generated from protobuf with the following properties:

PropertyTypeDescription
idString
The unique identifier of the account.
created_atu64
The timestamp when the account was created.
updated_atu64
The timestamp when the account was last updated.
nameString
The name of the account.
shielded_addressString
The shielded address of the account.
public_keyString
The public key of the account.
encrypted_secret_keyString
The encrypted secret key of the account.
wallet_idString
The unique identifier of the wallet to which the account belongs.
scanned_to_idString
The unique identifier of the last scanned transaction.

create

Create an Account.

use mystiko_protos::api::handler::v1::{CreateAccountRequest, CreateAccountResponse};
use mystiko_protos::core::handler::v1::CreateAccountOptions;
use mystiko_lib::account::create;
use mystiko_protos::api::v1::api_response;

let options = CreateAccountOptions::builder().wallet_password("P@ssw0rd".to_string()).build();
let request = CreateAccountRequest::builder().options(options).build();
let response = create(request);
let account = match response.result {
Some(api_response::Result::Data(data)) => match CreateAccountResponse::try_from(data) {
Ok(account) => account,
Err(e) => return Err(anyhow!(format!("Failed to parse response: {}", e))),
},
Some(api_response::Result::ErrorMessage(error)) => {
return Err(anyhow!(format!("API error: {}", error)));
}
None => {
return Err(anyhow!("No result found in response"));
}
};

The parameter list for CreateAccountOptions:

ParameterRequiredDescription
wallet_passwordtrue
The password of the encrypted wallet.
namefalse
The name of the account.
secret_keyfalse
The secret key of the account.

count

Query the number of accounts in the wallet based on specific conditions.

use mystiko_protos::storage::v1::{QueryFilter, SubFilter, ConditionOperator};
use mystiko_protos::api::handler::v1::{CountAccountRequest, CountAccountResponse};
use mystiko_lib::account::count;
use mystiko_protos::api::v1::api_response;

let filter = SubFilter::equal("name", "test_account");
let request= CountAccountRequest::builder()
.filter(
QueryFilter::builder()
.conditions_operator(ConditionOperator::And)
.conditions(vec![filter.clone().into()])
.build(),
)
.build();
let response = count(request);
let result = match response.result {
Some(api_response::Result::Data(data)) => match CountAccountResponse::try_from(data) {
Ok(result) => result,
Err(e) => return Err(anyhow!(format!("Failed to parse response: {}", e))),
},
Some(api_response::Result::ErrorMessage(error)) => {
return Err(anyhow!(format!("API error: {}", error)));
}
None => {
return Err(anyhow!("No result found in response"));
}
};

count_all

Query the total number of accounts in the wallet.

use mystiko_protos::api::handler::v1::CountAccountResponse;
use mystiko_lib::account::count_all;
use mystiko_protos::api::v1::api_response;

let response = count_all();
let result = match response.result {
Some(api_response::Result::Data(data)) => match CountAccountResponse::try_from(data) {
Ok(result) => result,
Err(e) => return Err(anyhow!(format!("Failed to parse response: {}", e))),
},
Some(api_response::Result::ErrorMessage(error)) => {
return Err(anyhow!(format!("API error: {}", error)));
}
None => {
return Err(anyhow!("No result found in response"));
}
};

find

Query accounts based on specific conditions.

use mystiko_protos::storage::v1::{QueryFilter, SubFilter, ConditionOperator};
use mystiko_protos::api::handler::v1::{FindAccountRequest, FindAccountResponse};
use mystiko_lib::account::find;
use mystiko_protos::api::v1::api_response;

let filter = SubFilter::equal("name", "test_account");
let request= FindAccountRequest::builder()
.filter(
QueryFilter::builder()
.conditions_operator(ConditionOperator::And)
.conditions(vec![filter.clone().into()])
.build(),
)
.build();
let response = find(request);
let accounts = match response.result {
Some(api_response::Result::Data(data)) => match FindAccountResponse::try_from(data) {
Ok(accounts) => accounts,
Err(e) => return Err(anyhow!(format!("Failed to parse response: {}", e))),
},
Some(api_response::Result::ErrorMessage(error)) => {
return Err(anyhow!(format!("API error: {}", error)));
}
None => {
return Err(anyhow!("No result found in response"));
}
};

find_all

Query all accounts in the wallet.

use mystiko_protos::api::handler::v1::FindAccountResponse;
use mystiko_lib::account::find_all;
use mystiko_protos::api::v1::api_response;

let response = find_all();
let accounts = match response.result {
Some(api_response::Result::Data(data)) => match FindAccountResponse::try_from(data) {
Ok(accounts) => accounts,
Err(e) => return Err(anyhow!(format!("Failed to parse response: {}", e))),
},
Some(api_response::Result::ErrorMessage(error)) => {
return Err(anyhow!(format!("API error: {}", error)));
}
None => {
return Err(anyhow!("No result found in response"));
}
};

find_by_id

Query an account by its unique identifier.

use mystiko_protos::api::handler::v1::FindAccountByIdentifierResponse;
use mystiko_lib::account::find_by_id;
use mystiko_protos::api::v1::api_response;

let response = find_by_id("accountId".to_string());
let account = match response.result {
Some(api_response::Result::Data(data)) => match FindAccountByIdentifierResponse::try_from(data) {
Ok(account) => account,
Err(e) => return Err(anyhow!(format!("Failed to parse response: {}", e))),
},
Some(api_response::Result::ErrorMessage(error)) => {
return Err(anyhow!(format!("API error: {}", error)));
}
None => {
return Err(anyhow!("No result found in response"));
}
};

find_by_shielded_address

Query an account by its shielded address.

use mystiko_protos::api::handler::v1::FindAccountByIdentifierResponse;
use mystiko_lib::account::find_by_shielded_address;
use mystiko_protos::api::v1::api_response;

let response = find_by_shielded_address("shielded address".to_string());
let account = match response.result {
Some(api_response::Result::Data(data)) => match FindAccountByIdentifierResponse::try_from(data) {
Ok(account) => account,
Err(e) => return Err(anyhow!(format!("Failed to parse response: {}", e))),
},
Some(api_response::Result::ErrorMessage(error)) => {
return Err(anyhow!(format!("API error: {}", error)));
}
None => {
return Err(anyhow!("No result found in response"));
}
};

find_by_public_key

Query an account by its public key.

use mystiko_protos::api::handler::v1::FindAccountByIdentifierResponse;
use mystiko_lib::account::find_by_public_key;
use mystiko_protos::api::v1::api_response;

let response = find_by_public_key("public key".to_string());
let account = match response.result {
Some(api_response::Result::Data(data)) => match FindAccountByIdentifierResponse::try_from(data) {
Ok(account) => account,
Err(e) => return Err(anyhow!(format!("Failed to parse response: {}", e))),
},
Some(api_response::Result::ErrorMessage(error)) => {
return Err(anyhow!(format!("API error: {}", error)));
}
None => {
return Err(anyhow!("No result found in response"));
}
};

update

The parameter list for UpdateAccountOptions:

ParameterRequiredDescription
wallet_passwordtrue
The password of the encrypted wallet.
namefalse
The name of the account.

update_by_id

Update an account by its unique identifier.

use mystiko_protos::api::handler::v1::{UpdateAccountOptions, UpdateAccountRequest, UpdateAccountResponse};
use mystiko_lib::account::update_by_id;
use mystiko_protos::api::v1::api_response;

let request = UpdateAccountRequest::builder()
.identifier("accountId".to_string())
.options(
UpdateAccountOptions::builder()
.wallet_password("P@ssw0rd".to_string())
.name("new_name".to_string())
.build(),
)
.build();
let response = update_by_id(request);
let account = match response.result {
Some(api_response::Result::Data(data)) => match UpdateAccountResponse::try_from(data) {
Ok(account) => account,
Err(e) => return Err(anyhow!(format!("Failed to parse response: {}", e))),
},
Some(api_response::Result::ErrorMessage(error)) => {
return Err(anyhow!(format!("API error: {}", error)));
}
None => {
return Err(anyhow!("No result found in response"));
}
};

update_by_shielded_address

Update an account by its shielded address.

use mystiko_protos::api::handler::v1::{UpdateAccountOptions, UpdateAccountRequest, UpdateAccountResponse};
use mystiko_lib::account::update_by_shielded_address;
use mystiko_protos::api::v1::api_response;

let request = UpdateAccountRequest::builder()
.identifier("shielded address".to_string())
.options(
UpdateAccountOptions::builder()
.wallet_password("P@ssw0rd".to_string())
.name("new_name".to_string())
.build(),
)
.build();
let response = update_by_shielded_address(request);
let account = match response.result {
Some(api_response::Result::Data(data)) => match UpdateAccountResponse::try_from(data) {
Ok(account) => account,
Err(e) => return Err(anyhow!(format!("Failed to parse response: {}", e))),
},
Some(api_response::Result::ErrorMessage(error)) => {
return Err(anyhow!(format!("API error: {}", error)));
}
None => {
return Err(anyhow!("No result found in response"));
}
};

update_by_public_key

Update an account by its public key.

use mystiko_protos::api::handler::v1::{UpdateAccountOptions, UpdateAccountRequest, UpdateAccountResponse};
use mystiko_lib::account::update_by_public_key;
use mystiko_protos::api::v1::api_response;

let request = UpdateAccountRequest::builder()
.identifier("public key".to_string())
.options(
UpdateAccountOptions::builder()
.wallet_password("P@ssw0rd".to_string())
.name("new_name".to_string())
.build(),
)
.build();
let response = update_by_public_key(request);
let account = match response.result {
Some(api_response::Result::Data(data)) => match UpdateAccountResponse::try_from(data) {
Ok(account) => account,
Err(e) => return Err(anyhow!(format!("Failed to parse response: {}", e))),
},
Some(api_response::Result::ErrorMessage(error)) => {
return Err(anyhow!(format!("API error: {}", error)));
}
None => {
return Err(anyhow!("No result found in response"));
}
};

update_encryption

Update the encryption of an account.

use mystiko_protos::api::handler::v1::{UpdateEncryptionRequest, UpdateEncryptionResponse};
use mystiko_lib::account::update_encryption;
use mystiko_protos::api::v1::api_response;

let request = UpdateEncryptionRequest::builder()
.old_wallet_password("P@ssw0rd".to_string())
.new_wallet_password("new_P@ssw0rd".to_string())
.build();
let response = update_encryption(request);
let accounts = match response.result {
Some(api_response::Result::Data(data)) => match UpdateEncryptionResponse::try_from(data) {
Ok(accounts) => accounts,
Err(e) => return Err(anyhow!(format!("Failed to parse response: {}", e))),
},
Some(api_response::Result::ErrorMessage(error)) => {
return Err(anyhow!(format!("API error: {}", error)));
}
None => {
return Err(anyhow!("No result found in response"));
}
};

cxport

export_secret_key_by_id

Export the secret key of an account by its unique identifier.

import mystiko from '@mystikonetwork/node';

const key = mystiko.account?.exportSecretKeyById('walletPassword', "accountId");

use mystiko_protos::api::handler::v1::{ExportSecretKeyRequest, ExportSecretKeyResponse};
use mystiko_lib::account::export_secret_key_by_id;
use mystiko_protos::api::v1::api_response;

let request = ExportSecretKeyRequest::builder()
.wallet_password("P@ssw0rd".to_string())
.identifier("account id".to_string())
.build();
let response = export_secret_key_by_id(request);
let account = match response.result {
Some(api_response::Result::Data(data)) => match ExportSecretKeyResponse::try_from(data) {
Ok(account) => account,
Err(e) => return Err(anyhow!(format!("Failed to parse response: {}", e))),
},
Some(api_response::Result::ErrorMessage(error)) => {
return Err(anyhow!(format!("API error: {}", error)));
}
None => {
return Err(anyhow!("No result found in response"));
}
};

export_secret_key_by_public_key

Export the secret key of an account by its public key.

use mystiko_protos::api::handler::v1::{ExportSecretKeyRequest, ExportSecretKeyResponse};
use mystiko_lib::account::export_secret_key_by_public_key;
use mystiko_protos::api::v1::api_response;

let request = ExportSecretKeyRequest::builder()
.wallet_password("P@ssw0rd".to_string())
.identifier("public key".to_string())
.build();
let response = export_secret_key_by_public_key(request);
let account = match response.result {
Some(api_response::Result::Data(data)) => match ExportSecretKeyResponse::try_from(data) {
Ok(account) => account,
Err(e) => return Err(anyhow!(format!("Failed to parse response: {}", e))),
},
Some(api_response::Result::ErrorMessage(error)) => {
return Err(anyhow!(format!("API error: {}", error)));
}
None => {
return Err(anyhow!("No result found in response"));
}
};

export_secret_key_by_shielded_address

Export the secret key of an account by its shielded address.

use mystiko_protos::api::handler::v1::{ExportSecretKeyRequest, ExportSecretKeyResponse};
use mystiko_lib::account::export_secret_key_by_shielded_address;
use mystiko_protos::api::v1::api_response;

let request = ExportSecretKeyRequest::builder()
.wallet_password("P@ssw0rd".to_string())
.identifier("shieleded address".to_string())
.build();
let response = export_secret_key_by_shielded_address(request);
let account = match response.result {
Some(api_response::Result::Data(data)) => match ExportSecretKeyResponse::try_from(data) {
Ok(account) => account,
Err(e) => return Err(anyhow!(format!("Failed to parse response: {}", e))),
},
Some(api_response::Result::ErrorMessage(error)) => {
return Err(anyhow!(format!("API error: {}", error)));
}
None => {
return Err(anyhow!("No result found in response"));
}
};