Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lykhoyda/transparent address #66

Merged
merged 4 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions crates/webz-wallet/src/bindgen/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use zcash_client_backend::proto::service::{
compact_tx_streamer_client::CompactTxStreamerClient, ChainSpec,
};
use zcash_client_memory::MemoryWalletDb;
use zcash_keys::encoding::AddressCodec;
use zcash_keys::keys::UnifiedFullViewingKey;
use zcash_primitives::transaction::TxId;

Expand Down Expand Up @@ -369,6 +370,21 @@ impl WebWallet {
}
}

/// Get the current unified address for a given account and extracts the transparent component. This is returned as a string in canonical encoding
///
/// # Arguments
///
/// * `account_id` - The ID of the account to get the address for
///
pub async fn get_current_address_transparent(&self, account_id: u32) -> Result<String, Error> {
let db = self.inner.db.read().await;
if let Some(address) = db.get_current_address(account_id.into())? {
Ok(address.transparent().unwrap().encode(&self.inner.network))
} else {
Err(Error::AccountNotFound(account_id))
}
}

///////////////////////////////////////////////////////////////////////////////////////
// lightwalletd gRPC methods
///////////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/template-snap-monorepo.git"
},
"source": {
"shasum": "H66kPf5KzVuH2Wh122uNoA/nMaSXORtaHfgw8z8u71o=",
"shasum": "SA1fH7ay1ietGUsIELeD6rgpMuXu/rusR65jSdjCBrk=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
26 changes: 14 additions & 12 deletions packages/web-wallet/src/hooks/useWebzjsActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ interface WebzjsActions {
ufvk: string,
birthdayHeight: number,
) => Promise<void>;
getAccountData: () => Promise<{ unifiedAddress: string } | undefined>;
getAccountData: () => Promise<
{ unifiedAddress: string; transparentAddress: string } | undefined
>;
triggerRescan: () => Promise<void>;
flushDbToStore: () => Promise<void>;
syncStateWithWallet: () => Promise<void>;
Expand All @@ -18,17 +20,17 @@ export function useWebZjsActions(): WebzjsActions {

const getAccountData = useCallback(async () => {
try {
if (state.activeAccount !== undefined) {
return {
unifiedAddress: await state.webWallet!.get_current_address(
state.activeAccount,
),
};
} else {
return {
unifiedAddress: await state.webWallet!.get_current_address(0),
};
}
const accountIndex = state.activeAccount ?? 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about general activeAccount state in the web-wallet, why it is hardcocded to 0, and not throw error if its missing from state

Also in addNewAccountFromUfvk with setting it upon install it defaults to hardcoded 0 instead of error handle

const account_id =
        (await state.webWallet?.create_account_ufvk(ufvk, birthdayHeight)) || 0;
      dispatch({ type: 'set-active-account', payload: account_id });

Copy link
Contributor Author

@Lykhoyda Lykhoyda Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We expect to manage only one account now, so it will always be zero. But if you take the example of a demo wallet, we can switch accounts and import it from MetaMask or seed, so an active account is required.


const unifiedAddress =
await state.webWallet!.get_current_address(accountIndex);
const transparentAddress =
await state.webWallet!.get_current_address_transparent(accountIndex);

return {
unifiedAddress,
transparentAddress,
};
} catch (error) {
dispatch({
type: 'set-error',
Expand Down
28 changes: 21 additions & 7 deletions packages/web-wallet/src/pages/Receive/Receive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,29 @@ enum AddressType {
function Receive(): React.JSX.Element {
const [loading, setLoading] = useState(true);
const [activeTab, setActiveTab] = useState<AddressType>(AddressType.UNIFIED);
const [unifiedAddress, setUnifiedAddress] = useState('');
const [addresses, setAddresses] = useState<{
unifiedAddress: string;
transparentAddress: string;
}>({
unifiedAddress: '',
transparentAddress: '',
});
const [error, setError] = useState<string | null>(null);
const { getAccountData } = useWebZjsActions();

useEffect(() => {
const fetchData = async () => {
try {
const data = await getAccountData();
if (data) setUnifiedAddress(data.unifiedAddress);
setLoading(false);
if (data)
setAddresses({
unifiedAddress: data.unifiedAddress,
transparentAddress: data.transparentAddress,
});
} catch (err) {
setError('Failed to fetch account data');
} finally {
setLoading(false);
}
};

Expand All @@ -34,11 +45,9 @@ function Receive(): React.JSX.Element {
const tabs = {
[AddressType.UNIFIED]: {
label: 'Unified Address',
component: <QrCode address={unifiedAddress} />,
},
[AddressType.TRANSPARENT]: {
label: 'Transparent Address',
component: <div>TODO: Transparent address</div>,
},
};

Expand All @@ -54,14 +63,19 @@ function Receive(): React.JSX.Element {
{Object.keys(tabs).map((tab) => (
<Tab
key={tab}
tabName={tab}
label={tabs[tab as AddressType].label}
isActive={activeTab === tab}
onClick={() => setActiveTab(tab as AddressType)}
/>
))}
</div>
{/* Tabs content */}
{tabs[activeTab].component}
{activeTab === AddressType.UNIFIED && (
<QrCode address={addresses.unifiedAddress} />
)}
{activeTab === AddressType.TRANSPARENT && (
<QrCode address={addresses.transparentAddress} />
)}
</>
)}
{error && <div className="text-red-500">{error}</div>}
Expand Down
7 changes: 4 additions & 3 deletions packages/web-wallet/src/pages/Receive/Tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import { CircleDashedSvg, CircleSvg } from '../../assets';
import cn from 'classnames';

interface TabProps {
tabName: string;
label: string;
isActive: boolean;
onClick: () => void;
onClick: (key: string) => void;
}

const Tab: React.FC<TabProps> = ({ label, isActive, onClick }) => {
const Tab: React.FC<TabProps> = ({ tabName, label, isActive, onClick }) => {
return (
<div
onClick={onClick}
onClick={() => onClick(tabName)}
className={cn(
'px-4 py-2 justify-center items-center gap-1.5 flex rounded-3xl cursor-pointer',
{
Expand Down