fix; first refactor

This commit is contained in:
Oleksandr Pravdyvyi 2025-10-23 17:33:25 +03:00
parent d7089eac96
commit 4e36ae4679
No known key found for this signature in database
GPG Key ID: 9F8955C63C443871
16 changed files with 202 additions and 135 deletions

View File

@ -46,6 +46,7 @@ bip39 = "2.2.0"
hmac-sha512 = "1.1.7"
chrono = "0.4.41"
borsh = "1.5.7"
base58 = "0.2.0"
rocksdb = { version = "0.21.0", default-features = false, features = [
"snappy",

View File

@ -15,7 +15,8 @@ rs_merkle.workspace = true
sha2.workspace = true
log.workspace = true
elliptic-curve.workspace = true
hex.workspace = true
base58.workspace = true
hex = "0.4.3"
nssa-core = { path = "../nssa/core", features = ["host"] }
borsh.workspace = true

View File

@ -1,3 +1,4 @@
use base58::ToBase58;
use borsh::{BorshDeserialize, BorshSerialize};
use k256::ecdsa::{Signature, SigningKey, VerifyingKey};
use log::info;
@ -125,7 +126,7 @@ impl From<OwnedUTXO> for OwnedUTXOForPublication {
fn from(value: OwnedUTXO) -> Self {
Self {
hash: hex::encode(value.hash),
owner: hex::encode(value.owner),
owner: value.owner.to_base58(),
amount: value.amount,
}
}
@ -150,7 +151,7 @@ impl ActionData {
ActionData::MintMoneyPublicTx(action) => {
format!(
"Account {:?} minted {:?} balance",
hex::encode(action.acc),
action.acc.to_base58(),
action.amount
)
}
@ -160,14 +161,14 @@ impl ActionData {
action
.receiver_data
.into_iter()
.map(|(amount, rec)| (amount, hex::encode(rec)))
.map(|(amount, rec)| (amount, rec.to_base58()))
.collect::<Vec<_>>()
)
}
ActionData::SendMoneyShieldedTx(action) => {
format!(
"Shielded send from {:?} for {:?} balance",
hex::encode(action.acc_sender),
action.acc_sender.to_base58(),
action.amount
)
}

View File

@ -7,7 +7,7 @@ use nssa_core::{NullifierPublicKey, encryption::shared_key_derivation::Secp256k1
use wallet::{
Command, SubcommandReturnValue, WalletCore,
cli::{
account::{AccountSubcommand, FetchSubcommand, RegisterSubcommand},
account::{AccountSubcommand, FetchSubcommand, NewSubcommand},
native_token_transfer_program::{
NativeTokenTransferProgramSubcommand, NativeTokenTransferProgramSubcommandPrivate,
NativeTokenTransferProgramSubcommandShielded,
@ -40,7 +40,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
#[test_suite_fn]
pub async fn test_success() {
info!("test_success");
let command = Command::Transfer(NativeTokenTransferProgramSubcommand::Public {
let command = Command::AuthTransfer(NativeTokenTransferProgramSubcommand::Public {
from: ACC_SENDER.to_string(),
to: ACC_RECEIVER.to_string(),
amount: 100,
@ -77,7 +77,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
#[test_suite_fn]
pub async fn test_success_move_to_another_account() {
info!("test_success_move_to_another_account");
let command = Command::Account(AccountSubcommand::Register(RegisterSubcommand::Public {}));
let command = Command::Account(AccountSubcommand::New(NewSubcommand::Public {}));
let wallet_config = fetch_config().await.unwrap();
@ -101,7 +101,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
panic!("Failed to produce new account, not present in persistent accounts");
}
let command = Command::Transfer(NativeTokenTransferProgramSubcommand::Public {
let command = Command::AuthTransfer(NativeTokenTransferProgramSubcommand::Public {
from: ACC_SENDER.to_string(),
to: new_persistent_account_addr.clone(),
amount: 100,
@ -134,7 +134,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
#[test_suite_fn]
pub async fn test_failure() {
info!("test_failure");
let command = Command::Transfer(NativeTokenTransferProgramSubcommand::Public {
let command = Command::AuthTransfer(NativeTokenTransferProgramSubcommand::Public {
from: ACC_SENDER.to_string(),
to: ACC_RECEIVER.to_string(),
amount: 1000000,
@ -173,7 +173,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
#[test_suite_fn]
pub async fn test_success_two_transactions() {
info!("test_success_two_transactions");
let command = Command::Transfer(NativeTokenTransferProgramSubcommand::Public {
let command = Command::AuthTransfer(NativeTokenTransferProgramSubcommand::Public {
from: ACC_SENDER.to_string(),
to: ACC_RECEIVER.to_string(),
amount: 100,
@ -206,7 +206,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
info!("First TX Success!");
let command = Command::Transfer(NativeTokenTransferProgramSubcommand::Public {
let command = Command::AuthTransfer(NativeTokenTransferProgramSubcommand::Public {
from: ACC_SENDER.to_string(),
to: ACC_RECEIVER.to_string(),
amount: 100,
@ -264,20 +264,20 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
let wallet_config = fetch_config().await.unwrap();
// Create new account for the token definition
wallet::execute_subcommand(Command::Account(AccountSubcommand::Register(
RegisterSubcommand::Public {},
wallet::execute_subcommand(Command::Account(AccountSubcommand::New(
NewSubcommand::Public {},
)))
.await
.unwrap();
// Create new account for the token supply holder
wallet::execute_subcommand(Command::Account(AccountSubcommand::Register(
RegisterSubcommand::Public {},
wallet::execute_subcommand(Command::Account(AccountSubcommand::New(
NewSubcommand::Public {},
)))
.await
.unwrap();
// Create new account for receiving a token transaction
wallet::execute_subcommand(Command::Account(AccountSubcommand::Register(
RegisterSubcommand::Public {},
wallet::execute_subcommand(Command::Account(AccountSubcommand::New(
NewSubcommand::Public {},
)))
.await
.unwrap();
@ -311,7 +311,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
name: "A NAME".to_string(),
total_supply: 37,
});
wallet::execute_subcommand(Command::TokenProgram(subcommand))
wallet::execute_subcommand(Command::Token(subcommand))
.await
.unwrap();
info!("Waiting for next block creation");
@ -365,7 +365,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
recipient_addr: recipient_addr.to_string(),
balance_to_move: 7,
});
wallet::execute_subcommand(Command::TokenProgram(subcommand))
wallet::execute_subcommand(Command::Token(subcommand))
.await
.unwrap();
info!("Waiting for next block creation");
@ -416,8 +416,8 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
// Create new account for the token definition (public)
let SubcommandReturnValue::RegisterAccount {
addr: definition_addr,
} = wallet::execute_subcommand(Command::Account(AccountSubcommand::Register(
RegisterSubcommand::Public {},
} = wallet::execute_subcommand(Command::Account(AccountSubcommand::New(
NewSubcommand::Public {},
)))
.await
.unwrap()
@ -426,8 +426,8 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
};
// Create new account for the token supply holder (private)
let SubcommandReturnValue::RegisterAccount { addr: supply_addr } =
wallet::execute_subcommand(Command::Account(AccountSubcommand::Register(
RegisterSubcommand::Private {},
wallet::execute_subcommand(Command::Account(AccountSubcommand::New(
NewSubcommand::Private {},
)))
.await
.unwrap()
@ -437,8 +437,8 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
// Create new account for receiving a token transaction
let SubcommandReturnValue::RegisterAccount {
addr: recipient_addr,
} = wallet::execute_subcommand(Command::Account(AccountSubcommand::Register(
RegisterSubcommand::Private {},
} = wallet::execute_subcommand(Command::Account(AccountSubcommand::New(
NewSubcommand::Private {},
)))
.await
.unwrap()
@ -456,7 +456,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
},
);
wallet::execute_subcommand(Command::TokenProgram(subcommand))
wallet::execute_subcommand(Command::Token(subcommand))
.await
.unwrap();
@ -501,7 +501,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
},
);
wallet::execute_subcommand(Command::TokenProgram(subcommand))
wallet::execute_subcommand(Command::Token(subcommand))
.await
.unwrap();
@ -532,7 +532,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
},
);
wallet::execute_subcommand(Command::TokenProgram(subcommand))
wallet::execute_subcommand(Command::Token(subcommand))
.await
.unwrap();
@ -564,8 +564,8 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
// Create new account for the token definition (public)
let SubcommandReturnValue::RegisterAccount {
addr: definition_addr,
} = wallet::execute_subcommand(Command::Account(AccountSubcommand::Register(
RegisterSubcommand::Public {},
} = wallet::execute_subcommand(Command::Account(AccountSubcommand::New(
NewSubcommand::Public {},
)))
.await
.unwrap()
@ -574,8 +574,8 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
};
// Create new account for the token supply holder (private)
let SubcommandReturnValue::RegisterAccount { addr: supply_addr } =
wallet::execute_subcommand(Command::Account(AccountSubcommand::Register(
RegisterSubcommand::Private {},
wallet::execute_subcommand(Command::Account(AccountSubcommand::New(
NewSubcommand::Private {},
)))
.await
.unwrap()
@ -585,8 +585,8 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
// Create new account for receiving a token transaction
let SubcommandReturnValue::RegisterAccount {
addr: recipient_addr,
} = wallet::execute_subcommand(Command::Account(AccountSubcommand::Register(
RegisterSubcommand::Private {},
} = wallet::execute_subcommand(Command::Account(AccountSubcommand::New(
NewSubcommand::Private {},
)))
.await
.unwrap()
@ -604,7 +604,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
},
);
wallet::execute_subcommand(Command::TokenProgram(subcommand))
wallet::execute_subcommand(Command::Token(subcommand))
.await
.unwrap();
@ -657,7 +657,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
);
let SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash } =
wallet::execute_subcommand(Command::TokenProgram(subcommand))
wallet::execute_subcommand(Command::Token(subcommand))
.await
.unwrap()
else {
@ -700,8 +700,8 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
// Create new account for the token definition (public)
let SubcommandReturnValue::RegisterAccount {
addr: definition_addr,
} = wallet::execute_subcommand(Command::Account(AccountSubcommand::Register(
RegisterSubcommand::Public {},
} = wallet::execute_subcommand(Command::Account(AccountSubcommand::New(
NewSubcommand::Public {},
)))
.await
.unwrap()
@ -710,8 +710,8 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
};
// Create new account for the token supply holder (public)
let SubcommandReturnValue::RegisterAccount { addr: supply_addr } =
wallet::execute_subcommand(Command::Account(AccountSubcommand::Register(
RegisterSubcommand::Public {},
wallet::execute_subcommand(Command::Account(AccountSubcommand::New(
NewSubcommand::Public {},
)))
.await
.unwrap()
@ -721,8 +721,8 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
// Create new account for receiving a token transaction
let SubcommandReturnValue::RegisterAccount {
addr: recipient_addr,
} = wallet::execute_subcommand(Command::Account(AccountSubcommand::Register(
RegisterSubcommand::Private {},
} = wallet::execute_subcommand(Command::Account(AccountSubcommand::New(
NewSubcommand::Private {},
)))
.await
.unwrap()
@ -739,7 +739,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
total_supply: 37,
});
wallet::execute_subcommand(Command::TokenProgram(subcommand))
wallet::execute_subcommand(Command::Token(subcommand))
.await
.unwrap();
@ -774,7 +774,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
},
);
wallet::execute_subcommand(Command::TokenProgram(subcommand))
wallet::execute_subcommand(Command::Token(subcommand))
.await
.unwrap();
@ -800,7 +800,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
},
);
wallet::execute_subcommand(Command::TokenProgram(subcommand))
wallet::execute_subcommand(Command::Token(subcommand))
.await
.unwrap();
@ -827,8 +827,8 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
// Create new account for the token definition (public)
let SubcommandReturnValue::RegisterAccount {
addr: definition_addr,
} = wallet::execute_subcommand(Command::Account(AccountSubcommand::Register(
RegisterSubcommand::Public {},
} = wallet::execute_subcommand(Command::Account(AccountSubcommand::New(
NewSubcommand::Public {},
)))
.await
.unwrap()
@ -837,8 +837,8 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
};
// Create new account for the token supply holder (private)
let SubcommandReturnValue::RegisterAccount { addr: supply_addr } =
wallet::execute_subcommand(Command::Account(AccountSubcommand::Register(
RegisterSubcommand::Private {},
wallet::execute_subcommand(Command::Account(AccountSubcommand::New(
NewSubcommand::Private {},
)))
.await
.unwrap()
@ -848,8 +848,8 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
// Create new account for receiving a token transaction
let SubcommandReturnValue::RegisterAccount {
addr: recipient_addr,
} = wallet::execute_subcommand(Command::Account(AccountSubcommand::Register(
RegisterSubcommand::Public {},
} = wallet::execute_subcommand(Command::Account(AccountSubcommand::New(
NewSubcommand::Public {},
)))
.await
.unwrap()
@ -867,7 +867,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
},
);
wallet::execute_subcommand(Command::TokenProgram(subcommand))
wallet::execute_subcommand(Command::Token(subcommand))
.await
.unwrap();
@ -912,7 +912,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
},
);
wallet::execute_subcommand(Command::TokenProgram(subcommand))
wallet::execute_subcommand(Command::Token(subcommand))
.await
.unwrap();
@ -938,7 +938,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
},
);
wallet::execute_subcommand(Command::TokenProgram(subcommand))
wallet::execute_subcommand(Command::Token(subcommand))
.await
.unwrap();
@ -962,7 +962,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
let from: Address = ACC_SENDER_PRIVATE.parse().unwrap();
let to: Address = ACC_RECEIVER_PRIVATE.parse().unwrap();
let command = Command::Transfer(NativeTokenTransferProgramSubcommand::Private(
let command = Command::AuthTransfer(NativeTokenTransferProgramSubcommand::Private(
NativeTokenTransferProgramSubcommandPrivate::PrivateOwned {
from: from.to_string(),
to: to.to_string(),
@ -1000,7 +1000,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
let to_npk_string = hex::encode(to_npk.0);
let to_ipk = Secp256k1Point::from_scalar(to_npk.0);
let command = Command::Transfer(NativeTokenTransferProgramSubcommand::Private(
let command = Command::AuthTransfer(NativeTokenTransferProgramSubcommand::Private(
NativeTokenTransferProgramSubcommandPrivate::PrivateForeign {
from: from.to_string(),
to_npk: to_npk_string,
@ -1044,7 +1044,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
info!("test_success_private_transfer_to_another_owned_account_claiming_path");
let from: Address = ACC_SENDER_PRIVATE.parse().unwrap();
let command = Command::Account(AccountSubcommand::Register(RegisterSubcommand::Private {}));
let command = Command::Account(AccountSubcommand::New(NewSubcommand::Private {}));
let sub_ret = wallet::execute_subcommand(command).await.unwrap();
let SubcommandReturnValue::RegisterAccount { addr: to_addr } = sub_ret else {
@ -1065,7 +1065,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
.cloned()
.unwrap();
let command = Command::Transfer(NativeTokenTransferProgramSubcommand::Private(
let command = Command::AuthTransfer(NativeTokenTransferProgramSubcommand::Private(
NativeTokenTransferProgramSubcommandPrivate::PrivateForeign {
from: from.to_string(),
to_npk: hex::encode(to_keys.nullifer_public_key.0),
@ -1115,7 +1115,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
let from: Address = ACC_SENDER_PRIVATE.parse().unwrap();
let command = Command::Account(AccountSubcommand::Register(RegisterSubcommand::Private {}));
let command = Command::Account(AccountSubcommand::New(NewSubcommand::Private {}));
let sub_ret = wallet::execute_subcommand(command).await.unwrap();
let SubcommandReturnValue::RegisterAccount { addr: to_addr } = sub_ret else {
@ -1136,7 +1136,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
.cloned()
.unwrap();
let command = Command::Transfer(NativeTokenTransferProgramSubcommand::Private(
let command = Command::AuthTransfer(NativeTokenTransferProgramSubcommand::Private(
NativeTokenTransferProgramSubcommandPrivate::PrivateForeign {
from: from.to_string(),
to_npk: hex::encode(to_keys.nullifer_public_key.0),
@ -1184,7 +1184,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
info!("test_success_deshielded_transfer_to_another_account");
let from: Address = ACC_SENDER_PRIVATE.parse().unwrap();
let to: Address = ACC_RECEIVER.parse().unwrap();
let command = Command::Transfer(NativeTokenTransferProgramSubcommand::Deshielded {
let command = Command::AuthTransfer(NativeTokenTransferProgramSubcommand::Deshielded {
from: from.to_string(),
to: to.to_string(),
amount: 100,
@ -1230,7 +1230,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
info!("test_success_shielded_transfer_to_another_owned_account");
let from: Address = ACC_SENDER.parse().unwrap();
let to: Address = ACC_RECEIVER_PRIVATE.parse().unwrap();
let command = Command::Transfer(NativeTokenTransferProgramSubcommand::Shielded(
let command = Command::AuthTransfer(NativeTokenTransferProgramSubcommand::Shielded(
NativeTokenTransferProgramSubcommandShielded::ShieldedOwned {
from: from.to_string(),
to: to.to_string(),
@ -1274,7 +1274,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
let to_ipk = Secp256k1Point::from_scalar(to_npk.0);
let from: Address = ACC_SENDER.parse().unwrap();
let command = Command::Transfer(NativeTokenTransferProgramSubcommand::Shielded(
let command = Command::AuthTransfer(NativeTokenTransferProgramSubcommand::Shielded(
NativeTokenTransferProgramSubcommandShielded::ShieldedForeign {
from: from.to_string(),
to_npk: to_npk_string,
@ -1318,7 +1318,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
let pinata_addr = "cafe".repeat(16);
let pinata_prize = 150;
let solution = 989106;
let command = Command::PinataProgram(PinataProgramSubcommand::Public(
let command = Command::Pinata(PinataProgramSubcommand::Public(
PinataProgramSubcommandPublic::Claim {
pinata_addr: pinata_addr.clone(),
winner_addr: ACC_SENDER.to_string(),
@ -1446,7 +1446,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
let pinata_prize = 150;
let solution = 989106;
let command = Command::PinataProgram(PinataProgramSubcommand::Private(
let command = Command::Pinata(PinataProgramSubcommand::Private(
PinataProgramSubcommandPrivate::ClaimPrivateOwned {
pinata_addr: pinata_addr.clone(),
winner_addr: ACC_SENDER_PRIVATE.to_string(),
@ -1512,8 +1512,8 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
// Create new account for the token supply holder (private)
let SubcommandReturnValue::RegisterAccount { addr: winner_addr } =
wallet::execute_subcommand(Command::Account(AccountSubcommand::Register(
RegisterSubcommand::Private {},
wallet::execute_subcommand(Command::Account(AccountSubcommand::New(
NewSubcommand::Private {},
)))
.await
.unwrap()
@ -1521,7 +1521,7 @@ pub fn prepare_function_map() -> HashMap<String, TestFunction> {
panic!("invalid subcommand return value");
};
let command = Command::PinataProgram(PinataProgramSubcommand::Private(
let command = Command::Pinata(PinataProgramSubcommand::Private(
PinataProgramSubcommandPrivate::ClaimPrivateOwned {
pinata_addr: pinata_addr.clone(),
winner_addr: winner_addr.to_string(),

View File

@ -9,7 +9,8 @@ serde.workspace = true
k256.workspace = true
sha2.workspace = true
rand.workspace = true
hex.workspace = true
base58.workspace = true
hex = "0.4.3"
aes-gcm.workspace = true
bip39.workspace = true
hmac-sha512.workspace = true

View File

@ -55,6 +55,7 @@ impl KeyChain {
#[cfg(test)]
mod tests {
use aes_gcm::aead::OsRng;
use base58::ToBase58;
use k256::AffinePoint;
use k256::elliptic_curve::group::GroupEncoding;
use rand::RngCore;
@ -119,7 +120,7 @@ mod tests {
println!("======Public data======");
println!();
println!("Address{:?}", hex::encode(address.value()));
println!("Address{:?}", address.value().to_base58());
println!(
"Nulifier public key {:?}",
hex::encode(nullifer_public_key.to_byte_array())

View File

@ -10,8 +10,9 @@ thiserror = { version = "2.0.12", optional = true }
bytemuck = { version = "1.13", optional = true }
chacha20 = { version = "0.9", default-features = false }
k256 = { version = "0.13.3", optional = true }
hex = { version = "0.4.3", optional = true }
base58 = { version = "0.2.0", optional = true }
anyhow = { version = "1.0.98", optional = true }
[features]
default = []
host = ["thiserror", "bytemuck", "k256", "hex"]
host = ["thiserror", "bytemuck", "k256", "base58", "anyhow"]

View File

@ -3,6 +3,9 @@ use serde::{Deserialize, Serialize};
#[cfg(feature = "host")]
use std::{fmt::Display, str::FromStr};
#[cfg(feature = "host")]
use base58::{FromBase58, ToBase58};
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(
any(feature = "host", test),
@ -31,8 +34,8 @@ impl AsRef<[u8]> for Address {
#[cfg(feature = "host")]
#[derive(Debug, thiserror::Error)]
pub enum AddressError {
#[error("invalid hex")]
InvalidHex(#[from] hex::FromHexError),
#[error("invalid base58")]
InvalidBase58(#[from] anyhow::Error),
#[error("invalid length: expected 32 bytes, got {0}")]
InvalidLength(usize),
}
@ -41,7 +44,9 @@ pub enum AddressError {
impl FromStr for Address {
type Err = AddressError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let bytes = hex::decode(s)?;
let bytes = s
.from_base58()
.map_err(|err| anyhow::anyhow!("Invalid base58 err {err:?}"))?;
if bytes.len() != 32 {
return Err(AddressError::InvalidLength(bytes.len()));
}
@ -54,7 +59,7 @@ impl FromStr for Address {
#[cfg(feature = "host")]
impl Display for Address {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", hex::encode(self.value))
write!(f, "{}", self.value.to_base58())
}
}
@ -74,7 +79,7 @@ mod tests {
fn parse_invalid_hex() {
let hex_str = "zz".repeat(32); // invalid hex chars
let result = hex_str.parse::<Address>().unwrap_err();
assert!(matches!(result, AddressError::InvalidHex(_)));
assert!(matches!(result, AddressError::InvalidBase58(_)));
}
#[test]

View File

@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2024"
[dependencies]
hex.workspace = true
base58.workspace = true
anyhow.workspace = true
serde.workspace = true
rand.workspace = true

View File

@ -204,6 +204,7 @@ impl SequencerCore {
#[cfg(test)]
mod tests {
use base58::{FromBase58, ToBase58};
use common::test_utils::sequencer_sign_key_for_testing;
use crate::config::AccountInitialData;
@ -237,23 +238,23 @@ mod tests {
}
fn setup_sequencer_config() -> SequencerConfig {
let acc1_addr = vec![
let acc1_addr: Vec<u8> = vec![
208, 122, 210, 232, 75, 39, 250, 0, 194, 98, 240, 161, 238, 160, 255, 53, 202, 9, 115,
84, 126, 106, 16, 111, 114, 241, 147, 194, 220, 131, 139, 68,
];
let acc2_addr = vec![
let acc2_addr: Vec<u8> = vec![
231, 174, 119, 197, 239, 26, 5, 153, 147, 68, 175, 73, 159, 199, 138, 23, 5, 57, 141,
98, 237, 6, 207, 46, 20, 121, 246, 222, 248, 154, 57, 188,
];
let initial_acc1 = AccountInitialData {
addr: hex::encode(acc1_addr),
addr: acc1_addr.to_base58(),
balance: 10000,
};
let initial_acc2 = AccountInitialData {
addr: hex::encode(acc2_addr),
addr: acc2_addr.to_base58(),
balance: 20000,
};
@ -288,11 +289,17 @@ mod tests {
assert_eq!(sequencer.sequencer_config.max_num_tx_in_block, 10);
assert_eq!(sequencer.sequencer_config.port, 8080);
let acc1_addr = hex::decode(config.initial_accounts[0].addr.clone())
let acc1_addr = config.initial_accounts[0]
.addr
.clone()
.from_base58()
.unwrap()
.try_into()
.unwrap();
let acc2_addr = hex::decode(config.initial_accounts[1].addr.clone())
let acc2_addr = config.initial_accounts[1]
.addr
.clone()
.from_base58()
.unwrap()
.try_into()
.unwrap();
@ -314,23 +321,23 @@ mod tests {
#[test]
fn test_start_different_intial_accounts_balances() {
let acc1_addr = vec![
let acc1_addr: Vec<u8> = vec![
27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5, 101, 215, 30, 24,
52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143,
];
let acc2_addr = vec![
let acc2_addr: Vec<u8> = vec![
77, 75, 108, 209, 54, 16, 50, 202, 155, 210, 174, 185, 217, 0, 170, 77, 69, 217, 234,
216, 10, 201, 66, 51, 116, 196, 81, 167, 37, 77, 7, 102,
];
let initial_acc1 = AccountInitialData {
addr: hex::encode(acc1_addr),
addr: acc1_addr.to_base58(),
balance: 10000,
};
let initial_acc2 = AccountInitialData {
addr: hex::encode(acc2_addr),
addr: acc2_addr.to_base58(),
balance: 20000,
};
@ -339,11 +346,17 @@ mod tests {
let config = setup_sequencer_config_variable_initial_accounts(initial_accounts);
let sequencer = SequencerCore::start_from_config(config.clone());
let acc1_addr = hex::decode(config.initial_accounts[0].addr.clone())
let acc1_addr = config.initial_accounts[0]
.addr
.clone()
.from_base58()
.unwrap()
.try_into()
.unwrap();
let acc2_addr = hex::decode(config.initial_accounts[1].addr.clone())
let acc2_addr = config.initial_accounts[1]
.addr
.clone()
.from_base58()
.unwrap()
.try_into()
.unwrap();
@ -386,11 +399,17 @@ mod tests {
common_setup(&mut sequencer);
let acc1 = hex::decode(sequencer.sequencer_config.initial_accounts[0].addr.clone())
let acc1 = sequencer.sequencer_config.initial_accounts[0]
.addr
.clone()
.from_base58()
.unwrap()
.try_into()
.unwrap();
let acc2 = hex::decode(sequencer.sequencer_config.initial_accounts[1].addr.clone())
let acc2 = sequencer.sequencer_config.initial_accounts[1]
.addr
.clone()
.from_base58()
.unwrap()
.try_into()
.unwrap();
@ -412,11 +431,17 @@ mod tests {
common_setup(&mut sequencer);
let acc1 = hex::decode(sequencer.sequencer_config.initial_accounts[0].addr.clone())
let acc1 = sequencer.sequencer_config.initial_accounts[0]
.addr
.clone()
.from_base58()
.unwrap()
.try_into()
.unwrap();
let acc2 = hex::decode(sequencer.sequencer_config.initial_accounts[1].addr.clone())
let acc2 = sequencer.sequencer_config.initial_accounts[1]
.addr
.clone()
.from_base58()
.unwrap()
.try_into()
.unwrap();
@ -448,11 +473,17 @@ mod tests {
common_setup(&mut sequencer);
let acc1 = hex::decode(sequencer.sequencer_config.initial_accounts[0].addr.clone())
let acc1 = sequencer.sequencer_config.initial_accounts[0]
.addr
.clone()
.from_base58()
.unwrap()
.try_into()
.unwrap();
let acc2 = hex::decode(sequencer.sequencer_config.initial_accounts[1].addr.clone())
let acc2 = sequencer.sequencer_config.initial_accounts[1]
.addr
.clone()
.from_base58()
.unwrap()
.try_into()
.unwrap();
@ -484,11 +515,17 @@ mod tests {
common_setup(&mut sequencer);
let acc1 = hex::decode(sequencer.sequencer_config.initial_accounts[0].addr.clone())
let acc1 = sequencer.sequencer_config.initial_accounts[0]
.addr
.clone()
.from_base58()
.unwrap()
.try_into()
.unwrap();
let acc2 = hex::decode(sequencer.sequencer_config.initial_accounts[1].addr.clone())
let acc2 = sequencer.sequencer_config.initial_accounts[1]
.addr
.clone()
.from_base58()
.unwrap()
.try_into()
.unwrap();
@ -576,11 +613,17 @@ mod tests {
common_setup(&mut sequencer);
let acc1 = hex::decode(sequencer.sequencer_config.initial_accounts[0].addr.clone())
let acc1 = sequencer.sequencer_config.initial_accounts[0]
.addr
.clone()
.from_base58()
.unwrap()
.try_into()
.unwrap();
let acc2 = hex::decode(sequencer.sequencer_config.initial_accounts[1].addr.clone())
let acc2 = sequencer.sequencer_config.initial_accounts[1]
.addr
.clone()
.from_base58()
.unwrap()
.try_into()
.unwrap();
@ -618,11 +661,17 @@ mod tests {
common_setup(&mut sequencer);
let acc1 = hex::decode(sequencer.sequencer_config.initial_accounts[0].addr.clone())
let acc1 = sequencer.sequencer_config.initial_accounts[0]
.addr
.clone()
.from_base58()
.unwrap()
.try_into()
.unwrap();
let acc2 = hex::decode(sequencer.sequencer_config.initial_accounts[1].addr.clone())
let acc2 = sequencer.sequencer_config.initial_accounts[1]
.addr
.clone()
.from_base58()
.unwrap()
.try_into()
.unwrap();

View File

@ -10,7 +10,8 @@ log.workspace = true
serde.workspace = true
actix-cors.workspace = true
futures.workspace = true
hex.workspace = true
base58.workspace = true
hex = "0.4.3"
tempfile.workspace = true
base64.workspace = true

View File

@ -1,6 +1,7 @@
use std::collections::HashMap;
use actix_web::Error as HttpError;
use base58::FromBase58;
use base64::{Engine, engine::general_purpose};
use nssa::{self, program::Program};
use sequencer_core::config::AccountInitialData;
@ -163,8 +164,10 @@ impl JsonHandler {
/// The address must be a valid hex string of the correct length.
async fn process_get_account_balance(&self, request: Request) -> Result<Value, RpcErr> {
let get_account_req = GetAccountBalanceRequest::parse(Some(request.params))?;
let address_bytes = hex::decode(get_account_req.address)
.map_err(|_| RpcError::invalid_params("invalid hex".to_string()))?;
let address_bytes = get_account_req
.address
.from_base58()
.map_err(|_| RpcError::invalid_params("invalid base58".to_string()))?;
let address = nssa::Address::new(
address_bytes
.try_into()
@ -312,6 +315,7 @@ mod tests {
use std::sync::Arc;
use crate::{JsonHandler, rpc_handler};
use base58::ToBase58;
use base64::{Engine, engine::general_purpose};
use common::{
rpc_primitives::RpcPollingConfig, test_utils::sequencer_sign_key_for_testing,
@ -329,23 +333,23 @@ mod tests {
fn sequencer_config_for_tests() -> SequencerConfig {
let tempdir = tempdir().unwrap();
let home = tempdir.path().to_path_buf();
let acc1_addr = vec![
let acc1_addr: Vec<u8> = vec![
208, 122, 210, 232, 75, 39, 250, 0, 194, 98, 240, 161, 238, 160, 255, 53, 202, 9, 115,
84, 126, 106, 16, 111, 114, 241, 147, 194, 220, 131, 139, 68,
];
let acc2_addr = vec![
let acc2_addr: Vec<u8> = vec![
231, 174, 119, 197, 239, 26, 5, 153, 147, 68, 175, 73, 159, 199, 138, 23, 5, 57, 141,
98, 237, 6, 207, 46, 20, 121, 246, 222, 248, 154, 57, 188,
];
let initial_acc1 = AccountInitialData {
addr: hex::encode(acc1_addr),
addr: acc1_addr.to_base58(),
balance: 10000,
};
let initial_acc2 = AccountInitialData {
addr: hex::encode(acc2_addr),
addr: acc2_addr.to_base58(),
balance: 20000,
};

View File

@ -16,7 +16,8 @@ nssa-core = { path = "../nssa/core" }
base64.workspace = true
bytemuck = "1.23.2"
borsh.workspace = true
hex.workspace = true
base58.workspace = true
hex = "0.4.3"
rand.workspace = true
[dependencies.key_protocol]

View File

@ -1,6 +1,7 @@
use std::str::FromStr;
use anyhow::Result;
use base58::ToBase58;
use clap::Subcommand;
use common::transaction::NSSATransaction;
use nssa::Address;
@ -18,9 +19,9 @@ pub enum AccountSubcommand {
///Fetch
#[command(subcommand)]
Fetch(FetchSubcommand),
///Register
///New
#[command(subcommand)]
Register(RegisterSubcommand),
New(NewSubcommand),
}
///Represents generic getter CLI subcommand
@ -72,7 +73,7 @@ pub enum FetchSubcommand {
///Represents generic register CLI subcommand
#[derive(Subcommand, Debug, Clone)]
pub enum RegisterSubcommand {
pub enum NewSubcommand {
///Register new public account
Public {},
///Register new private account
@ -190,13 +191,13 @@ impl WalletSubcommand for FetchSubcommand {
}
}
impl WalletSubcommand for RegisterSubcommand {
impl WalletSubcommand for NewSubcommand {
async fn handle_subcommand(
self,
wallet_core: &mut WalletCore,
) -> Result<SubcommandReturnValue> {
match self {
RegisterSubcommand::Public {} => {
NewSubcommand::Public {} => {
let addr = wallet_core.create_new_account_public();
println!("Generated new account with addr {addr}");
@ -207,7 +208,7 @@ impl WalletSubcommand for RegisterSubcommand {
Ok(SubcommandReturnValue::RegisterAccount { addr })
}
RegisterSubcommand::Private {} => {
NewSubcommand::Private {} => {
let addr = wallet_core.create_new_account_private();
let (key, _) = wallet_core
@ -216,8 +217,8 @@ impl WalletSubcommand for RegisterSubcommand {
.get_private_account(&addr)
.unwrap();
println!("Generated new account with addr {addr}");
println!("With npk {}", hex::encode(&key.nullifer_public_key));
println!("Generated new account with addr {}", addr.to_bytes().to_base58());
println!("With npk {}", hex::encode(&key.nullifer_public_key.0));
println!(
"With ipk {}",
hex::encode(key.incoming_viewing_public_key.to_bytes())
@ -245,8 +246,8 @@ impl WalletSubcommand for AccountSubcommand {
AccountSubcommand::Fetch(fetch_subcommand) => {
fetch_subcommand.handle_subcommand(wallet_core).await
}
AccountSubcommand::Register(register_subcommand) => {
register_subcommand.handle_subcommand(wallet_core).await
AccountSubcommand::New(new_subcommand) => {
new_subcommand.handle_subcommand(wallet_core).await
}
}
}

View File

@ -6,12 +6,12 @@ use crate::{SubcommandReturnValue, WalletCore, cli::WalletSubcommand};
///Represents generic chain CLI subcommand
#[derive(Subcommand, Debug, Clone)]
pub enum ChainSubcommand {
GetLatestBlockId {},
GetBlockAtId {
CurrentBlockId {},
Block {
#[arg(short, long)]
id: u64,
},
GetTransactionAtHash {
Transaction {
#[arg(short, long)]
hash: String,
},
@ -23,17 +23,17 @@ impl WalletSubcommand for ChainSubcommand {
wallet_core: &mut WalletCore,
) -> Result<SubcommandReturnValue> {
match self {
ChainSubcommand::GetLatestBlockId {} => {
ChainSubcommand::CurrentBlockId {} => {
let latest_block_res = wallet_core.sequencer_client.get_last_block().await?;
println!("Last block id is {}", latest_block_res.last_block);
}
ChainSubcommand::GetBlockAtId { id } => {
ChainSubcommand::Block { id } => {
let block_res = wallet_core.sequencer_client.get_block(id).await?;
println!("Last block id is {:#?}", block_res.block);
}
ChainSubcommand::GetTransactionAtHash { hash } => {
ChainSubcommand::Transaction { hash } => {
let tx_res = wallet_core
.sequencer_client
.get_transaction_by_hash(hash)

View File

@ -193,19 +193,19 @@ impl WalletCore {
pub enum Command {
///Transfer command
#[command(subcommand)]
Transfer(NativeTokenTransferProgramSubcommand),
AuthTransfer(NativeTokenTransferProgramSubcommand),
///Chain command
#[command(subcommand)]
Chain(ChainSubcommand),
ChainInfo(ChainSubcommand),
///Chain command
#[command(subcommand)]
Account(AccountSubcommand),
///Pinata command
#[command(subcommand)]
PinataProgram(PinataProgramSubcommand),
Pinata(PinataProgramSubcommand),
///Token command
#[command(subcommand)]
TokenProgram(TokenProgramSubcommand),
Token(TokenProgramSubcommand),
AuthenticatedTransferInitializePublicAccount {},
// Check the wallet can connect to the node and builtin local programs
// match the remote versions
@ -237,12 +237,12 @@ pub async fn execute_subcommand(command: Command) -> Result<SubcommandReturnValu
let mut wallet_core = WalletCore::start_from_config_update_chain(wallet_config).await?;
let subcommand_ret = match command {
Command::Transfer(transfer_subcommand) => {
Command::AuthTransfer(transfer_subcommand) => {
transfer_subcommand
.handle_subcommand(&mut wallet_core)
.await?
}
Command::Chain(chain_subcommand) => {
Command::ChainInfo(chain_subcommand) => {
chain_subcommand.handle_subcommand(&mut wallet_core).await?
}
Command::Account(account_subcommand) => {
@ -250,7 +250,7 @@ pub async fn execute_subcommand(command: Command) -> Result<SubcommandReturnValu
.handle_subcommand(&mut wallet_core)
.await?
}
Command::PinataProgram(pinata_subcommand) => {
Command::Pinata(pinata_subcommand) => {
pinata_subcommand
.handle_subcommand(&mut wallet_core)
.await?
@ -304,7 +304,7 @@ pub async fn execute_subcommand(command: Command) -> Result<SubcommandReturnValu
SubcommandReturnValue::RegisterAccount { addr }
}
Command::TokenProgram(token_subcommand) => {
Command::Token(token_subcommand) => {
token_subcommand.handle_subcommand(&mut wallet_core).await?
}
};