Skip to content

Commit

Permalink
fixup! fixup! fix: hardware wallet integration
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelCastilloB authored and mirceahasegan committed Oct 9, 2024
1 parent 32fdc3d commit 0b918d6
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 68 deletions.
4 changes: 3 additions & 1 deletion apps/browser-extension-wallet/src/hooks/useCollateral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type UseCollateralReturn = {
isSubmitting: boolean;
txFee: Cardano.Lovelace;
hasEnoughAda: boolean;
txBuilder?: TxBuilder;
};

export const useCollateral = (): UseCollateralReturn => {
Expand Down Expand Up @@ -109,6 +110,7 @@ export const useCollateral = (): UseCollateralReturn => {
isInitializing,
isSubmitting,
txFee,
hasEnoughAda
hasEnoughAda,
txBuilder
};
};
10 changes: 9 additions & 1 deletion apps/browser-extension-wallet/src/views/nami-mode/NamiView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,13 @@ export const NamiView = withDappContext((): React.ReactElement => {
}),
[currentChain.networkId, walletUI.cardanoCoin]
);
const { txFee, isInitializing, initializeCollateralTx, submitCollateralTx } = useCollateral();
const {
txFee,
isInitializing,
initializeCollateralTx,
submitCollateralTx,
txBuilder: collateralTxBuilder
} = useCollateral();

const cardanoPrice = priceResult.cardano.price;
const walletAddresses = walletInfo?.addresses
Expand Down Expand Up @@ -306,6 +312,8 @@ export const NamiView = withDappContext((): React.ReactElement => {
signAndSubmitTransaction,
passwordUtil,
delegationTxFee: !!delegationTxBuilder && delegationTxFee,
delegationStoreDelegationTxBuilder: delegationTxBuilder,
collateralTxBuilder,
setSelectedStakePool,
isBuildingTx,
stakingError,
Expand Down
3 changes: 3 additions & 0 deletions packages/nami/src/features/outside-handles-provider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Events } from '../../features/analytics/events';
import type { CreateWalletParams } from '../../types/wallet';
import type { Serialization, EraSummary } from '@cardano-sdk/core';
import type { Cip30DataSignature } from '@cardano-sdk/dapp-connector';
import type { TxBuilder } from '@cardano-sdk/tx-construction';
import type {
AnyBip32Wallet,
WalletManagerActivateProps,
Expand Down Expand Up @@ -120,6 +121,8 @@ export interface OutsideHandlesContextValue {
setPassword: (pw: Readonly<Partial<Password>>) => void;
};
delegationTxFee: string;
delegationStoreDelegationTxBuilder?: TxBuilder;
collateralTxBuilder?: TxBuilder;
setSelectedStakePool: (
pool: Readonly<Wallet.Cardano.StakePool | undefined>,
) => void;
Expand Down
24 changes: 19 additions & 5 deletions packages/nami/src/ui/app/components/confirmModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { MdUsb } from 'react-icons/md';

import { ERROR } from '../../../config/config';
import { WalletType } from '@cardano-sdk/web-extension';
import { useHistory } from 'react-router-dom';

interface Props {
ready: boolean;
Expand All @@ -37,10 +36,12 @@ interface Props {
walletType: WalletType;
openHWFlow: (path: string) => void;
getCbor: () => Promise<string>;
setCollateral?: boolean;
isPopup?: boolean;
}

const ConfirmModal = React.forwardRef<unknown, Props>(
({ ready, onConfirm, sign, onCloseBtn, title, info, setPassword, walletType, openHWFlow }, ref) => {
({ ready, onConfirm, sign, onCloseBtn, title, info, setPassword, walletType, openHWFlow, getCbor, setCollateral, isPopup }, ref) => {
const {
isOpen: isOpenNormal,
onOpen: onOpenNormal,
Expand All @@ -59,7 +60,10 @@ const ConfirmModal = React.forwardRef<unknown, Props>(
title,
info,
walletType,
openHWFlow
openHWFlow,
getCbor,
setCollateral,
isPopup
};
React.useImperativeHandle(ref, () => ({
openModal() {
Expand Down Expand Up @@ -213,9 +217,19 @@ const ConfirmModalHw = ({ props, isOpen, onClose }) => {
const [error, setError] = React.useState('');

const confirmHandler = async () => {
if (props.walletType === WalletType.Trezor) {
if (props.walletType === WalletType.Trezor && props.isPopup) {
const cbor = await props.getCbor();
props.openHWFlow(`hwTab/trezorTx/${cbor}`);

if (cbor === '') {
setError('An error occurred');
return;
}

if (props.setCollateral) {
props.openHWFlow(`hwTab/trezorTx/${cbor}/${props.setCollateral}`);
} else {
props.openHWFlow(`hwTab/trezorTx/${cbor}`);
}
} else {
if (props.ready === false || !waitReady) return;
setWaitReady(false);
Expand Down
94 changes: 90 additions & 4 deletions packages/nami/src/ui/app/components/transactionBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { useOutsideHandles } from '../../../features/outside-handles-provider/us

import ConfirmModal from './confirmModal';
import UnitDisplay from './unitDisplay';
import {Cardano, Serialization} from "@cardano-sdk/core";

type States = 'DONE' | 'EDITING' | 'ERROR' | 'LOADING';
const PoolStates: Record<States, States> = {
Expand Down Expand Up @@ -119,7 +120,9 @@ const TransactionBuilder = React.forwardRef<unknown, undefined>(
resetDelegationState,
hasNoFunds,
openExternalLink,
openHWFlow
openHWFlow,
delegationStoreDelegationTxBuilder,
collateralTxBuilder
} = useOutsideHandles();
const { initDelegation, stakeRegistration } = useDelegation({
inMemoryWallet,
Expand All @@ -130,7 +133,7 @@ const TransactionBuilder = React.forwardRef<unknown, undefined>(
useCollateral({
inMemoryWallet,
submitCollateralTx,
withSignTxConfirmation,
withSignTxConfirmation
});
const toast = useToast();
const {
Expand Down Expand Up @@ -191,7 +194,6 @@ const TransactionBuilder = React.forwardRef<unknown, undefined>(
},
}));
} catch (error_) {
console.log(error_);
setData(d => ({
...d,
pool: {
Expand All @@ -218,7 +220,6 @@ const TransactionBuilder = React.forwardRef<unknown, undefined>(
try {
await initDelegation();
} catch {
console.error(error);
setData(d => ({
...d,
error: 'Transaction not possible (maybe account balance too low)',
Expand Down Expand Up @@ -248,6 +249,7 @@ const TransactionBuilder = React.forwardRef<unknown, undefined>(
return (
<>
<ConfirmModal
isPopup={true}
onCloseBtn={() => {
setData({ pool: { ...poolDefaultValue } });
resetDelegationState();
Expand Down Expand Up @@ -282,6 +284,35 @@ const TransactionBuilder = React.forwardRef<unknown, undefined>(
}
delegationRef.current.closeModal();
}}
getCbor={async ()=> {
if (!delegationStoreDelegationTxBuilder) {
toast({
title: 'Transaction failed',
description: 'Transaction could not be built',
status: 'error',
duration: 3000,
});
delegationRef.current.closeModal();
return '';
}

const tx = await delegationStoreDelegationTxBuilder.build();

const inspection = await tx.inspect();
const transaction = new Serialization.Transaction(
Serialization.TransactionBody.fromCore(inspection.body),
Serialization.TransactionWitnessSet.fromCore(
inspection.witness
? (inspection.witness as Cardano.Witness)
: { signatures: new Map() },
),
inspection.auxiliaryData
? Serialization.AuxiliaryData.fromCore(inspection.auxiliaryData)
: undefined,
);

return transaction.toCbor();
}}
info={
<Box
width="100%"
Expand Down Expand Up @@ -423,6 +454,7 @@ const TransactionBuilder = React.forwardRef<unknown, undefined>(
ref={delegationRef}
/>
<ConfirmModal
isPopup={true}
onCloseBtn={() => {
setData({ pool: { ...poolDefaultValue } });
resetDelegationState();
Expand Down Expand Up @@ -456,6 +488,32 @@ const TransactionBuilder = React.forwardRef<unknown, undefined>(
});
}
}}
getCbor={async ()=> {
if (!delegationStoreDelegationTxBuilder) {
toast({
title: 'Transaction failed',
description: 'Transaction could not be built',
status: 'error',
duration: 3000,
});
delegationRef.current.closeModal();
return '';
}

const tx = await delegationStoreDelegationTxBuilder.build();

const inspection = await tx.inspect();
const transaction = new Serialization.Transaction(
Serialization.TransactionBody.fromCore(inspection.body),
Serialization.TransactionWitnessSet.fromCore(
inspection.witness
? (inspection.witness as Cardano.Witness)
: { signatures: new Map() },
)
);

return transaction.toCbor();
}}
info={
<Box
width="100%"
Expand Down Expand Up @@ -519,6 +577,7 @@ const TransactionBuilder = React.forwardRef<unknown, undefined>(
ref={undelegateRef}
/>
<ConfirmModal
isPopup={true}
ready={!isInitializingCollateral}
title={
<Box display="flex" alignItems="center">
Expand Down Expand Up @@ -558,6 +617,33 @@ const TransactionBuilder = React.forwardRef<unknown, undefined>(
collateralRef.current.closeModal();
capture(Events.SettingsCollateralXClick);
}}
setCollateral={true}
getCbor={async ()=> {
if (!collateralTxBuilder) {
toast({
title: 'Transaction failed',
description: 'Transaction could not be built',
status: 'error',
duration: 3000,
});
delegationRef.current.closeModal();
return '';
}

const tx = await collateralTxBuilder.build();

const inspection = await tx.inspect();

const transaction = new Serialization.Transaction(
Serialization.TransactionBody.fromCore(inspection.body),
Serialization.TransactionWitnessSet.fromCore(
inspection.witness
? (inspection.witness as Cardano.Witness)
: { signatures: new Map() },
),
);
return transaction.toCbor();
}}
info={
<Box
width="100%"
Expand Down
Loading

0 comments on commit 0b918d6

Please sign in to comment.