Skip to content

Commit

Permalink
feat: add test for invoice handling for lightning latch
Browse files Browse the repository at this point in the history
  • Loading branch information
DhananjayPurohit committed Aug 15, 2024
1 parent c207e50 commit d2deda3
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 7 deletions.
16 changes: 12 additions & 4 deletions clients/apps/nodejs/test/tb04-simple-lightning-latch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,12 @@ describe('TB04 - Lightning Latch', function() {
expect(transferReceiveResult.isThereBatchLocked).is.false;
expect(transferReceiveResult.receivedStatechainIds).is.empty;

const { preimage } = await mercurynodejslib.retrievePreImage(clientConfig, wallet_1_name, coin.statechain_id, paymentHash.batchId);
console.log("Preimage: ", preimage);
try {
const { preimage } = await mercurynodejslib.retrievePreImage(clientConfig, wallet_1_name, coin.statechain_id, paymentHash.batchId);
} catch (error) {
console.error('Error:', error);
expect(error.message).to.include('failed');
}

const hash = crypto.createHash('sha256')
.update(Buffer.from(preimage, 'hex'))
Expand Down Expand Up @@ -536,8 +540,12 @@ describe('TB04 - Lightning Latch', function() {
expect(transferReceiveResult.isThereBatchLocked).is.false;
expect(transferReceiveResult.receivedStatechainIds).is.empty;

const { preimage } = await mercurynodejslib.retrievePreImage(clientConfig, wallet_1_name, coin.statechain_id, paymentHash.batchId);
console.log("Preimage: ", preimage);
try {
const { preimage } = await mercurynodejslib.retrievePreImage(clientConfig, wallet_1_name, coin.statechain_id, paymentHash.batchId);
} catch (error) {
console.error('Error:', error);
expect(error.message).to.include('failed');
}

const hash = crypto.createHash('sha256')
.update(Buffer.from(preimage, 'hex'))
Expand Down
223 changes: 220 additions & 3 deletions clients/tests/web/test/tb04-simple-lightning-latch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ describe('TB04 - The sender tries to get the pre-image before the batch is unloc
let toAddress = "bcrt1q805t9k884s5qckkxv7l698hqlz7t6alsfjsqym";

await mercuryweblib.withdrawCoin(clientConfig, wallet2.name, statechainId1, toAddress, null, null);
await mercuryweblib.withdrawCoin(clientConfig, wallet1.name, statechainId2, toAddress, null, null);

const { preimage } = await mercuryweblib.retrievePreImage(clientConfig, wallet1.name, statechainId1, paymentHash1.batchId);

Expand Down Expand Up @@ -461,7 +460,7 @@ describe('TB04 - Receiver tries to transfer invoice amount to another invoice be

await mercuryweblib.transferSend(clientConfig, wallet1.name, statechainId, transferAddress.transfer_receive, false, paymentHash.batchId );

const hashFromServer = await mercurynodejslib.getPaymentHash(clientConfig, paymentHash.batchId);
const hashFromServer = await mercuryweblib.getPaymentHash(clientConfig, paymentHash.batchId);

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

Expand Down Expand Up @@ -495,4 +494,222 @@ describe('TB04 - Receiver tries to transfer invoice amount to another invoice be
expect(error.message).to.include('failed');
}
});
}, 50000);
}, 50000);

describe('Statecoin sender sends coin without batch_id (receiver should still be able to receive, but no pre-image revealed)', () => {
test("expected flow", async () => {

localStorage.removeItem("mercury-layer:wallet1_tb04");
localStorage.removeItem("mercury-layer:wallet2_tb04");

let wallet1 = await mercuryweblib.createWallet(clientConfig, "wallet1_tb04");
let wallet2 = await mercuryweblib.createWallet(clientConfig, "wallet2_tb04");

await mercuryweblib.newToken(clientConfig, wallet1.name);

const amount = 1000;

let result = await mercuryweblib.getDepositBitcoinAddress(clientConfig, wallet1.name, amount);

const statechainId = result.statechain_id;

let isDepositInMempool = false;
let isDepositConfirmed = false;
let areBlocksGenerated = false;

await depositCoin(result.deposit_address, amount);

while (!isDepositConfirmed) {

const coins = await mercuryweblib.listStatecoins(clientConfig, wallet1.name);

for (let coin of coins) {
if (coin.statechain_id === statechainId && coin.status === CoinStatus.IN_MEMPOOL && !isDepositInMempool) {
isDepositInMempool = true;
} else if (coin.statechain_id === statechainId && coin.status === CoinStatus.CONFIRMED) {
isDepositConfirmed = true;
break;
}
}

if (isDepositInMempool && !areBlocksGenerated) {
areBlocksGenerated = true;
await generateBlocks(clientConfig.confirmationTarget);
}

await new Promise(r => setTimeout(r, 1000));
}

const paymentHash = await mercuryweblib.paymentHash(clientConfig, wallet1.name, statechainId);

let transferAddress = await mercuryweblib.newTransferAddress(wallet2.name);

await mercuryweblib.transferSend(clientConfig, wallet1.name, statechainId, transferAddress.transfer_receive, false, null);

const hashFromServer = await mercuryweblib.getPaymentHash(clientConfig, paymentHash.batchId);

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

let transferReceive = await mercuryweblib.transferReceive(clientConfig, wallet2.name);

expect(transferReceive.isThereBatchLocked).toBe(true);

Check failure on line 555 in clients/tests/web/test/tb04-simple-lightning-latch.test.js

View workflow job for this annotation

GitHub Actions / test

test/tb04-simple-lightning-latch.test.js > Statecoin sender sends coin without batch_id (receiver should still be able to receive, but no pre-image revealed) > expected flow

AssertionError: expected false to be true // Object.is equality - Expected + Received - true + false ❯ test/tb04-simple-lightning-latch.test.js:555:52

Check failure on line 555 in clients/tests/web/test/tb04-simple-lightning-latch.test.js

View workflow job for this annotation

GitHub Actions / test

test/tb04-simple-lightning-latch.test.js > Statecoin sender sends coin without batch_id (receiver should still be able to receive, but no pre-image revealed) > expected flow

AssertionError: expected false to be true // Object.is equality - Expected + Received - true + false ❯ test/tb04-simple-lightning-latch.test.js:555:52

await mercuryweblib.confirmPendingInvoice(clientConfig, wallet1.name, statechainId);

transferReceive = await mercuryweblib.transferReceive(clientConfig, wallet2.name);

expect(transferReceive.isThereBatchLocked).toBe(false);

let toAddress = "bcrt1q805t9k884s5qckkxv7l698hqlz7t6alsfjsqym";

await mercuryweblib.withdrawCoin(clientConfig, wallet2.name, statechainId, toAddress, null, null);

try {
const { preimage } = await mercuryweblib.retrievePreImage(clientConfig, wallet1.name, statechainId, paymentHash.batchId);
} catch (error) {
console.error('Error:', error);
expect(error.message).to.include('failed');
}

let hashPreImage = await sha256(preimage);

expect(hashPreImage).toEqual(paymentHash.hash);
});
}, 50000);

describe('TB04 - Sender sends coin without batch_id, and then resends to a different address (to attempt to steal), and then attempts to retrieve the pre-image, should fail (and LN payment cannot be claimed)', () => {
test("expected flow", async () => {
localStorage.removeItem("mercury-layer:wallet1_tb04");
localStorage.removeItem("mercury-layer:wallet2_tb04");

let wallet1 = await mercuryweblib.createWallet(clientConfig, "wallet1_tb04");
let wallet2 = await mercuryweblib.createWallet(clientConfig, "wallet2_tb04");
let wallet3 = await mercuryweblib.createWallet(clientConfig, "wallet3_tb04");

await mercuryweblib.newToken(clientConfig, wallet1.name);

const amount = 1000;

let result = await mercuryweblib.getDepositBitcoinAddress(clientConfig, wallet1.name, amount);

const statechainId = result.statechain_id;

let isDepositInMempool = false;
let isDepositConfirmed = false;
let areBlocksGenerated = false;

await depositCoin(result.deposit_address, amount);

while (!isDepositConfirmed) {

const coins = await mercuryweblib.listStatecoins(clientConfig, wallet1.name);

for (let coin of coins) {
if (coin.statechain_id === statechainId && coin.status === CoinStatus.IN_MEMPOOL && !isDepositInMempool) {
isDepositInMempool = true;
} else if (coin.statechain_id === statechainId && coin.status === CoinStatus.CONFIRMED) {
isDepositConfirmed = true;
break;
}
}

if (isDepositInMempool && !areBlocksGenerated) {
areBlocksGenerated = true;
await generateBlocks(clientConfig.confirmationTarget);
}

await new Promise(r => setTimeout(r, 1000));
}

const paymentHash = await mercuryweblib.paymentHash(clientConfig, wallet1.name, statechainId);

let transferAddress = await mercuryweblib.newTransferAddress(wallet2.name);

await mercuryweblib.transferSend(clientConfig, wallet1.name, statechainId, transferAddress.transfer_receive, false, null);

const transferAddressSecond = await mercuryweblib.newTransferAddress(wallet3.name);

await mercuryweblib.transferSend(clientConfig, wallet1.name, statechainId, transferAddressSecond.transfer_receive, false, null);

const hashFromServer = await mercuryweblib.getPaymentHash(clientConfig, paymentHash.batchId);

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

let transferReceive = await mercuryweblib.transferReceive(clientConfig, wallet2.name);

expect(transferReceive.isThereBatchLocked).toBe(false);

await mercuryweblib.confirmPendingInvoice(clientConfig, wallet1.name, statechainId);

transferReceive = await mercuryweblib.transferReceive(clientConfig, wallet2.name);

expect(transferReceive.isThereBatchLocked).toBe(false);

let toAddress = "bcrt1q805t9k884s5qckkxv7l698hqlz7t6alsfjsqym";

await mercuryweblib.withdrawCoin(clientConfig, wallet2.name, statechainId, toAddress, null, null);

try {
const { preimage } = await mercuryweblib.retrievePreImage(clientConfig, wallet1.name, statechainId, paymentHash.batchId);
} catch (error) {
console.error('Error:', error);
expect(error.message).to.include('failed');
}

let hashPreImage = await sha256(preimage);

expect(hashPreImage).toEqual(paymentHash.hash);
});
}, 50000);

describe('Coin receiver creates a non hold invoice, and sends to sender (i.e. an invoice with the a different payment hash). Sender should be able to determine this.', () => {
test("expected flow", async () => {

localStorage.removeItem("mercury-layer:wallet1_tb04");

let wallet1 = await mercuryweblib.createWallet(clientConfig, "wallet1_tb04");

await mercuryweblib.newToken(clientConfig, wallet1.name);

const amount = 1000;

let result = await mercuryweblib.getDepositBitcoinAddress(clientConfig, wallet1.name, amount);

const statechainId = result.statechain_id;

let isDepositInMempool = false;
let isDepositConfirmed = false;
let areBlocksGenerated = false;

await depositCoin(result.deposit_address, amount);

while (!isDepositConfirmed) {

const coins = await mercuryweblib.listStatecoins(clientConfig, wallet1.name);

for (let coin of coins) {
if (coin.statechain_id === statechainId && coin.status === CoinStatus.IN_MEMPOOL && !isDepositInMempool) {
isDepositInMempool = true;
} else if (coin.statechain_id === statechainId && coin.status === CoinStatus.CONFIRMED) {
isDepositConfirmed = true;
break;
}
}

if (isDepositInMempool && !areBlocksGenerated) {
areBlocksGenerated = true;
await generateBlocks(clientConfig.confirmationTarget);
}

await new Promise(r => setTimeout(r, 1000));
}

const paymentHash = await mercuryweblib.paymentHash(clientConfig, wallet1.name, statechainId);

const paymentHashSecond = "b4eab5e663aebe5fc645865b27b33c04c4e057e7c844fa61519df6de1398cdb3"
const invoiceSecond = await generateInvoice(paymentHashSecond, amount);

const isInvoiceValid = await mercurynodejslib.verifyInvoice(clientConfig, paymentHash.batchId, invoiceSecond.payment_request);

Check failure on line 712 in clients/tests/web/test/tb04-simple-lightning-latch.test.js

View workflow job for this annotation

GitHub Actions / test

test/tb04-simple-lightning-latch.test.js > Coin receiver creates a non hold invoice, and sends to sender (i.e. an invoice with the a different payment hash). Sender should be able to determine this. > expected flow

ReferenceError: mercurynodejslib is not defined ❯ test/tb04-simple-lightning-latch.test.js:712:32
expect(isInvoiceValid).is.false;
});
}, 50000);

0 comments on commit d2deda3

Please sign in to comment.