From 6ff4790b28dc71a467edbcf0de51c6931a70f972 Mon Sep 17 00:00:00 2001 From: "S. Santos" Date: Mon, 12 Aug 2024 09:40:04 -0300 Subject: [PATCH] Add payment hash verification to web client --- clients/libs/web/lightning-latch.js | 21 ++++++++++++++++++- clients/libs/web/main.js | 8 ++++++- clients/tests/web/package.json | 3 ++- .../test/tb04-simple-lightning-latch.test.js | 4 ++++ clients/tests/web/vitest.workspace.js | 9 ++++---- token-server/Cargo.toml | 2 +- 6 files changed, 39 insertions(+), 8 deletions(-) diff --git a/clients/libs/web/lightning-latch.js b/clients/libs/web/lightning-latch.js index f032f731..f105890d 100644 --- a/clients/libs/web/lightning-latch.js +++ b/clients/libs/web/lightning-latch.js @@ -113,4 +113,23 @@ const retrievePreImage = async (clientConfig, walletName, statechainId, batchId) return { preimage: response?.data?.preimage }; } -export default { createPreImage, confirmPendingInvoice, retrievePreImage }; +const getPaymentHash = async (clientConfig, batchId) => { + + const url = `${clientConfig.statechainEntity}/transfer/paymenthash/${batchId}`; + + try { + let response = await axios.get(url); + + return response?.data?.hash; + + } catch (error) { + if (error.response.status == 401) { + return null; + } else { + throw new Error(`Failed to retrieve payment hash: ${JSON.stringify(error.response.data)}`); + } + } + +} + +export default { createPreImage, confirmPendingInvoice, retrievePreImage, getPaymentHash }; diff --git a/clients/libs/web/main.js b/clients/libs/web/main.js index 6d4930f9..5da25a5b 100644 --- a/clients/libs/web/main.js +++ b/clients/libs/web/main.js @@ -122,6 +122,11 @@ const retrievePreImage = async (clientConfig, walletName, statechainId, batchId) return await lightningLatch.retrievePreImage(clientConfig, walletName, statechainId, batchId); } +const getPaymentHash = async (clientConfig, batchId) => { + + return await lightningLatch.getPaymentHash(clientConfig, batchId); +} + export default { greet, createWallet, @@ -135,5 +140,6 @@ export default { transferReceive, paymentHash, confirmPendingInvoice, - retrievePreImage + retrievePreImage, + getPaymentHash } diff --git a/clients/tests/web/package.json b/clients/tests/web/package.json index 6e021223..466c1efb 100644 --- a/clients/tests/web/package.json +++ b/clients/tests/web/package.json @@ -8,7 +8,8 @@ "build": "vite build", "preview": "vite preview", "test": "vitest", - "test:browser": "vitest --workspace=vitest.workspace.js" + "test:browser": "vitest --workspace=vitest.workspace.js", + "test:headless": "vitest --browser.name=chromium --browser.headless --exclude '**/data_bitcoin_regtest/**'" }, "dependencies": { "axios": "^1.7.2", diff --git a/clients/tests/web/test/tb04-simple-lightning-latch.test.js b/clients/tests/web/test/tb04-simple-lightning-latch.test.js index ef9e030b..c92b26cb 100644 --- a/clients/tests/web/test/tb04-simple-lightning-latch.test.js +++ b/clients/tests/web/test/tb04-simple-lightning-latch.test.js @@ -85,6 +85,10 @@ describe('TB04 - Simple Lightning Latch', () => { await mercuryweblib.transferSend(clientConfig, wallet1.name, statechainId, transferAddress.transfer_receive, false, paymentHash.batchId ); + 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); diff --git a/clients/tests/web/vitest.workspace.js b/clients/tests/web/vitest.workspace.js index c72e861d..c7917684 100644 --- a/clients/tests/web/vitest.workspace.js +++ b/clients/tests/web/vitest.workspace.js @@ -16,10 +16,11 @@ export default defineWorkspace([ // https://playwright.dev providerOptions: {}, }, + exclude:[ + ...configDefaults.exclude, + '**/data_bitcoin_regtest/**' + ], }, - exclude:[ - ...configDefaults.exclude, - './data_bitcoin_regtest/*' - ], + }, ]) diff --git a/token-server/Cargo.toml b/token-server/Cargo.toml index 829e905f..d43f7f28 100644 --- a/token-server/Cargo.toml +++ b/token-server/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" config = "0.13.1" reqwest = { version = "0.11.16", features = ["blocking", "json", "socks"] } rocket = { version = "0.5.0-rc", features = ["json"] } -sqlx = { version = "0.8.0", features = [ "runtime-tokio", "postgres", "time", "uuid", "tls-rustls" ] } +sqlx = { version = "0.7", features = [ "runtime-tokio", "postgres", "time", "uuid", "tls-rustls" ] } serde = { version = "1.0.163", features = ["derive"] } serde_json = "1.0.96" schemars = { version = "0.8.12", features = ["chrono", "uuid"] }