1+ import type { Network } from 'bitcoin-address-validation'
12import type { TFunction } from 'i18next'
23import * as yup from 'yup'
34import {
@@ -15,7 +16,7 @@ import {
1516} from '@/constants/jam'
1617import { JM_NG_DEFAULT_TUMBLER_PARAMS , type TumblerParameters } from '@/constants/jm'
1718import type { AddressSummary } from '@/context/JamWalletInfoContext'
18- import { isValidAddress } from '@/lib/formValidation'
19+ import { isAddressOnNetwork , isValidAddress } from '@/lib/formValidation'
1920import { factorToPercentage , isValidNumber , percentageToFactor , pseudoRandomInteger } from '@/lib/utils'
2021import type { Seconds } from '@/types/global'
2122import { buildDestinationErrors , normalizeDestinationAddresses } from './destinationValidation'
@@ -115,14 +116,17 @@ export const sweepFormSchema = (
115116 minNumberOfDestinations,
116117 maxNumberOfDestinations,
117118 addressSummary,
119+ network,
118120 } : {
119121 minNumberOfDestinations : number
120122 maxNumberOfDestinations : number
121123 addressSummary : AddressSummary
124+ network : Network
122125 } ,
123126 t : TFunction < 'translation' , undefined > ,
124127) : yup . ObjectSchema < SweepFormValues > => {
125128 const invalidDestinationAddressMessage = t ( 'scheduler.feedback_invalid_destination_address' )
129+ const networkMismatchDestinationAddressMessage = t ( 'scheduler.feedback_destination_network_mismatch' )
126130 const invalidNumberOfDestinationsMessage = t ( 'send.feedback_invalid_number_of_destination_addresses' , {
127131 // TODO: i18n
128132 defaultValue : 'Please provide between {{ min }} and {{ max }} destination addresses.' ,
@@ -137,20 +141,20 @@ export const sweepFormSchema = (
137141 . of (
138142 yup
139143 . object ( {
140- // TODO: use formValidation#destinationAddressField ?
141144 address : yup
142145 . string ( )
143146 . transform ( ( _ , originalValue : unknown ) =>
144147 typeof originalValue === 'string' ? normalizeDestinationAddresses ( [ originalValue ] ) [ 0 ] : '' ,
145148 )
146149 . defined ( )
147- . test ( 'valid-sweep-destination' , invalidDestinationAddressMessage , function ( value ) {
148- if ( ! isValidAddress ( value ) ) {
149- return false
150- }
151-
152- return true
153- } ) ,
150+ . test ( 'valid-sweep-destination' , invalidDestinationAddressMessage , ( value ) => isValidAddress ( value ) )
151+ // Only run once the address itself is valid, so an invalid address surfaces a
152+ // single, correct error instead of also reporting a network mismatch.
153+ . test (
154+ 'sweep-destination-network-mismatch' ,
155+ networkMismatchDestinationAddressMessage ,
156+ ( value ) => ! isValidAddress ( value ) || isAddressOnNetwork ( value , network ) ,
157+ ) ,
154158 } )
155159 . required ( ) ,
156160 )
0 commit comments