-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0605eee
commit ce129fa
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |