Skip to content

Commit

Permalink
feat: add SwapTree compare function
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Mar 22, 2024
1 parent 0605eee commit ce129fa
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/Boltz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import swapTree, {
extractClaimPublicKeyFromSwapTree,
extractRefundPublicKeyFromSwapTree,
} from './swap/SwapTree';
import { compareTrees } from './swap/SwapTreeCompare';
import * as SwapTreeSerializer from './swap/SwapTreeSerializer';
import * as SwapUtils from './swap/SwapUtils';
import * as TaprootUtils from './swap/TaprootUtils';
Expand Down Expand Up @@ -57,6 +58,7 @@ export {
targetFee,
swapScript,
detectSwap,
compareTrees,
detectPreimage,
reverseSwapTree,
reverseSwapScript,
Expand Down
26 changes: 26 additions & 0 deletions lib/swap/SwapTreeCompare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Taptree } from 'bitcoinjs-lib/src/types';
import { SwapTree, Tapleaf } from '../consts/Types';

const compareLeaf = (leaf: Tapleaf, compareLeaf: Tapleaf) =>
leaf.version === compareLeaf.version &&
leaf.output.equals(compareLeaf.output);

const compareTree = (tree: Taptree, compare: Taptree) => {
if (Array.isArray(tree) !== Array.isArray(compare)) {
return false;
}

if (Array.isArray(tree) && Array.isArray(compare)) {
return (
tree.length === compare.length &&
tree.every((leaf, i) => compareTree(leaf, compare[i]))
);
} else {
return compareLeaf(tree as Tapleaf, compare as Tapleaf);
}
};

export const compareTrees = <T extends SwapTree>(
tree: T,
compare: T,
): boolean => compareTree(tree.tree, compare.tree);
77 changes: 77 additions & 0 deletions test/unit/swap/SwapTreeCompare.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { randomBytes } from 'crypto';
import swapTree from '../../../lib/swap/SwapTree';
import { compareTrees } from '../../../lib/swap/SwapTreeCompare';
import { ECPair } from '../Utils';

describe('SwapTreeCompare', () => {
test('should compare SwapTrees', () => {
const preimageHash = randomBytes(32);
const claimKey = ECPair.makeRandom().publicKey;
const refundKey = ECPair.makeRandom().publicKey;
const timeoutBlockHeight = 123;

expect(
compareTrees(
swapTree(false, preimageHash, claimKey, refundKey, timeoutBlockHeight),
swapTree(false, preimageHash, claimKey, refundKey, timeoutBlockHeight),
),
).toEqual(true);
});

test('should return false on version mismatch', () => {
const preimageHash = randomBytes(32);
const claimKey = ECPair.makeRandom().publicKey;
const refundKey = ECPair.makeRandom().publicKey;
const timeoutBlockHeight = 123;

expect(
compareTrees(
swapTree(true, preimageHash, claimKey, refundKey, timeoutBlockHeight),
swapTree(false, preimageHash, claimKey, refundKey, timeoutBlockHeight),
),
).toEqual(false);
});

test('should return false on script mismatch', () => {
const preimageHash = randomBytes(32);
const claimKey = ECPair.makeRandom().publicKey;
const refundKey = ECPair.makeRandom().publicKey;
const timeoutBlockHeight = 123;

expect(
compareTrees(
swapTree(true, preimageHash, claimKey, refundKey, timeoutBlockHeight),
swapTree(
true,
randomBytes(32),
claimKey,
refundKey,
timeoutBlockHeight,
),
),
).toEqual(false);
});

test('should return false on depth mismatch', () => {
const preimageHash = randomBytes(32);
const claimKey = ECPair.makeRandom().publicKey;
const refundKey = ECPair.makeRandom().publicKey;
const timeoutBlockHeight = 123;

const compare = swapTree(
false,
preimageHash,
claimKey,
refundKey,
timeoutBlockHeight,
);
compare.tree = compare.claimLeaf;

expect(
compareTrees(
swapTree(false, preimageHash, claimKey, refundKey, timeoutBlockHeight),
compare,
),
).toEqual(false);
});
});

0 comments on commit ce129fa

Please sign in to comment.