Skip to content

Commit 427a57c

Browse files
authored
Merge pull request #4445 from anoma/murisi/generate-whitelisted-addresses
Adjusted test vector generator to also generate tokens in whitelist.
2 parents 5c15d2a + 619f79c commit 427a57c

File tree

8 files changed

+121
-111
lines changed

8 files changed

+121
-111
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Generate test vectors that use whitelisted tokens for the hardware wallet.
2+
([\#4445](https://github.com/anoma/namada/pull/4445))

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ env:
3535
AWS_REGION: us-west-2
3636
NIGHTLY: nightly-2024-09-08
3737
NAMADA_MASP_PARAMS_DIR: /masp/.masp-params
38-
LEDGER_APP_VERSION: "3.0.1"
38+
LEDGER_APP_VERSION: "3.0.4"
3939
ROLE: arn:aws:iam::375643557360:role/github-runners-ci-shared
4040

4141
jobs:

crates/core/src/address.rs

+37
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ pub mod testing {
811811
prop_oneof![
812812
arb_established_address().prop_map(Address::Established),
813813
arb_implicit_address().prop_map(Address::Implicit),
814+
arb_whitelisted_address(),
814815
]
815816
}
816817

@@ -820,9 +821,45 @@ pub mod testing {
820821
arb_established_address().prop_map(Address::Established),
821822
arb_implicit_address().prop_map(Address::Implicit),
822823
arb_internal_address().prop_map(Address::Internal),
824+
arb_whitelisted_address(),
823825
]
824826
}
825827

828+
/// A list of tokens whitelisted on hardware wallets
829+
pub fn address_whitelist() -> HashMap<Address, &'static str> {
830+
const EXPECT: &str = "The token address decoding shouldn't fail";
831+
let nam =
832+
Address::decode("tnam1q9gr66cvu4hrzm0sd5kmlnjje82gs3xlfg3v6nu7");
833+
let osmo =
834+
Address::decode("tnam1p5z8ruwyu7ha8urhq2l0dhpk2f5dv3ts7uyf2n75");
835+
let atom =
836+
Address::decode("tnam1pkg30gnt4q0zn7j00r6hms4ajrxn6f5ysyyl7w9m");
837+
let tia =
838+
Address::decode("tnam1pklj3kwp0cpsdvv56584rsajty974527qsp8n0nm");
839+
let st_osmo =
840+
Address::decode("tnam1p4px8sw3am4qvetj7eu77gftm4fz4hcw2ulpldc7");
841+
let st_atom =
842+
Address::decode("tnam1p5z5538v3kdk3wdx7r2hpqm4uq9926dz3ughcp7n");
843+
let st_tia =
844+
Address::decode("tnam1ph6xhf0defk65hm7l5ursscwqdj8ehrcdv300u4g");
845+
let mut whitelist = HashMap::new();
846+
whitelist.insert(nam.expect(EXPECT), "NAM");
847+
whitelist.insert(osmo.expect(EXPECT), "uOSMO");
848+
whitelist.insert(atom.expect(EXPECT), "uATOM");
849+
whitelist.insert(tia.expect(EXPECT), "uTIA");
850+
whitelist.insert(st_osmo.expect(EXPECT), "ustOSMO");
851+
whitelist.insert(st_atom.expect(EXPECT), "ustATOM");
852+
whitelist.insert(st_tia.expect(EXPECT), "ustTIA");
853+
whitelist
854+
}
855+
856+
/// Generate an arbitrary whitelisted address
857+
pub fn arb_whitelisted_address() -> impl Strategy<Value = Address> {
858+
any::<prop::sample::Selector>().prop_map(|selector| {
859+
selector.select(address_whitelist().keys().cloned())
860+
})
861+
}
862+
826863
/// Generate an arbitrary [`EstablishedAddress`].
827864
pub fn arb_established_address() -> impl Strategy<Value = EstablishedAddress>
828865
{

crates/sdk/src/lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -857,9 +857,7 @@ pub mod testing {
857857
use governance::ProposalType;
858858
use masp_primitives::transaction::components::sapling::builder::StoredBuildParams;
859859
use namada_account::{InitAccount, UpdateAccount};
860-
use namada_core::address::testing::{
861-
arb_established_address, arb_non_internal_address,
862-
};
860+
use namada_core::address::testing::arb_non_internal_address;
863861
use namada_core::collections::{HashMap, HashSet};
864862
use namada_core::eth_bridge_pool::PendingTransfer;
865863
use namada_core::hash::testing::arb_hash;
@@ -1020,7 +1018,7 @@ pub mod testing {
10201018
/// Generate an arbitrary fee
10211019
pub fn arb_fee()(
10221020
amount_per_gas_unit in arb_denominated_amount(),
1023-
token in arb_established_address().prop_map(Address::Established),
1021+
token in arb_non_internal_address(),
10241022
) -> Fee {
10251023
Fee {
10261024
amount_per_gas_unit,

crates/sdk/src/signing.rs

+40-72
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ use crate::error::{EncodingError, Error, TxSubmitError};
5050
use crate::eth_bridge_pool::PendingTransfer;
5151
use crate::governance::storage::proposal::{AddRemove, PGFAction, PGFTarget};
5252
use crate::rpc::validate_amount;
53-
use crate::token::Account;
5453
use crate::tx::{
5554
Commitment, TX_BECOME_VALIDATOR_WASM, TX_BOND_WASM, TX_BRIDGE_POOL_WASM,
5655
TX_CHANGE_COMMISSION_WASM, TX_CHANGE_CONSENSUS_KEY_WASM,
@@ -707,7 +706,7 @@ pub struct LedgerVector {
707706

708707
/// Adds a Ledger output line describing a given transaction amount and address
709708
fn make_ledger_amount_addr(
710-
tokens: &HashMap<Address, String>,
709+
tokens: &HashMap<Address, &str>,
711710
output: &mut Vec<String>,
712711
amount: DenominatedAmount,
713712
token: &Address,
@@ -717,7 +716,7 @@ fn make_ledger_amount_addr(
717716
output.push(format!(
718717
"{}Amount : {} {}",
719718
prefix,
720-
token.to_uppercase(),
719+
token,
721720
to_ledger_decimal_variable_token(amount),
722721
));
723722
} else {
@@ -735,7 +734,7 @@ fn make_ledger_amount_addr(
735734
/// Adds a Ledger output line describing a given transaction amount and asset
736735
/// type
737736
async fn make_ledger_amount_asset(
738-
tokens: &HashMap<Address, String>,
737+
tokens: &HashMap<Address, &str>,
739738
output: &mut Vec<String>,
740739
amount: u64,
741740
token: &AssetType,
@@ -752,7 +751,7 @@ async fn make_ledger_amount_asset(
752751
output.push(format!(
753752
"{}Amount : {} {}",
754753
prefix,
755-
token.to_uppercase(),
754+
token,
756755
to_ledger_decimal_variable_token(amount),
757756
));
758757
} else {
@@ -822,37 +821,26 @@ fn format_outputs(output: &mut Vec<String>) {
822821
}
823822
}
824823

825-
/// Convert a map with key pairs into a nested structure
826-
fn nest_map<V>(
827-
map: BTreeMap<Account, V>,
828-
) -> BTreeMap<Address, BTreeMap<Address, V>> {
829-
let mut nested = BTreeMap::new();
830-
for (account, v) in map {
831-
let inner: &mut BTreeMap<_, _> =
832-
nested.entry(account.owner).or_default();
833-
inner.insert(account.token, v);
834-
}
835-
nested
836-
}
837-
838824
/// Adds a Ledger output for the senders and destinations for transparent and
839825
/// MASP transactions
840826
async fn make_ledger_token_transfer_endpoints(
841-
tokens: &HashMap<Address, String>,
827+
tokens: &HashMap<Address, &str>,
842828
output: &mut Vec<String>,
843829
transfer: &token::Transfer,
844830
builder: Option<&MaspBuilder>,
845831
assets: &HashMap<AssetType, AssetData>,
846832
) -> Result<(), Error> {
847-
for (owner, changes) in nest_map(transfer.sources.clone()) {
833+
for (owner, amount) in transfer.sources.clone() {
848834
// MASP inputs will be printed below
849-
if owner != MASP {
850-
output.push(format!("Sender : {}", owner));
851-
for (token, amount) in changes {
852-
make_ledger_amount_addr(
853-
tokens, output, amount, &token, "Sending ",
854-
);
855-
}
835+
if owner.owner != MASP {
836+
output.push(format!("Sender : {}", owner.owner));
837+
make_ledger_amount_addr(
838+
tokens,
839+
output,
840+
amount,
841+
&owner.token,
842+
"Sending ",
843+
);
856844
}
857845
}
858846
if let Some(builder) = builder {
@@ -870,19 +858,17 @@ async fn make_ledger_token_transfer_endpoints(
870858
.await;
871859
}
872860
}
873-
for (owner, changes) in nest_map(transfer.targets.clone()) {
861+
for (owner, amount) in transfer.targets.clone() {
874862
// MASP outputs will be printed below
875-
if owner != MASP {
876-
output.push(format!("Destination : {}", owner));
877-
for (token, amount) in changes {
878-
make_ledger_amount_addr(
879-
tokens,
880-
output,
881-
amount,
882-
&token,
883-
"Receiving ",
884-
);
885-
}
863+
if owner.owner != MASP {
864+
output.push(format!("Destination : {}", owner.owner));
865+
make_ledger_amount_addr(
866+
tokens,
867+
output,
868+
amount,
869+
&owner.token,
870+
"Receiving ",
871+
);
886872
}
887873
}
888874
if let Some(builder) = builder {
@@ -1156,16 +1142,9 @@ fn format_timeout_height(height: &TimeoutHeight) -> String {
11561142
/// Converts the given transaction to the form that is displayed on the Ledger
11571143
/// device
11581144
pub async fn to_ledger_vector(
1159-
wallet: &Wallet<impl WalletIo>,
1145+
tokens: &HashMap<Address, &str>,
11601146
tx: &Tx,
11611147
) -> Result<LedgerVector, Error> {
1162-
// To facilitate lookups of human-readable token names
1163-
let tokens: HashMap<Address, String> = wallet
1164-
.get_addresses()
1165-
.into_iter()
1166-
.map(|(alias, addr)| (addr, alias))
1167-
.collect();
1168-
11691148
let mut tv = LedgerVector {
11701149
blob: HEXLOWER.encode(&tx.serialize_to_vec()),
11711150
index: 0,
@@ -1498,15 +1477,15 @@ pub async fn to_ledger_vector(
14981477
)
14991478
.map_err(|_| Error::Other("Invalid Data".to_string()))?;
15001479
make_ledger_token_transfer_endpoints(
1501-
&tokens,
1480+
tokens,
15021481
&mut tv.output,
15031482
&transfer,
15041483
builder,
15051484
&asset_types,
15061485
)
15071486
.await?;
15081487
make_ledger_token_transfer_endpoints(
1509-
&tokens,
1488+
tokens,
15101489
&mut tv.output_expert,
15111490
&transfer,
15121491
builder,
@@ -1596,15 +1575,15 @@ pub async fn to_ledger_vector(
15961575
)
15971576
.map_err(|_| Error::Other("Invalid Data".to_string()))?;
15981577
make_ledger_token_transfer_endpoints(
1599-
&tokens,
1578+
tokens,
16001579
&mut tv.output,
16011580
&transfer,
16021581
builder,
16031582
&asset_types,
16041583
)
16051584
.await?;
16061585
make_ledger_token_transfer_endpoints(
1607-
&tokens,
1586+
tokens,
16081587
&mut tv.output_expert,
16091588
&transfer,
16101589
builder,
@@ -1765,15 +1744,15 @@ pub async fn to_ledger_vector(
17651744
)
17661745
.map_err(|_| Error::Other("Invalid Data".to_string()))?;
17671746
make_ledger_token_transfer_endpoints(
1768-
&tokens,
1747+
tokens,
17691748
&mut tv.output,
17701749
&transfer,
17711750
builder,
17721751
&asset_types,
17731752
)
17741753
.await?;
17751754
make_ledger_token_transfer_endpoints(
1776-
&tokens,
1755+
tokens,
17771756
&mut tv.output_expert,
17781757
&transfer,
17791758
builder,
@@ -2218,15 +2197,10 @@ pub async fn to_ledger_vector(
22182197
format!("Gas limit : {}", u64::from(wrapper.gas_limit)),
22192198
]);
22202199
if let Some(token) = tokens.get(&wrapper.fee.token) {
2221-
tv.output.push(format!(
2222-
"Fee : {} {}",
2223-
token.to_uppercase(),
2224-
fee_limit
2225-
));
2200+
tv.output.push(format!("Fee : {} {}", token, fee_limit));
22262201
tv.output_expert.push(format!(
22272202
"Fees/gas unit : {} {}",
2228-
token.to_uppercase(),
2229-
fee_amount_per_gas_unit,
2203+
token, fee_amount_per_gas_unit,
22302204
));
22312205
} else {
22322206
tv.output.extend(vec![
@@ -2274,6 +2248,7 @@ mod test_signing {
22742248
use crate::args::InputAmount;
22752249
use crate::masp::fs::FsShieldedUtils;
22762250
use crate::masp::{ShieldedContext, WalletMap};
2251+
use crate::token::Account;
22772252

22782253
fn arbitrary_args() -> args::Tx {
22792254
args::Tx {
@@ -2688,14 +2663,8 @@ mod test_signing {
26882663
shielded_section_hash: None,
26892664
};
26902665
let tokens = HashMap::from([
2691-
(
2692-
Address::Internal(InternalAddress::Governance),
2693-
"SuperMoney".to_string(),
2694-
),
2695-
(
2696-
Address::Internal(InternalAddress::Pgf),
2697-
"BloodMoney".to_string(),
2698-
),
2666+
(Address::Internal(InternalAddress::Governance), "SuperMoney"),
2667+
(Address::Internal(InternalAddress::Pgf), "BloodMoney"),
26992668
]);
27002669

27012670
let mut output = vec![];
@@ -2714,12 +2683,12 @@ mod test_signing {
27142683
"Sender : {}",
27152684
Address::Internal(InternalAddress::Governance)
27162685
),
2717-
"Sending Amount : SUPERMONEY 1".to_string(),
2686+
"Sending Amount : SuperMoney 1".to_string(),
27182687
format!(
27192688
"Destination : {}",
27202689
Address::Internal(InternalAddress::Pgf)
27212690
),
2722-
"Receiving Amount : BLOODMONEY 2".to_string(),
2691+
"Receiving Amount : BloodMoney 2".to_string(),
27232692
];
27242693
assert_eq!(output, expected);
27252694
output.clear();
@@ -2761,8 +2730,7 @@ mod test_signing {
27612730
/// extracts and validates the presence of a code section
27622731
#[tokio::test]
27632732
async fn test_to_ledger_vector_code_sections() {
2764-
let wallet =
2765-
Wallet::<TestWalletUtils>::new(TestWalletUtils, Default::default());
2733+
let wallet = HashMap::new();
27662734
let mut tx = Tx::new(ChainId::default(), None);
27672735
// an empty tx should work correctly
27682736
to_ledger_vector(&wallet, &tx).await.expect("Test failed");

crates/token/src/lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,7 @@ pub mod testing {
338338
use masp_primitives::transaction::components::{TxOut, U64Sum};
339339
use masp_primitives::transaction::fees::fixed::FeeRule;
340340
use masp_primitives::zip32::PseudoExtendedKey;
341-
use namada_core::address::testing::{
342-
arb_established_address, arb_non_internal_address,
343-
};
341+
use namada_core::address::testing::arb_non_internal_address;
344342
use namada_core::address::{Address, MASP};
345343
use namada_core::collections::HashMap;
346344
use namada_core::masp::{encode_asset_type, AssetData, TAddrData};
@@ -367,7 +365,7 @@ pub mod testing {
367365
fn arb_single_transparent_transfer()(
368366
source in arb_non_internal_address(),
369367
target in arb_non_internal_address(),
370-
token in arb_established_address().prop_map(Address::Established),
368+
token in arb_non_internal_address(),
371369
amount in arb_denominated_amount(),
372370
) -> (Address, Address, Address, DenominatedAmount) {
373371
(

0 commit comments

Comments
 (0)