From 0d429f0a0cf1600da6f366b606679cf5b40fdda1 Mon Sep 17 00:00:00 2001 From: michael1011 Date: Thu, 2 Jan 2025 16:04:14 +0100 Subject: [PATCH] feat: try all possible key extraction functions in CLI --- lib/cli/Command.ts | 54 +++++++++++++---------- lib/cli/TaprootHelper.ts | 13 +++--- lib/cli/commands/Claim.ts | 10 ++++- lib/cli/commands/CooperativeClaim.ts | 6 ++- lib/cli/commands/CooperativeClaimChain.ts | 4 +- lib/cli/commands/CooperativeRefund.ts | 6 ++- lib/cli/commands/Refund.ts | 10 ++++- 7 files changed, 65 insertions(+), 38 deletions(-) diff --git a/lib/cli/Command.ts b/lib/cli/Command.ts index 2a2c2d4d..1f3d174a 100644 --- a/lib/cli/Command.ts +++ b/lib/cli/Command.ts @@ -88,7 +88,7 @@ export const callback = ( export const prepareTx = async ( argv: Arguments, - keyExtractionFunc: (tree: Types.SwapTree) => Buffer, + keyExtractionFuncs: ((tree: Types.SwapTree) => Buffer)[], ) => { await setup(); @@ -122,7 +122,7 @@ export const prepareTx = async ( const { musig, swapOutput } = musigFromExtractedKey( type, res.keys, - keyExtractionFunc(tree), + keyExtractionFuncs.map((fn) => fn(tree)), tree, transaction, ); @@ -184,31 +184,37 @@ export const printError = (error: Error): void => { export const musigFromExtractedKey = ( type: CurrencyType, ourKeys: ECPairInterface, - theirPublicKey: Buffer, + possibleTheirPublicKeys: Buffer[], tree: Types.SwapTree, lockupTx: Transaction | LiquidTransaction, ) => { - for (const tieBreaker of ['02', '03']) { - const compressedKey = Buffer.concat([ - getHexBuffer(tieBreaker), - theirPublicKey, - ]); - - for (const keys of [ - [compressedKey, ourKeys.publicKey], - [ourKeys.publicKey, compressedKey], - ]) { - const musig = new Musig(zkp, ourKeys, randomBytes(32), keys); - const tweakedKey = tweakMusig(type, musig, tree); - - const swapOutput = detectSwap(tweakedKey, lockupTx); - if (swapOutput !== undefined) { - return { - musig, - tweakedKey, - swapOutput, - theirPublicKey: compressedKey, - }; + for (const theirPublicKey of possibleTheirPublicKeys) { + if (!Buffer.isBuffer(theirPublicKey)) { + continue; + } + + for (const tieBreaker of ['02', '03']) { + const compressedKey = Buffer.concat([ + getHexBuffer(tieBreaker), + theirPublicKey, + ]); + + for (const keys of [ + [compressedKey, ourKeys.publicKey], + [ourKeys.publicKey, compressedKey], + ]) { + const musig = new Musig(zkp, ourKeys, randomBytes(32), keys); + const tweakedKey = tweakMusig(type, musig, tree); + + const swapOutput = detectSwap(tweakedKey, lockupTx); + if (swapOutput !== undefined) { + return { + musig, + tweakedKey, + swapOutput, + theirPublicKey: compressedKey, + }; + } } } } diff --git a/lib/cli/TaprootHelper.ts b/lib/cli/TaprootHelper.ts index f01e6e56..cc909ec0 100644 --- a/lib/cli/TaprootHelper.ts +++ b/lib/cli/TaprootHelper.ts @@ -25,26 +25,27 @@ export const setupCooperativeTransaction = async ( currencyType: CurrencyType, swapTree: Types.SwapTree, keys: ECPairInterface, - keyExtractionFunc: (tree: Types.SwapTree) => Buffer, + keyExtractionFuncs: ((tree: Types.SwapTree) => Buffer)[], lockupTx: Transaction | LiquidTransaction, ) => { await setup(); - const { musig, tweakedKey, theirPublicKey } = musigFromExtractedKey( + const foundOutput = musigFromExtractedKey( currencyType, keys, - keyExtractionFunc(swapTree), + keyExtractionFuncs.map((fn) => fn(swapTree)), swapTree, lockupTx, ); + if (foundOutput === undefined) { + throw 'could not find swap output'; + } return { + ...foundOutput, keys, - musig, network, - tweakedKey, currencyType, - theirPublicKey, }; }; diff --git a/lib/cli/commands/Claim.ts b/lib/cli/commands/Claim.ts index 306c6a3a..061ecac3 100644 --- a/lib/cli/commands/Claim.ts +++ b/lib/cli/commands/Claim.ts @@ -1,4 +1,7 @@ -import { extractRefundPublicKeyFromReverseSwapTree } from 'boltz-core'; +import { + extractRefundPublicKeyFromReverseSwapTree, + extractRefundPublicKeyFromSwapTree, +} from 'boltz-core'; import { Arguments } from 'yargs'; import { constructClaimTransaction } from '../../Core'; import { getHexBuffer, stringify } from '../../Utils'; @@ -30,7 +33,10 @@ export const handler = async (argv: Arguments): Promise => { transaction, redeemScript, destinationAddress, - } = await prepareTx(argv, extractRefundPublicKeyFromReverseSwapTree); + } = await prepareTx(argv, [ + extractRefundPublicKeyFromSwapTree, + extractRefundPublicKeyFromReverseSwapTree, + ]); const claimTransaction = constructClaimTransaction( walletStub, diff --git a/lib/cli/commands/CooperativeClaim.ts b/lib/cli/commands/CooperativeClaim.ts index dabfc12f..9a7218ef 100644 --- a/lib/cli/commands/CooperativeClaim.ts +++ b/lib/cli/commands/CooperativeClaim.ts @@ -1,6 +1,7 @@ import { SwapTreeSerializer, extractRefundPublicKeyFromReverseSwapTree, + extractRefundPublicKeyFromSwapTree, } from 'boltz-core'; import { Arguments } from 'yargs'; import { parseTransaction } from '../../Core'; @@ -52,7 +53,10 @@ export const handler = async ( currencyType, SwapTreeSerializer.deserializeSwapTree(argv.swapTree), ECPair.fromPrivateKey(getHexBuffer(argv.privateKey)), - extractRefundPublicKeyFromReverseSwapTree, + [ + extractRefundPublicKeyFromSwapTree, + extractRefundPublicKeyFromReverseSwapTree, + ], lockupTx, ); diff --git a/lib/cli/commands/CooperativeClaimChain.ts b/lib/cli/commands/CooperativeClaimChain.ts index f15ef0e0..8549b207 100644 --- a/lib/cli/commands/CooperativeClaimChain.ts +++ b/lib/cli/commands/CooperativeClaimChain.ts @@ -91,7 +91,7 @@ export const handler = async ( claimType, claimTree, ECPair.fromPrivateKey(getHexBuffer(argv.claimKeys)), - extractRefundPublicKeyFromReverseSwapTree, + [extractRefundPublicKeyFromReverseSwapTree], lockupTx, ); const claimTx = prepareCooperativeTransaction( @@ -118,7 +118,7 @@ export const handler = async ( theirClaimType, refundTree, ECPair.fromPrivateKey(getHexBuffer(argv.refundKeys)), - extractClaimPublicKeyFromReverseSwapTree, + [extractClaimPublicKeyFromReverseSwapTree], parseTransaction( theirClaimType, swapTransactions.userLock!.transaction.hex!, diff --git a/lib/cli/commands/CooperativeRefund.ts b/lib/cli/commands/CooperativeRefund.ts index 37a4af6b..899928d2 100644 --- a/lib/cli/commands/CooperativeRefund.ts +++ b/lib/cli/commands/CooperativeRefund.ts @@ -1,5 +1,6 @@ import { SwapTreeSerializer, + extractClaimPublicKeyFromReverseSwapTree, extractClaimPublicKeyFromSwapTree, } from 'boltz-core'; import { Arguments } from 'yargs'; @@ -48,7 +49,10 @@ export const handler = async ( currencyType, SwapTreeSerializer.deserializeSwapTree(argv.swapTree), ECPair.fromPrivateKey(getHexBuffer(argv.privateKey)), - extractClaimPublicKeyFromSwapTree, + [ + extractClaimPublicKeyFromSwapTree, + extractClaimPublicKeyFromReverseSwapTree, + ], lockupTx, ); diff --git a/lib/cli/commands/Refund.ts b/lib/cli/commands/Refund.ts index 3f814160..a28cebb2 100644 --- a/lib/cli/commands/Refund.ts +++ b/lib/cli/commands/Refund.ts @@ -1,4 +1,7 @@ -import { extractClaimPublicKeyFromSwapTree } from 'boltz-core'; +import { + extractClaimPublicKeyFromReverseSwapTree, + extractClaimPublicKeyFromSwapTree, +} from 'boltz-core'; import { Arguments } from 'yargs'; import { constructRefundTransaction } from '../../Core'; import { stringify } from '../../Utils'; @@ -33,7 +36,10 @@ export const handler = async (argv: Arguments): Promise => { transaction, redeemScript, destinationAddress, - } = await prepareTx(argv, extractClaimPublicKeyFromSwapTree); + } = await prepareTx(argv, [ + extractClaimPublicKeyFromSwapTree, + extractClaimPublicKeyFromReverseSwapTree, + ]); const refundTransaction = constructRefundTransaction( walletStub,