diff --git a/examples/account_key_pooling.rs b/examples/account_key_pooling.rs index c3b0efe..8a0042f 100644 --- a/examples/account_key_pooling.rs +++ b/examples/account_key_pooling.rs @@ -3,8 +3,9 @@ /// /// This is an example of how to use account key pooling to send multiple transactions /// using different keys. -use near_api::prelude::*; +use near_api::*; use near_token::NearToken; +use signer::generate_secret_key; use std::sync::Arc; @@ -42,7 +43,7 @@ async fn main() { .unwrap(); let txs = (0..2).map(|_| { - Tokens::of(account.id().clone()) + Tokens::account(account.id().clone()) .send_to(second_account.id().clone()) .near(NearToken::from_near(1)) .with_signer(Arc::clone(&signer)) diff --git a/examples/create_account_and_send_near.rs b/examples/create_account_and_send_near.rs index 931ce8a..e7b94e0 100644 --- a/examples/create_account_and_send_near.rs +++ b/examples/create_account_and_send_near.rs @@ -1,7 +1,7 @@ -use near_account_id::AccountId; -use near_api::prelude::*; +use near_api::*; use near_token::NearToken; +use signer::generate_secret_key; #[tokio::main] async fn main() { @@ -9,7 +9,7 @@ async fn main() { let account = network.dev_create_account().await.unwrap(); let network = NetworkConfig::from(network); - let balance = Tokens::of(account.id().clone()) + let balance = Tokens::account(account.id().clone()) .near_balance() .fetch_from(&network) .await @@ -29,7 +29,7 @@ async fn main() { .await .unwrap(); - Tokens::of(account.id().clone()) + Tokens::account(account.id().clone()) .send_to(new_account.clone()) .near(NearToken::from_near(1)) .with_signer(signer) @@ -37,12 +37,12 @@ async fn main() { .await .unwrap(); - let new_acccount_balance = Tokens::of(account.id().clone()) + let new_acccount_balance = Tokens::account(account.id().clone()) .near_balance() .fetch_from(&network) .await .unwrap(); - let bob_balance = Tokens::of(new_account) + let bob_balance = Tokens::account(new_account) .near_balance() .fetch_from(&network) .await diff --git a/examples/deploy_and_call_method.rs b/examples/deploy_and_call_method.rs index 5951bb8..70c8086 100644 --- a/examples/deploy_and_call_method.rs +++ b/examples/deploy_and_call_method.rs @@ -1,4 +1,4 @@ -use near_api::{prelude::*, types::Data}; +use near_api::*; #[tokio::main] async fn main() { diff --git a/examples/ft.rs b/examples/ft.rs index e9d4d90..748d0e3 100644 --- a/examples/ft.rs +++ b/examples/ft.rs @@ -1,4 +1,4 @@ -use near_api::prelude::*; +use near_api::*; use serde_json::json; @@ -29,7 +29,7 @@ async fn main() { .unwrap(); // Verifying that user has 1000 tokens - let tokens = Tokens::of(token.id().clone()) + let tokens = Tokens::account(token.id().clone()) .ft_balance(token.id().clone()) .unwrap() .fetch_from(&network) @@ -40,7 +40,7 @@ async fn main() { // Transfer 100 tokens to the account // We handle internally the storage deposit for the receiver account - Tokens::of(token.id().clone()) + Tokens::account(token.id().clone()) .send_to(account.id().clone()) .ft( token.id().clone(), @@ -54,7 +54,7 @@ async fn main() { .unwrap() .assert_success(); - let tokens = Tokens::of(account.id().clone()) + let tokens = Tokens::account(account.id().clone()) .ft_balance(token.id().clone()) .unwrap() .fetch_from(&network) @@ -63,7 +63,7 @@ async fn main() { println!("Account has {}", tokens); - let tokens = Tokens::of(token.id().clone()) + let tokens = Tokens::account(token.id().clone()) .ft_balance(token.id().clone()) .unwrap() .fetch_from(&network) @@ -73,7 +73,7 @@ async fn main() { println!("Owner has {}", tokens); // We validate decimals at the network level so this should fail with a validation error - let token = Tokens::of(token.id().clone()) + let token = Tokens::account(token.id().clone()) .send_to(account.id().clone()) .ft( token.id().clone(), diff --git a/examples/nft.rs b/examples/nft.rs index 1bc69c2..4d65438 100644 --- a/examples/nft.rs +++ b/examples/nft.rs @@ -1,7 +1,6 @@ -use near_api::prelude::*; +use near_api::*; use near_contract_standards::non_fungible_token::metadata::TokenMetadata; -use near_token::NearToken; use serde_json::json; #[tokio::main] @@ -55,7 +54,7 @@ async fn main() { .unwrap(); // Verifying that account has our nft token - let tokens = Tokens::of(account.id().clone()) + let tokens = Tokens::account(account.id().clone()) .nft_assets(nft.id().clone()) .unwrap() .fetch_from(&network) @@ -65,7 +64,7 @@ async fn main() { assert_eq!(tokens.data.len(), 1); println!("Account has {}", tokens.data.first().unwrap().token_id); - Tokens::of(account.id().clone()) + Tokens::account(account.id().clone()) .send_to(nft.id().clone()) .nft(nft.id().clone(), "1".to_string()) .unwrap() @@ -75,7 +74,7 @@ async fn main() { .unwrap(); // Verifying that account doesn't have nft anymore - let tokens = Tokens::of(account.id().clone()) + let tokens = Tokens::account(account.id().clone()) .nft_assets(nft.id().clone()) .unwrap() .fetch_from(&network) @@ -84,7 +83,7 @@ async fn main() { assert!(tokens.data.is_empty()); - let tokens = Tokens::of(nft.id().clone()) + let tokens = Tokens::account(nft.id().clone()) .nft_assets(nft.id().clone()) .unwrap() .fetch_from(&network) diff --git a/examples/sign_options.rs b/examples/sign_options.rs index 6eac192..c4af55c 100644 --- a/examples/sign_options.rs +++ b/examples/sign_options.rs @@ -1,6 +1,7 @@ -use near_api::prelude::*; +use near_api::*; use near_crypto::SecretKey; use near_primitives::account::AccessKeyPermission; +use signer::generate_seed_phrase; #[tokio::main] async fn main() { diff --git a/examples/specify_backup_rpc.rs b/examples/specify_backup_rpc.rs index 1d0937c..06988f7 100644 --- a/examples/specify_backup_rpc.rs +++ b/examples/specify_backup_rpc.rs @@ -1,4 +1,4 @@ -use near_api::{prelude::*, types::reference::Reference}; +use near_api::*; #[tokio::main] async fn main() { diff --git a/examples/specifying_block.rs b/examples/specifying_block.rs index 156a86d..03d6130 100644 --- a/examples/specifying_block.rs +++ b/examples/specifying_block.rs @@ -1,4 +1,4 @@ -use near_api::{prelude::*, types::reference::Reference}; +use near_api::*; #[tokio::main] async fn main() { diff --git a/src/account/create.rs b/src/account/create.rs index cee89a3..5a7499d 100644 --- a/src/account/create.rs +++ b/src/account/create.rs @@ -11,9 +11,9 @@ use url::Url; use crate::{ common::send::Transactionable, errors::{AccountCreationError, FaucetError, ValidationError}, - prelude::*, transactions::{ConstructTransaction, TransactionWithSign}, types::transactions::PrepopulateTransaction, + Contract, NetworkConfig, }; #[derive(Clone, Debug)] diff --git a/src/common/mod.rs b/src/common/mod.rs index 6ea45ce..4a0ac70 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -3,7 +3,6 @@ use near_primitives::types::BlockHeight; const META_TRANSACTION_VALID_FOR_DEFAULT: BlockHeight = 1000; pub mod query; -pub mod secret; pub mod send; pub mod signed_delegate_action; pub mod utils; diff --git a/src/common/secret.rs b/src/common/secret.rs deleted file mode 100644 index 5643e63..0000000 --- a/src/common/secret.rs +++ /dev/null @@ -1,70 +0,0 @@ -use near_crypto::{PublicKey, SecretKey}; -use slipped10::BIP32Path; - -use crate::{errors::SecretError, signer::get_secret_key_from_seed}; - -const DEFAULT_HD_PATH: &str = "m/44'/397'/0'"; -const DEFAULT_WORD_COUNT: usize = 12; - -/// Generates a new seed phrase with optional customization -pub fn generate_seed_phrase_custom( - word_count: Option, - hd_path: Option, - passphrase: Option, -) -> Result<(String, PublicKey), SecretError> { - let mnemonic = bip39::Mnemonic::generate(word_count.unwrap_or(DEFAULT_WORD_COUNT))?; - let seed_phrase = mnemonic.words().collect::>().join(" "); - - let secret_key = get_secret_key_from_seed( - hd_path.unwrap_or_else(|| DEFAULT_HD_PATH.parse().expect("Valid HD path")), - seed_phrase.clone(), - passphrase, - )?; - - Ok((seed_phrase, secret_key.public_key())) -} - -/// Generates a new seed phrase with default settings (12 words, default HD path) -pub fn generate_seed_phrase() -> Result<(String, PublicKey), SecretError> { - generate_seed_phrase_custom(None, None, None) -} - -/// Generates a new seed phrase with a custom HD path -pub fn generate_seed_phrase_with_hd_path( - hd_path: BIP32Path, -) -> Result<(String, PublicKey), SecretError> { - generate_seed_phrase_custom(None, Some(hd_path), None) -} - -/// Generates a new seed phrase with a custom passphrase -pub fn generate_seed_phrase_with_passphrase( - passphrase: String, -) -> Result<(String, PublicKey), SecretError> { - generate_seed_phrase_custom(None, None, Some(passphrase)) -} - -/// Generates a new seed phrase with a custom word count -pub fn generate_seed_phrase_with_word_count( - word_count: usize, -) -> Result<(String, PublicKey), SecretError> { - generate_seed_phrase_custom(Some(word_count), None, None) -} - -/// Generates a secret key from a new seed phrase using default settings -pub fn generate_secret_key() -> Result { - let (seed_phrase, _) = generate_seed_phrase()?; - let secret_key = get_secret_key_from_seed( - DEFAULT_HD_PATH.parse().expect("Valid HD path"), - seed_phrase, - None, - )?; - Ok(secret_key) -} - -pub fn generate_secret_key_from_seed_phrase(seed_phrase: String) -> Result { - get_secret_key_from_seed( - DEFAULT_HD_PATH.parse().expect("Valid HD path"), - seed_phrase, - None, - ) -} diff --git a/src/lib.rs b/src/lib.rs index 13d2983..eaeecb1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,35 +6,33 @@ mod stake; mod storage; mod tokens; mod transactions; +// TODO: to be honest, there is almost nothing in this file +// we should maybe intergrate with them more tightly +// for now, i comment it out +// mod fastnear; mod common; -mod fastnear; pub mod errors; pub mod signer; pub mod types; -pub mod prelude { - pub use crate::{ - account::Account, - chain::Chain, - common::secret::*, - config::{retry, NetworkConfig, RPCEndpoint, RetryResponse}, - contract::Contract, - fastnear::FastNear, - signer::{Signer, SignerTrait}, - stake::Delegation, - stake::Staking, - storage::StorageDeposit, - tokens::Tokens, - transactions::Transaction, - types::{ - reference::{EpochReference, Reference}, - tokens::{FTBalance, USDT_BALANCE, W_NEAR_BALANCE}, - Data, - }, - }; +pub use crate::{ + account::Account, + chain::Chain, + config::{NetworkConfig, RPCEndpoint}, + contract::Contract, + signer::{Signer, SignerTrait}, + stake::Staking, + storage::StorageDeposit, + tokens::Tokens, + transactions::Transaction, + types::{ + reference::{EpochReference, Reference}, + tokens::{FTBalance, USDT_BALANCE, W_NEAR_BALANCE}, + Data, + }, +}; - pub use near_account_id::AccountId; - pub use near_token::NearToken; -} +pub use near_account_id::AccountId; +pub use near_token::NearToken; diff --git a/src/signer/mod.rs b/src/signer/mod.rs index 0d88f86..0154cd9 100644 --- a/src/signer/mod.rs +++ b/src/signer/mod.rs @@ -34,6 +34,8 @@ pub mod ledger; pub mod secret_key; const SIGNER_TARGET: &str = "near_api::signer"; +const DEFAULT_HD_PATH: &str = "m/44'/397'/0'"; +const DEFAULT_WORD_COUNT: usize = 12; #[derive(Debug, Deserialize, Clone)] pub struct AccountKeyPair { @@ -329,3 +331,66 @@ pub fn get_secret_key_from_seed( Ok(SecretKey::ED25519(secret_key)) } + +/// Generates a new seed phrase with optional customization +pub fn generate_seed_phrase_custom( + word_count: Option, + hd_path: Option, + passphrase: Option, +) -> Result<(String, PublicKey), SecretError> { + let mnemonic = bip39::Mnemonic::generate(word_count.unwrap_or(DEFAULT_WORD_COUNT))?; + let seed_phrase = mnemonic.words().collect::>().join(" "); + + let secret_key = get_secret_key_from_seed( + hd_path.unwrap_or_else(|| DEFAULT_HD_PATH.parse().expect("Valid HD path")), + seed_phrase.clone(), + passphrase, + )?; + + Ok((seed_phrase, secret_key.public_key())) +} + +/// Generates a new seed phrase with default settings (12 words, default HD path) +pub fn generate_seed_phrase() -> Result<(String, PublicKey), SecretError> { + generate_seed_phrase_custom(None, None, None) +} + +/// Generates a new seed phrase with a custom HD path +pub fn generate_seed_phrase_with_hd_path( + hd_path: BIP32Path, +) -> Result<(String, PublicKey), SecretError> { + generate_seed_phrase_custom(None, Some(hd_path), None) +} + +/// Generates a new seed phrase with a custom passphrase +pub fn generate_seed_phrase_with_passphrase( + passphrase: String, +) -> Result<(String, PublicKey), SecretError> { + generate_seed_phrase_custom(None, None, Some(passphrase)) +} + +/// Generates a new seed phrase with a custom word count +pub fn generate_seed_phrase_with_word_count( + word_count: usize, +) -> Result<(String, PublicKey), SecretError> { + generate_seed_phrase_custom(Some(word_count), None, None) +} + +/// Generates a secret key from a new seed phrase using default settings +pub fn generate_secret_key() -> Result { + let (seed_phrase, _) = generate_seed_phrase()?; + let secret_key = get_secret_key_from_seed( + DEFAULT_HD_PATH.parse().expect("Valid HD path"), + seed_phrase, + None, + )?; + Ok(secret_key) +} + +pub fn generate_secret_key_from_seed_phrase(seed_phrase: String) -> Result { + get_secret_key_from_seed( + DEFAULT_HD_PATH.parse().expect("Valid HD path"), + seed_phrase, + None, + ) +} diff --git a/src/tokens.rs b/src/tokens.rs index f02953f..41743b4 100644 --- a/src/tokens.rs +++ b/src/tokens.rs @@ -23,13 +23,13 @@ use crate::{ }, contract::Contract, errors::{BuilderError, FTValidatorError, ValidationError}, - prelude::*, transactions::{ConstructTransaction, TransactionWithSign}, types::{ tokens::{FTBalance, UserBalance}, transactions::PrepopulateTransaction, Data, }, + NetworkConfig, StorageDeposit, }; type Result = core::result::Result; @@ -40,7 +40,7 @@ pub struct Tokens { } impl Tokens { - pub const fn of(account_id: AccountId) -> Self { + pub const fn account(account_id: AccountId) -> Self { Self { account_id } } diff --git a/tests/account.rs b/tests/account.rs index a6804c3..9cf1ff5 100644 --- a/tests/account.rs +++ b/tests/account.rs @@ -1,7 +1,6 @@ -use near_account_id::AccountId; -use near_api::prelude::*; +use near_api::*; use near_primitives::{account::AccessKeyPermission, views::AccessKeyPermissionView}; -use near_token::NearToken; +use signer::generate_secret_key; #[tokio::test] async fn create_and_delete_account() { @@ -21,7 +20,7 @@ async fn create_and_delete_account() { .unwrap() .assert_success(); - let balance_before_del = Tokens::of(new_account.clone()) + let balance_before_del = Tokens::account(new_account.clone()) .near_balance() .fetch_from(&network) .await @@ -37,7 +36,7 @@ async fn create_and_delete_account() { .unwrap()) .assert_success(); - Tokens::of(account.id().clone()) + Tokens::account(account.id().clone()) .near_balance() .fetch_from(&network) .await @@ -46,7 +45,7 @@ async fn create_and_delete_account() { // TODO: why do we need a sleep to wait for beneficiary transfer? tokio::time::sleep(std::time::Duration::from_secs(1)).await; - let balance_after_del = Tokens::of(new_account.clone()) + let balance_after_del = Tokens::account(new_account.clone()) .near_balance() .fetch_from(&network) .await @@ -61,7 +60,7 @@ async fn transfer_funds() { let bob = network.dev_create_account().await.unwrap(); let network: NetworkConfig = NetworkConfig::from(network); - Tokens::of(alice.id().clone()) + Tokens::account(alice.id().clone()) .send_to(bob.id().clone()) .near(NearToken::from_near(50)) .with_signer(Signer::new(Signer::from_workspace(&alice)).unwrap()) @@ -70,13 +69,13 @@ async fn transfer_funds() { .unwrap() .assert_success(); - let alice_balance = Tokens::of(alice.id().clone()) + let alice_balance = Tokens::account(alice.id().clone()) .near_balance() .fetch_from(&network) .await .unwrap(); - let bob_balance = Tokens::of(bob.id().clone()) + let bob_balance = Tokens::account(bob.id().clone()) .near_balance() .fetch_from(&network) .await diff --git a/tests/contract.rs b/tests/contract.rs index c434017..c470e06 100644 --- a/tests/contract.rs +++ b/tests/contract.rs @@ -1,4 +1,4 @@ -use near_api::prelude::*; +use near_api::*; use serde_json::json; diff --git a/tests/multiple_tx_at_same_time_from_same-_user.rs b/tests/multiple_tx_at_same_time_from_same-_user.rs index 10e02f4..4b8eef4 100644 --- a/tests/multiple_tx_at_same_time_from_same-_user.rs +++ b/tests/multiple_tx_at_same_time_from_same-_user.rs @@ -1,11 +1,11 @@ use std::{collections::HashMap, sync::Arc}; use futures::future::join_all; -use near_api::prelude::*; +use near_api::*; use near_crypto::PublicKey; use near_primitives::account::AccessKeyPermission; -use near_token::NearToken; +use signer::generate_secret_key; #[tokio::test] async fn multiple_tx_at_same_time_from_same_key() { @@ -24,7 +24,7 @@ async fn multiple_tx_at_same_time_from_same_key() { .nonce; let tx = (0..100).map(|i| { - Tokens::of(account.id().clone()) + Tokens::account(account.id().clone()) .send_to(tmp_account.id().clone()) .near(NearToken::from_millinear(i)) }); @@ -85,7 +85,7 @@ async fn multiple_tx_at_same_time_from_different_keys() { .unwrap(); let tx = (0..12).map(|i| { - Tokens::of(account.id().clone()) + Tokens::account(account.id().clone()) .send_to(tmp_account.id().clone()) .near(NearToken::from_millinear(i)) });