Skip to content

Commit

Permalink
feat: try all possible key extraction functions in CLI (#765)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 authored Jan 2, 2025
1 parent efe6b8a commit 730e26b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 38 deletions.
54 changes: 30 additions & 24 deletions lib/cli/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const callback = <T extends GrpcResponse>(

export const prepareTx = async (
argv: Arguments<any>,
keyExtractionFunc: (tree: Types.SwapTree) => Buffer,
keyExtractionFuncs: ((tree: Types.SwapTree) => Buffer)[],
) => {
await setup();

Expand Down Expand Up @@ -122,7 +122,7 @@ export const prepareTx = async (
const { musig, swapOutput } = musigFromExtractedKey(
type,
res.keys,
keyExtractionFunc(tree),
keyExtractionFuncs.map((fn) => fn(tree)),
tree,
transaction,
);
Expand Down Expand Up @@ -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,
};
}
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions lib/cli/TaprootHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
};

Expand Down
10 changes: 8 additions & 2 deletions lib/cli/commands/Claim.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -30,7 +33,10 @@ export const handler = async (argv: Arguments<any>): Promise<void> => {
transaction,
redeemScript,
destinationAddress,
} = await prepareTx(argv, extractRefundPublicKeyFromReverseSwapTree);
} = await prepareTx(argv, [
extractRefundPublicKeyFromSwapTree,
extractRefundPublicKeyFromReverseSwapTree,
]);

const claimTransaction = constructClaimTransaction(
walletStub,
Expand Down
6 changes: 5 additions & 1 deletion lib/cli/commands/CooperativeClaim.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
SwapTreeSerializer,
extractRefundPublicKeyFromReverseSwapTree,
extractRefundPublicKeyFromSwapTree,
} from 'boltz-core';
import { Arguments } from 'yargs';
import { parseTransaction } from '../../Core';
Expand Down Expand Up @@ -52,7 +53,10 @@ export const handler = async (
currencyType,
SwapTreeSerializer.deserializeSwapTree(argv.swapTree),
ECPair.fromPrivateKey(getHexBuffer(argv.privateKey)),
extractRefundPublicKeyFromReverseSwapTree,
[
extractRefundPublicKeyFromSwapTree,
extractRefundPublicKeyFromReverseSwapTree,
],
lockupTx,
);

Expand Down
4 changes: 2 additions & 2 deletions lib/cli/commands/CooperativeClaimChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const handler = async (
claimType,
claimTree,
ECPair.fromPrivateKey(getHexBuffer(argv.claimKeys)),
extractRefundPublicKeyFromReverseSwapTree,
[extractRefundPublicKeyFromReverseSwapTree],
lockupTx,
);
const claimTx = prepareCooperativeTransaction(
Expand All @@ -118,7 +118,7 @@ export const handler = async (
theirClaimType,
refundTree,
ECPair.fromPrivateKey(getHexBuffer(argv.refundKeys)),
extractClaimPublicKeyFromReverseSwapTree,
[extractClaimPublicKeyFromReverseSwapTree],
parseTransaction(
theirClaimType,
swapTransactions.userLock!.transaction.hex!,
Expand Down
6 changes: 5 additions & 1 deletion lib/cli/commands/CooperativeRefund.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
SwapTreeSerializer,
extractClaimPublicKeyFromReverseSwapTree,
extractClaimPublicKeyFromSwapTree,
} from 'boltz-core';
import { Arguments } from 'yargs';
Expand Down Expand Up @@ -48,7 +49,10 @@ export const handler = async (
currencyType,
SwapTreeSerializer.deserializeSwapTree(argv.swapTree),
ECPair.fromPrivateKey(getHexBuffer(argv.privateKey)),
extractClaimPublicKeyFromSwapTree,
[
extractClaimPublicKeyFromSwapTree,
extractClaimPublicKeyFromReverseSwapTree,
],
lockupTx,
);

Expand Down
10 changes: 8 additions & 2 deletions lib/cli/commands/Refund.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -33,7 +36,10 @@ export const handler = async (argv: Arguments<any>): Promise<void> => {
transaction,
redeemScript,
destinationAddress,
} = await prepareTx(argv, extractClaimPublicKeyFromSwapTree);
} = await prepareTx(argv, [
extractClaimPublicKeyFromSwapTree,
extractClaimPublicKeyFromReverseSwapTree,
]);

const refundTransaction = constructRefundTransaction(
walletStub,
Expand Down

0 comments on commit 730e26b

Please sign in to comment.