Skip to content

Commit

Permalink
Unify OASIS settlement instruction parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Sep 4, 2024
1 parent cb20092 commit 9f01464
Showing 1 changed file with 69 additions and 90 deletions.
159 changes: 69 additions & 90 deletions src/request/sign-swap/SignSwapApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/* global Iban */
/* global ethers */
/* global CONFIG */
/* global Constants */
/* global PolygonContractABIs */

class SignSwapApi extends PolygonRequestParserMixin(BitcoinRequestParserMixin(TopLevelApi)) {
Expand Down Expand Up @@ -152,12 +153,16 @@ class SignSwapApi extends PolygonRequestParserMixin(BitcoinRequestParserMixin(To
amount: this.parsePositiveInteger(request.redeem.amount, false, 'redeem.amount'),
};
} else if (request.redeem.type === 'EUR') {
/** @type MockSettlementInstruction | SepaSettlementInstruction | null */
const settlement = this.parseOasisSepaSettlementInstruction(request.redeem.settlement, 'redeem.settlement')
|| this.parseOasisMockSettlementInstruction(request.redeem.settlement);
const settlement = this.parseOasisSettlementInstruction(request.redeem.settlement, 'redeem.settlement');
if (!settlement) {
throw new Errors.InvalidRequestError('Invalid redeeming settlement');
}
if (
settlement.type !== 'sepa'
&& (CONFIG.NETWORK === Constants.NETWORK.MAIN || settlement.type !== 'mock')
) {
throw new Errors.InvalidRequestError('Invalid redeeming settlement type');
}
parsedRequest.redeem = {
type: 'EUR',
keyPath: this.parsePath(request.redeem.keyPath, 'redeem.keyPath'),
Expand All @@ -167,13 +172,16 @@ class SignSwapApi extends PolygonRequestParserMixin(BitcoinRequestParserMixin(To
bankLabel: this.parseLabel(request.redeem.bankLabel, true, 'redeem.bankLabel'),
};
} else if (request.redeem.type === 'CRC') {
/** @type MockSettlementInstruction | SinpeMovilSettlementInstruction | null */
const settlement = this.parseOasisSinpeMovilSettlementInstruction(request.redeem.settlement)
|| this.parseOasisMockSettlementInstruction(request.redeem.settlement);
const settlement = this.parseOasisSettlementInstruction(request.redeem.settlement, 'redeem.settlement');
if (!settlement) {
throw new Errors.InvalidRequestError('Invalid redeeming settlement');
}

if (
settlement.type !== 'sinpemovil'
&& (CONFIG.NETWORK === Constants.NETWORK.MAIN || settlement.type !== 'mock')
) {
throw new Errors.InvalidRequestError('Invalid redeeming settlement type');
}
parsedRequest.redeem = {
type: 'CRC',
keyPath: this.parsePath(request.redeem.keyPath, 'redeem.keyPath'),
Expand Down Expand Up @@ -404,99 +412,70 @@ class SignSwapApi extends PolygonRequestParserMixin(BitcoinRequestParserMixin(To
* @typedef {Omit<KeyguardRequest.SinpeMovilSettlementInstruction, 'contractId'>} SinpeMovilSettlementInstruction
*/


/**
* Checks that the given instruction is a valid OASIS MockSettlementInstruction
* @param {unknown} obj
* @returns {MockSettlementInstruction | null}
* @throws {Errors.InvalidRequestError}
*/
parseOasisMockSettlementInstruction(obj) {
if (typeof obj !== 'object' || obj === null) {
throw new Errors.InvalidRequestError('Invalid settlement');
}

if (/** @type {{type: unknown}} */ (obj).type !== 'mock') {
return null;
}

/** @type {MockSettlementInstruction} */
const settlement = { type: 'mock' };
return settlement;
}

/**
* Checks that the given instruction is a valid OASIS SepaSettlementInstruction
* Checks that the given instruction is a valid OASIS SettlementInstruction
* @param {unknown} obj
* @param {string} parameterName
* @returns {SepaSettlementInstruction | null}
* @throws {Errors.InvalidRequestError}
* @returns {MockSettlementInstruction | SepaSettlementInstruction | SinpeMovilSettlementInstruction}
*/
parseOasisSepaSettlementInstruction(obj, parameterName) {
parseOasisSettlementInstruction(obj, parameterName) {
if (typeof obj !== 'object' || obj === null) {
throw new Errors.InvalidRequestError('Invalid settlement');
}

if (/** @type {{type: unknown}} */ (obj).type !== 'sepa') {
return null;
}

const recipient = /** @type {{recipient: unknown}} */ (obj).recipient;
if (typeof recipient !== 'object' || recipient === null) {
throw new Errors.InvalidRequestError('Invalid settlement recipient');
}

/** @type {SepaSettlementInstruction} */
const settlement = {
type: 'sepa',
recipient: {
name: /** @type {string} */ (
this.parseLabel(
/** @type {{name: unknown}} */(recipient).name,
false,
`${parameterName}.recipient.name`,
)
),
iban: this.parseIban(
/** @type {{iban: unknown}} */(recipient).iban,
`${parameterName}.recipient.iban`,
),
bic: this.parseBic(
/** @type {{bic: unknown}} */(recipient).bic,
`${parameterName}.recipient.bic`,
),
},
};
return settlement;
}

/**
* Checks that the given instruction is a valid OASIS SinpeMovilSettlementInstruction
* @param {unknown} obj
* @returns {SinpeMovilSettlementInstruction | null}
* @throws {Errors.InvalidRequestError}
*/
parseOasisSinpeMovilSettlementInstruction(obj) {
if (typeof obj !== 'object' || obj === null) {
throw new Errors.InvalidRequestError('Invalid settlement');
}
switch (/** @type {{type: unknown}} */ (obj).type) {
case 'mock': {
/** @type {MockSettlementInstruction} */
const settlement = {
type: 'mock',
};
return settlement;
}
case 'sepa': {
const recipient = /** @type {{recipient: unknown}} */ (obj).recipient;
if (typeof recipient !== 'object' || recipient === null) {
throw new Errors.InvalidRequestError('Invalid settlement recipient');
}

if (/** @type {{type: unknown}} */ (obj).type !== 'sinpemovil') {
return null;
/** @type {SepaSettlementInstruction} */
const settlement = {
type: 'sepa',
recipient: {
name: /** @type {string} */ (
this.parseLabel(
/** @type {{name: unknown}} */(recipient).name,
false,
`${parameterName}.recipient.name`,
)
),
iban: this.parseIban(
/** @type {{iban: unknown}} */(recipient).iban,
`${parameterName}.recipient.iban`,
),
bic: this.parseBic(
/** @type {{bic: unknown}} */(recipient).bic,
`${parameterName}.recipient.bic`,
),
},
};
return settlement;
}
case 'sinpemovil': {
/** @type {SinpeMovilSettlementInstruction} */
const settlement = {
type: 'sinpemovil',
phoneNumber: /** @type {string} */ (
this.parseLabel(
/** @type {{phoneNumber: unknown}} */(obj).phoneNumber,
false,
`${parameterName}.phoneNumber`,
)
),
};
return settlement;
}
default: throw new Errors.InvalidRequestError('Invalid settlement type');
}

/** @type {SinpeMovilSettlementInstruction} */
const settlement = {
type: 'sinpemovil',
phoneNumber: /** @type {string} */ (
this.parseLabel(
/** @type {{phoneNumber: unknown}} */(obj).phoneNumber,
false,
'phoneNumber',
)
),
};
return settlement;
}

/**
Expand Down

0 comments on commit 9f01464

Please sign in to comment.