import { PUBLIC_KEY_BYTES_LENGTH , ADA , AddressType , CurveType , ASSET_NAME_LENGTH , POLICY_ID_LENGTH , CatalystDataIndexes , CatalystSigIndexes , SUCCESS_OPERATION_STATE , INVALID_OPERATION_STATE , OPERATIONS_STATUSES , OPERATION_TYPES } from './constants' ; import { ErrorFactory } from './errors' ; import { hexStringToBuffer , isEmptyHexString } from './formatters' ; import CardanoWasm from 'cardano-serialization-lib' ; import { CoinIdentifier } from '../models' ; const tokenNameValidation = new RegExp ( `^[0-9a-fA-F]{0,${ ASSET_NAME_LENGTH }}$` ) ; const policyIdValidation = new RegExp ( `^[0-9a-fA-F]{${ POLICY_ID_LENGTH }}$` ) ; export const VALID_CURVE_TYPE = CurveType . edwards25519 ; export const isKeyValid = ( publicKeyBytes , curveType ) : boolean => publicKeyBytes . length === PUBLIC_KEY_BYTES_LENGTH && curveType === VALID_CURVE_TYPE ; export const isAddressTypeValid = ( type ) : boolean => [ ... Object . values ( AddressType ) , '' , undefined ] . includes ( type ) ; export const isTokenNameValid = ( name ) : boolean => tokenNameValidation . test ( name ) || isEmptyHexString ( name ) ; export const isPolicyIdValid = ( policyId ) : boolean => policyIdValidation . test ( policyId ) ; export const validateCurrencies = ( currencies ) : void => { currencies . forEach ( ( { symbol , metadata } ) => { if ( ! isTokenNameValid ( symbol ) ) throw ErrorFactory . invalidTokenNameError ( `Given name is ${ symbol }` ) ; if ( symbol !== ADA && ! isPolicyIdValid ( metadata . policyId ) ) throw ErrorFactory . invalidPolicyIdError ( `Given policy id is ${ metadata . policyId }` ) ; } ) ; } ; export const isEd25519KeyHash = ( hash ) : boolean => { let edd25519Hash ; try { edd25519Hash = CardanoWasm . Ed25519KeyHash . from_bytes ( Buffer . from ( hash , 'hex' ) ) ; } catch ( error ) { return false ; } return ! ! edd25519Hash ; } ; export const isEd25519Signature = ( hash ) : boolean => { let ed25519Signature ; try { ed25519Signature = CardanoWasm . Ed25519Signature . from_bytes ( hexStringToBuffer ( hash ) ) ; } catch ( error ) { return false ; } return ! ! ed25519Signature ; } ; export const isVoteDataValid = ( jsonObject ) : boolean => { const isObject = typeof jsonObject === 'object' ; const dataIndexes = Object . keys ( CatalystDataIndexes ) . filter ( key => parseInt ( key ) > 0 ) ; return isObject && dataIndexes . every ( index => index in jsonObject ) ; } ; export const isVoteSignatureValid = ( jsonObject ) : boolean => { const isObject = typeof jsonObject === 'object' ; const dataIndexes = Object . keys ( CatalystSigIndexes ) . filter ( key => parseInt ( key ) > 0 ) ; return isObject && dataIndexes . every ( index => index in jsonObject ) ; } ; const validateStatus = ( status ) : void => { const isValid = OPERATIONS_STATUSES . some ( opStatus => opStatus === status ) ; if ( ! isValid ) { throw ErrorFactory . invalidOperationStatus ( `Given status is ${ status }` ) ; } } ; const getOperationState = ( status ) => { if ( status === SUCCESS_OPERATION_STATE . status ) return SUCCESS_OPERATION_STATE ; return INVALID_OPERATION_STATE ; } ; const validateAndGetOperationState = ( status , success ? ) : Components . Schemas . OperationStatus => { const operationState = getOperationState ( status ) ; if ( success === undefined ) return operationState ; if ( success !== operationState . successful ) throw ErrorFactory . statusAndSuccessMatchError ( ) ; return operationState ; } ; export const validateAndGetStatus = ( status ? , success ? ) : boolean | undefined => { const isStatusUndefined = status === undefined ; const isSuccessUndefined = success === undefined ; if ( isStatusUndefined && isSuccessUndefined ) return ; if ( isStatusUndefined && success !== undefined ) return success ; if ( status !== undefined ) { validateStatus ( status ) ; const operationState = validateAndGetOperationState ( status , success ) ; return operationState . successful ; } } ; export const validateTransactionCoinMatch = ( transactionHash ? , coinIdentifier ? ) : void => { if ( transactionHash && coinIdentifier && transactionHash !== coinIdentifier . hash ) { throw ErrorFactory . txHashAndCoinNotMatchError ( ) ; } } ; export const validateAccountIdAddressMatch = ( address ? , accountIdentifier ? ) : void => { if ( address && accountIdentifier && address !== accountIdentifier . address ) { throw ErrorFactory . addressAndAccountIdNotMatchError ( `Address ${ address } does not match account identifier's address ${ accountIdentifier . address }` ) ; } } ; export const validateOperationType = ( type ) : void => { const isValid = OPERATION_TYPES . some ( opType => opType === type ) ; if ( ! isValid ) { throw ErrorFactory . invalidOperationTypeError ( ) ; } } ;