Skip to content

Commit

Permalink
Allow use of sign-polygon-transaction flow for re-signing swap redeem…
Browse files Browse the repository at this point in the history
… transactions
  • Loading branch information
sisou committed Feb 29, 2024
1 parent 8dd1d00 commit ad57014
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
23 changes: 8 additions & 15 deletions src/request/sign-polygon-transaction/SignPolygonTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SignPolygonTransaction {

/** @type {HTMLLinkElement} */
const $sender = (this.$el.querySelector('.accounts .sender'));
if (request.description.name === 'refund') {
if (['redeem', 'redeemWithSecretInData', 'refund'].includes(request.description.name)) {
new PolygonAddressInfo(relayRequest.to, request.senderLabel, 'unknown').renderTo($sender);
} else if (request.description.name === 'swap' || request.description.name === 'swapWithApproval') {
new PolygonAddressInfo(relayRequest.from, 'USDC.e', 'usdc_dark').renderTo($sender);
Expand All @@ -42,20 +42,13 @@ class SignPolygonTransaction {

/** @type {HTMLLinkElement} */
const $recipient = (this.$el.querySelector('.accounts .recipient'));
if (request.description.name === 'refund') {
new PolygonAddressInfo(
/** @type {string} */ (request.description.args.target),
request.keyLabel,
'usdc',
).renderTo($recipient);
const recipientAddress = /** @type {string} */ (request.description.args.target);
if (['redeem', 'redeemWithSecretInData', 'refund'].includes(request.description.name)) {
new PolygonAddressInfo(recipientAddress, request.keyLabel, 'usdc').renderTo($recipient);
} else if (request.description.name === 'swap' || request.description.name === 'swapWithApproval') {
new PolygonAddressInfo(relayRequest.from, 'USDC', 'usdc').renderTo($recipient);
} else {
new PolygonAddressInfo(
/** @type {string} */ (request.description.args.target),
request.recipientLabel,
'none',
).renderTo($recipient);
new PolygonAddressInfo(recipientAddress, request.recipientLabel, 'none').renderTo($recipient);
}

/** @type {HTMLDivElement} */
Expand All @@ -65,7 +58,7 @@ class SignPolygonTransaction {

// Set value and fee.
$value.textContent = NumberFormatting.formatNumber(
PolygonUtils.unitsToCoins(request.description.name === 'refund'
PolygonUtils.unitsToCoins(['redeem', 'redeemWithSecretInData', 'refund'].includes(request.description.name)
? /** @type {number} */ (request.amount)
: request.description.args.amount.toNumber()),
6,
Expand Down Expand Up @@ -192,10 +185,10 @@ class SignPolygonTransaction {
]);
}

if (request.description.name === 'refund') {
if (['redeem', 'redeemWithSecretInData', 'refund'].includes(request.description.name)) {
const derivedAddress = polygonKey.deriveAddress(request.keyPath);
if (request.description.args.target !== derivedAddress) {
reject(new Errors.InvalidRequestError('Refund target does not match derived address'));
reject(new Errors.InvalidRequestError('Target address argument does not match derived address'));
return;
}
}
Expand Down
25 changes: 18 additions & 7 deletions src/request/sign-polygon-transaction/SignPolygonTransactionApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class SignPolygonTransactionApi extends PolygonRequestParserMixin(TopLevelApi) {
* KeyguardRequest.OpenGsnForwardRequest,
* PolygonTransferDescription
* | PolygonTransferWithPermitDescription
* | PolygonRedeemDescription
* | PolygonRedeemWithSecretInDataDescription
* | PolygonRefundDescription
* | PolygonSwapDescription
* | PolygonSwapWithApprovalDescription,
Expand All @@ -70,6 +72,8 @@ class SignPolygonTransactionApi extends PolygonRequestParserMixin(TopLevelApi) {
/**
* @type {PolygonTransferDescription
* | PolygonTransferWithPermitDescription
* | PolygonRedeemDescription
* | PolygonRedeemWithSecretInDataDescription
* | PolygonRefundDescription
* | PolygonSwapDescription
* | PolygonSwapWithApprovalDescription}
Expand Down Expand Up @@ -101,13 +105,18 @@ class SignPolygonTransactionApi extends PolygonRequestParserMixin(TopLevelApi) {
PolygonContractABIs.NATIVE_USDC_HTLC_CONTRACT_ABI,
);

/** @type {PolygonRefundDescription} */
/**
* @type {PolygonRedeemDescription
* | PolygonRedeemWithSecretInDataDescription
* | PolygonRefundDescription
* }
*/
description = (usdcHtlcContract.interface.parseTransaction({
data: forwardRequest.data,
value: forwardRequest.value,
}));

if (!['refund'].includes(description.name)) {
if (!['redeem', 'redeemWithSecretInData', 'refund'].includes(description.name)) {
throw new Errors.InvalidRequestError('Requested Polygon contract method is invalid');
}
} else if (forwardRequest.to === CONFIG.BRIDGED_USDC_HTLC_CONTRACT_ADDRESS) {
Expand Down Expand Up @@ -164,17 +173,19 @@ class SignPolygonTransactionApi extends PolygonRequestParserMixin(TopLevelApi) {
throw new Errors.InvalidRequestError('request.to address is not allowed');
}

// Check that amount exists when request is for refund or redeem, and unset for other methods.
if (['redeem', 'redeemWithSecretInData', 'refund'].includes(description.name) !== !!request.amount) {
throw new Errors.InvalidRequestError(
'`amount` is only allowed for contract methods "refund", "redeem" and "redeemWithSecretInData"',
);
}

// Check that permit object exists when method is 'transferWithPermit', and unset for other methods.
if ((description.name === 'transferWithPermit') !== !!request.permit) {
throw new Errors.InvalidRequestError('`permit` object is only allowed for contract method '
+ '"transferWithPermit"');
}

// Check that amount exists when method is 'refund', and unset for other methods.
if ((description.name === 'refund') !== !!request.amount) {
throw new Errors.InvalidRequestError('`amount` is only allowed for contract method "refund"');
}

// Check that approval object exists when method is 'swapWithApproval', and unset for other methods.
if ((description.name === 'swapWithApproval') !== !!request.approval) {
throw new Errors.InvalidRequestError('`approval` object is only allowed for contract method '
Expand Down
2 changes: 1 addition & 1 deletion src/request/swap-iframe/SwapIFrameApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ class SwapIFrameApi extends BitcoinRequestParserMixin(RequestParser) { // eslint
/* bytes32 id */ parsedRequest.redeem.htlcId,
/* address target */ storedRequest.redeem.description.args.target,
...(storedRequest.redeem.description.name === 'redeem' ? [
/* uint256 secret */ storedRequest.redeem.description.args.secret,
/* bytes32 secret */ storedRequest.redeem.description.args.secret,
] : []),
/* uint256 fee */ storedRequest.redeem.description.args.fee,
],
Expand Down
2 changes: 2 additions & 0 deletions types/Keyguard.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ type Parsed<T extends KeyguardRequest.Request> =
KeyId2KeyInfo<KeyguardRequest.SignPolygonTransactionRequest>
& { description: PolygonTransferDescription
| PolygonTransferWithPermitDescription
| PolygonRedeemDescription
| PolygonRedeemWithSecretInDataDescription
| PolygonRefundDescription
| PolygonSwapDescription
| PolygonSwapWithApprovalDescription } :
Expand Down

0 comments on commit ad57014

Please sign in to comment.