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

Feature/GBI-1652 - Null script size verification #208

Merged
merged 3 commits into from
Feb 16, 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 @@ -30,7 +30,7 @@ jobs:
node-version: '19.6.0'

- name: NPM Login
run: echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" >> .npmrc
run: npm config set //npm.pkg.github.com/:_authToken ${{ secrets.GITHUB_TOKEN }}

- name: Install truffle
run: npm install -g truffle
Expand Down
1 change: 1 addition & 0 deletions contracts/LiquidityBridgeContractV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,7 @@ contract LiquidityBridgeContractV2 is Initializable, OwnableUpgradeable, Reentra
require(quote.lbcAddress != address(0), "LBC042");
BtcUtils.TxRawOutput[] memory outputs = BtcUtils.getOutputs(btcTx);
bytes memory scriptContent = BtcUtils.parseNullDataScript(outputs[QUOTE_HASH_OUTPUT].pkScript);
require(scriptContent.length == 33 && scriptContent[0] == 0x20, "LBC075");
// shift the array to remove the first byte (the size)
for (uint8 i = 0 ; i < scriptContent.length - 1; i++) {
scriptContent[i] = scriptContent[i + 1];
Expand Down
3 changes: 2 additions & 1 deletion errorCodes.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@
"LBC071": "Intentional overflow on quote values",
"LBC072": "Minimum collateral for registration can't be lower than 0.6 RBTC",
"LBC073": "Resign delay blocks lower than minimal",
"LBC074": "Error sending fee to DAO"
"LBC074": "Error sending fee to DAO",
"LBC075": "Malformed BTC transaction output"
}
62 changes: 62 additions & 0 deletions test/basic.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,68 @@ contract("LiquidityBridgeContractV2.sol", async (accounts) => {
);
});

it("Should fail on refundPegout if btc tx null data script has wrong format", async () => {
const blockHeaderHash =
"0x02327049330a25d4d17e53e79f478cbb79c53a509679b1d8a1505c5697afb326";
const partialMerkleTree =
"0x02327049330a25d4d17e53e79f478cbb79c53a509679b1d8a1505c5697afb426";
const merkleBranchHashes = [
"0x02327049330a25d4d17e53e79f478cbb79c53a509679b1d8a1505c5697afb326",
];
let quote = utils.getTestPegOutQuote(
instance.address, //lbc address
liquidityProviderRskAddress,
accounts[2],
web3.utils.toBN(1)
);
quote.transferConfirmations = 0;

// configure mocked block on mockBridge
const firstConfirmationTime = utils.reverseHexBytes(
web3.utils.toHex(quote.agreementTimestamp + 300).substring(2)
);
const firstHeader =
"0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
firstConfirmationTime +
"0000000000000000";
await bridgeMockInstance.setHeaderByHash(blockHeaderHash, firstHeader);

const msgValue = quote.value.add(quote.callFee).add(quote.productFeeAmount).add(quote.gasFee);
const quoteHash = await instance.hashPegoutQuote(utils.asArray(quote));
const signature = await web3.eth.sign(quoteHash, liquidityProviderRskAddress);
const pegOut = await instance.depositPegout(utils.asArray(quote), signature, {
value: msgValue.toNumber()
});
await truffleAssertions.eventEmitted(pegOut, "PegOutDeposit");

let incorrectSizeByteTx = await utils.generateRawTx(instance, quote);
incorrectSizeByteTx = web3.utils.bytesToHex(incorrectSizeByteTx).replace("6a20", "6a40");
await truffleAssertions.reverts(
instance.refundPegOut(
quoteHash,
incorrectSizeByteTx,
blockHeaderHash,
partialMerkleTree,
merkleBranchHashes
),
"LBC075"
);

let incorrectHashSizeTx = await utils.generateRawTx(instance, quote);
incorrectHashSizeTx = web3.utils.bytesToHex(incorrectHashSizeTx)
.replace("226a20"+quoteHash.slice(2), "216a19"+quoteHash.slice(2, -2));
await truffleAssertions.reverts(
instance.refundPegOut(
quoteHash,
incorrectHashSizeTx,
blockHeaderHash,
partialMerkleTree,
merkleBranchHashes
),
"LBC075"
);
});

it("Should fail on refundPegout if btc tx doesn't have correct amount", async () => {
const blockHeaderHash = "0x02327049330a25d4d17e53e79f478cbb79c53a509679b1d8a1505c5697afb326";
const partialMerkleTree = "0x02327049330a25d4d17e53e79f478cbb79c53a509679b1d8a1505c5697afb426";
Expand Down
Loading