Skip to content

Commit

Permalink
feat: add test to get preimage before batch unlock
Browse files Browse the repository at this point in the history
  • Loading branch information
DhananjayPurohit committed Jul 22, 2024
1 parent cf6cea4 commit 88f504b
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ jobs:
cd clients/apps/nodejs
node test_basic_workflow2.js
node test_atomic_swap.js
mocha ./test/tb04-simple-lightning-latch.mjs
mocha ./test/tb04-simple-lightning-latch.mjs --exit
- name: Tear Down
run: |
docker-compose -f docker-compose-test.yml down
102 changes: 102 additions & 0 deletions clients/apps/nodejs/test/tb04-simple-lightning-latch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,106 @@ describe('TB04 - Lightning Latch', function() {
expect(hash).to.equal(paymentHash.hash);
})
})

context('The sender tries to get the pre-image before the batch is unlocked', () => {
it('must fail', async () => {

const clientConfig = client_config.load();
let wallet_1_name = "w_ln_3";
let wallet_2_name = "w_ln_4";
let wallet_3_name = "w_ln_5";
let wallet_4_name = "w_ln_6";
await createWallet(clientConfig, wallet_1_name);
await createWallet(clientConfig, wallet_2_name);
await createWallet(clientConfig, wallet_3_name);
await createWallet(clientConfig, wallet_4_name);

const amount = 10000;
let token = undefined;
let tokenId = undefined;
let depositInfo = undefined;
let tokenList = undefined;
let usedToken = undefined;
let listCoins = undefined;

token = await mercurynodejslib.newToken(clientConfig, wallet_1_name);
tokenId = token.token_id;

depositInfo = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_1_name, amount);

tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_1_name);
usedToken = tokenList.find(token => token.token_id === tokenId);

expect(usedToken.spent).is.true;

await depositCoin(clientConfig, wallet_1_name, amount, depositInfo);

listCoins = await mercurynodejslib.listStatecoins(clientConfig, wallet_1_name);

expect(listCoins.length).to.equal(1);

const coin1 = listCoins[0];

expect(coin1.status).to.equal(CoinStatus.CONFIRMED);

const paymentHash1 = await mercurynodejslib.paymentHash(clientConfig, wallet_1_name, coin1.statechain_id);

token = await mercurynodejslib.newToken(clientConfig, wallet_2_name);
tokenId = token.token_id;

depositInfo = await mercurynodejslib.getDepositBitcoinAddress(clientConfig, wallet_2_name, amount);

tokenList = await mercurynodejslib.getWalletTokens(clientConfig, wallet_2_name);
usedToken = tokenList.find(token => token.token_id === tokenId);

expect(usedToken.spent).is.true;

await depositCoin(clientConfig, wallet_2_name, amount, depositInfo);

listCoins = await mercurynodejslib.listStatecoins(clientConfig, wallet_2_name);

expect(listCoins.length).to.equal(1);

const coin2 = listCoins[0];

expect(coin2.status).to.equal(CoinStatus.CONFIRMED);

const paymentHash2 = await mercurynodejslib.paymentHash(clientConfig, wallet_2_name, coin2.statechain_id);

const transferAddress1 = await mercurynodejslib.newTransferAddress(clientConfig, wallet_1_name, null);
const transferAddress2 = await mercurynodejslib.newTransferAddress(clientConfig, wallet_2_name, null);

await mercurynodejslib.transferSend(clientConfig, wallet_1_name, coin1.statechain_id, transferAddress1.transfer_receive, paymentHash1.batchId);
await mercurynodejslib.transferSend(clientConfig, wallet_2_name, coin2.statechain_id, transferAddress2.transfer_receive, paymentHash2.batchId);

let transferReceiveResult = await mercurynodejslib.transferReceive(clientConfig, wallet_1_name);

expect(transferReceiveResult.isThereBatchLocked).is.true;
expect(transferReceiveResult.receivedStatechainIds).empty;

try {
const { preimage } = await mercurynodejslib.retrievePreImage(clientConfig, wallet_1_name, coin1.statechain_id, paymentHash1.batchId);
} catch (error) {
// Assert the captured error message
const expectedMessage = 'Request failed with status code 404';
expect(error.message).to.equal(expectedMessage);
}

await mercurynodejslib.confirmPendingInvoice(clientConfig, wallet_1_name, coin1.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, coin1.statechain_id, paymentHash1.batchId);

const hash = crypto.createHash('sha256')
.update(Buffer.from(preimage, 'hex'))
.digest('hex')

expect(hash).to.equal(paymentHash.hash);
})
})

})

0 comments on commit 88f504b

Please sign in to comment.