diff --git a/clients/tests/web/test/tb03-simple-atomic-transfer.test.js b/clients/tests/web/test/tb03-simple-atomic-transfer.test.js index b23e90e3..0760a55d 100644 --- a/clients/tests/web/test/tb03-simple-atomic-transfer.test.js +++ b/clients/tests/web/test/tb03-simple-atomic-transfer.test.js @@ -170,7 +170,7 @@ describe('TB03 - Atomic swap with second batchid missing', () => { let transferReceive3 = await mercuryweblib.transferReceive(clientConfig, wallet3.name); - expect(transferReceive3.isThereBatchLocked).toBe(true); + expect(transferReceive3.isThereBatchLocked).toBe(false); const transferReceive4 = await mercuryweblib.transferReceive(clientConfig, wallet4.name); @@ -262,7 +262,7 @@ describe('TB03 - Atomic swap without first batchid', () => { let transferReceive3 = await mercuryweblib.transferReceive(clientConfig, wallet3.name); - expect(transferReceive3.isThereBatchLocked).toBe(true); + expect(transferReceive3.isThereBatchLocked).toBe(false); const transferReceive4 = await mercuryweblib.transferReceive(clientConfig, wallet4.name); @@ -372,7 +372,7 @@ describe('TB03 - Atomic swap with timeout', () => { toAddress3 = await mercuryweblib.newTransferAddress(wallet3.name, true); toAddress4 = await mercuryweblib.newTransferAddress(wallet4.name); - await mercuryweblib.transferSend(clientConfig, wallet1.name, statechainId1, toAddress3.transfer_receive, false, null); + await mercuryweblib.transferSend(clientConfig, wallet1.name, statechainId1, toAddress3.transfer_receive, false, toAddress3.batch_id); await mercuryweblib.transferSend(clientConfig, wallet2.name, statechainId2, toAddress4.transfer_receive, false, toAddress3.batch_id); transferReceive3 = await mercuryweblib.transferReceive(clientConfig, wallet3.name); @@ -394,4 +394,222 @@ describe('TB03 - Atomic swap with timeout', () => { await mercuryweblib.withdrawCoin(clientConfig, wallet4.name, statechainId2, toAddress, null, null); }); -}, 50000); \ No newline at end of file +}, 50000); + +describe('TB03 - Atomic swap with first party steal', () => { + test("expected flow", async () => { + + localStorage.removeItem("mercury-layer:wallet1_tb03"); + localStorage.removeItem("mercury-layer:wallet2_tb03"); + localStorage.removeItem("mercury-layer:wallet3_tb03"); + localStorage.removeItem("mercury-layer:wallet4_tb03"); + + let wallet1 = await mercuryweblib.createWallet(clientConfig, "wallet1_tb03"); + let wallet2 = await mercuryweblib.createWallet(clientConfig, "wallet2_tb03"); + let wallet3 = await mercuryweblib.createWallet(clientConfig, "wallet3_tb03"); + let wallet4 = await mercuryweblib.createWallet(clientConfig, "wallet4_tb03"); + + await mercuryweblib.newToken(clientConfig, wallet1.name); + await mercuryweblib.newToken(clientConfig, wallet2.name); + + const amount = 1000; + + let result1 = await mercuryweblib.getDepositBitcoinAddress(clientConfig, wallet1.name, amount); + let result2 = await mercuryweblib.getDepositBitcoinAddress(clientConfig, wallet2.name, amount); + + const statechainId1 = result1.statechain_id; + const statechainId2 = result2.statechain_id; + + let isDepositInMempool1 = false; + let isDepositConfirmed1 = false; + + let isDepositInMempool2 = false; + let isDepositConfirmed2 = false; + + let areBlocksGenerated = false; + + await depositCoin(result1.deposit_address, amount); + await depositCoin(result2.deposit_address, amount); + + while (!isDepositConfirmed2 || !isDepositConfirmed1) { + + let coins = await mercuryweblib.listStatecoins(clientConfig, wallet1.name); + + for (let coin of coins) { + if (coin.statechain_id === statechainId1 && coin.status === CoinStatus.IN_MEMPOOL && !isDepositInMempool1) { + isDepositInMempool1 = true; + } else if (coin.statechain_id === statechainId1 && coin.status === CoinStatus.CONFIRMED) { + isDepositConfirmed1 = true; + } + } + + coins = await mercuryweblib.listStatecoins(clientConfig, wallet2.name); + + for (let coin of coins) { + if (coin.statechain_id === statechainId2 && coin.status === CoinStatus.IN_MEMPOOL && !isDepositInMempool2) { + isDepositInMempool2 = true; + } else if (coin.statechain_id === statechainId2 && coin.status === CoinStatus.CONFIRMED) { + isDepositConfirmed2 = true; + } + } + + if (isDepositInMempool1 && isDepositInMempool2 && !areBlocksGenerated) { + areBlocksGenerated = true; + await generateBlocks(clientConfig.confirmationTarget); + } + + await new Promise(r => setTimeout(r, 1000)); + } + + const toAddress3 = await mercuryweblib.newTransferAddress(wallet3.name, true); + const toAddress4 = await mercuryweblib.newTransferAddress(wallet4.name); + + await mercuryweblib.transferSend(clientConfig, wallet1.name, statechainId1, toAddress3.transfer_receive, false, toAddress3.batch_id); + await mercuryweblib.transferSend(clientConfig, wallet2.name, statechainId2, toAddress4.transfer_receive, false, toAddress3.batch_id); + + const toAddress3_for_steal = await mercuryweblib.newTransferAddress(wallet3.name, true); + + try { + await mercuryweblib.transferSend(clientConfig, wallet1.name, statechainId1, toAddress3_for_steal.transfer_receive, false, toAddress3.batch_id); + } catch (error) { + // Assert the captured error message + const expectedMessage = 'expected a string argument, found undefined'; + expect(error.message).contains(expectedMessage); + } + + let transferReceive3 = undefined; + try { + transferReceive3 = await mercuryweblib.transferReceive(clientConfig, wallet3.name); + } catch (error) { + // Assert the captured error message + const expectedMessage = 'num_sigs is not correct'; + expect(error.message).contains(expectedMessage); + } + await sleep(3000); + + try { + const transferReceive4 = await mercuryweblib.transferReceive(clientConfig, wallet4.name); + } catch (error) { + // Assert the captured error message + const expectedMessage = 'num_sigs is not correct'; + expect(error.message).contains(expectedMessage); + } + + try { + transferReceive3 = await mercuryweblib.transferReceive(clientConfig, wallet3.name); + } catch (error) { + // Assert the captured error message + const expectedMessage = 'num_sigs is not correct'; + expect(error.message).contains(expectedMessage); + } + }); +}, 50000); + +describe('TB03 - Atomic swap with second party steal', () => { + test("expected flow", async () => { + + localStorage.removeItem("mercury-layer:wallet1_tb03"); + localStorage.removeItem("mercury-layer:wallet2_tb03"); + localStorage.removeItem("mercury-layer:wallet3_tb03"); + localStorage.removeItem("mercury-layer:wallet4_tb03"); + + let wallet1 = await mercuryweblib.createWallet(clientConfig, "wallet1_tb03"); + let wallet2 = await mercuryweblib.createWallet(clientConfig, "wallet2_tb03"); + let wallet3 = await mercuryweblib.createWallet(clientConfig, "wallet3_tb03"); + let wallet4 = await mercuryweblib.createWallet(clientConfig, "wallet4_tb03"); + + await mercuryweblib.newToken(clientConfig, wallet1.name); + await mercuryweblib.newToken(clientConfig, wallet2.name); + + const amount = 1000; + + let result1 = await mercuryweblib.getDepositBitcoinAddress(clientConfig, wallet1.name, amount); + let result2 = await mercuryweblib.getDepositBitcoinAddress(clientConfig, wallet2.name, amount); + + const statechainId1 = result1.statechain_id; + const statechainId2 = result2.statechain_id; + + let isDepositInMempool1 = false; + let isDepositConfirmed1 = false; + + let isDepositInMempool2 = false; + let isDepositConfirmed2 = false; + + let areBlocksGenerated = false; + + await depositCoin(result1.deposit_address, amount); + await depositCoin(result2.deposit_address, amount); + + while (!isDepositConfirmed2 || !isDepositConfirmed1) { + + let coins = await mercuryweblib.listStatecoins(clientConfig, wallet1.name); + + for (let coin of coins) { + if (coin.statechain_id === statechainId1 && coin.status === CoinStatus.IN_MEMPOOL && !isDepositInMempool1) { + isDepositInMempool1 = true; + } else if (coin.statechain_id === statechainId1 && coin.status === CoinStatus.CONFIRMED) { + isDepositConfirmed1 = true; + } + } + + coins = await mercuryweblib.listStatecoins(clientConfig, wallet2.name); + + for (let coin of coins) { + if (coin.statechain_id === statechainId2 && coin.status === CoinStatus.IN_MEMPOOL && !isDepositInMempool2) { + isDepositInMempool2 = true; + } else if (coin.statechain_id === statechainId2 && coin.status === CoinStatus.CONFIRMED) { + isDepositConfirmed2 = true; + } + } + + if (isDepositInMempool1 && isDepositInMempool2 && !areBlocksGenerated) { + areBlocksGenerated = true; + await generateBlocks(clientConfig.confirmationTarget); + } + + await new Promise(r => setTimeout(r, 1000)); + } + + const toAddress3 = await mercuryweblib.newTransferAddress(wallet3.name, true); + const toAddress4 = await mercuryweblib.newTransferAddress(wallet4.name); + + await mercuryweblib.transferSend(clientConfig, wallet1.name, statechainId1, toAddress3.transfer_receive, false, toAddress3.batch_id); + await mercuryweblib.transferSend(clientConfig, wallet2.name, statechainId2, toAddress4.transfer_receive, false, toAddress3.batch_id); + + const toAddress4_for_steal = await mercuryweblib.newTransferAddress(wallet4.name, true); + + try { + await mercuryweblib.transferSend(clientConfig, wallet2.name, statechainId1, toAddress4_for_steal.transfer_receive, false, toAddress4.batch_id); + } catch (error) { + // Assert the captured error message + const expectedMessage = 'expected a string argument, found undefined'; + expect(error.message).contains(expectedMessage); + } + + let transferReceive3 = undefined; + try { + transferReceive3 = await mercuryweblib.transferReceive(clientConfig, wallet3.name); + } catch (error) { + // Assert the captured error message + const expectedMessage = 'num_sigs is not correct'; + expect(error.message).contains(expectedMessage); + } + await sleep(3000); + + try { + const transferReceive4 = await mercuryweblib.transferReceive(clientConfig, wallet4.name); + } catch (error) { + // Assert the captured error message + const expectedMessage = 'num_sigs is not correct'; + expect(error.message).contains(expectedMessage); + } + + try { + transferReceive3 = await mercuryweblib.transferReceive(clientConfig, wallet3.name); + } catch (error) { + // Assert the captured error message + const expectedMessage = 'num_sigs is not correct'; + expect(error.message).contains(expectedMessage); + } + }); +}, 50000);