-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #455 from BitGo/v1-sweep
feat: add flow for sweeping funds from a V1 BTC wallet
- Loading branch information
Showing
7 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { Form, FormikHelpers, FormikProvider, useFormik } from 'formik'; | ||
import { Link } from 'react-router-dom'; | ||
import * as Yup from 'yup'; | ||
import { Button, FormikPasswordfield, FormikTextfield } from '~/components'; | ||
import { FormikFilefield } from "~/components/FormikFilefield"; | ||
|
||
const validationSchema = Yup.object({ | ||
walletId: Yup.string().required(), | ||
walletPassphrase: Yup.string().required(), | ||
unspents: Yup.mixed().required(), | ||
destinationAddress: Yup.string().required(), | ||
encryptedUserKey: Yup.string().required(), | ||
otp: Yup.string().required(), | ||
}).required(); | ||
|
||
export type V1BtcSweepFormProps = { | ||
onSubmit: ( | ||
values: V1BtcSweepFormValues, | ||
formikHelpers: FormikHelpers<V1BtcSweepFormValues> | ||
) => void | Promise<void>; | ||
}; | ||
|
||
type V1BtcSweepFormValues = Yup.Asserts<typeof validationSchema>; | ||
|
||
export function V1BtcSweepForm({onSubmit}: V1BtcSweepFormProps) { | ||
const formik = useFormik<V1BtcSweepFormValues>({ | ||
onSubmit, | ||
initialValues: { | ||
walletId: '', | ||
walletPassphrase: '', | ||
unspents: undefined, | ||
destinationAddress: '', | ||
encryptedUserKey: '', | ||
otp: '', | ||
}, | ||
validationSchema, | ||
}); | ||
|
||
return ( | ||
<FormikProvider value={formik}> | ||
<Form> | ||
<div className="tw-mb-4"> | ||
<FormikTextfield | ||
HelperText={`This is the wallet ID of the wallet from where to sweep.`} | ||
Label="Wallet ID" | ||
name="walletId" | ||
Width="fill" | ||
/> | ||
</div> | ||
<div className="tw-mb-4"> | ||
<FormikPasswordfield | ||
HelperText={`The wallet passphrase of the wallet from where to sweep.`} | ||
Label="Wallet Passphrase" | ||
name="walletPassphrase" | ||
Width="fill" | ||
/> | ||
</div> | ||
<div className="tw-mb-4"> | ||
<FormikTextfield | ||
HelperText={`The encrypted user private key for the wallet.`} | ||
Label="Encrypted User Private Key" | ||
name="encryptedUserKey" | ||
Width="fill" | ||
/> | ||
</div> | ||
<div className="tw-mb-4"> | ||
<FormikTextfield | ||
HelperText={`The address your sweep transaction will send to.`} | ||
Label="Destination Address" | ||
name="destinationAddress" | ||
Width="fill" | ||
/> | ||
</div> | ||
<div className="tw-mb-4"> | ||
<FormikFilefield | ||
name="tx" | ||
accept=".json" | ||
Width="fill" | ||
Label="Upload unspents file" | ||
onChange={event => { | ||
formik | ||
.setFieldValue('unspents', event.currentTarget.files?.[0]) | ||
.catch(console.error); | ||
}} | ||
/> | ||
</div> | ||
<div className="tw-mb-4"> | ||
<FormikTextfield | ||
HelperText={`2FA`} | ||
Label="2FA Code" | ||
name="otp" | ||
Width="fill" | ||
/> | ||
</div> | ||
|
||
<div className="tw-flex tw-flex-col-reverse sm:tw-justify-between sm:tw-flex-row tw-gap-1 tw-mt-4"> | ||
<Button Tag={Link} to="/" Variant="secondary" Width="hug"> | ||
Cancel | ||
</Button> | ||
<Button | ||
Variant="primary" | ||
Width="hug" | ||
type="submit" | ||
Disabled={formik.isSubmitting} | ||
disabled={formik.isSubmitting} | ||
> | ||
{formik.isSubmitting ? 'Sweeping...' : 'Sweep Funds'} | ||
</Button> | ||
</div> | ||
</Form> | ||
</FormikProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import {useNavigate, useParams} from 'react-router-dom'; | ||
import {useAlertBanner} from '~/contexts'; | ||
import {safeEnv} from '~/helpers'; | ||
import {V1BtcSweepForm} from "~/containers/V1BtcSweep/V1BtcSweepForm"; | ||
import { BitGoV1Unspent } from "@bitgo/abstract-utxo"; | ||
|
||
|
||
export function V1BtcSweep() { | ||
const {env} = useParams<'env'>(); | ||
const navigate = useNavigate(); | ||
const [_, setAlert] = useAlertBanner(); | ||
|
||
const environment = safeEnv(env); | ||
const coin = env === 'test' ? 'tbtc' : 'btc'; | ||
|
||
return ( | ||
<V1BtcSweepForm | ||
onSubmit={async (values, {setFieldError, setSubmitting}) => { | ||
if (!values.unspents) { | ||
setAlert('Unspents file is required'); | ||
return; | ||
} | ||
|
||
setAlert(undefined); | ||
const isSdkAuth = await window.queries.isSdkAuthenticated(); | ||
if (!isSdkAuth) { | ||
setAlert('Not logged in'); | ||
setSubmitting(false); | ||
navigate(`/${environment}/v1btc-sweep`); | ||
return; | ||
} | ||
|
||
const fileReader = new FileReader(); | ||
fileReader.readAsText(values.unspents, 'UTF-8'); | ||
fileReader.onload = async event => { | ||
try { | ||
const unspents = JSON.parse(event.target?.result as string) as BitGoV1Unspent[]; | ||
setSubmitting(true); | ||
await window.commands.unlock(values.otp); | ||
const chainData = await window.queries.getChain(coin); | ||
const result = await window.commands.sweepV1(coin, { | ||
walletId: values.walletId, | ||
walletPassphrase: values.walletPassphrase, | ||
recoveryDestination: values.destinationAddress, | ||
userKey: values.encryptedUserKey, | ||
unspents, | ||
}); | ||
setSubmitting(false); | ||
|
||
const showSaveDialogData = await window.commands.showSaveDialog({ | ||
filters: [ | ||
{ | ||
name: 'Custom File Type', | ||
extensions: ['json'], | ||
}, | ||
], | ||
defaultPath: `~/${chainData}-fullsigned-tx-${Date.now()}.json`, | ||
}); | ||
if (!showSaveDialogData.filePath) { | ||
throw new Error('No file path selected'); | ||
} | ||
|
||
await window.commands.writeFile( | ||
showSaveDialogData.filePath, | ||
JSON.stringify(result, null, 2), | ||
{ encoding: 'utf8' } | ||
); | ||
navigate( | ||
`/${environment}/v1btc-sweep/${coin}/success` | ||
); | ||
} catch (error) { | ||
setSubmitting(false); | ||
console.log(error); | ||
if (error instanceof Error) { | ||
setAlert(error.message); | ||
} | ||
} finally { | ||
setSubmitting(false); | ||
} | ||
}; | ||
|
||
}} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters