Skip to content

Commit

Permalink
Merge pull request #461 from BitGo/v1-sweep
Browse files Browse the repository at this point in the history
feat: add function types, parseBitGoV1Unspents function
  • Loading branch information
rushilbg authored Jul 19, 2024
2 parents faf1a8f + 5a74bc9 commit d94a62a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
4 changes: 3 additions & 1 deletion electron/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import type {
FormattedOfflineVaultTxInfo,
RecoverFromWrongChainOptions,
RecoverParams,
V1SweepParams,
} from '@bitgo/abstract-utxo';
import { v1Sweep } from '@bitgo/abstract-utxo';
import type {
ConsolidationRecoveryOptions as TrxConsolidationRecoveryOptions,
ConsolidationRecoveryBatch as TrxConsolidationRecoveryBatch,
Expand Down Expand Up @@ -47,7 +49,7 @@ type Commands = {
| createSolBroadcastableSweepTransactionParameters
): Promise<Error | BroadcastableSweepTransaction>;
unlock(otp: string);
sweepV1(coin: string, parameters);
sweepV1(coin: string, parameters: V1SweepParams): ReturnType<typeof v1Sweep>;
recoverConsolidations(
coin: string,
params:
Expand Down
18 changes: 15 additions & 3 deletions src/containers/V1BtcSweep/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import {useNavigate, useParams} from 'react-router-dom';
import {useAlertBanner} from '~/contexts';
import {safeEnv} from '~/helpers';
import {assert, safeEnv} from '~/helpers';
import {V1BtcSweepForm} from "~/containers/V1BtcSweep/V1BtcSweepForm";
import { BitGoV1Unspent } from "@bitgo/abstract-utxo";

function parseBitGoV1Unspents(input: string): BitGoV1Unspent[] {
const unspentsArray: Array<BitGoV1Unspent> = JSON.parse(input);
assert(unspentsArray.length > 0, 'Unspents file is empty');

unspentsArray.forEach((unspent, index) => {
assert(unspent.tx_hash !== undefined, `Unspent at index ${index} is missing tx_hash`);
assert(unspent.tx_output_n !== undefined, `Unspent at index ${index} is missing tx_output_n`);
assert(unspent.value !== undefined, `Unspent at index ${index} is missing value`);
});

return unspentsArray;
}

export function V1BtcSweep() {
const {env} = useParams<'env'>();
Expand All @@ -15,7 +27,7 @@ export function V1BtcSweep() {

return (
<V1BtcSweepForm
onSubmit={async (values, {setFieldError, setSubmitting}) => {
onSubmit={async (values, {setSubmitting}) => {
if (!values.unspents) {
setAlert('Unspents file is required');
return;
Expand All @@ -34,7 +46,7 @@ export function V1BtcSweep() {
fileReader.readAsText(values.unspents, 'UTF-8');
fileReader.onload = async event => {
try {
const unspents = JSON.parse(event.target?.result as string) as BitGoV1Unspent[];
const unspents = parseBitGoV1Unspents(event.target?.result as string);
setSubmitting(true);
await window.commands.unlock(values.otp);
const chainData = await window.queries.getChain(coin);
Expand Down

0 comments on commit d94a62a

Please sign in to comment.