Skip to content

Commit

Permalink
tests: integration tests for Liquid Tapleaf scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Jan 22, 2024
1 parent 4cf0964 commit 8fd70e6
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 14 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"test": "npm run test:solidity && npm run test:unit && npm run test:int",
"test:solidity": "forge test",
"test:unit": "jest test/unit",
"test:int": "jest test/integration",
"test:int": "jest test/integration --runInBand",
"deploy:solidity": "forge script Deploy --broadcast --rpc-url http://127.0.0.1:8545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
"changelog": "git-cliff -o CHANGELOG.md",
"preversion": "npm run lint && npm run compile && npm run test",
Expand Down Expand Up @@ -80,7 +80,7 @@
"prettier": "^3.2.4",
"slip77": "^0.2.0",
"tiny-secp256k1": "^2.2.3",
"ts-jest": "^29.1.1",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"typechain": "^8.3.2",
"typescript": "^5.3.3",
Expand Down
35 changes: 34 additions & 1 deletion test/integration/liquid/swapTree/SwapTreeClaim.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { hashForWitnessV1 } from '../../../../lib/liquid/swap/TaprooUtils';
import reverseSwapTree from '../../../../lib/swap/ReverseSwapTree';
import swapTree from '../../../../lib/swap/SwapTree';
import { slip77 } from '../../../unit/Utils';
import { ECPair, slip77 } from '../../../unit/Utils';
import {
claimSwap,
createSwapOutput,
Expand Down Expand Up @@ -106,5 +106,38 @@ describe.each`
);
await claimSwap([utxo], blindingKey);
});

test('should not claim via script path when preimage is invalid', async () => {
const { utxo } = await createSwapOutput<LiquidClaimDetails>(
OutputType.Taproot,
false,
treeFunc,
undefined,
blindInputs,
);
utxo.preimage = randomBytes(32);

await expect(claimSwap([utxo], blindingKey)).rejects.toEqual({
code: -26,
message:
'non-mandatory-script-verify-flag (Script failed an OP_EQUALVERIFY operation)',
});
});

test('should not claim via script path when claim key is invalid', async () => {
const { utxo } = await createSwapOutput<LiquidClaimDetails>(
OutputType.Taproot,
false,
treeFunc,
undefined,
blindInputs,
);
utxo.keys = ECPair.makeRandom();

await expect(claimSwap([utxo], blindingKey)).rejects.toEqual({
code: -26,
message: 'non-mandatory-script-verify-flag (Invalid Schnorr signature)',
});
});
},
);
46 changes: 40 additions & 6 deletions test/integration/liquid/swapTree/SwapTreeRefund.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import zkp from '@vulpemventures/secp256k1-zkp';
import { OutputType } from '../../../../lib/consts/Enums';
import { init } from '../../../../lib/liquid';
import { LiquidClaimDetails, init } from '../../../../lib/liquid';
import reverseSwapTree from '../../../../lib/swap/ReverseSwapTree';
import swapTree from '../../../../lib/swap/SwapTree';
import { slip77 } from '../../../unit/Utils';
import { ECPair, slip77 } from '../../../unit/Utils';
import {
createSwapOutput,
destinationOutput,
Expand All @@ -25,6 +25,10 @@ describe.each`
`(
'$name refund (inputs blinded $blindInputs; output blinded $blindOutput)',
({ treeFunc, blindInputs, blindOutput }) => {
const blindingKey = blindOutput
? slip77.derive(destinationOutput).publicKey!
: undefined;

beforeAll(async () => {
init(await zkp());
await Promise.all([utilsInit(), elementsClient.init()]);
Expand All @@ -36,18 +40,48 @@ describe.each`

test('should refund via script path', async () => {
const timeout = (await elementsClient.getBlockchainInfo()).blocks;
const { utxo } = await createSwapOutput(
const { utxo } = await createSwapOutput<LiquidClaimDetails>(
OutputType.Taproot,
true,
treeFunc,
timeout,
blindInputs,
);
await refundSwap(
[utxo],
await refundSwap([utxo], timeout, blindingKey);
});

test('should not refund via script path when timelock is not reached', async () => {
const timeout = (await elementsClient.getBlockchainInfo()).blocks;
const { utxo } = await createSwapOutput<LiquidClaimDetails>(
OutputType.Taproot,
true,
treeFunc,
timeout + 21,
blindInputs,
);

await expect(refundSwap([utxo], timeout, blindingKey)).rejects.toEqual({
code: -26,
message:
'non-mandatory-script-verify-flag (Locktime requirement not satisfied)',
});
});

test('should not refund via script path when refund key is invalid', async () => {
const timeout = (await elementsClient.getBlockchainInfo()).blocks;
const { utxo } = await createSwapOutput<LiquidClaimDetails>(
OutputType.Taproot,
true,
treeFunc,
timeout,
blindOutput ? slip77.derive(destinationOutput).publicKey! : undefined,
blindInputs,
);
utxo.keys = ECPair.makeRandom();

await expect(refundSwap([utxo], timeout, blindingKey)).rejects.toEqual({
code: -26,
message: 'non-mandatory-script-verify-flag (Invalid Schnorr signature)',
});
});
},
);

0 comments on commit 8fd70e6

Please sign in to comment.