Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
69 changes: 50 additions & 19 deletions src/containers/BuildUnsignedConsolidation/GenericEcdsaForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import {
FormikTextarea,
FormikTextfield,
} from '~/components';
import { getWalletTypeLabels, WalletType } from './useWalletTypeLabels';
import { WalletTypeSelector } from './WalletTypeSelector';

const validationSchema = Yup.object({
userKey: Yup.string(),
backupKey: Yup.string(),
const getValidationSchema = (walletType: WalletType) => Yup.object({
walletType: Yup.string().oneOf(['cold', 'hot']).required(),
userKey: Yup.string().required(),
backupKey: Yup.string().required(),
bitgoKey: Yup.string().required(),
walletPassphrase: Yup.string(),
walletPassphrase: walletType === 'hot' ? Yup.string().required() : Yup.string(),
startingScanIndex: Yup.number(),
endingScanIndex: Yup.number().moreThan(
Yup.ref('startingScanIndex'),
Expand All @@ -21,7 +24,7 @@ const validationSchema = Yup.object({
seed: Yup.string(),
}).required();

export type EcdsaFormValues = Yup.Asserts<typeof validationSchema>;
export type EcdsaFormValues = Yup.Asserts<ReturnType<typeof getValidationSchema>>;

export type GenericEcdsaFormValues = {
onSubmit: (
Expand All @@ -34,6 +37,7 @@ export function GenericEcdsaForm({ onSubmit }: GenericEcdsaFormValues) {
const formik = useFormik<EcdsaFormValues>({
onSubmit,
initialValues: {
Copy link
Contributor

Choose a reason for hiding this comment

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

add walletType as the form state, and default to cold. That way you won't need an extra state, and you won't need to add a hook, it could be a straight forward function (useWalletTypeLabels)

walletType: 'cold' as WalletType,
userKey: '',
backupKey: '',
bitgoKey: '',
Expand All @@ -42,18 +46,43 @@ export function GenericEcdsaForm({ onSubmit }: GenericEcdsaFormValues) {
endingScanIndex: 21,
seed: undefined,
},
validate: (values) => {
try {
getValidationSchema(values.walletType as WalletType).validateSync(values, { abortEarly: false });
return {};
} catch (error: any) {
const errors: Record<string, string> = {};
error.inner?.forEach((err: any) => {
if (err.path) {
errors[err.path] = err.message;
}
});
return errors;
}
},
Copy link
Contributor

Choose a reason for hiding this comment

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

do you need this?

});

const {
userKeyLabel,
userKeyHelperText,
backupKeyLabel,
backupKeyHelperText,
bitgoKeyLabel,
bitgoKeyHelperText,
showWalletPassphrase,
} = getWalletTypeLabels(formik.values.walletType as WalletType);

return (
<FormikProvider value={formik}>
<Form>
<h4 className="tw-text-body tw-font-semibold tw-border-b-0.5 tw-border-solid tw-border-gray-700 tw-mb-4">
Self-managed cold or Hot wallet details
</h4>
<WalletTypeSelector />
<div className="tw-mb-4">
<FormikTextarea
HelperText="Your user public key, as found on your recovery KeyCard. Required for hot wallets."
Label="User Public Key (optional)"
HelperText={userKeyHelperText}
Label={userKeyLabel}
name="userKey"
Width="fill"
/>
Expand All @@ -68,28 +97,30 @@ export function GenericEcdsaForm({ onSubmit }: GenericEcdsaFormValues) {
</div>
<div className="tw-mb-4">
<FormikTextarea
HelperText="The backup public key for the wallet, as found on your recovery KeyCard. Required for hot wallets."
Label="Backup Public Key (optional)"
HelperText={backupKeyHelperText}
Label={backupKeyLabel}
name="backupKey"
Width="fill"
/>
</div>
<div className="tw-mb-4">
<FormikPasswordfield
HelperText="Your wallet passphrase, required for hot wallets."
Label="Wallet Passphrase (optional)"
name="walletPassphrase"
Width="fill"
/>
</div>
<div className="tw-mb-4">
<FormikTextfield
HelperText="The BitGo public key for the wallet, as found on your recovery KeyCard."
Label="BitGo Public Key"
HelperText={bitgoKeyHelperText}
Label={bitgoKeyLabel}
name="bitgoKey"
Width="fill"
/>
</div>
{showWalletPassphrase && (
<div className="tw-mb-4">
<FormikPasswordfield
HelperText="The wallet passphrase that you set when creating the wallet."
Label="Wallet Passphrase"
name="walletPassphrase"
Width="fill"
/>
</div>
)}
<div className="tw-mb-4">
<FormikTextfield
HelperText="The starting index (inclusive) of addresses to consolidate"
Expand Down
69 changes: 50 additions & 19 deletions src/containers/BuildUnsignedConsolidation/SolForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import {
FormikTextarea,
FormikTextfield,
} from '~/components';
import { getWalletTypeLabels, WalletType } from './useWalletTypeLabels';
import { WalletTypeSelector } from './WalletTypeSelector';

const validationSchema = Yup.object({
userKey: Yup.string(),
backupKey: Yup.string(),
const getValidationSchema = (walletType: WalletType) => Yup.object({
walletType: Yup.string().oneOf(['cold', 'hot']).required(),
userKey: Yup.string().required(),
backupKey: Yup.string().required(),
bitgoKey: Yup.string().required(),
walletPassphrase: Yup.string(),
walletPassphrase: walletType === 'hot' ? Yup.string().required() : Yup.string(),
apiKey: Yup.string().test(
'not-url-or-alchemy',
'API key should not be a URL',
Expand All @@ -35,7 +38,7 @@ const validationSchema = Yup.object({
seed: Yup.string(),
}).required();

export type SolFormValues = Yup.Asserts<typeof validationSchema>;
export type SolFormValues = Yup.Asserts<ReturnType<typeof getValidationSchema>>;

export type SolFormProps = {
onSubmit: (
Expand All @@ -48,6 +51,7 @@ export function SolForm({ onSubmit }: SolFormProps) {
const formik = useFormik<SolFormValues>({
onSubmit,
initialValues: {
walletType: 'cold' as WalletType,
userKey: '',
backupKey: '',
bitgoKey: '',
Expand All @@ -61,18 +65,43 @@ export function SolForm({ onSubmit }: SolFormProps) {
endingScanIndex: 21,
seed: undefined,
},
validate: (values) => {
try {
getValidationSchema(values.walletType as WalletType).validateSync(values, { abortEarly: false });
return {};
} catch (error: any) {
const errors: Record<string, string> = {};
error.inner?.forEach((err: any) => {
if (err.path) {
errors[err.path] = err.message;
}
});
return errors;
}
},
});
Copy link
Contributor

Choose a reason for hiding this comment

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

same here.


const {
userKeyLabel,
userKeyHelperText,
backupKeyLabel,
backupKeyHelperText,
bitgoKeyLabel,
bitgoKeyHelperText,
showWalletPassphrase,
} = getWalletTypeLabels(formik.values.walletType as WalletType);

return (
<FormikProvider value={formik}>
<Form>
<h4 className="tw-text-body tw-font-semibold tw-border-b-0.5 tw-border-solid tw-border-gray-700 tw-mb-4">
Self-managed cold or Hot wallet details
</h4>
<WalletTypeSelector />
<div className="tw-mb-4">
<FormikTextarea
HelperText="Your user public key, as found on your recovery KeyCard. Required for hot wallets."
Label="User Public Key (optional)"
HelperText={userKeyHelperText}
Label={userKeyLabel}
name="userKey"
Width="fill"
/>
Expand All @@ -87,20 +116,12 @@ export function SolForm({ onSubmit }: SolFormProps) {
</div>
<div className="tw-mb-4">
<FormikTextarea
HelperText="The backup public key for the wallet, as found on your recovery KeyCard. Required for hot wallets."
Label="Backup Public Key (optional)"
HelperText={backupKeyHelperText}
Label={backupKeyLabel}
name="backupKey"
Width="fill"
/>
</div>
<div className="tw-mb-4">
<FormikPasswordfield
HelperText="Your wallet passphrase, required for hot wallets."
Label="Wallet Passphrase (optional)"
name="walletPassphrase"
Width="fill"
/>
</div>
<div className="tw-mb-4">
<FormikTextarea
HelperText="Public Keys for your durable Nonces"
Expand All @@ -120,12 +141,22 @@ export function SolForm({ onSubmit }: SolFormProps) {
</div>
<div className="tw-mb-4">
<FormikTextfield
HelperText="The BitGo public key for the wallet, as found on your recovery KeyCard."
Label="BitGo Public Key"
HelperText={bitgoKeyHelperText}
Label={bitgoKeyLabel}
name="bitgoKey"
Width="fill"
/>
</div>
{showWalletPassphrase && (
<div className="tw-mb-4">
<FormikPasswordfield
HelperText="The wallet passphrase that you set when creating the wallet."
Label="Wallet Passphrase"
name="walletPassphrase"
Width="fill"
/>
</div>
)}
<div className="tw-mb-4">
<FormikTextfield
HelperText="The starting index (inclusive) of addresses to consolidate"
Expand Down
69 changes: 50 additions & 19 deletions src/containers/BuildUnsignedConsolidation/SolTokenForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import {
FormikTextarea,
FormikTextfield,
} from '~/components';
import { getWalletTypeLabels, WalletType } from './useWalletTypeLabels';
import { WalletTypeSelector } from './WalletTypeSelector';

const validationSchema = Yup.object({
userKey: Yup.string(),
backupKey: Yup.string(),
const getValidationSchema = (walletType: WalletType) => Yup.object({
walletType: Yup.string().oneOf(['cold', 'hot']).required(),
userKey: Yup.string().required(),
backupKey: Yup.string().required(),
bitgoKey: Yup.string().required(),
walletPassphrase: Yup.string(),
walletPassphrase: walletType === 'hot' ? Yup.string().required() : Yup.string(),
apiKey: Yup.string().test(
'not-url-or-alchemy',
'API key should not be a URL',
Expand All @@ -38,7 +41,7 @@ const validationSchema = Yup.object({
seed: Yup.string(),
}).required();

export type SolFormValues = Yup.Asserts<typeof validationSchema>;
export type SolFormValues = Yup.Asserts<ReturnType<typeof getValidationSchema>>;

export type SolFormProps = {
onSubmit: (
Expand All @@ -51,6 +54,7 @@ export function SolTokenForm({ onSubmit }: SolFormProps) {
const formik = useFormik<SolFormValues>({
onSubmit,
initialValues: {
walletType: 'cold' as WalletType,
userKey: '',
backupKey: '',
bitgoKey: '',
Expand All @@ -66,18 +70,43 @@ export function SolTokenForm({ onSubmit }: SolFormProps) {
tokenProgramId: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
seed: undefined,
},
validate: (values) => {
try {
getValidationSchema(values.walletType as WalletType).validateSync(values, { abortEarly: false });
return {};
} catch (error: any) {
const errors: Record<string, string> = {};
error.inner?.forEach((err: any) => {
if (err.path) {
errors[err.path] = err.message;
}
});
return errors;
}
},
});

const {
userKeyLabel,
userKeyHelperText,
backupKeyLabel,
backupKeyHelperText,
bitgoKeyLabel,
bitgoKeyHelperText,
showWalletPassphrase,
} = getWalletTypeLabels(formik.values.walletType as WalletType);

return (
<FormikProvider value={formik}>
<Form>
<h4 className="tw-text-body tw-font-semibold tw-border-b-0.5 tw-border-solid tw-border-gray-700 tw-mb-4">
Self-managed cold or Hot wallet details
</h4>
<WalletTypeSelector />
<div className="tw-mb-4">
<FormikTextarea
HelperText="Your user public key, as found on your recovery KeyCard. Required for hot wallets."
Label="User Public Key (optional)"
HelperText={userKeyHelperText}
Label={userKeyLabel}
name="userKey"
Width="fill"
/>
Expand All @@ -92,20 +121,12 @@ export function SolTokenForm({ onSubmit }: SolFormProps) {
</div>
<div className="tw-mb-4">
<FormikTextarea
HelperText="The backup public key for the wallet, as found on your recovery KeyCard. Required for hot wallets."
Label="Backup Public Key (optional)"
HelperText={backupKeyHelperText}
Label={backupKeyLabel}
name="backupKey"
Width="fill"
/>
</div>
<div className="tw-mb-4">
<FormikPasswordfield
HelperText="Your wallet passphrase, required for hot wallets."
Label="Wallet Passphrase (optional)"
name="walletPassphrase"
Width="fill"
/>
</div>
<div className="tw-mb-4">
<FormikTextarea
HelperText="Public Keys for your durable Nonces"
Expand All @@ -125,12 +146,22 @@ export function SolTokenForm({ onSubmit }: SolFormProps) {
</div>
<div className="tw-mb-4">
<FormikTextfield
HelperText="The BitGo public key for the wallet, as found on your recovery KeyCard."
Label="BitGo Public Key"
HelperText={bitgoKeyHelperText}
Label={bitgoKeyLabel}
name="bitgoKey"
Width="fill"
/>
</div>
{showWalletPassphrase && (
<div className="tw-mb-4">
<FormikPasswordfield
HelperText="The wallet passphrase that you set when creating the wallet."
Label="Wallet Passphrase"
name="walletPassphrase"
Width="fill"
/>
</div>
)}
<div className="tw-mb-4">
<FormikTextfield
HelperText="The address of the smart contract of the token to consolidate. This is unique to each token, and is NOT your wallet address."
Expand Down
Loading