Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow use of sign-polygon-transaction flow for re-signing USDC redeem transactions #491

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 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,14 @@ 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);
if (['redeem', 'redeemWithSecretInData', 'refund'].includes(request.description.name)) {
const recipientAddress = /** @type {string} */ (request.description.args.target);
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);
const recipientAddress = /** @type {string} */ (request.description.args.target);
new PolygonAddressInfo(recipientAddress, request.recipientLabel, 'none').renderTo($recipient);
}

/** @type {HTMLDivElement} */
Expand All @@ -65,7 +59,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 +186,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
Loading