Skip to content

Commit

Permalink
Merge of #3653
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Aug 16, 2024
2 parents 80268fa + 00df744 commit 63f0fd7
Show file tree
Hide file tree
Showing 14 changed files with 496 additions and 81 deletions.
4 changes: 4 additions & 0 deletions .changelog/unreleased/features/3653-masp-key-birthdays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- Partially addresses Issue [\#2900](https://github.com/anoma/namada/issues/2900). Viewing and spending keys can now
be given birthdays in the form of block heights which are loaded into
shielded sync. Shielded sync will not try to decrypt a block before a
keys birthday with said key. ([\#3653](https://github.com/anoma/namada/pull/3653))
47 changes: 40 additions & 7 deletions crates/apps_lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3211,6 +3211,7 @@ pub mod args {
Err(_) => config::get_default_namada_folder(),
}),
);
pub const BIRTHDAY: ArgOpt<BlockHeight> = arg_opt("birthday");
pub const BLOCK_HEIGHT: Arg<BlockHeight> = arg("block-height");
pub const BLOCK_HEIGHT_OPT: ArgOpt<BlockHeight> = arg_opt("height");
pub const BLOCK_HEIGHT_FROM_OPT: ArgOpt<BlockHeight> =
Expand Down Expand Up @@ -3255,6 +3256,10 @@ pub mod args {
arg_opt("success-sleep");
pub const DATA_PATH_OPT: ArgOpt<PathBuf> = arg_opt("data-path");
pub const DATA_PATH: Arg<PathBuf> = arg("data-path");
pub const DATED_SPENDING_KEYS: ArgMulti<WalletDatedSpendingKey, GlobStar> =
arg_multi("spending-keys");
pub const DATED_VIEWING_KEYS: ArgMulti<WalletDatedViewingKey, GlobStar> =
arg_multi("viewing-keys");
pub const DB_KEY: Arg<String> = arg("db-key");
pub const DB_COLUMN_FAMILY: ArgDefault<String> = arg_default(
"db-column-family",
Expand Down Expand Up @@ -6598,8 +6603,8 @@ pub mod args {
let ledger_address = CONFIG_RPC_LEDGER_ADDRESS.parse(matches);
let start_query_height = BLOCK_HEIGHT_FROM_OPT.parse(matches);
let last_query_height = BLOCK_HEIGHT_TO_OPT.parse(matches);
let spending_keys = SPENDING_KEYS.parse(matches);
let viewing_keys = VIEWING_KEYS.parse(matches);
let spending_keys = DATED_SPENDING_KEYS.parse(matches);
let viewing_keys = DATED_VIEWING_KEYS.parse(matches);
let with_indexer = WITH_INDEXER.parse(matches);
Self {
ledger_address,
Expand All @@ -6621,13 +6626,17 @@ pub mod args {
.def()
.help(wrap!("Option block height to sync from.")),
)
.arg(SPENDING_KEYS.def().help(wrap!(
.arg(DATED_SPENDING_KEYS.def().help(wrap!(
"List of new spending keys with which to check note \
ownership. These will be added to the shielded context."
ownership. These will be added to the shielded context. \
Appending \"<<$BLOCKHEIGHT\" to the end of each key adds \
a birthday."
)))
.arg(VIEWING_KEYS.def().help(wrap!(
.arg(DATED_VIEWING_KEYS.def().help(wrap!(
"List of new viewing keys with which to check note \
ownership. These will be added to the shielded context."
ownership. These will be added to the shielded context. \
Appending \"<<$BLOCKHEIGHT\" to the end of each key adds \
a birthday."
)))
.arg(WITH_INDEXER.def().help(wrap!(
"Address of a `namada-masp-indexer` live instance. If \
Expand Down Expand Up @@ -6982,6 +6991,8 @@ pub mod args {
type BpConversionTable = PathBuf;
type ConfigRpcTendermintAddress = ConfigRpcAddress;
type Data = PathBuf;
type DatedSpendingKey = WalletDatedSpendingKey;
type DatedViewingKey = WalletDatedViewingKey;
type EthereumAddress = String;
type Keypair = WalletKeypair;
type MaspIndexerAddress = String;
Expand Down Expand Up @@ -7338,7 +7349,8 @@ pub mod args {
find_viewing_key(&mut wallet)
} else {
find_viewing_key(&mut ctx.borrow_mut_chain_or_exit().wallet)
};
}
.key;

Ok(PayAddressGen::<SdkTypes> {
alias: self.alias,
Expand Down Expand Up @@ -7377,6 +7389,7 @@ pub mod args {
let shielded = SHIELDED.parse(matches);
let alias = ALIAS.parse(matches);
let alias_force = ALIAS_FORCE.parse(matches);
let birthday = BIRTHDAY.parse(matches);
let unsafe_dont_encrypt = UNSAFE_DONT_ENCRYPT.parse(matches);
let derivation_path = HD_DERIVATION_PATH.parse(matches);
let allow_non_compliant =
Expand All @@ -7396,6 +7409,7 @@ pub mod args {
prompt_bip39_passphrase,
use_device,
device_transport,
birthday,
}
}

Expand All @@ -7417,6 +7431,11 @@ pub mod args {
"Force overwrite the alias if it already exists."
)),
)
.arg(BIRTHDAY.def().help(wrap!(
"A block height after which this key is being created. Used \
for optimizing MASP operations. If none is provided, \
defaults to the first block height."
)))
.arg(UNSAFE_DONT_ENCRYPT.def().help(wrap!(
"UNSAFE: Do not encrypt the keypair. Do not use this for keys \
used in a live network."
Expand Down Expand Up @@ -7465,6 +7484,7 @@ pub mod args {
let raw = RAW_KEY_GEN.parse(matches);
let alias = ALIAS.parse(matches);
let alias_force = ALIAS_FORCE.parse(matches);
let birthday = BIRTHDAY.parse(matches);
let unsafe_dont_encrypt = UNSAFE_DONT_ENCRYPT.parse(matches);
let derivation_path = HD_DERIVATION_PATH.parse(matches);
let allow_non_compliant =
Expand All @@ -7477,6 +7497,7 @@ pub mod args {
raw,
alias,
alias_force,
birthday,
unsafe_dont_encrypt,
derivation_path,
allow_non_compliant,
Expand Down Expand Up @@ -7509,6 +7530,11 @@ pub mod args {
.arg(ALIAS_FORCE.def().help(wrap!(
"Override the alias without confirmation if it already exists."
)))
.arg(BIRTHDAY.def().help(wrap!(
"A block height after which this key is being created. Used \
for optimizing MASP operations. If none is provided, \
defaults to the first block height."
)))
.arg(UNSAFE_DONT_ENCRYPT.def().help(wrap!(
"UNSAFE: Do not encrypt the keypair. Do not use this for keys \
used in a live network."
Expand Down Expand Up @@ -7680,11 +7706,13 @@ pub mod args {
fn parse(matches: &ArgMatches) -> Self {
let alias = ALIAS.parse(matches);
let alias_force = ALIAS_FORCE.parse(matches);
let birthday = BIRTHDAY.parse(matches);
let value = VALUE.parse(matches);
let unsafe_dont_encrypt = UNSAFE_DONT_ENCRYPT.parse(matches);
Self {
alias,
alias_force,
birthday,
value,
unsafe_dont_encrypt,
}
Expand All @@ -7699,6 +7727,11 @@ pub mod args {
.arg(ALIAS_FORCE.def().help(wrap!(
"Override the alias without confirmation if it already exists."
)))
.arg(BIRTHDAY.def().help(wrap!(
"A block height after which this key is being created. Used \
for optimizing MASP operations. If none is provided, \
defaults to the first block height."
)))
.arg(VALUE.def().help(wrap!(
"Any value of the following:\n- transparent pool secret \
key\n- transparent pool public key\n- transparent pool \
Expand Down
14 changes: 3 additions & 11 deletions crates/apps_lib/src/cli/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::io::Read;

use color_eyre::eyre::Result;
use masp_primitives::zip32::ExtendedFullViewingKey;
use namada_sdk::io::Io;
use namada_sdk::{display_line, Namada, NamadaImpl};

Expand Down Expand Up @@ -351,15 +350,8 @@ impl CliApi {
.get_viewing_keys()
.values()
.copied()
.map(|vk| ExtendedFullViewingKey::from(vk).fvk.vk)
.chain(args.viewing_keys.into_iter().map(|vk| {
ExtendedFullViewingKey::from(vk).fvk.vk
}))
.collect::<Vec<_>>();
let sks = args
.spending_keys
.into_iter()
.map(|sk| sk.into())
.chain(args.viewing_keys)
.map(|vk| vk.map(|vk| vk.as_viewing_key()))
.collect::<Vec<_>>();
crate::client::masp::syncing(
chain_ctx.shielded,
Expand All @@ -368,7 +360,7 @@ impl CliApi {
&io,
args.start_query_height,
args.last_query_height,
&sks,
&args.spending_keys,
&vks,
)
.await?;
Expand Down
45 changes: 44 additions & 1 deletion crates/apps_lib/src/cli/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use namada_sdk::io::Io;
use namada_sdk::key::*;
use namada_sdk::masp::fs::FsShieldedUtils;
use namada_sdk::masp::{ShieldedContext, *};
use namada_sdk::wallet::Wallet;
use namada_sdk::wallet::{DatedSpendingKey, DatedViewingKey, Wallet};
use namada_sdk::{Namada, NamadaImpl};

use super::args;
Expand Down Expand Up @@ -45,6 +45,10 @@ pub type WalletAddrOrNativeToken = FromContext<AddrOrNativeToken>;
/// spending key in the wallet
pub type WalletSpendingKey = FromContext<ExtendedSpendingKey>;

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

/// A raw payment address (bech32m encoding) or an alias of a payment address
/// in the wallet
pub type WalletPaymentAddr = FromContext<PaymentAddress>;
Expand All @@ -53,6 +57,10 @@ pub type WalletPaymentAddr = FromContext<PaymentAddress>;
/// in the wallet
pub type WalletViewingKey = FromContext<ExtendedViewingKey>;

/// A raw full dated viewing key (bech32m encoding) or an alias of a full
/// viewing key in the wallet
pub type WalletDatedViewingKey = FromContext<DatedViewingKey>;

/// A raw address or a raw extended spending key (bech32m encoding) or an alias
/// of either in the wallet
pub type WalletTransferSource = FromContext<TransferSource>;
Expand Down Expand Up @@ -561,6 +569,23 @@ impl ArgFromContext for common::PublicKey {
}

impl ArgFromMutContext for ExtendedSpendingKey {
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
FromStr::from_str(raw).or_else(|_parse_err| {
// Or it is a stored alias of one
ctx.wallet
.find_spending_key(raw, None)
.map(|k| k.key)
.map_err(|_find_err| format!("Unknown spending key {}", raw))
})
}
}

impl ArgFromMutContext for DatedSpendingKey {
fn arg_from_mut_ctx(
ctx: &mut ChainContext,
raw: impl AsRef<str>,
Expand All @@ -577,6 +602,24 @@ impl ArgFromMutContext for ExtendedSpendingKey {
}

impl ArgFromMutContext for ExtendedViewingKey {
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 full viewing key
FromStr::from_str(raw).or_else(|_parse_err| {
// Or it is a stored alias of one
ctx.wallet
.find_viewing_key(raw)
.copied()
.map(|k| k.key)
.map_err(|_find_err| format!("Unknown viewing key {}", raw))
})
}
}

impl ArgFromMutContext for DatedViewingKey {
fn arg_from_mut_ctx(
ctx: &mut ChainContext,
raw: impl AsRef<str>,
Expand Down
Loading

0 comments on commit 63f0fd7

Please sign in to comment.