Skip to content

Commit 609544a

Browse files
committed
Unify OASIS settlement instruction parsing
1 parent cb20092 commit 609544a

File tree

1 file changed

+68
-90
lines changed

1 file changed

+68
-90
lines changed

src/request/sign-swap/SignSwapApi.js

+68-90
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,16 @@ class SignSwapApi extends PolygonRequestParserMixin(BitcoinRequestParserMixin(To
152152
amount: this.parsePositiveInteger(request.redeem.amount, false, 'redeem.amount'),
153153
};
154154
} else if (request.redeem.type === 'EUR') {
155-
/** @type MockSettlementInstruction | SepaSettlementInstruction | null */
156-
const settlement = this.parseOasisSepaSettlementInstruction(request.redeem.settlement, 'redeem.settlement')
157-
|| this.parseOasisMockSettlementInstruction(request.redeem.settlement);
155+
const settlement = this.parseOasisSettlementInstruction(request.redeem.settlement, 'redeem.settlement')
158156
if (!settlement) {
159157
throw new Errors.InvalidRequestError('Invalid redeeming settlement');
160158
}
159+
if (
160+
settlement.type !== 'sepa'
161+
&& (CONFIG.NETWORK === Constants.NETWORK.MAIN || settlement.type !== 'mock')
162+
) {
163+
throw new Errors.InvalidRequestError('Invalid redeeming settlement type');
164+
}
161165
parsedRequest.redeem = {
162166
type: 'EUR',
163167
keyPath: this.parsePath(request.redeem.keyPath, 'redeem.keyPath'),
@@ -167,13 +171,16 @@ class SignSwapApi extends PolygonRequestParserMixin(BitcoinRequestParserMixin(To
167171
bankLabel: this.parseLabel(request.redeem.bankLabel, true, 'redeem.bankLabel'),
168172
};
169173
} else if (request.redeem.type === 'CRC') {
170-
/** @type MockSettlementInstruction | SinpeMovilSettlementInstruction | null */
171-
const settlement = this.parseOasisSinpeMovilSettlementInstruction(request.redeem.settlement)
172-
|| this.parseOasisMockSettlementInstruction(request.redeem.settlement);
174+
const settlement = this.parseOasisSettlementInstruction(request.redeem.settlement, 'redeem.settlement');
173175
if (!settlement) {
174176
throw new Errors.InvalidRequestError('Invalid redeeming settlement');
175177
}
176-
178+
if (
179+
settlement.type !== 'sinpemovil'
180+
&& (CONFIG.NETWORK === Constants.NETWORK.MAIN || settlement.type !== 'mock')
181+
) {
182+
throw new Errors.InvalidRequestError('Invalid redeeming settlement type');
183+
}
177184
parsedRequest.redeem = {
178185
type: 'CRC',
179186
keyPath: this.parsePath(request.redeem.keyPath, 'redeem.keyPath'),
@@ -404,99 +411,70 @@ class SignSwapApi extends PolygonRequestParserMixin(BitcoinRequestParserMixin(To
404411
* @typedef {Omit<KeyguardRequest.SinpeMovilSettlementInstruction, 'contractId'>} SinpeMovilSettlementInstruction
405412
*/
406413

407-
408414
/**
409-
* Checks that the given instruction is a valid OASIS MockSettlementInstruction
410-
* @param {unknown} obj
411-
* @returns {MockSettlementInstruction | null}
412-
* @throws {Errors.InvalidRequestError}
413-
*/
414-
parseOasisMockSettlementInstruction(obj) {
415-
if (typeof obj !== 'object' || obj === null) {
416-
throw new Errors.InvalidRequestError('Invalid settlement');
417-
}
418-
419-
if (/** @type {{type: unknown}} */ (obj).type !== 'mock') {
420-
return null;
421-
}
422-
423-
/** @type {MockSettlementInstruction} */
424-
const settlement = { type: 'mock' };
425-
return settlement;
426-
}
427-
428-
/**
429-
* Checks that the given instruction is a valid OASIS SepaSettlementInstruction
415+
* Checks that the given instruction is a valid OASIS SettlementInstruction
430416
* @param {unknown} obj
431417
* @param {string} parameterName
432-
* @returns {SepaSettlementInstruction | null}
433-
* @throws {Errors.InvalidRequestError}
418+
* @returns {MockSettlementInstruction | SepaSettlementInstruction | SinpeMovilSettlementInstruction}
434419
*/
435-
parseOasisSepaSettlementInstruction(obj, parameterName) {
420+
parseOasisSettlementInstruction(obj, parameterName) {
436421
if (typeof obj !== 'object' || obj === null) {
437422
throw new Errors.InvalidRequestError('Invalid settlement');
438423
}
439424

440-
if (/** @type {{type: unknown}} */ (obj).type !== 'sepa') {
441-
return null;
442-
}
443-
444-
const recipient = /** @type {{recipient: unknown}} */ (obj).recipient;
445-
if (typeof recipient !== 'object' || recipient === null) {
446-
throw new Errors.InvalidRequestError('Invalid settlement recipient');
447-
}
448-
449-
/** @type {SepaSettlementInstruction} */
450-
const settlement = {
451-
type: 'sepa',
452-
recipient: {
453-
name: /** @type {string} */ (
454-
this.parseLabel(
455-
/** @type {{name: unknown}} */(recipient).name,
456-
false,
457-
`${parameterName}.recipient.name`,
458-
)
459-
),
460-
iban: this.parseIban(
461-
/** @type {{iban: unknown}} */(recipient).iban,
462-
`${parameterName}.recipient.iban`,
463-
),
464-
bic: this.parseBic(
465-
/** @type {{bic: unknown}} */(recipient).bic,
466-
`${parameterName}.recipient.bic`,
467-
),
468-
},
469-
};
470-
return settlement;
471-
}
472-
473-
/**
474-
* Checks that the given instruction is a valid OASIS SinpeMovilSettlementInstruction
475-
* @param {unknown} obj
476-
* @returns {SinpeMovilSettlementInstruction | null}
477-
* @throws {Errors.InvalidRequestError}
478-
*/
479-
parseOasisSinpeMovilSettlementInstruction(obj) {
480-
if (typeof obj !== 'object' || obj === null) {
481-
throw new Errors.InvalidRequestError('Invalid settlement');
482-
}
425+
switch (/** @type {{type: unknown}} */ (obj).type) {
426+
case 'mock': {
427+
/** @type {MockSettlementInstruction} */
428+
const settlement = {
429+
type: 'mock',
430+
};
431+
return settlement;
432+
}
433+
case 'sepa': {
434+
const recipient = /** @type {{recipient: unknown}} */ (obj).recipient;
435+
if (typeof recipient !== 'object' || recipient === null) {
436+
throw new Errors.InvalidRequestError('Invalid settlement recipient');
437+
}
483438

484-
if (/** @type {{type: unknown}} */ (obj).type !== 'sinpemovil') {
485-
return null;
439+
/** @type {SepaSettlementInstruction} */
440+
const settlement = {
441+
type: 'sepa',
442+
recipient: {
443+
name: /** @type {string} */ (
444+
this.parseLabel(
445+
/** @type {{name: unknown}} */(recipient).name,
446+
false,
447+
`${parameterName}.recipient.name`,
448+
)
449+
),
450+
iban: this.parseIban(
451+
/** @type {{iban: unknown}} */(recipient).iban,
452+
`${parameterName}.recipient.iban`,
453+
),
454+
bic: this.parseBic(
455+
/** @type {{bic: unknown}} */(recipient).bic,
456+
`${parameterName}.recipient.bic`,
457+
),
458+
},
459+
};
460+
return settlement;
461+
}
462+
case 'sinpemovil': {
463+
/** @type {SinpeMovilSettlementInstruction} */
464+
const settlement = {
465+
type: 'sinpemovil',
466+
phoneNumber: /** @type {string} */ (
467+
this.parseLabel(
468+
/** @type {{phoneNumber: unknown}} */(obj).phoneNumber,
469+
false,
470+
`${parameterName}.phoneNumber`,
471+
)
472+
),
473+
};
474+
return settlement;
475+
}
476+
default: throw new Errors.InvalidRequestError('Invalid settlement type');
486477
}
487-
488-
/** @type {SinpeMovilSettlementInstruction} */
489-
const settlement = {
490-
type: 'sinpemovil',
491-
phoneNumber: /** @type {string} */ (
492-
this.parseLabel(
493-
/** @type {{phoneNumber: unknown}} */(obj).phoneNumber,
494-
false,
495-
'phoneNumber',
496-
)
497-
),
498-
};
499-
return settlement;
500478
}
501479

502480
/**

0 commit comments

Comments
 (0)