All files / app/utils/validators stx-address-validator.ts

0% Statements 0/18
0% Branches 0/3
0% Functions 0/4
0% Lines 0/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36                                                                       
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import { validateAddressChain } from '../../crypto/validate-address-net';
import { validateStacksPrincipal } from '../get-stx-transfer-direction';
import { NETWORK } from '@constants/index';
import * as yup from 'yup';
 
// accepts standard and contract addresses
export function stxPrincipalSchema() {
  let timer = 0;
  return yup
    .string()
    .defined('Must define a STX address')
    .test({
      name: 'address-validation',
      async test(value: any, context) {
        return new Promise(resolve => {
          clearTimeout(timer);
          timer = window.setTimeout(() => {
            Iif (!value) return resolve(false);
            const valid = validateStacksPrincipal(value);
 
            Iif (!valid) {
              return resolve(
                context.createError({ message: 'Input address is not a valid STX address' })
              );
            }
            Iif (!validateAddressChain(value)) {
              return resolve(context.createError({ message: `Must use a ${NETWORK} STX address` }));
            }
            return resolve(true);
          }, 400);
        });
      },
    });
}