Skip to content

Commit

Permalink
Merge pull request #118 from BoltzExchange/swaptree-compare
Browse files Browse the repository at this point in the history
SwapTree compare function
  • Loading branch information
michael1011 authored Mar 22, 2024
2 parents d75f189 + ce129fa commit a3cbe6d
Show file tree
Hide file tree
Showing 10 changed files with 417 additions and 175 deletions.
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

0 comments on commit a3cbe6d

Please sign in to comment.