From 7c99f41e48420b4e3f40acd914ad83c7895843f0 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Mon, 10 Oct 2022 19:23:20 +0530 Subject: [PATCH] API for close-blockchain-bonds --- docs/scripts/floExchangeAPI.js | 31 +++++++++++++++++++++++++++++++ src/app.js | 3 +++ src/request.js | 16 ++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/docs/scripts/floExchangeAPI.js b/docs/scripts/floExchangeAPI.js index 959b455..b49a75d 100644 --- a/docs/scripts/floExchangeAPI.js +++ b/docs/scripts/floExchangeAPI.js @@ -1307,6 +1307,37 @@ }) } + exchangeAPI.closeBlockchainBond = function (bond_id, floID, privKey) { + return new Promise((resolve, reject) => { + if (!floCrypto.verifyPrivKey(privKey, floID)) + return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, "Invalid Private Key", errorCode.INVALID_PRIVATE_KEY)); + let request = { + floID: floID, + bond_id: bond_id, + timestamp: Date.now() + }; + request.pubKey = floCrypto.getPubKeyHex(privKey); + request.sign = signRequest({ + type: "close_blockchain_bond", + bond_id: data.bond_id, + timestamp: data.timestamp + }, privKey); + console.debug(request); + + fetch_api('/close-blockchain-bonds', { + method: "POST", + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(request) + }).then(result => { + responseParse(result) + .then(result => resolve(result)) + .catch(error => reject(error)) + }).catch(error => reject(error)) + }) + } + exchangeAPI.init = function refreshDataFromBlockchain(adminID = floGlobals.adminID, appName = floGlobals.application) { return new Promise((resolve, reject) => { let nodes, assets, tags, lastTx; diff --git a/src/app.js b/src/app.js index cb63e7e..4892120 100644 --- a/src/app.js +++ b/src/app.js @@ -89,6 +89,9 @@ module.exports = function App(secret, DB) { app.post('/convert-to', Request.ConvertTo); app.post('/convert-from', Request.ConvertFrom); + //close blockchain-bond + app.post('/close-blockchain-bonds', Request.CloseBlockchainBond); + //Manage user tags (Access to trusted IDs only) app.post('/add-tag', Request.AddUserTag); app.post('/remove-tag', Request.RemoveUserTag); diff --git a/src/request.js b/src/request.js index 67a4710..ed9a932 100644 --- a/src/request.js +++ b/src/request.js @@ -2,6 +2,7 @@ const market = require("./market"); const conversion = require('./services/conversion'); +const blokchain_bonds = require("./services/bonds"); const { SIGN_EXPIRE_TIME, @@ -322,6 +323,18 @@ function ConvertFrom(req, res) { }, () => conversion.convertFromCoin(data.floID, data.txid, data.coin)); } +function CloseBlockchainBond(req, res) { + let data = req.body; + if (!data.pubKey) + res.status(INVALID.e_code).send(INVALID.str(eCode.MISSING_PARAMETER, "Public key missing")); + else + processRequest(res, data.floID, data.pubKey, data.sign, "Conversion", { + type: "close_blockchain_bond", + bond_id: data.bond_id, + timestamp: data.timestamp + }, () => blokchain_bonds.closeBond(data.bond_id, data.floID, `${data.timestamp}.${data.sign}`)); +} + /* Public Requests */ function GetLoginCode(req, res) { @@ -517,6 +530,7 @@ module.exports = { RemoveDistributor, ConvertTo, ConvertFrom, + CloseBlockchainBond, set trustedIDs(ids) { trustedIDs = ids; }, @@ -532,6 +546,8 @@ module.exports = { set DB(db) { DB = db; market.DB = db; + conversion.DB; + blokchain_bonds.DB; }, set secret(s) { secret = s;