From 6f1f09d7d1b9a2aa809a8697e196aa168146d6b1 Mon Sep 17 00:00:00 2001 From: DhananjayPurohit Date: Mon, 5 Aug 2024 21:36:12 +0530 Subject: [PATCH] feat: add test for sender sends coin without batch_id --- .../test/tb04-simple-lightning-latch.mjs | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/clients/apps/nodejs/test/tb04-simple-lightning-latch.mjs b/clients/apps/nodejs/test/tb04-simple-lightning-latch.mjs index fc27819f..6f719c52 100644 --- a/clients/apps/nodejs/test/tb04-simple-lightning-latch.mjs +++ b/clients/apps/nodejs/test/tb04-simple-lightning-latch.mjs @@ -414,5 +414,65 @@ describe('TB04 - Lightning Latch', function() { console.error('Error:', error); expect(error.message).to.include('failed'); } + }) + }) + + context('Statecoin sender sends coin without batch_id (receiver should still be able to receive, but no pre-image revealed)', () => { + it('should complete successfully', async () => { + + // await removeDatabase(); + const clientConfig = client_config.load(); + let wallet_1_name = "w_ln_11"; + let wallet_2_name = "w_ln_12"; + await createWallet(clientConfig, wallet_1_name); + await createWallet(clientConfig, wallet_2_name); + + const token = await mercurynodejslib.newToken(clientConfig, wallet_1_name); + const tokenId = token.token_id; + + const amount = 10000; + const depositInfo = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount); + + const tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name); + const usedToken = tokenList.find(token => token.token_id === tokenId); + + expect(usedToken.spent).is.true; + + await depositCoin(clientConfig, wallet_1_name, amount, depositInfo); + + const listCoins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name); + + expect(listCoins.length).to.equal(1); + + const coin = listCoins[0]; + + expect(coin.status).to.equal(CoinStatus.CONFIRMED); + + const paymentHash = await mercurynodejslib.paymentHash(clientConfig, wallet_1_name, coin.statechain_id); + + const transferAddress = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null); + + await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin.statechain_id, transferAddress.transfer_receive, false, null); + + let transferReceiveResult = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); + + expect(transferReceiveResult.isThereBatchLocked).is.true; + expect(transferReceiveResult.receivedStatechainIds).empty; + + await mercurynodejslib.confirmPendingInvoice(clientConfig, wallet_1_name, coin.statechain_id); + + transferReceiveResult = await mercurynodejslib.transferReceive(clientConfig, wallet_2_name); + + expect(transferReceiveResult.isThereBatchLocked).is.false; + expect(transferReceiveResult.receivedStatechainIds).not.empty; + + const { preimage } = await mercurynodejslib.retrievePreImage(clientConfig, wallet_1_name, coin.statechain_id, paymentHash.batchId); + + const hash = crypto.createHash('sha256') + .update(Buffer.from(preimage, 'hex')) + .digest('hex') + + expect(hash).to.equal(paymentHash.hash); + }) }) })