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 2e32593
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 63 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
21 changes: 12 additions & 9 deletions crates/apps_lib/src/cli/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ 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 masp_primitives::zip32::sapling::PseudoExtendedKey;
use masp_primitives::zip32::{
ExtendedFullViewingKey as MaspExtendedViewingKey, ExtendedSpendingKey as MaspExtendedSpendingKey,
};

use super::args;
use crate::cli::utils;
Expand Down Expand Up @@ -44,7 +47,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,31 +588,31 @@ 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())
|x| PseudoExtendedKey::from(MaspExtendedSpendingKey::from(x))
).or_else(|_parse_err| {
ExtendedViewingKey::from_str(raw).map(
|x| PseudoExtendedSpendingKey::from_viewing_key(x.into())
|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| PseudoExtendedSpendingKey::from_spending_key(k.key.into()))
.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| PseudoExtendedSpendingKey::from_viewing_key(k.key.into()))
.map(|k| PseudoExtendedKey::from(MaspExtendedViewingKey::from(k.key)))
.map_err(|_find_err| format!("Unknown viewing key {}", raw))
})
}
Expand Down Expand Up @@ -694,11 +697,11 @@ impl ArgFromMutContext for TransferSource {
.map(Self::Address)
.or_else(|_| {
ExtendedSpendingKey::arg_from_mut_ctx(ctx, raw)
.map(|x| Self::ExtendedSpendingKey(PseudoExtendedSpendingKey::from_spending_key(x.into())))
.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())))
.map(|x| Self::ExtendedSpendingKey(PseudoExtendedKey::from(MaspExtendedViewingKey::from(x))))
})
}
}
Expand Down
15 changes: 11 additions & 4 deletions crates/apps_lib/src/client/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use namada_sdk::collections::HashMap;
use masp_primitives::sapling::redjubjub;
use masp_primitives::transaction::components::sapling::fees::InputView;
use crate::masp_primitives::transaction::components::sapling;
use masp_primitives::zip32::ExtendedKey;
use masp_primitives::sapling::redjubjub::PrivateKey;

use masp_primitives::transaction::components::sapling::builder::{
BuildParams, ConvertBuildParams, OutputBuildParams, RngBuildParams,
Expand Down Expand Up @@ -838,7 +840,7 @@ pub async fn submit_shielded_transfer(
// Augment the pseudo spending key with a proof authorization key
for data in &mut args.data {
// Only attempt an augmentation if proof authorization is not there
if data.source.partial_spending_key().is_none() {
if data.source.to_spending_key().is_none() {
// First find the derivation path corresponding to this viewing
// key
let viewing_key =
Expand Down Expand Up @@ -900,11 +902,14 @@ pub async fn submit_shielded_transfer(
hardware wallet: {}.",
err,
)))?;
// Finally augment the pseudo spending key
data.source.augment(pgk).map_err(|_| error::Error::Other(format!(
// Augment the pseudo spending key
data.source.augment_proof_generation_key(pgk).map_err(|_| error::Error::Other(format!(
"Proof generation key in response from the hardware wallet \
does not correspond to stored viewing key.",
)))?;
// Finally, augment an incorrect spend authorization key just to
// make sure that the Transaction is built.
data.source.augment_spend_authorizing_key_unchecked(PrivateKey(jubjub::Fr::default()));
shielded_hw_keys.insert(path.path, viewing_key);
}
}
Expand Down Expand Up @@ -988,7 +993,7 @@ pub async fn submit_shielded_transfer(
for (path, vk) in shielded_hw_keys {
// Sign the MASP Transaction using the current viewing key
let path = BIP44Path { path: path.to_string() };
let response = app
app
.sign_masp(&path, &tx.serialize_to_vec())
.await
.map_err(|err| error::Error::Other(err.to_string()))?;
Expand Down Expand Up @@ -1027,6 +1032,8 @@ pub async fn submit_shielded_transfer(
err,
)))?;
}
tx.remove_masp_section(&shielded_hash);
tx.add_section(Section::MaspTx(masp_tx));
}
sign(namada, &mut tx, &args.tx, signing_data).await?;
namada.submit(tx, &args.tx).await?;
Expand Down
9 changes: 5 additions & 4 deletions crates/core/src/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use borsh_ext::BorshSerializeExt;
use masp_primitives::asset_type::AssetType;
use masp_primitives::sapling::ViewingKey;
use masp_primitives::transaction::TransparentAddress;
use masp_primitives::zip32::sapling::PseudoExtendedSpendingKey;
use masp_primitives::zip32::sapling::PseudoExtendedKey;
use masp_primitives::zip32::ExtendedKey;
pub use masp_primitives::transaction::TxId as TxIdInner;
use namada_macros::BorshDeserializer;
#[cfg(feature = "migrations")]
Expand Down Expand Up @@ -68,7 +69,7 @@ pub struct MaspTxId(
serialize_with = "serialize_txid",
deserialize_with = "deserialize_txid"
)]
TxIdInner,
pub TxIdInner,
);

impl From<TxIdInner> for MaspTxId {
Expand Down Expand Up @@ -518,7 +519,7 @@ pub enum TransferSource {
/// A transfer coming from a transparent address
Address(Address),
/// A transfer coming from a shielded address
ExtendedSpendingKey(PseudoExtendedSpendingKey),
ExtendedSpendingKey(PseudoExtendedKey),
}

impl TransferSource {
Expand All @@ -533,7 +534,7 @@ impl TransferSource {
}

/// Get the contained ExtendedSpendingKey contained, if any
pub fn spending_key(&self) -> Option<PseudoExtendedSpendingKey> {
pub fn spending_key(&self) -> Option<PseudoExtendedKey> {
match self {
Self::ExtendedSpendingKey(x) => Some(*x),
_ => None,
Expand Down
4 changes: 2 additions & 2 deletions crates/sdk/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::str::FromStr;
use std::time::Duration as StdDuration;

use masp_primitives::transaction::components::sapling::builder::BuildParams;
use masp_primitives::zip32::sapling::PseudoExtendedSpendingKey;
use masp_primitives::zip32::sapling::PseudoExtendedKey;

use namada_core::address::Address;
use namada_core::chain::{BlockHeight, ChainId, Epoch};
Expand Down Expand Up @@ -121,7 +121,7 @@ impl NamadaTypes for SdkTypes {
type MaspIndexerAddress = String;
type PaymentAddress = namada_core::masp::PaymentAddress;
type PublicKey = namada_core::key::common::PublicKey;
type SpendingKey = PseudoExtendedSpendingKey;
type SpendingKey = PseudoExtendedKey;
type TendermintAddress = tendermint_rpc::Url;
type TransferSource = namada_core::masp::TransferSource;
type TransferTarget = namada_core::masp::TransferTarget;
Expand Down
10 changes: 5 additions & 5 deletions crates/sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub use std::marker::Sync as MaybeSync;
use std::path::PathBuf;
use std::str::FromStr;

use masp_primitives::zip32::sapling::PseudoExtendedSpendingKey;
use masp_primitives::zip32::sapling::PseudoExtendedKey;
use args::{DeviceTransport, InputAmount, SdkTypes};
use io::Io;
use masp::{ShieldedContext, ShieldedUtils};
Expand All @@ -62,7 +62,7 @@ use namada_core::dec::Dec;
use namada_core::ethereum_events::EthAddress;
use namada_core::ibc::core::host::types::identifiers::{ChannelId, PortId};
use namada_core::key::*;
use namada_core::masp::{ExtendedSpendingKey, PaymentAddress, TransferSource};
use namada_core::masp::{PaymentAddress, TransferSource};
use namada_tx::data::wrapper::GasLimit;
use namada_tx::Tx;
use rpc::{denominate_amount, format_denominated_amount, query_native_token};
Expand Down Expand Up @@ -190,7 +190,7 @@ pub trait Namada: Sized + MaybeSync + MaybeSend {
fn new_shielded_transfer(
&self,
data: Vec<args::TxShieldedTransferData>,
gas_spending_keys: Vec<PseudoExtendedSpendingKey>,
gas_spending_keys: Vec<PseudoExtendedKey>,
disposable_signing_key: bool,
) -> args::TxShieldedTransfer {
args::TxShieldedTransfer {
Expand Down Expand Up @@ -221,9 +221,9 @@ pub trait Namada: Sized + MaybeSync + MaybeSend {
/// arguments
fn new_unshielding_transfer(
&self,
source: PseudoExtendedSpendingKey,
source: PseudoExtendedKey,
data: Vec<args::TxUnshieldingTransferData>,
gas_spending_keys: Vec<PseudoExtendedSpendingKey>,
gas_spending_keys: Vec<PseudoExtendedKey>,
disposable_signing_key: bool,
) -> args::TxUnshieldingTransfer {
args::TxUnshieldingTransfer {
Expand Down
Loading

0 comments on commit 2e32593

Please sign in to comment.