Skip to content

Commit

Permalink
feat: make transaction optional in reverse claim endpoint (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 authored Jan 8, 2025
1 parent a32ed36 commit 3ed922d
Show file tree
Hide file tree
Showing 5 changed files with 342 additions and 258 deletions.
41 changes: 29 additions & 12 deletions lib/api/v2/routers/SwapRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ class SwapRouter extends RouterBase {
* schemas:
* ReverseClaimRequest:
* type: object
* required: ["preimage", "pubNonce", "transaction", "index"]
* required: ["preimage"]
* properties:
* preimage:
* type: string
Expand All @@ -939,7 +939,7 @@ class SwapRouter extends RouterBase {
* @openapi
* /swap/reverse/{id}/claim:
* post:
* description: Requests a partial signature for a cooperative Reverse Swap claim transaction
* description: Requests a partial signature for a cooperative Reverse Swap claim transaction. To settle the invoice, but not claim the onchain HTLC (eg to create a batched claim in the future), only the preimage is required. If no transaction is provided, an empty object is returned as response.
* tags: [Reverse]
* parameters:
* - in: path
Expand Down Expand Up @@ -1948,25 +1948,42 @@ class SwapRouter extends RouterBase {
req.body,
[
{ name: 'id', type: 'string', optional: params.id !== undefined },
{ name: 'index', type: 'number' },
{ name: 'preimage', type: 'string', hex: true },
{ name: 'pubNonce', type: 'string', hex: true },
{ name: 'transaction', type: 'string', hex: true },
{ name: 'index', type: 'number', optional: true },
{ name: 'pubNonce', type: 'string', hex: true, optional: true },
{ name: 'transaction', type: 'string', hex: true, optional: true },
],
);

const toSignParams = [pubNonce, index, transaction];
const allDefined = toSignParams.every((param) => param !== undefined);
const allUndefined = toSignParams.every((param) => param === undefined);

if (!allDefined && !allUndefined) {
throw 'pubNonce, index and transaction must be all set or all undefined';
}

const sig = await this.service.musigSigner.signReverseSwapClaim(
params.id || id,
preimage,
pubNonce,
transaction,
index,
allDefined
? {
index,
theirNonce: pubNonce,
rawTransaction: transaction,
}
: undefined,
);

successResponse(res, {
pubNonce: getHexString(sig.pubNonce),
partialSignature: getHexString(sig.signature),
});
successResponse(
res,
sig !== undefined
? {
pubNonce: getHexString(sig.pubNonce),
partialSignature: getHexString(sig.signature),
}
: {},
);
};

private getChain = async (req: Request, res: Response) => {
Expand Down
23 changes: 16 additions & 7 deletions lib/service/cooperative/MusigSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@ class MusigSigner {
public signReverseSwapClaim = (
swapId: string,
preimage: Buffer,
theirNonce: Buffer,
rawTransaction: Buffer,
index: number,
): Promise<PartialSignature> => {
toSign?: {
theirNonce: Buffer;
rawTransaction: Buffer;
index: number;
},
): Promise<PartialSignature | undefined> => {
return this.lock.acquire(
MusigSigner.reverseSwapClaimSignatureLock,
async () => {
Expand Down Expand Up @@ -154,6 +156,13 @@ class MusigSigner {
await this.nursery.settleReverseSwapInvoice(swap, preimage);
}

if (toSign === undefined) {
this.logger.debug(
`No transaction to sign for claim of Reverse Swap ${swap.id}`,
);
return undefined;
}

this.logger.debug(
`Creating partial signature for claim of Reverse Swap ${swap.id}`,
);
Expand All @@ -175,9 +184,9 @@ class MusigSigner {
swapTree,
swap.keyIndex!,
getHexBuffer(swap.claimPublicKey!),
theirNonce,
rawTransaction,
index,
toSign.theirNonce,
toSign.rawTransaction,
toSign.index,
);
},
);
Expand Down
7 changes: 2 additions & 5 deletions swagger-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -1465,7 +1465,7 @@
},
"/swap/reverse/{id}/claim": {
"post": {
"description": "Requests a partial signature for a cooperative Reverse Swap claim transaction",
"description": "Requests a partial signature for a cooperative Reverse Swap claim transaction. To settle the invoice, but not claim the onchain HTLC (eg to create a batched claim in the future), only the preimage is required. If no transaction is provided, an empty object is returned as response.",
"tags": [
"Reverse"
],
Expand Down Expand Up @@ -2648,10 +2648,7 @@
"ReverseClaimRequest": {
"type": "object",
"required": [
"preimage",
"pubNonce",
"transaction",
"index"
"preimage"
],
"properties": {
"preimage": {
Expand Down
Loading

0 comments on commit 3ed922d

Please sign in to comment.