From 5a74bc946fa21ef77e402c01480bc3d23a6c417c Mon Sep 17 00:00:00 2001 From: Rushil Kapoor Date: Fri, 19 Jul 2024 14:16:14 +0530 Subject: [PATCH] feat: add function types, parseBitGoV1Unspents function BTC-1236 --- electron/preload/index.ts | 4 +++- src/containers/V1BtcSweep/index.tsx | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/electron/preload/index.ts b/electron/preload/index.ts index 46c20599..9c7c05ab 100644 --- a/electron/preload/index.ts +++ b/electron/preload/index.ts @@ -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, @@ -47,7 +49,7 @@ type Commands = { | createSolBroadcastableSweepTransactionParameters ): Promise; unlock(otp: string); - sweepV1(coin: string, parameters); + sweepV1(coin: string, parameters: V1SweepParams): ReturnType; recoverConsolidations( coin: string, params: diff --git a/src/containers/V1BtcSweep/index.tsx b/src/containers/V1BtcSweep/index.tsx index 1a64eab1..87f63635 100644 --- a/src/containers/V1BtcSweep/index.tsx +++ b/src/containers/V1BtcSweep/index.tsx @@ -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 = 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'>(); @@ -15,7 +27,7 @@ export function V1BtcSweep() { return ( { + onSubmit={async (values, {setSubmitting}) => { if (!values.unspents) { setAlert('Unspents file is required'); return; @@ -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);