Skip to content

Commit

Permalink
feat: detailed cooperative refund rejection error (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Jun 26, 2024
1 parent 1f61a44 commit e08c854
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 57 deletions.
4 changes: 2 additions & 2 deletions lib/service/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ export default {
message: 'swap version not supported for pair',
code: concatErrorCode(ErrorCodePrefix.Service, 34),
}),
NOT_ELIGIBLE_FOR_COOPERATIVE_REFUND: (): Error => ({
message: 'swap not eligible for a cooperative refund',
NOT_ELIGIBLE_FOR_COOPERATIVE_REFUND: (reason: string): Error => ({
message: `swap not eligible for a cooperative refund${reason !== undefined ? `: ${reason}` : ''}`,
code: concatErrorCode(ErrorCodePrefix.Service, 35),
}),
INCORRECT_PREIMAGE: (): Error => ({
Expand Down
14 changes: 9 additions & 5 deletions lib/service/cooperative/ChainSwapSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ class ChainSwapSigner extends CoopSignerBase<
throw Errors.CURRENCY_NOT_UTXO_BASED();
}

if (!(await MusigSigner.isEligibleForRefund(swap))) {
this.logger.verbose(
`Not creating partial signature for refund of ${swapTypeToPrettyString(swap.type)} Swap ${swap.id}: it is not eligible`,
);
throw Errors.NOT_ELIGIBLE_FOR_COOPERATIVE_REFUND();
{
const rejectionReason =
await MusigSigner.refundNonEligibilityReason(swap);
if (rejectionReason !== undefined) {
this.logger.verbose(
`Not creating partial signature for refund of ${swapTypeToPrettyString(swap.type)} Swap ${swap.id}: ${rejectionReason}`,
);
throw Errors.NOT_ELIGIBLE_FOR_COOPERATIVE_REFUND(rejectionReason);
}
}

this.logger.debug(
Expand Down
16 changes: 11 additions & 5 deletions lib/service/cooperative/EipSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
splitPairId,
} from '../../Utils';
import { etherDecimals } from '../../consts/Consts';
import { SwapType } from '../../consts/Enums';
import { SwapType, swapTypeToPrettyString } from '../../consts/Enums';
import Swap from '../../db/models/Swap';
import ChainSwapRepository, {
ChainSwapInfo,
Expand Down Expand Up @@ -35,11 +35,17 @@ class EipSigner {
throw 'chain currency is not EVM based';
}

if (!(await MusigSigner.isEligibleForRefund(swap, lightningCurrency))) {
this.logger.verbose(
`Not creating EIP-712 signature for refund of Swap ${swap.id}: it is not eligible`,
{
const rejectionReason = await MusigSigner.refundNonEligibilityReason(
swap,
lightningCurrency,
);
throw Errors.NOT_ELIGIBLE_FOR_COOPERATIVE_REFUND();
if (rejectionReason !== undefined) {
this.logger.verbose(
`Not creating EIP-712 signature for refund of ${swapTypeToPrettyString(swap.type)} Swap ${swap.id}: ${rejectionReason}`,
);
throw Errors.NOT_ELIGIBLE_FOR_COOPERATIVE_REFUND(rejectionReason);
}
}

this.logger.debug(
Expand Down
51 changes: 36 additions & 15 deletions lib/service/cooperative/MusigSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
SwapType,
SwapUpdateEvent,
SwapVersion,
swapTypeToPrettyString,
} from '../../consts/Enums';
import Swap from '../../db/models/Swap';
import { ChainSwapInfo } from '../../db/repositories/ChainSwapRepository';
Expand All @@ -31,6 +32,12 @@ type PartialSignature = {
signature: Buffer;
};

enum RefundRejectionReason {
VersionNotTaproot = 'swap version is not Taproot',
StatusNotEligible = 'status not eligible',
LightningPaymentPending = 'lightning payment still in progress, try again in a couple minutes',
}

// TODO: Should we verify what we are signing? And if so, how strict should we be?

class MusigSigner {
Expand Down Expand Up @@ -65,19 +72,19 @@ class MusigSigner {
throw Errors.CURRENCY_NOT_UTXO_BASED();
}

if (
swap.version !== SwapVersion.Taproot ||
!(await MusigSigner.isEligibleForRefund(
{
const rejectionReason = await MusigSigner.refundNonEligibilityReason(
swap,
this.currencies.get(
getLightningCurrency(base, quote, swap.orderSide, false),
)!,
))
) {
this.logger.verbose(
`Not creating partial signature for refund of Swap ${swap.id}: it is not eligible`,
);
throw Errors.NOT_ELIGIBLE_FOR_COOPERATIVE_REFUND();
if (rejectionReason !== undefined) {
this.logger.verbose(
`Not creating partial signature for refund of ${swapTypeToPrettyString(swap.type)} Swap ${swap.id}: ${rejectionReason}`,
);
throw Errors.NOT_ELIGIBLE_FOR_COOPERATIVE_REFUND(rejectionReason);
}
}

this.logger.debug(
Expand Down Expand Up @@ -177,16 +184,30 @@ class MusigSigner {
);
};

public static isEligibleForRefund = async (
public static refundNonEligibilityReason = async (
swap: Swap | ChainSwapInfo,
lightningCurrency?: Currency,
) =>
FailedSwapUpdateEvents.includes(swap.status as SwapUpdateEvent) &&
(lightningCurrency === undefined ||
!(await MusigSigner.hasPendingOrSuccessfulLightningPayment(
): Promise<string | undefined> => {
if (swap.version !== SwapVersion.Taproot) {
return RefundRejectionReason.VersionNotTaproot;
}

if (!FailedSwapUpdateEvents.includes(swap.status as SwapUpdateEvent)) {
return RefundRejectionReason.StatusNotEligible;
}

if (
lightningCurrency !== undefined &&
(await MusigSigner.hasPendingOrSuccessfulLightningPayment(
lightningCurrency,
swap,
)));
))
) {
return RefundRejectionReason.LightningPaymentPending;
}

return undefined;
};

private static hasPendingOrSuccessfulLightningPayment = async (
currency: Currency,
Expand Down Expand Up @@ -224,4 +245,4 @@ class MusigSigner {
}

export default MusigSigner;
export { PartialSignature };
export { PartialSignature, RefundRejectionReason };
10 changes: 9 additions & 1 deletion test/integration/service/cooperative/ChainSwapSigner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ import {
CurrencyType,
SwapType,
SwapUpdateEvent,
SwapVersion,
} from '../../../../lib/consts/Enums';
import ChainSwapRepository, {
ChainSwapInfo,
} from '../../../../lib/db/repositories/ChainSwapRepository';
import WrappedSwapRepository from '../../../../lib/db/repositories/WrappedSwapRepository';
import Errors from '../../../../lib/service/Errors';
import ChainSwapSigner from '../../../../lib/service/cooperative/ChainSwapSigner';
import { RefundRejectionReason } from '../../../../lib/service/cooperative/MusigSigner';
import SwapOutputType from '../../../../lib/swap/SwapOutputType';
import Wallet from '../../../../lib/wallet/Wallet';
import WalletLiquid from '../../../../lib/wallet/WalletLiquid';
Expand Down Expand Up @@ -205,6 +207,7 @@ describe('ChainSwapSigner', () => {
id: 'asdf',
type: SwapType.Chain,
status: 'swap.expired',
version: SwapVersion.Taproot,
preimageHash: getHexString(crypto.sha256(preimage)),
receivingData: {
symbol: 'BTC',
Expand Down Expand Up @@ -348,14 +351,19 @@ describe('ChainSwapSigner', () => {
ChainSwapRepository.getChainSwap = jest.fn().mockResolvedValue({
status,
id: 'asdf',
version: SwapVersion.Taproot,
receivingData: {
symbol: 'BTC',
},
});

await expect(
signer.signRefund('asdf', Buffer.alloc(0), Buffer.alloc(0), 0),
).rejects.toEqual(Errors.NOT_ELIGIBLE_FOR_COOPERATIVE_REFUND());
).rejects.toEqual(
Errors.NOT_ELIGIBLE_FOR_COOPERATIVE_REFUND(
RefundRejectionReason.StatusNotEligible,
),
);
},
);
});
Expand Down
16 changes: 14 additions & 2 deletions test/integration/service/cooperative/EipSigner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import { Signature } from 'ethers';
import Logger from '../../../../lib/Logger';
import { getHexString } from '../../../../lib/Utils';
import { etherDecimals } from '../../../../lib/consts/Consts';
import { SwapType, SwapUpdateEvent } from '../../../../lib/consts/Enums';
import {
SwapType,
SwapUpdateEvent,
SwapVersion,
} from '../../../../lib/consts/Enums';
import ChainSwapRepository from '../../../../lib/db/repositories/ChainSwapRepository';
import SwapRepository from '../../../../lib/db/repositories/SwapRepository';
import Errors from '../../../../lib/service/Errors';
import EipSigner from '../../../../lib/service/cooperative/EipSigner';
import { RefundRejectionReason } from '../../../../lib/service/cooperative/MusigSigner';
import WalletManager from '../../../../lib/wallet/WalletManager';
import {
EthereumSetup,
Expand Down Expand Up @@ -109,10 +114,13 @@ describe('EipSigner', () => {
SwapRepository.getSwap = jest.fn().mockResolvedValue({
orderSide: 1,
pair: 'TOKEN/BTC',
version: SwapVersion.Taproot,
status: SwapUpdateEvent.SwapCreated,
});
await expect(signer.signSwapRefund('not eligible')).rejects.toEqual(
Errors.NOT_ELIGIBLE_FOR_COOPERATIVE_REFUND(),
Errors.NOT_ELIGIBLE_FOR_COOPERATIVE_REFUND(
RefundRejectionReason.StatusNotEligible,
),
);
});

Expand Down Expand Up @@ -147,6 +155,7 @@ describe('EipSigner', () => {
orderSide: 1,
pair: 'RBTC/BTC',
type: SwapType.Submarine,
version: SwapVersion.Taproot,
timeoutBlockHeight: timelock,
preimageHash: getHexString(preimageHash),
status: SwapUpdateEvent.InvoiceFailedToPay,
Expand Down Expand Up @@ -187,6 +196,7 @@ describe('EipSigner', () => {

ChainSwapRepository.getChainSwap = jest.fn().mockResolvedValue({
type: SwapType.Chain,
version: SwapVersion.Taproot,
preimageHash: getHexString(preimageHash),
status: SwapUpdateEvent.InvoiceFailedToPay,
receivingData: {
Expand Down Expand Up @@ -232,6 +242,7 @@ describe('EipSigner', () => {
orderSide: 1,
pair: 'TOKEN/BTC',
type: SwapType.Submarine,
version: SwapVersion.Taproot,
timeoutBlockHeight: timelock,
onchainAmount: Number(amount),
preimageHash: getHexString(preimageHash),
Expand Down Expand Up @@ -273,6 +284,7 @@ describe('EipSigner', () => {

ChainSwapRepository.getChainSwap = jest.fn().mockResolvedValue({
type: SwapType.Chain,
version: SwapVersion.Taproot,
preimageHash: getHexString(preimageHash),
status: SwapUpdateEvent.InvoiceFailedToPay,
receivingData: {
Expand Down
Loading

0 comments on commit e08c854

Please sign in to comment.