import { getPhoneHash as baseGetPhoneHash , isE164Number , ParsedPhoneNumber , } from '@celo/base/lib/phoneNumbers' ; import CountryData from 'country-data' ; import { PhoneNumber , PhoneNumberFormat , PhoneNumberType , PhoneNumberUtil , } from 'google-libphonenumber' ; import { soliditySha3 } from 'web3-utils' ; export { anonymizedPhone , isE164Number , ParsedPhoneNumber } from '@celo/base/lib/phoneNumbers' ; const sha3 = ( v ) : string | null => soliditySha3 ( { type : 'string' , value : v } ) ; export const getPhoneHash = ( phoneNumber , salt ? ) : string => { return baseGetPhoneHash ( sha3 , phoneNumber , salt ) ; } ; const phoneUtil = PhoneNumberUtil . getInstance ( ) ; const MIN_PHONE_LENGTH = 4 ; deprecated celo export function getCountryEmoji ( e164PhoneNumber , countryCodePossible ? , regionCodePossible ? ) { let countryCode ; let regionCode ; countryCode = countryCodePossible ; regionCode = regionCodePossible ; if ( ! countryCode || ! regionCode ) { countryCode = getCountryCode ( e164PhoneNumber ) ; regionCode = getRegionCode ( e164PhoneNumber ) ; } const countries = CountryData . lookup . countries ( { countryCallingCodes : `+${ countryCode }` } ) ; const userCountryArray = countries . filter ( ( c ) => c . alpha2 === regionCode ) ; const country = userCountryArray . length > 0 ? userCountryArray [ 0 ] : undefined ; return country ? country . emoji : '' ; } deprecated celo export function getCountryCode ( e164PhoneNumber ) { if ( ! e164PhoneNumber ) { return null ; } try { return phoneUtil . parse ( e164PhoneNumber ) . getCountryCode ( ) ; } catch ( error ) { console . debug ( `getCountryCode, number: ${ e164PhoneNumber }, error: ${ error }` ) ; return null ; } } deprecated celo export function getRegionCode ( e164PhoneNumber ) { if ( ! e164PhoneNumber ) { return null ; } try { return phoneUtil . getRegionCodeForNumber ( phoneUtil . parse ( e164PhoneNumber ) ) ; } catch ( error ) { console . debug ( `getRegionCodeForNumber, number: ${ e164PhoneNumber }, error: ${ error }` ) ; return null ; } } deprecated celo export function getRegionCodeFromCountryCode ( countryCode ) { if ( ! countryCode ) { return null ; } try { return phoneUtil . getRegionCodeForCountryCode ( parseInt ( countryCode , 10 ) ) ; } catch ( error ) { console . debug ( `getRegionCodeFromCountryCode, countrycode: ${ countryCode }, error: ${ error }` ) ; return null ; } } deprecated celo export function getDisplayPhoneNumber ( phoneNumber , defaultCountryCode ) { const phoneDetails = parsePhoneNumber ( phoneNumber , defaultCountryCode ) ; if ( phoneDetails ) { return phoneDetails . displayNumber ; } else { return phoneNumber ; } } deprecated celo export function getDisplayNumberInternational ( e164PhoneNumber ) { const countryCode = getCountryCode ( e164PhoneNumber ) ; const phoneDetails = parsePhoneNumber ( e164PhoneNumber , ( countryCode || '' ) . toString ( ) ) ; if ( phoneDetails ) { return phoneDetails . displayNumberInternational ; } else { return e164PhoneNumber ; } } deprecated celo export function getE164DisplayNumber ( e164PhoneNumber ) { const countryCode = getCountryCode ( e164PhoneNumber ) ; return getDisplayPhoneNumber ( e164PhoneNumber , ( countryCode || '' ) . toString ( ) ) ; } deprecated celo export function getE164Number ( phoneNumber , defaultCountryCode ) { const phoneDetails = parsePhoneNumber ( phoneNumber , defaultCountryCode ) ; if ( phoneDetails && isE164Number ( phoneDetails . e164Number ) ) { return phoneDetails . e164Number ; } else { return null ; } } deprecated celo export function isE164NumberStrict ( phoneNumber ) { try { const parsedPhoneNumber = phoneUtil . parse ( phoneNumber ) ; if ( ! phoneUtil . isValidNumber ( parsedPhoneNumber ) ) { return false ; } return phoneUtil . format ( parsedPhoneNumber , PhoneNumberFormat . E164 ) === phoneNumber ; } catch { return false ; } } deprecated celo export function parsePhoneNumber ( phoneNumberRaw , defaultCountryCode ? ) { try { if ( ! phoneNumberRaw || phoneNumberRaw . length < MIN_PHONE_LENGTH ) { return null ; } const defaultRegionCode = defaultCountryCode ? getRegionCodeFromCountryCode ( defaultCountryCode ) : null ; const parsedNumberUnfixed = phoneUtil . parse ( phoneNumberRaw , defaultRegionCode || undefined ) ; const parsedCountryCode = parsedNumberUnfixed . getCountryCode ( ) ; const parsedRegionCode = phoneUtil . getRegionCodeForNumber ( parsedNumberUnfixed ) ; const parsedNumber = handleSpecialCasesForParsing ( parsedNumberUnfixed , parsedCountryCode , parsedRegionCode ) ; if ( ! parsedNumber ) { return null ; } const isValid = phoneUtil . isValidNumberForRegion ( parsedNumber , parsedRegionCode ) ; return isValid ? { e164Number : phoneUtil . format ( parsedNumber , PhoneNumberFormat . E164 ) , displayNumber : handleSpecialCasesForDisplay ( parsedNumber , parsedCountryCode ) , displayNumberInternational : phoneUtil . format ( parsedNumber , PhoneNumberFormat . INTERNATIONAL ) , countryCode : parsedCountryCode , regionCode : parsedRegionCode } : null ; } catch ( error ) { console . debug ( `phoneNumbers/parsePhoneNumber/Failed to parse phone number, error: ${ error }` ) ; return null ; } } function handleSpecialCasesForParsing ( parsedNumber , countryCode ? , regionCode ? ) { if ( ! countryCode || ! regionCode ) { return parsedNumber ; } switch ( countryCode ) { case 54 : return prependToFormMobilePhoneNumber ( parsedNumber , regionCode , '9' ) ; default : return parsedNumber ; } } deprecated celo function handleSpecialCasesForDisplay ( parsedNumber , countryCode ? ) { switch ( countryCode ) { case 54 : return phoneUtil . format ( parsedNumber , PhoneNumberFormat . INTERNATIONAL ) . replace ( /\+54(\s)?/ , '' ) ; case 231 : const formatted = phoneUtil . format ( parsedNumber , PhoneNumberFormat . NATIONAL ) ; return formatted && formatted [ 0 ] === '0' ? formatted . slice ( 1 ) : formatted ; default : return phoneUtil . format ( parsedNumber , PhoneNumberFormat . NATIONAL ) ; } } /**
 * Some countries require a prefix before the area code depending on if the number is
 * mobile vs landline and international vs national
 */ deprecated celo function prependToFormMobilePhoneNumber ( parsedNumber , regionCode , prefix ) { if ( phoneUtil . getNumberType ( parsedNumber ) === PhoneNumberType . MOBILE ) { return parsedNumber ; } let nationalNumber = phoneUtil . format ( parsedNumber , PhoneNumberFormat . NATIONAL ) ; if ( nationalNumber . charAt ( 0 ) === '0' ) { nationalNumber = nationalNumber . slice ( 1 ) ; } if ( nationalNumber . startsWith ( prefix ) ) { return null ; } const adjustedNumber = phoneUtil . parse ( prefix + nationalNumber , regionCode ) ; return phoneUtil . getNumberType ( adjustedNumber ) === PhoneNumberType . MOBILE ? adjustedNumber : null ; } deprecated celo export function getExampleNumber ( regionCode , useOnlyZeroes = true , isInternational = false ) { const examplePhone = phoneUtil . getExampleNumber ( getRegionCodeFromCountryCode ( regionCode ) as string ) ; if ( ! examplePhone ) { return ; } const formatedExample = phoneUtil . format ( examplePhone , isInternational ? PhoneNumberFormat . INTERNATIONAL : PhoneNumberFormat . NATIONAL ) ; if ( useOnlyZeroes ) { if ( isInternational ) { return formatedExample . replace ( /(^\+[0-9]{1,3} |[0-9])/g , ( value , _ , i ) => ( i ? '0' : value ) ) ; } return formatedExample . replace ( /[0-9]/g , '0' ) ; } return formatedExample ; } export const PhoneNumberUtils = { getPhoneHash , getCountryCode , getRegionCode , getDisplayPhoneNumber , getE164Number , isE164Number , parsePhoneNumber } ;