From 808c3cc2064b1a8904304bf440461e4817a58415 Mon Sep 17 00:00:00 2001 From: ShahanaFarooqui Date: Mon, 20 Feb 2023 23:23:31 -0800 Subject: [PATCH 1/3] Offers Upgrade Offers Upgrade --- controllers/getinfo.js | 1 + controllers/offers.js | 57 ++++++++++++++--------- lightning-client-js.js | 4 +- package-lock.json | 100 ++++++++++++++++++++++++++++++++++------- package.json | 4 +- 5 files changed, 127 insertions(+), 39 deletions(-) diff --git a/controllers/getinfo.js b/controllers/getinfo.js index eb55a1b..a4ce675 100644 --- a/controllers/getinfo.js +++ b/controllers/getinfo.js @@ -127,6 +127,7 @@ exports.getinfo = (req,res) => { //Call the getinfo command ln.getinfo().then(data => { + global.version = data.version; data.api_version = require('../package.json').version; global.logger.log('getinfo success'); res.status(200).json(data); diff --git a/controllers/offers.js b/controllers/offers.js index afde0ed..44fdfb6 100644 --- a/controllers/offers.js +++ b/controllers/offers.js @@ -1,4 +1,15 @@ -//This controller houses all the offers functions +const isVersionCompatible = (currentVersion, checkVersion) => { + if (currentVersion) { + const versionsArr = currentVersion.trim()?.replace('v', '').split('-')[0].split('.') || []; + const checkVersionsArr = checkVersion.split('.'); + return (+versionsArr[0] > +checkVersionsArr[0]) || + (+versionsArr[0] === +checkVersionsArr[0] && +versionsArr[1] > +checkVersionsArr[1]) || + (+versionsArr[0] === +checkVersionsArr[0] && +versionsArr[1] === +checkVersionsArr[1] && +versionsArr[2] >= +checkVersionsArr[2]); + } + return false; +}; + + //This controller houses all the offers functions //Function # 1 //Invoke the 'offer' command to setup an offer @@ -28,7 +39,7 @@ * required: * - description * - in: body -* name: vendor +* name: vendor/issuer * description: Reflects who is issuing this offer * type: string * - in: body @@ -37,11 +48,11 @@ * type: string * - in: body * name: quantity_min -* description: The presence of quantity_min or quantity_max indicates that the invoice can specify more than one of the items within this (inclusive) range +* description: Available only in versions < 22.11.x. The presence of quantity_min or quantity_max indicates that the invoice can specify more than one of the items within this (inclusive) range. * type: number * - in: body * name: quantity_max -* description: The presence of quantity_min or quantity_max indicates that the invoice can specify more than one of the items within this (inclusive) range +* description: The presence of quantity_max indicates that the invoice can specify more than one of the items within this (inclusive) range * type: number * - in: body * name: absolute_expiry @@ -89,7 +100,7 @@ * description: The bolt12 offer, starting with "lno1" * bolt12_unsigned: * type: string -* description: The bolt12 encoding of the offer, without a signature +* description: Available only in versions < 22.11.x. The bolt12 encoding of the offer, without a signature. * used: * type: boolean * description: true if an associated invoice has been paid @@ -107,13 +118,13 @@ exports.offer = (req,res) => { function connFailed(err) { throw err } ln.on('error', connFailed); + //Set required params var amnt = req.body.amount; var desc = req.body.description; //Set optional params - var vndr = (req.body.vendor) ? req.body.vendor : null; + var issuer = req.body.issuer ? req.body.issuer : req.body.vendor ? req.body.vendor : null; var lbl = (req.body.label) ? req.body.label : null; - var qty_min = (req.body.quantity_min) ? req.body.quantity_min : null; var qty_max = (req.body.quantity_max) ? req.body.quantity_max : null; var abs_expry = (req.body.absolute_expiry) ? req.body.absolute_expiry : null; var rcrnc = (req.body.recurrence) ? req.body.recurrence : null; @@ -121,21 +132,27 @@ exports.offer = (req,res) => { var rcrnc_wndw = (req.body.recurrence_paywindow) ? req.body.recurrence_paywindow : null; var rcrnc_lmt = (req.body.recurrence_limit) ? req.body.recurrence_limit : null; var sngl_use = (req.body.single_use === '0' || req.body.single_use === 'false' || !req.body.single_use) ? false : true; + var qty_min = (req.body.quantity_min) ? req.body.quantity_min : null; //Call the fundchannel command with the pub key and amount specified - ln.offer(amount=amnt, - description=desc, - vendor=vndr, - label=lbl, - quantity_min=qty_min, - quantity_max=qty_max, - absolute_expiry=abs_expry, - recurrence=rcrnc, - recurrence_base=rcrnc_base, - recurrence_paywindow=rcrnc_wndw, - recurrence_limit=rcrnc_lmt, - single_use=sngl_use - ).then(data => { + var offerData = {}; + + // if (!(global.version && isVersionCompatible(global.version, '22.11.1'))) { + if (!(global.version && isVersionCompatible('22.11.1', '22.11.0'))) { + offerData = {amount:amnt, description:desc, vendor:issuer, label:lbl, + quantity_min:qty_min, quantity_max:qty_max, absolute_expiry:abs_expry, + recurrence:rcrnc, recurrence_base:rcrnc_base, recurrence_paywindow:rcrnc_wndw, + recurrence_limit:rcrnc_lmt, single_use:sngl_use}; + } else { + offerData = {amount:amnt, description:desc, issuer:issuer, label:lbl, + quantity_max:qty_max, absolute_expiry:abs_expry, + recurrence:rcrnc, recurrence_base:rcrnc_base, recurrence_paywindow:rcrnc_wndw, + recurrence_limit:rcrnc_lmt, single_use:sngl_use}; + } + + global.logger.warn(offerData); + + ln.offer(offerData).then(data => { global.logger.log('offer creation success'); res.status(201).json(data); }).catch(err => { diff --git a/lightning-client-js.js b/lightning-client-js.js index 6d2a062..5805b69 100644 --- a/lightning-client-js.js +++ b/lightning-client-js.js @@ -148,7 +148,9 @@ class LightningClient extends EventEmitter { if (typeof global.REST_PLUGIN_CONFIG === 'undefined') { this.parser.write(data) } else { - somedata += data + if(data.length && data.length > 0) { + somedata += data + } if (somedata.length > 1 && somedata.slice(-2) === "\n\n") { this.parser.write(somedata) somedata = '' diff --git a/package-lock.json b/package-lock.json index 0023050..956237b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "c-lightning-rest", - "version": "0.10.0", + "version": "0.10.1", "lockfileVersion": 2, "requires": true, "packages": { @@ -10,7 +10,7 @@ "license": "MIT", "dependencies": { "atob": "^2.1.2", - "body-parser": "^1.19.0", + "body-parser": "^1.20.1", "clightningjs": "^0.2.2", "error": "^7.0.2", "express": "^4.16.4", @@ -235,9 +235,9 @@ } }, "node_modules/body-parser": { - "version": "1.20.0", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.0.tgz", - "integrity": "sha512-DfJ+q6EPcGKZD1QWUjSpqp+Q7bDQTsQIF4zfUAtZ6qk+H/3/QRhg9CEp39ss+/T2vw0+HaidC0ecJj/DRLIaKg==", + "version": "1.20.1", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", + "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", "dependencies": { "bytes": "3.1.2", "content-type": "~1.0.4", @@ -247,7 +247,7 @@ "http-errors": "2.0.0", "iconv-lite": "0.4.24", "on-finished": "2.4.1", - "qs": "6.10.3", + "qs": "6.11.0", "raw-body": "2.5.1", "type-is": "~1.6.18", "unpipe": "1.0.0" @@ -257,6 +257,20 @@ "npm": "1.2.8000 || >= 1.4.16" } }, + "node_modules/body-parser/node_modules/qs": { + "version": "6.11.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", + "integrity": "sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkIk4Q==", + "dependencies": { + "side-channel": "^1.0.4" + }, + "engines": { + "node": ">=0.6" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -531,9 +545,9 @@ "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, "node_modules/cookiejar": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/cookiejar/-/cookiejar-2.1.3.tgz", - "integrity": "sha512-JxbCBUdrfr6AQjOXrxoTvAMJO4HBTUIlBzslcJPAz+/KT8yk53fXun51u+RenNYvad/+Vc2DIz5o9UxlCDymFQ==", + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/cookiejar/-/cookiejar-2.1.4.tgz", + "integrity": "sha512-LDx6oHrK+PhzLKJU9j5S7/Y3jM/mUHvD/DeI1WQmJn652iPC5Y4TBzC9l+5OMOXlyTTA+SmVUPm0HQUwpD5Jqw==", "dev": true }, "node_modules/core-util-is": { @@ -761,6 +775,29 @@ "node": ">= 0.10.0" } }, + "node_modules/express/node_modules/body-parser": { + "version": "1.20.0", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.0.tgz", + "integrity": "sha512-DfJ+q6EPcGKZD1QWUjSpqp+Q7bDQTsQIF4zfUAtZ6qk+H/3/QRhg9CEp39ss+/T2vw0+HaidC0ecJj/DRLIaKg==", + "dependencies": { + "bytes": "3.1.2", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "1.2.0", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "on-finished": "2.4.1", + "qs": "6.10.3", + "raw-body": "2.5.1", + "type-is": "~1.6.18", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8", + "npm": "1.2.8000 || >= 1.4.16" + } + }, "node_modules/extend": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", @@ -2838,9 +2875,9 @@ "dev": true }, "body-parser": { - "version": "1.20.0", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.0.tgz", - "integrity": "sha512-DfJ+q6EPcGKZD1QWUjSpqp+Q7bDQTsQIF4zfUAtZ6qk+H/3/QRhg9CEp39ss+/T2vw0+HaidC0ecJj/DRLIaKg==", + "version": "1.20.1", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", + "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", "requires": { "bytes": "3.1.2", "content-type": "~1.0.4", @@ -2850,10 +2887,20 @@ "http-errors": "2.0.0", "iconv-lite": "0.4.24", "on-finished": "2.4.1", - "qs": "6.10.3", + "qs": "6.11.0", "raw-body": "2.5.1", "type-is": "~1.6.18", "unpipe": "1.0.0" + }, + "dependencies": { + "qs": { + "version": "6.11.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", + "integrity": "sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkIk4Q==", + "requires": { + "side-channel": "^1.0.4" + } + } } }, "brace-expansion": { @@ -3067,9 +3114,9 @@ "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, "cookiejar": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/cookiejar/-/cookiejar-2.1.3.tgz", - "integrity": "sha512-JxbCBUdrfr6AQjOXrxoTvAMJO4HBTUIlBzslcJPAz+/KT8yk53fXun51u+RenNYvad/+Vc2DIz5o9UxlCDymFQ==", + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/cookiejar/-/cookiejar-2.1.4.tgz", + "integrity": "sha512-LDx6oHrK+PhzLKJU9j5S7/Y3jM/mUHvD/DeI1WQmJn652iPC5Y4TBzC9l+5OMOXlyTTA+SmVUPm0HQUwpD5Jqw==", "dev": true }, "core-util-is": { @@ -3239,6 +3286,27 @@ "type-is": "~1.6.18", "utils-merge": "1.0.1", "vary": "~1.1.2" + }, + "dependencies": { + "body-parser": { + "version": "1.20.0", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.0.tgz", + "integrity": "sha512-DfJ+q6EPcGKZD1QWUjSpqp+Q7bDQTsQIF4zfUAtZ6qk+H/3/QRhg9CEp39ss+/T2vw0+HaidC0ecJj/DRLIaKg==", + "requires": { + "bytes": "3.1.2", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "1.2.0", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "on-finished": "2.4.1", + "qs": "6.10.3", + "raw-body": "2.5.1", + "type-is": "~1.6.18", + "unpipe": "1.0.0" + } + } } }, "extend": { diff --git a/package.json b/package.json index b4c95d7..93d0f2c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "c-lightning-rest", - "version": "0.10.0", + "version": "0.10.1", "description": "c-lightning REST API suite", "main": "cl-rest.js", "scripts": { @@ -17,7 +17,7 @@ "license": "MIT", "dependencies": { "atob": "^2.1.2", - "body-parser": "^1.19.0", + "body-parser": "^1.20.1", "clightningjs": "^0.2.2", "error": "^7.0.2", "express": "^4.16.4", From bf4b7fd4b6490415cfa6addc28e044d1ca8b19fa Mon Sep 17 00:00:00 2001 From: ShahanaFarooqui Date: Mon, 20 Feb 2023 23:40:26 -0800 Subject: [PATCH 2/3] Removed comment --- controllers/offers.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/controllers/offers.js b/controllers/offers.js index 44fdfb6..ec3ed39 100644 --- a/controllers/offers.js +++ b/controllers/offers.js @@ -137,8 +137,7 @@ exports.offer = (req,res) => { //Call the fundchannel command with the pub key and amount specified var offerData = {}; - // if (!(global.version && isVersionCompatible(global.version, '22.11.1'))) { - if (!(global.version && isVersionCompatible('22.11.1', '22.11.0'))) { + if (!(global.version && isVersionCompatible(global.version, '22.11.1'))) { offerData = {amount:amnt, description:desc, vendor:issuer, label:lbl, quantity_min:qty_min, quantity_max:qty_max, absolute_expiry:abs_expry, recurrence:rcrnc, recurrence_base:rcrnc_base, recurrence_paywindow:rcrnc_wndw, From d6c959de84b320a8f40946e6ebe3de802de21e1d Mon Sep 17 00:00:00 2001 From: ShahanaFarooqui Date: Tue, 21 Feb 2023 10:56:26 -0800 Subject: [PATCH 3/3] Create Offer call signature fix Create Offer call signature fix --- cl-rest.js | 2 +- controllers/offers.js | 43 ++++++++++++++++++++++++------------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/cl-rest.js b/cl-rest.js index 980deec..98a71ca 100644 --- a/cl-rest.js +++ b/cl-rest.js @@ -141,7 +141,7 @@ server.listen(PORT, BIND, function() { //Start the docserver docserver.listen(DOCPORT, BIND, function() { - global.logger.warn('--- cl-rest doc server is ready and listening on ' + BIND + ':' + PORT + ' ---'); + global.logger.warn('--- cl-rest doc server is ready and listening on ' + BIND + ':' + DOCPORT + ' ---'); }) exports.closeServer = function(){ diff --git a/controllers/offers.js b/controllers/offers.js index ec3ed39..16bc6e6 100644 --- a/controllers/offers.js +++ b/controllers/offers.js @@ -135,29 +135,34 @@ exports.offer = (req,res) => { var qty_min = (req.body.quantity_min) ? req.body.quantity_min : null; //Call the fundchannel command with the pub key and amount specified - var offerData = {}; - if (!(global.version && isVersionCompatible(global.version, '22.11.1'))) { - offerData = {amount:amnt, description:desc, vendor:issuer, label:lbl, - quantity_min:qty_min, quantity_max:qty_max, absolute_expiry:abs_expry, - recurrence:rcrnc, recurrence_base:rcrnc_base, recurrence_paywindow:rcrnc_wndw, - recurrence_limit:rcrnc_lmt, single_use:sngl_use}; + ln.offer( + amount=amnt, description=desc, vendor=issuer, label=lbl, + quantity_min=qty_min, quantity_max=qty_max, absolute_expiry=abs_expry, + recurrence=rcrnc, recurrence_base=rcrnc_base, recurrence_paywindow=rcrnc_wndw, + recurrence_limit=rcrnc_lmt, single_use=sngl_use + ).then(data => { + global.logger.log('offer creation success'); + res.status(201).json(data); + }).catch(err => { + global.logger.warn(err); + res.status(500).json({error: err}); + }); } else { - offerData = {amount:amnt, description:desc, issuer:issuer, label:lbl, - quantity_max:qty_max, absolute_expiry:abs_expry, - recurrence:rcrnc, recurrence_base:rcrnc_base, recurrence_paywindow:rcrnc_wndw, - recurrence_limit:rcrnc_lmt, single_use:sngl_use}; + ln.offer( + amount=amnt, description=desc, issuer=issuer, label=lbl, + quantity_max=qty_max, absolute_expiry=abs_expry, + recurrence=rcrnc, recurrence_base=rcrnc_base, recurrence_paywindow=rcrnc_wndw, + recurrence_limit=rcrnc_lmt, single_use=sngl_use + ).then(data => { + global.logger.log('offer creation success'); + res.status(201).json(data); + }).catch(err => { + global.logger.warn(err); + res.status(500).json({error: err}); + }); } - global.logger.warn(offerData); - - ln.offer(offerData).then(data => { - global.logger.log('offer creation success'); - res.status(201).json(data); - }).catch(err => { - global.logger.warn(err); - res.status(500).json({error: err}); - }); ln.removeListener('error', connFailed); }