Skip to content

Commit

Permalink
Add payment hash verification to nodeJS client
Browse files Browse the repository at this point in the history
  • Loading branch information
ssantos21 committed Aug 6, 2024
1 parent 579d35b commit 746f5cd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
10 changes: 10 additions & 0 deletions clients/apps/nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ async function main() {

console.log(JSON.stringify(res, null, 2));
});

program.command('get-payment-hash')
.description('Confirm a pending invoice for lightning latch')
.argument('<batch-id>', 'transfer batch id')
.action(async (batch_id) => {

let res = await mercurynodejslib.getPaymentHash(clientConfig, batch_id);

console.log(JSON.stringify(res, null, 2));
});

program.parse();

Expand Down
4 changes: 4 additions & 0 deletions clients/apps/nodejs/test/tb04-simple-lightning-latch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ describe('TB04 - Lightning Latch', function() {

await mercurynodejslib.transferSend(clientConfig, wallet1.name, coin.statechain_id, transferAddress.transfer_receive, false, paymentHash.batchId);

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

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

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

expect(transferReceiveResult.isThereBatchLocked).is.true;
Expand Down
8 changes: 7 additions & 1 deletion clients/libs/nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ const retrievePreImage = async (clientConfig, walletName, statechainId, batchId)
return preImage;
}

const getPaymentHash = async (clientConfig, batchId) => {

return await lightningLatch.getPaymentHash(clientConfig, batchId);
}

module.exports = {
createWallet,
newToken,
Expand All @@ -260,5 +265,6 @@ module.exports = {
transferReceive,
paymentHash,
confirmPendingInvoice,
retrievePreImage
retrievePreImage,
getPaymentHash
};
27 changes: 26 additions & 1 deletion clients/libs/nodejs/lightning-latch.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,29 @@ const retrievePreImage = async (clientConfig, db, walletName, statechainId, batc
return { preimage: response?.data?.preimage };
}

module.exports = { createPreImage, confirmPendingInvoice, retrievePreImage };
const getPaymentHash = async (clientConfig, batchId) => {

const url = `${clientConfig.statechainEntity}/transfer/paymenthash/${batchId}`;

const torProxy = clientConfig.torProxy;

let socksAgent = undefined;

if (torProxy) {
socksAgent = { httpAgent: new SocksProxyAgent(torProxy) };
}

try {
const response = await axios.get(url, socksAgent);
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)}`);
}
}
}

module.exports = { createPreImage, confirmPendingInvoice, retrievePreImage, getPaymentHash };

0 comments on commit 746f5cd

Please sign in to comment.