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

SwapTree compare function #118

Merged
merged 3 commits into from
Mar 22, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
strategy:
matrix:
platform: [ubuntu-latest]
node-version: [18, 20]
node-version: [18, 20, 21]

runs-on: ${{ matrix.platform }}

Expand Down
4 changes: 2 additions & 2 deletions contracts/test/ERC20SwapTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ contract ERC20SwapTest is Test {
sigUtils = new SigUtils(swap.DOMAIN_SEPARATOR(), swap.TYPEHASH_REFUND());
}

function testCorrectVersion() external {
function testCorrectVersion() external view {
assertEq(swap.version(), 3);
}

Expand All @@ -49,7 +49,7 @@ contract ERC20SwapTest is Test {
require(!success);
}

function testHashSwapValues() external {
function testHashSwapValues() external view {
uint256 timelock = block.number;

assertEq(
Expand Down
4 changes: 2 additions & 2 deletions contracts/test/EtherSwapTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract EtherSwapTest is Test {

receive() payable external {}

function testCorrectVersion() external {
function testCorrectVersion() external view {
assertEq(swap.version(), 3);
}

Expand All @@ -44,7 +44,7 @@ contract EtherSwapTest is Test {
require(!success);
}

function testHashSwapValues() external {
function testHashSwapValues() external view {
uint256 timelock = block.number;

assertEq(
Expand Down
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
2 changes: 1 addition & 1 deletion lib/swap/SwapDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const detectSwap = <
vout,
type: scriptMatch[0],
...output,
} as DetectedSwap<T>;
} as unknown as DetectedSwap<T>;
}
}

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);
Loading