Skip to content

Commit

Permalink
refactor: shared wallets related naming improvements (#1290)
Browse files Browse the repository at this point in the history
* refactor: naming improvements

* test: fix broken unit tests
  • Loading branch information
szymonmaslowski committed Jul 17, 2024
1 parent 2158475 commit 9c042b2
Show file tree
Hide file tree
Showing 66 changed files with 494 additions and 474 deletions.
2 changes: 1 addition & 1 deletion apps/browser-extension-wallet/src/routes/wallet-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const walletRoutePaths = {
},
sharedWallet: {
root: '/shared-wallet',
generateKeys: '/shared-wallet/generate-keys',
generateKeys: '/shared-wallet/generate-key',
create: '/shared-wallet/create',
import: '/shared-wallet/import'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
AddSharedWalletMainPageFlow,
AddSharedWalletModal,
CoSigner,
GenerateSharedKeysFlow,
GenerateSharedWalletKeyFlow,
LinkedWalletType,
QuorumOptionValue,
SharedWalletCreationFlow,
Expand Down Expand Up @@ -34,7 +34,7 @@ export const SharedWallet = (): JSX.Element => {
const { walletInfo, cardanoWallet } = useWalletStore();
const { page, setBackgroundPage } = useBackgroundPage();

const [sharedKey, setSharedKey] = useState<Wallet.Crypto.Bip32PublicKeyHex>();
const [sharedWalletKey, setSharedWalletKey] = useState<Wallet.Crypto.Bip32PublicKeyHex>();
const [initialWalletName, setInitialWalletName] = useState('');
const [activeWalletType, setActiveWalletType] = useState<LinkedWalletType>();

Expand All @@ -45,16 +45,15 @@ export const SharedWallet = (): JSX.Element => {

const activeWalletId = cardanoWallet.source.wallet.walletId;
const activeWallet = wallets.find(({ walletId }) => walletId === activeWalletId);
setSharedKey(activeWallet.metadata.extendedAccountPublicKey);
setSharedWalletKey(activeWallet.metadata.extendedAccountPublicKey);
if (!activeWallet || activeWallet.type === WalletType.Script) return;
setActiveWalletType(activeWallet.type);
})();
}, [cardanoWallet.source.wallet.walletId, walletRepository]);

const handleCreateWallet = async (data: CreateWalletParams) => {
const activeWalletId = cardanoWallet.source.wallet.walletId;

const publicKeys = data.coSigners.map(({ keys }: CoSigner) => Wallet.Crypto.Bip32PublicKeyHex(keys));
const publicKeys = data.coSigners.map((c: CoSigner) => Wallet.Crypto.Bip32PublicKeyHex(c.sharedWalletKey));

await createInMemorySharedWallet({
name: data.name,
Expand All @@ -65,11 +64,11 @@ export const SharedWallet = (): JSX.Element => {
});
};

const generateKeys = async (enteredPassword: string) => {
if (sharedKey) return sharedKey;
const sharedWalletKey = await generateSharedWalletKey(enteredPassword, cardanoWallet.source.wallet.walletId);
setSharedKey(sharedWalletKey);
return sharedWalletKey;
const generateKey = async (enteredPassword: string) => {
if (sharedWalletKey) return sharedWalletKey;
const key = await generateSharedWalletKey(enteredPassword, cardanoWallet.source.wallet.walletId);
setSharedWalletKey(key);
return key;
};

return (
Expand All @@ -84,15 +83,15 @@ export const SharedWallet = (): JSX.Element => {
exact
path={walletRoutePaths.sharedWallet.generateKeys}
render={() => (
<GenerateSharedKeysFlow
<GenerateSharedWalletKeyFlow
activeWalletName={walletInfo?.name || ''}
activeWalletType={activeWalletType}
generateKeys={generateKeys}
generateKey={generateKey}
navigateToParentFlow={() => history.push(walletRoutePaths.sharedWallet.root)}
/>
)}
/>
{sharedKey && (
{sharedWalletKey && (
<Route
exact
path={walletRoutePaths.sharedWallet.create}
Expand All @@ -102,13 +101,13 @@ export const SharedWallet = (): JSX.Element => {
initialWalletName={initialWalletName}
navigateToAppHome={() => setBackgroundPage()}
exitTheFlow={() => history.push(walletRoutePaths.sharedWallet.root)}
sharedKeys={sharedKey}
sharedWalletKey={sharedWalletKey}
onCreateSharedWallet={handleCreateWallet}
/>
)}
/>
)}
{sharedKey && (
{sharedWalletKey && (
<Route
exact
path={walletRoutePaths.sharedWallet.import}
Expand All @@ -128,7 +127,7 @@ export const SharedWallet = (): JSX.Element => {
onCreateSharedWalletClick={() => history.push(walletRoutePaths.sharedWallet.create)}
onImportSharedWalletClick={() => history.push(walletRoutePaths.sharedWallet.import)}
onKeysGenerateClick={() => history.push(walletRoutePaths.sharedWallet.generateKeys)}
sharedKeys={sharedKey}
sharedWalletKey={sharedWalletKey}
/>
)}
/>
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ export * from '@ui/components/ListEmptyState';
export * from '@ui/components/MidnightEventBanner';
export * from '@ui/components/MidnightPreLaunchSettingsBanner';
export * from '@ui/components/ImageWithFallback';
export * from '@ui/components/Transaction';
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { useTranslation } from 'react-i18next';
import { CoSigner, CoSignerDirty, CoSignerError, CoSignerErrorKeys, CoSignerErrorName } from './type';

export const maxCoSignerNameLength = 20;
type FieldName = 'keys' | 'name';
type FieldName = 'sharedWalletKey' | 'name';

interface AddCoSignerInputProps {
dirty?: CoSignerDirty;
error?: CoSignerError;
keysFieldDisabled: boolean;
keyFieldDisabled: boolean;
labels: Record<FieldName, string>;
onChange: (coSigner: CoSigner) => void;
value: CoSigner;
Expand All @@ -32,24 +32,24 @@ const parseError = (error: CoSignerError | undefined, t: TFunction): Partial<Rec
});
}

let keysErrorMessage;
if (error.keys === CoSignerErrorKeys.Required) {
keysErrorMessage = t('sharedWallets.addSharedWallet.addCosigners.keysInputError.required');
let keyErrorMessage;
if (error.sharedWalletKey === CoSignerErrorKeys.Required) {
keyErrorMessage = t('sharedWallets.addSharedWallet.addCosigners.keyInputError.required');
}
if (error.keys === CoSignerErrorKeys.Invalid) {
keysErrorMessage = t('sharedWallets.addSharedWallet.addCosigners.keysInputError.invalid');
if (error.sharedWalletKey === CoSignerErrorKeys.Invalid) {
keyErrorMessage = t('sharedWallets.addSharedWallet.addCosigners.keyInputError.invalid');
}

return {
keys: keysErrorMessage,
name: nameErrorMessage,
sharedWalletKey: keyErrorMessage,
};
};

export const AddCoSignerInput = ({
dirty,
onChange,
keysFieldDisabled,
keyFieldDisabled,
labels,
value,
error,
Expand All @@ -74,12 +74,12 @@ export const AddCoSignerInput = ({
</Box>
<Box>
<TextBox
label={labels.keys}
value={value.keys}
errorMessage={(dirty?.keys && errorMessage.keys) || undefined}
onChange={makeChangeHandler('keys')}
label={labels.sharedWalletKey}
value={value.sharedWalletKey}
errorMessage={(dirty?.sharedWalletKey && errorMessage.sharedWalletKey) || undefined}
onChange={makeChangeHandler('sharedWalletKey')}
w="$fill"
disabled={keysFieldDisabled}
disabled={keyFieldDisabled}
/>
</Box>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export default meta;

export const Overview = (): JSX.Element => {
const [coSigners, setCoSigners] = useState<CoSigner[]>([
{ id: uuid(), keys: '', name: '' },
{ id: uuid(), keys: '', name: '' },
{ id: uuid(), name: '', sharedWalletKey: '' },
{ id: uuid(), name: '', sharedWalletKey: '' },
]);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const AddCoSigners = ({
coSignersDirty,
}: Props): JSX.Element => {
const { t } = useTranslation();
const atLeastOneValidCoSigner = coSigners.some((c) => c.keys && c.name) && errors.length === 0;
const atLeastOneValidCoSigner = coSigners.some((c) => c.sharedWalletKey && c.name) && errors.length === 0;

return (
<SharedWalletLayout
Expand All @@ -45,16 +45,16 @@ export const AddCoSigners = ({
<AddCoSignerInput
value={value}
onChange={onValueChange}
keysFieldDisabled={index === indexOfCoSignersDataOfCurrentUser}
keyFieldDisabled={index === indexOfCoSignersDataOfCurrentUser}
labels={{
keys: t(
name: t(
`sharedWallets.addSharedWallet.addCosigners.${
index === indexOfCoSignersDataOfCurrentUser ? 'yourKeysInputLabel' : 'coSignerKeysInputLabel'
index === indexOfCoSignersDataOfCurrentUser ? 'yourNameInputLabel' : 'coSignerNameInputLabel'
}`,
),
name: t(
sharedWalletKey: t(
`sharedWallets.addSharedWallet.addCosigners.${
index === indexOfCoSignersDataOfCurrentUser ? 'yourNameInputLabel' : 'coSignerNameInputLabel'
index === indexOfCoSignersDataOfCurrentUser ? 'yourKeysInputLabel' : 'coSignerKeysInputLabel'
}`,
),
}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export type CoSigner = {
id: string;
keys: string;
name: string;
sharedWalletKey: string;
};

export type CoSignerDirty = {
id: string;
keys: boolean;
name: boolean;
sharedWalletKey: boolean;
};

export enum CoSignerErrorName {
Expand All @@ -23,6 +23,6 @@ export enum CoSignerErrorKeys {

export type CoSignerError = {
id: string;
keys?: CoSignerErrorKeys;
name?: CoSignerErrorName;
sharedWalletKey?: CoSignerErrorKeys;
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ const mockStateShareDetails: CreationFlowState = {
coSigners: [
{
id: '1',
keys: '3a2b5f9e8d2a3b4e5f6c7d8e9f0a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8g',
name: 'Alice',
sharedWalletKey: '3a2b5f9e8d2a3b4e5f6c7d8e9f0a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8g',
},
{
id: '2',
keys: '1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t1u2v3w4x5y6z7a8b9c0d1e2f',
name: 'Bob',
sharedWalletKey: '1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t1u2v3w4x5y6z7a8b9c0d1e2f',
},
],
quorumRules: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const mapStateToSchema = (state: CreationFlowState): SharedWalletSchema => {
case QuorumRadioOption.AllAddresses: {
nativeScript = {
scripts: coSigners.map((coSigner) => ({
pubkey: coSigner.keys,
pubkey: coSigner.sharedWalletKey,
tag: 'pubkey',
})),
tag: 'all',
Expand All @@ -29,7 +29,7 @@ const mapStateToSchema = (state: CreationFlowState): SharedWalletSchema => {
case QuorumRadioOption.Any: {
nativeScript = {
scripts: coSigners.map((coSigner) => ({
pubkey: coSigner.keys,
pubkey: coSigner.sharedWalletKey,
tag: 'pubkey',
})),
tag: 'any',
Expand All @@ -40,7 +40,7 @@ const mapStateToSchema = (state: CreationFlowState): SharedWalletSchema => {
nativeScript = {
n: quorumRules.numberOfCosigner,
scripts: coSigners.map((coSigner) => ({
pubkey: coSigner.keys,
pubkey: coSigner.sharedWalletKey,
tag: 'pubkey',
})),
tag: 'n_of_k',
Expand All @@ -56,7 +56,7 @@ const mapStateToSchema = (state: CreationFlowState): SharedWalletSchema => {
metadata: {
participants: coSigners.map((coSigner) => ({
name: coSigner.name,
publicKey: coSigner.keys,
publicKey: coSigner.sharedWalletKey,
})),
sharedWalletName: walletName || '',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ const getNextCoSignersDirtyValue = ({
dirty.id === action.coSigner.id
? {
id: dirty.id,
keys: matchingPrevCoSigner.keys !== action.coSigner.keys ? true : dirty.keys,
name: matchingPrevCoSigner.name !== action.coSigner.name ? true : dirty.name,
sharedWalletKey:
matchingPrevCoSigner.sharedWalletKey !== action.coSigner.sharedWalletKey ? true : dirty.sharedWalletKey,
}
: dirty,
);
Expand All @@ -96,12 +97,12 @@ const makeStateMachine = ({
exitTheFlow,
navigateToAppHome,
onCreateSharedWallet,
sharedKeys,
sharedWalletKey,
}: {
exitTheFlow: () => void;
navigateToAppHome: () => void;
onCreateSharedWallet: (data: { coSigners: CoSigner[]; name: string; quorumRules: QuorumOptionValue }) => void;
sharedKeys: string;
sharedWalletKey: string;
}): SharedWalletCreationStateMachine => ({
[SharedWalletCreationStep.Setup]: (prevState, action) => {
if (action.type === SharedWalletCreationActionType.CHANGE_WALLET_NAME) {
Expand All @@ -116,10 +117,10 @@ const makeStateMachine = ({
}
if (action.type === SharedWalletCreationActionType.NEXT) {
if (!prevState.walletName) return prevState;
const coSigners = ensureCorrectCoSignersDataShape([createCoSignerObject(sharedKeys)]);
const coSigners = ensureCorrectCoSignersDataShape([createCoSignerObject(sharedWalletKey)]);
return stateCoSigners({
...prevState,
coSignerInputsDirty: coSigners.map(({ id }) => ({ id, keys: false, name: false })),
coSignerInputsDirty: coSigners.map(({ id }) => ({ id, name: false, sharedWalletKey: false })),
coSignerInputsErrors: [],
coSigners,
step: SharedWalletCreationStep.CoSigners,
Expand All @@ -138,8 +139,8 @@ const makeStateMachine = ({
);

if (
prevState.coSigners[indexOfCoSignersDataOfCurrentUser].keys !==
nextCoSigners[indexOfCoSignersDataOfCurrentUser].keys
prevState.coSigners[indexOfCoSignersDataOfCurrentUser].sharedWalletKey !==
nextCoSigners[indexOfCoSignersDataOfCurrentUser].sharedWalletKey
) {
return prevState;
}
Expand All @@ -155,7 +156,7 @@ const makeStateMachine = ({
// fields should stay empty and should not show errors.
// This filter should be removed once we enable the "add cosigner" button
const filteredCosignerErrors = coSignersErrors.filter(
({ id }) => !nextCoSigners.some((c) => c.id === id && !c.keys && !c.name),
({ id }) => !nextCoSigners.some((c) => c.id === id && !c.sharedWalletKey && !c.name),
);

return stateCoSigners({
Expand Down Expand Up @@ -192,7 +193,7 @@ const makeStateMachine = ({
});
}
if (action.type === SharedWalletCreationActionType.NEXT) {
const coSigners = prevState.coSigners.filter((c) => c.keys && c.name);
const coSigners = prevState.coSigners.filter((c) => c.sharedWalletKey && c.name);
const minCoSignersCount = 2;
if (coSigners.length < minCoSignersCount) return prevState;

Expand Down Expand Up @@ -251,7 +252,7 @@ export type SharedWalletCreationStoreSharedProps = {
initialWalletName: string;
navigateToAppHome: () => void;
onCreateSharedWallet: (data: { coSigners: CoSigner[]; name: string; quorumRules: QuorumOptionValue }) => void;
sharedKeys: string;
sharedWalletKey: string;
};

export type SharedWalletCreationStoreProps = SharedWalletCreationStoreSharedProps & {
Expand All @@ -265,7 +266,7 @@ export const SharedWalletCreationStore = ({
initialWalletName,
navigateToAppHome,
onCreateSharedWallet,
sharedKeys,
sharedWalletKey,
}: SharedWalletCreationStoreProps): ReactElement => {
const initialState = useInitialState(makeInitialState(activeWalletName));
const [state, dispatch] = useReducer(
Expand All @@ -274,7 +275,7 @@ export const SharedWalletCreationStore = ({
exitTheFlow,
navigateToAppHome,
onCreateSharedWallet,
sharedKeys,
sharedWalletKey,
});
const handler = stateMachine[prevState.step] as Handler<CreationFlowState>;
return handler(prevState, action);
Expand Down
Loading

0 comments on commit 9c042b2

Please sign in to comment.