Skip to content

Commit

Permalink
Merge branch 'develop' into dev-tools/test-data-alias-ownership
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex6323 committed Jul 3, 2024
2 parents b58939a + 53b55ec commit 88da01a
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 42 deletions.
6 changes: 6 additions & 0 deletions apps/wallet/src/ui/app/hooks/useCreateAccountMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ export function useCreateAccountsMutation() {
type === AccountType.SeedDerived &&
validateAccountFormValues(type, accountsFormValues, password)
) {
if (password) {
await backgroundClient.unlockAccountSourceOrAccount({
password,
id: accountsFormValues.sourceID,
});
}
createdAccounts = await backgroundClient.createAccounts({
type: AccountType.SeedDerived,
sourceID: accountsFormValues.sourceID,
Expand Down
5 changes: 4 additions & 1 deletion apps/wallet/src/ui/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ const App = () => {
<Route path="import-private-key" element={<ImportPrivateKeyPage />} />
<Route path="import-seed" element={<ImportSeedPage />} />
<Route path="manage" element={<ManageAccountsPage />} />
<Route path="manage/accounts-finder" element={<AccountsFinderPage />} />
<Route
path="manage/accounts-finder/:accountSourceId"
element={<AccountsFinderPage />}
/>
<Route path="protect-account" element={<ProtectAccountPage />} />
<Route path="backup/:accountSourceID" element={<BackupMnemonicPage />} />
<Route path="export/:accountID" element={<ExportAccountPage />} />
Expand Down
10 changes: 7 additions & 3 deletions apps/wallet/src/ui/app/pages/accounts/ProtectAccountPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { Text } from '_app/shared/text';
import { isMnemonicSerializedUiAccount } from '_src/background/accounts/MnemonicAccount';
import { isSeedSerializedUiAccount } from '_src/background/accounts/SeedAccount';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { toast } from 'react-hot-toast';
import { Navigate, useNavigate, useSearchParams } from 'react-router-dom';
Expand Down Expand Up @@ -32,7 +33,6 @@ const ALLOWED_ACCOUNT_TYPES: CreateType[] = [
const REDIRECT_TO_ACCOUNTS_FINDER: CreateType[] = [
CreateAccountType.ImportMnemonic,
CreateAccountType.ImportSeed,
AccountType.Imported,
];

type AllowedAccountTypes = (typeof ALLOWED_ACCOUNT_TYPES)[number];
Expand Down Expand Up @@ -78,8 +78,12 @@ export function ProtectAccountPage() {
onboarding: true,
},
});
} else if (REDIRECT_TO_ACCOUNTS_FINDER.includes(type)) {
const path = '/accounts/manage/accounts-finder/';
} else if (
REDIRECT_TO_ACCOUNTS_FINDER.includes(type) &&
(isMnemonicSerializedUiAccount(createdAccounts[0]) ||
isSeedSerializedUiAccount(createdAccounts[0]))
) {
const path = `/accounts/manage/accounts-finder/${createdAccounts[0].sourceID}`;
navigate(path, {
replace: true,
state: {
Expand Down
59 changes: 40 additions & 19 deletions apps/wallet/src/ui/app/pages/accounts/manage/AccountGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,24 @@ import {
import { Heading } from '_src/ui/app/shared/heading';
import { Text } from '_src/ui/app/shared/text';
import { ButtonOrLink, type ButtonOrLinkProps } from '_src/ui/app/shared/utils/ButtonOrLink';
import { ArrowBgFill16, Plus12 } from '@iota/icons';
import { ArrowBgFill16, Plus12, Search16 } from '@iota/icons';
import * as CollapsiblePrimitive from '@radix-ui/react-collapsible';
import { useMutation } from '@tanstack/react-query';
import { forwardRef, useState } from 'react';
import toast from 'react-hot-toast';
import { useNavigate } from 'react-router-dom';

const ACCOUNT_TYPE_TO_LABEL: Record<AccountType, string> = {
[AccountType.MnemonicDerived]: 'Passphrase Derived',
[AccountType.SeedDerived]: 'Seed Derived',
[AccountType.Imported]: 'Imported',
[AccountType.Ledger]: 'Ledger',
};
const ACCOUNTS_WITH_ENABLED_BALANCE_FINDER: AccountType[] = [
AccountType.MnemonicDerived,
AccountType.SeedDerived,
AccountType.Ledger,
];

export function getGroupTitle(aGroupAccount: SerializedUIAccount) {
return ACCOUNT_TYPE_TO_LABEL[aGroupAccount?.type] || '';
Expand Down Expand Up @@ -149,6 +155,7 @@ export function AccountGroup({
type: AccountType;
accountSourceID?: string;
}) {
const navigate = useNavigate();
const createAccountMutation = useCreateAccountsMutation();
const isMnemonicDerivedGroup = type === AccountType.MnemonicDerived;
const isSeedDerivedGroup = type === AccountType.SeedDerived;
Expand All @@ -168,27 +175,41 @@ export function AccountGroup({
</Heading>
<div className="flex h-px flex-1 flex-shrink-0 bg-gray-45" />
{(isMnemonicDerivedGroup || isSeedDerivedGroup) && accountSource ? (
<>
<ButtonOrLink
loading={createAccountMutation.isPending}
onClick={async (e) => {
// prevent the collapsible from closing when clicking the "new" button
e.stopPropagation();
setAccountsFormValues({
type,
sourceID: accountSource.id,
});
if (accountSource.isLocked) {
setPasswordModalVisible(true);
} else {
createAccountMutation.mutate({ type });
}
}}
className="flex cursor-pointer appearance-none items-center justify-center gap-0.5 border-0 bg-transparent uppercase text-hero outline-none hover:text-hero-darkest"
>
<Plus12 />
<Text variant="bodySmall" weight="semibold">
New
</Text>
</ButtonOrLink>
</>
) : null}
{ACCOUNTS_WITH_ENABLED_BALANCE_FINDER.includes(type) ? (
<ButtonOrLink
loading={createAccountMutation.isPending}
onClick={async (e) => {
// prevent the collapsible from closing when clicking the "new" button
e.stopPropagation();
setAccountsFormValues({
type,
sourceID: accountSource.id,
});
if (accountSource.isLocked) {
setPasswordModalVisible(true);
} else {
createAccountMutation.mutate({ type });
}
}}
className="flex cursor-pointer appearance-none items-center justify-center gap-0.5 border-0 bg-transparent uppercase text-hero outline-none hover:text-hero-darkest"
onClick={() => {
navigate(
`/accounts/manage/accounts-finder/${accountSourceID}`,
);
}}
>
<Plus12 />
<Text variant="bodySmall" weight="semibold">
New
</Text>
<Search16 />
</ButtonOrLink>
) : null}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! and verifying the total supply.
use std::fs::File;

use iota_genesis_builder::stardust::parse::HornetGenesisSnapshotParser;
use iota_genesis_builder::stardust::parse::HornetSnapshotParser;
use iota_types::gas_coin::TOTAL_SUPPLY_IOTA;

fn main() -> anyhow::Result<()> {
Expand All @@ -14,7 +14,7 @@ fn main() -> anyhow::Result<()> {
};
let file = File::open(path)?;

let mut parser = HornetGenesisSnapshotParser::new(file)?;
let mut parser = HornetSnapshotParser::new::<true>(file)?;
println!("Output count: {}", parser.header.output_count());

let total_supply = parser.outputs().try_fold(0, |acc, output| {
Expand Down
12 changes: 6 additions & 6 deletions crates/iota-genesis-builder/examples/snapshot_test_outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
use std::{fs::File, path::Path};

use iota_genesis_builder::stardust::{
parse::HornetGenesisSnapshotParser, test_outputs::add_snapshot_test_outputs,
parse::HornetSnapshotParser, test_outputs::add_snapshot_test_outputs,
};
use iota_types::gas_coin::TOTAL_SUPPLY_IOTA;

fn parse_snapshot(path: impl AsRef<Path>) -> anyhow::Result<()> {
fn parse_snapshot<const VERIFY: bool>(path: impl AsRef<Path>) -> anyhow::Result<()> {
let file = File::open(path)?;
let mut parser = HornetGenesisSnapshotParser::new(file)?;
let mut parser = HornetSnapshotParser::new::<VERIFY>(file)?;

println!("Output count: {}", parser.header.output_count());

Expand Down Expand Up @@ -43,11 +43,11 @@ async fn main() -> anyhow::Result<()> {
new_path.push_str(&current_path);
}

parse_snapshot(&current_path)?;
parse_snapshot::<_, true>(&current_path)?;

add_snapshot_test_outputs(&current_path, &new_path).await?;
add_snapshot_test_outputs::<_, true>(&current_path, &new_path).await?;

parse_snapshot(&new_path)?;
parse_snapshot::<_, true>(&new_path)?;

Ok(())
}
15 changes: 13 additions & 2 deletions crates/iota-genesis-builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use clap::{Parser, Subcommand};
use iota_genesis_builder::{
stardust::{
migration::{Migration, MigrationTargetNetwork},
parse::HornetGenesisSnapshotParser,
parse::HornetSnapshotParser,
},
BROTLI_COMPRESSOR_BUFFER_SIZE, BROTLI_COMPRESSOR_LG_WINDOW_SIZE, BROTLI_COMPRESSOR_QUALITY,
OBJECT_SNAPSHOT_FILE_PATH,
Expand All @@ -40,6 +40,12 @@ struct Cli {
help = "Compress the resulting object snapshot"
)]
compress: bool,
#[clap(
long,
help = "Enable global snapshot verification",
default_value_t = true
)]
global_snapshot_verification: bool,
}

#[derive(Subcommand, Debug)]
Expand Down Expand Up @@ -81,7 +87,11 @@ fn main() -> Result<()> {
};

// Start the Hornet snapshot parser
let mut snapshot_parser = HornetGenesisSnapshotParser::new(File::open(snapshot_path)?)?;
let mut snapshot_parser = if cli.global_snapshot_verification {
HornetSnapshotParser::new::<true>(File::open(snapshot_path)?)?
} else {
HornetSnapshotParser::new::<false>(File::open(snapshot_path)?)?
};
let total_supply = match coin_type {
CoinType::Iota => scale_amount_for_iota(snapshot_parser.total_supply()?)?,
CoinType::Shimmer => snapshot_parser.total_supply()?,
Expand Down Expand Up @@ -121,6 +131,7 @@ fn main() -> Result<()> {
}
})
.process_results(|outputs| migration.run(outputs, object_snapshot_writer))??;

Ok(())
}

Expand Down
13 changes: 7 additions & 6 deletions crates/iota-genesis-builder/src/stardust/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ use packable::{
use super::types::{output_header::OutputHeader, snapshot::FullSnapshotHeader};

/// Parse a Hornet genesis snapshot using a [`BufReader`] internally.
pub struct HornetGenesisSnapshotParser<R: Read> {
pub struct HornetSnapshotParser<R: Read> {
reader: IoUnpacker<BufReader<R>>,
/// The full-snapshot header
pub header: FullSnapshotHeader,
}

impl<R: Read> HornetGenesisSnapshotParser<R> {
pub fn new(reader: R) -> Result<Self> {
impl<R: Read> HornetSnapshotParser<R> {
/// Creates a new [`HornetSnapshotParser`].
///
/// `VERIFY = true` ensures that only global snapshots parse successfully.
pub fn new<const VERIFY: bool>(reader: R) -> Result<Self> {
let mut reader = IoUnpacker::new(std::io::BufReader::new(reader));
// `true` ensures that only genesis snapshots unpack successfully
let header = FullSnapshotHeader::unpack::<_, false>(&mut reader, &())?;

let header = FullSnapshotHeader::unpack::<_, VERIFY>(&mut reader, &())?;
Ok(Self { reader, header })
}

Expand Down
6 changes: 3 additions & 3 deletions crates/iota-genesis-builder/src/stardust/test_outputs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use packable::{
};

use crate::stardust::{
parse::HornetGenesisSnapshotParser,
parse::HornetSnapshotParser,
types::{output_header::OutputHeader, output_index::random_output_index},
};

Expand Down Expand Up @@ -61,15 +61,15 @@ pub(crate) fn new_vested_output(
}

/// Adds outputs to test specific and intricate scenario in the full snapshot.
pub async fn add_snapshot_test_outputs<P: AsRef<Path> + core::fmt::Debug>(
pub async fn add_snapshot_test_outputs<P: AsRef<Path> + core::fmt::Debug, const VERIFY: bool>(
current_path: P,
new_path: P,
) -> anyhow::Result<()> {
let current_file = File::open(current_path)?;
let new_file = File::create(new_path)?;

let mut writer = IoPacker::new(BufWriter::new(new_file));
let mut parser = HornetGenesisSnapshotParser::new(current_file)?;
let mut parser = HornetSnapshotParser::new::<VERIFY>(current_file)?;
let output_to_decrease_amount_from = OutputId::from_str(OUTPUT_TO_DECREASE_AMOUNT_FROM)?;
let mut new_header = parser.header.clone();
let mut vested_index = u32::MAX;
Expand Down

0 comments on commit 88da01a

Please sign in to comment.