Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: try all possible key extraction functions in CLI #765

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading