Skip to content

Commit

Permalink
fix: harden Taproot signature generation
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Apr 8, 2024
1 parent acd0b56 commit 622f291
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
3 changes: 3 additions & 0 deletions lib/db/repositories/ReverseSwapRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class ReverseSwapRepository {
});
};

public static setPreimage = (reverseSwap: ReverseSwap, preimage: string) =>
reverseSwap.update({ preimage });

public static setInvoiceSettled = (
reverseSwap: ReverseSwap,
preimage: string,
Expand Down
41 changes: 25 additions & 16 deletions lib/service/cooperative/MusigSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,27 +131,36 @@ class MusigSigner {
}

this.logger.debug(
`Creating partial signature for claim of Reverse Swap ${swap.id}`,
`Got preimage for Reverse Swap ${swap.id}: ${getHexString(preimage)}`,
);
await ReverseSwapRepository.setPreimage(swap, getHexString(preimage));

if (swap.status !== SwapUpdateEvent.InvoiceSettled) {
await this.nursery.lock.acquire(SwapNursery.reverseSwapLock, async () => {
return this.nursery.lock.acquire(SwapNursery.reverseSwapLock, async () => {
if (swap.status !== SwapUpdateEvent.InvoiceSettled) {
await this.nursery.settleReverseSwapInvoice(swap, preimage);
});
}
}

const { base, quote } = splitPairId(swap.pair);
const swapTree = SwapTreeSerializer.deserializeSwapTree(swap.redeemScript!);
this.logger.debug(
`Creating partial signature for claim of Reverse Swap ${swap.id}`,
);

return this.createPartialSignature(
this.currencies.get(getChainCurrency(base, quote, swap.orderSide, true))!,
swapTree,
swap.keyIndex!,
getHexBuffer(swap.claimPublicKey!),
theirNonce,
rawTransaction,
index,
);
const { base, quote } = splitPairId(swap.pair);
const swapTree = SwapTreeSerializer.deserializeSwapTree(
swap.redeemScript!,
);

return this.createPartialSignature(
this.currencies.get(
getChainCurrency(base, quote, swap.orderSide, true),
)!,
swapTree,
swap.keyIndex!,
getHexBuffer(swap.claimPublicKey!),
theirNonce,
rawTransaction,
index,
);
});
};

public static isEligibleForRefund = async (
Expand Down
2 changes: 0 additions & 2 deletions test/integration/lightning/LndClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ describe('LndClient', () => {

expect(bindPort).toEqual(serverPort);

server.start();

// Connect to the mocked LND gRPC server
const lndClient = new LndClient(Logger.disabledLogger, 'MOCK', {
host: serverHost,
Expand Down
5 changes: 5 additions & 0 deletions test/integration/service/cooperative/MusigSigner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ describe('MusigSigner', () => {
preimageHash: getHexString(crypto.sha256(preimage)),
redeemScript: JSON.stringify(SwapTreeSerializer.serializeSwapTree(tree)),
});
ReverseSwapRepository.setPreimage = jest.fn();

btcWallet.getKeysByIndex = jest.fn().mockReturnValue(refundKeys);

Expand All @@ -365,6 +366,10 @@ describe('MusigSigner', () => {
await ReverseSwapRepository.getReverseSwap({}),
preimage,
);
expect(ReverseSwapRepository.setPreimage).toHaveBeenCalledWith(
expect.anything(),
getHexString(preimage),
);

musig.aggregateNonces([[refundKeys.publicKey, boltzPartialSig.pubNonce]]);
musig.initializeSession(await hashForWitnessV1(btcCurrency, claimTx, 0));
Expand Down

0 comments on commit 622f291

Please sign in to comment.