Skip to content

Commit

Permalink
feat: added phone number parser
Browse files Browse the repository at this point in the history
  • Loading branch information
onmax committed Oct 24, 2024
1 parent d0fbade commit 9533950
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
49 changes: 45 additions & 4 deletions src/lib/RequestParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ class RequestParser { // eslint-disable-line no-unused-vars
throw new Errors.InvalidRequestError('Data must not exceed 64 bytes');
}
if (flags === Nimiq.Transaction.Flag.CONTRACT_CREATION
&& data.byteLength !== 78 // HTLC
&& data.byteLength !== 24 // Vesting
&& data.byteLength !== 36 // Vesting
&& data.byteLength !== 44) { // Vesting
&& data.byteLength !== 78 // HTLC
&& data.byteLength !== 24 // Vesting
&& data.byteLength !== 36 // Vesting
&& data.byteLength !== 44) { // Vesting
throw new Errors.InvalidRequestError(
'Contract creation data must be 78 bytes for HTLC and 24, 36, or 44 bytes for vesting contracts',
);
Expand Down Expand Up @@ -359,4 +359,45 @@ class RequestParser { // eslint-disable-line no-unused-vars
}
return parsedUrl;
}

/**
* Parses and validates a phone number.
* @param {string} phoneNumber - The phone number to parse. Should be in E.164 format.
* @param {object} [options] - Parsing options
* @param {boolean} [options.required] - Whether the phone number is required
* @param {string[]} [options.expectedCountryCodes] - Allowed country codes.
* @returns {string|undefined} - The formatted phone number or undefined
*/
parsePhoneNumber(phoneNumber, options = {}) {
const { required = false, expectedCountryCodes = [] } = options;

if (phoneNumber === undefined && !required) return undefined;

if (typeof phoneNumber !== 'string') {
throw new Errors.InvalidRequestError('The phone number must be a string');
}

// If it contains spaces, it's not a valid number
if (phoneNumber.includes(' ')) {
throw new Errors.InvalidRequestError('The phone number must not contain spaces');
}

// Remove all non-digit characters
const digitsOnly = phoneNumber.replace(/\D/g, '');

// Check if the number has a valid length (assuming international format)
if (digitsOnly.length < 7 || digitsOnly.length > 15) {
throw new Errors.InvalidRequestError('The phone number has an invalid length');
}

if (expectedCountryCodes.length > 0) {
const hasValidCountryCode = expectedCountryCodes.some(countryCode => phoneNumber.startsWith(countryCode));
if (!hasValidCountryCode) {
throw new Errors.InvalidRequestError('The phone number has an invalid country code');
}
}

// Format the number (simple E.164 format)
return `+${digitsOnly}`;
}
}
15 changes: 7 additions & 8 deletions src/request/sign-swap/SignSwapApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ class SignSwapApi extends PolygonRequestParserMixin(BitcoinRequestParserMixin(To
* | PolygonRedeemDescription
* | PolygonRedeemWithSecretInDataDescription}
*/ (usdcHtlcContract.interface.parseTransaction({
data: forwardRequest.data,
value: forwardRequest.value,
}));
data: forwardRequest.data,
value: forwardRequest.value,
}));

if (!allowedMethods.includes(description.name)) {
throw new Errors.InvalidRequestError('Requested Polygon contract method is invalid');
Expand Down Expand Up @@ -459,10 +459,9 @@ class SignSwapApi extends PolygonRequestParserMixin(BitcoinRequestParserMixin(To
const settlement = {
type: 'sinpemovil',
phoneNumber: /** @type {string} */ (
this.parseLabel(
/** @type {{phoneNumber: unknown}} */(obj).phoneNumber,
false,
`${parameterName}.phoneNumber`,
this.parsePhoneNumber(
/** @type {{phoneNumber: string}} */(obj).phoneNumber,
{ expectedCountryCodes: ['+506'] },
)
),
};
Expand All @@ -481,7 +480,7 @@ class SignSwapApi extends PolygonRequestParserMixin(BitcoinRequestParserMixin(To
if (!Iban.isValid(iban)) {
throw new Errors.InvalidRequestError(`${parameterName} is not a valid IBAN`);
}
return Iban.printFormat(/** @type {string} */ (iban), ' ');
return Iban.printFormat(/** @type {string} */(iban), ' ');
}

get Handler() {
Expand Down

0 comments on commit 9533950

Please sign in to comment.