Skip to content

Commit

Permalink
Now ensure that the MASP crate gets the correct key ak.
Browse files Browse the repository at this point in the history
  • Loading branch information
murisi committed Sep 10, 2024
1 parent a938eb2 commit 8fac040
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 163 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ libc = "0.2.97"
libloading = "0.7.2"
linkme = "0.3.24"
# branch = "tomas/arbitrary"
masp_primitives = { git = "https://github.com/anoma/masp", rev = "e6451ecf64d519409f9b1a67aa1d8322a9fe0717" }
masp_proofs = { git = "https://github.com/anoma/masp", rev = "e6451ecf64d519409f9b1a67aa1d8322a9fe0717", default-features = false, features = ["local-prover"] }
masp_primitives = { git = "https://github.com/anoma/masp", rev = "f2b0cae3e495e4f7d482e587432ec4e5f2793528" }
masp_proofs = { git = "https://github.com/anoma/masp", rev = "f2b0cae3e495e4f7d482e587432ec4e5f2793528", default-features = false, features = ["local-prover"] }
num256 = "0.3.5"
num_cpus = "1.13.0"
num-derive = "0.4"
Expand Down
76 changes: 49 additions & 27 deletions crates/apps_lib/src/cli/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ use std::path::{Path, PathBuf};
use std::str::FromStr;

use color_eyre::eyre::Result;
use masp_primitives::zip32::sapling::PseudoExtendedKey;
use masp_primitives::zip32::{
ExtendedFullViewingKey as MaspExtendedViewingKey,
ExtendedSpendingKey as MaspExtendedSpendingKey,
};
use namada_sdk::address::{Address, InternalAddress};
use namada_sdk::chain::ChainId;
use namada_sdk::ethereum_events::EthAddress;
Expand All @@ -16,7 +21,6 @@ use namada_sdk::masp::fs::FsShieldedUtils;
use namada_sdk::masp::{ShieldedContext, *};
use namada_sdk::wallet::{DatedSpendingKey, DatedViewingKey, Wallet};
use namada_sdk::{Namada, NamadaImpl};
use masp_primitives::zip32::sapling::PseudoExtendedSpendingKey;

use super::args;
use crate::cli::utils;
Expand Down Expand Up @@ -44,7 +48,7 @@ pub type WalletAddrOrNativeToken = FromContext<AddrOrNativeToken>;

/// A raw extended spending key (bech32m encoding) or an alias of an extended
/// spending key in the wallet
pub type WalletSpendingKey = FromContext<PseudoExtendedSpendingKey>;
pub type WalletSpendingKey = FromContext<PseudoExtendedKey>;

/// A raw dated extended spending key (bech32m encoding) or an alias of an
/// extended spending key in the wallet
Expand Down Expand Up @@ -585,33 +589,45 @@ impl ArgFromMutContext for ExtendedSpendingKey {
}
}

impl ArgFromMutContext for PseudoExtendedSpendingKey {
impl ArgFromMutContext for PseudoExtendedKey {
fn arg_from_mut_ctx(
ctx: &mut ChainContext,
raw: impl AsRef<str>,
) -> Result<Self, String> {
let raw = raw.as_ref();
// Either the string is a raw extended spending key
ExtendedSpendingKey::from_str(raw).map(
|x| PseudoExtendedSpendingKey::from_spending_key(x.into())
).or_else(|_parse_err| {
ExtendedViewingKey::from_str(raw).map(
|x| PseudoExtendedSpendingKey::from_viewing_key(x.into())
)
}).or_else(|_parse_err| {
// Or it is a stored alias of one
ctx.wallet
.find_spending_key(raw, None)
.map(|k| PseudoExtendedSpendingKey::from_spending_key(k.key.into()))
.map_err(|_find_err| format!("Unknown spending key {}", raw))
}).or_else(|_parse_err| {
// Or it is a stored alias of one
ctx.wallet
.find_viewing_key(raw)
.copied()
.map(|k| PseudoExtendedSpendingKey::from_viewing_key(k.key.into()))
.map_err(|_find_err| format!("Unknown viewing key {}", raw))
})
ExtendedSpendingKey::from_str(raw)
.map(|x| PseudoExtendedKey::from(MaspExtendedSpendingKey::from(x)))
.or_else(|_parse_err| {
ExtendedViewingKey::from_str(raw).map(|x| {
PseudoExtendedKey::from(MaspExtendedViewingKey::from(x))
})
})
.or_else(|_parse_err| {
// Or it is a stored alias of one
ctx.wallet
.find_spending_key(raw, None)
.map(|k| {
PseudoExtendedKey::from(MaspExtendedSpendingKey::from(
k.key,
))
})
.map_err(|_find_err| {
format!("Unknown spending key {}", raw)
})
})
.or_else(|_parse_err| {
// Or it is a stored alias of one
ctx.wallet
.find_viewing_key(raw)
.copied()
.map(|k| {
PseudoExtendedKey::from(MaspExtendedViewingKey::from(
k.key,
))
})
.map_err(|_find_err| format!("Unknown viewing key {}", raw))
})
}
}

Expand Down Expand Up @@ -693,12 +709,18 @@ impl ArgFromMutContext for TransferSource {
Address::arg_from_ctx(ctx, raw)
.map(Self::Address)
.or_else(|_| {
ExtendedSpendingKey::arg_from_mut_ctx(ctx, raw)
.map(|x| Self::ExtendedSpendingKey(PseudoExtendedSpendingKey::from_spending_key(x.into())))
ExtendedSpendingKey::arg_from_mut_ctx(ctx, raw).map(|x| {
Self::ExtendedSpendingKey(PseudoExtendedKey::from(
MaspExtendedSpendingKey::from(x),
))
})
})
.or_else(|_| {
ExtendedViewingKey::arg_from_mut_ctx(ctx, raw)
.map(|x| Self::ExtendedSpendingKey(PseudoExtendedSpendingKey::from_viewing_key(x.into())))
ExtendedViewingKey::arg_from_mut_ctx(ctx, raw).map(|x| {
Self::ExtendedSpendingKey(PseudoExtendedKey::from(
MaspExtendedViewingKey::from(x),
))
})
})
}
}
Expand Down
13 changes: 11 additions & 2 deletions crates/apps_lib/src/cli/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,10 @@ async fn shielded_key_derive(
cli::safe_exit(1)
};
let xfvk = ExtendedFullViewingKey::try_from_slice(&response_key.xfvk)
.expect("unable to decode extended full viewing key from the hardware wallet");
.expect(
"unable to decode extended full viewing key from the hardware \
wallet",
);

wallet
.insert_viewing_key(
Expand Down Expand Up @@ -416,7 +419,13 @@ fn shielded_key_address_add(
let (alias, typ) = match masp_value {
MaspValue::FullViewingKey(viewing_key) => {
let alias = wallet
.insert_viewing_key(alias, viewing_key, birthday, alias_force, None)
.insert_viewing_key(
alias,
viewing_key,
birthday,
alias_force,
None,
)
.unwrap_or_else(|| {
edisplay_line!(io, "Viewing key not added");
cli::safe_exit(1);
Expand Down
Loading

0 comments on commit 8fac040

Please sign in to comment.