Skip to content

Commit

Permalink
fix: better detection for transfer type
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Jan 15, 2025
1 parent 6f39cdd commit 0661fba
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
15 changes: 6 additions & 9 deletions apps/extension/src/Approvals/Commitment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ import { FaVoteYea } from "react-icons/fa";
import { FaRegEye, FaWallet } from "react-icons/fa6";
import { GoStack } from "react-icons/go";
import { PiDotsNineBold } from "react-icons/pi";
import {
hasShieldedSection,
parseTransferType,
ShieldedPoolLabel,
} from "utils";
import { isShieldedPool, parseTransferType, ShieldedPoolLabel } from "utils";
import { TransactionCard } from "./TransactionCard";

type CommitmentProps = {
Expand Down Expand Up @@ -59,6 +55,9 @@ const TitleMap: Record<TxType, string> = {
const formatAddress = (address: string): string =>
shortenAddress(address, 6, 6);

const formatTransferAddress = (address: string): string =>
isShieldedPool(address) ? ShieldedPoolLabel : formatAddress(address);

const renderContent = (tx: CommitmentDetailProps): ReactNode => {
switch (tx.txType) {
case TxType.Bond:
Expand Down Expand Up @@ -119,10 +118,8 @@ const renderContent = (tx: CommitmentDetailProps): ReactNode => {
const { source, target } = parseTransferType(transferTx);
return (
<>
Transfer from {formatAddress(source)} to{" "}
{hasShieldedSection(transferTx) ?
ShieldedPoolLabel
: formatAddress(target)}
Transfer from {formatTransferAddress(source)} to{" "}
{formatTransferAddress(target)}
</>
);

Expand Down
20 changes: 14 additions & 6 deletions apps/extension/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ export const ShieldedPoolLabel = "the shielded pool";
export const hasShieldedSection = (tx: TransferProps): boolean => {
return Boolean(tx.shieldedSectionHash);
};
export const isShieldedPool = (address: string): boolean => {
const shieldedPoolRegex = /qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq/;
return Boolean(address.match(shieldedPoolRegex));
};

/**
* Create label to indicate specific type of Transfer
Expand All @@ -149,12 +153,16 @@ export const parseTransferType = (
let type: TransferType = "Transparent";
const txHasShieldedSection = hasShieldedSection(tx);

if (source.startsWith("tnam") && txHasShieldedSection) {
type = "Shielding";
} else if (source.startsWith("znam") && txHasShieldedSection) {
type = "Shielded";
} else if (source.startsWith("znam") && target.startsWith("tnam")) {
type = "Unshielding";
if (txHasShieldedSection) {
if (isShieldedPool(source)) {
if (target.startsWith("znam")) {
type = "Shielded";
} else {
type = "Unshielding";
}
} else if (isShieldedPool(target)) {
type = "Shielding";
}
}

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/lib/src/sdk/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl TransactionKind {
pub fn from(tx_type: TxType, data: &[u8]) -> Self {
match tx_type {
TxType::Transfer => TransactionKind::Transfer(
Transfer::try_from_slice(data).expect("Cannot deserialize TransparentTransfer"),
Transfer::try_from_slice(data).expect("Cannot deserialize Transfer"),
),
TxType::Bond => {
TransactionKind::Bond(Bond::try_from_slice(data).expect("Cannot deserialize Bond"))
Expand Down

0 comments on commit 0661fba

Please sign in to comment.