From 402e0ab6fb1431117d86aba2af45103512e1c11d Mon Sep 17 00:00:00 2001 From: Abhishek Sinha Date: Fri, 1 Feb 2019 19:10:10 +0530 Subject: [PATCH] added autoupdate external scripts feature --- supernode/index.html | 599 ++++++++++++++++++++++++++----------------- 1 file changed, 357 insertions(+), 242 deletions(-) diff --git a/supernode/index.html b/supernode/index.html index 642da63..eef0c20 100644 --- a/supernode/index.html +++ b/supernode/index.html @@ -9104,10 +9104,13 @@ // remove this line later // btcTradeMargin is tolerable difference between Crypto trader should deposit and cryptos he actually deposited RMAssets = - `tradableAsset1=BTC,FLO,BTC_TEST,FLO_TEST#!#tradableAsset2=INR,USD,BTC,FLO,BTC_TEST,FLO_TEST#!#supernodes=127.0.0.1,212.88.88.2#!#MASTER_NODE=023B9F60692A17FAC805D012C5C8ADA3DD19A980A3C5F0D8A5B3500CC54D6E8B75 + `tradableAsset1=BTC,FLO,BTC_TEST,FLO_TEST#!#tradableAsset2=INR,USD,BTC,FLO,BTC_TEST,FLO_TEST + #!#supernodes=127.0.0.1,212.88.88.2#!#MASTER_NODE=023B9F60692A17FAC805D012C5C8ADA3DD19A980A3C5F0D8A5B3500CC54D6E8B75 #!#MASTER_RECEIVING_ADDRESS=oVRq2nka1GtALQT8pbuLHAGjqAQ7PAo6uy#!#validTradingAmount=10000,50000,100000#!#btcTradeMargin=5000 - #!#supernodesPubKeys=033038E5F9A9CD59F21E4E1577BAC732DF7F4988634CF1ADE2EB74FC512689FC27,038C6D5C0DF3AA49501C20DD8333F9F67A854E87AA639A688FAE4F434718F2792D, - #!#d3js=58f54395efa8346e8e94d12609770f66b916897e7f4e05f6c98780cffa5c70a3#!#ShamirsMaxShares=8`; + #!#supernodesPubKeys=02BCBC8186CE5E978BCBC6B70F6CAD8FED936A725FEF377EFE4A56AD6E6D4E8392, + #!#externalFiles={"testjs":"bebabf9b08571c23064b35c25850fdbc004044779af58f6327e3b316410f5884", + "d3js":"58f54395efa8346e8e94d12609770f66b916897e7f4e05f6c98780cffa5c70a3"} + #!#ShamirsMaxShares=8`; let floAssetsArray = RMAssets.split('#!#'); if (floAssetsArray.length > 0 && typeof floAssetsArray[0] !== undefined && @@ -9554,7 +9557,7 @@ let publicKey_for_users_entered_private_key; try { - publicKey_for_users_entered_private_key = RM_WALLET.generateFloKeys(users_entered_private_key).pubKeyHex; + publicKey_for_users_entered_private_key = localbitcoinplusplus.wallets.prototype.generateFloKeys(users_entered_private_key).pubKeyHex; } catch (error) { throw new Error(error); } @@ -10032,232 +10035,234 @@ await localbitcoinplusplus.trade.prototype.resolve_current_crypto_price_in_fiat(params.product, params.currency); let trade_margin = await localbitcoinplusplus.trade.prototype.getAssetTradeAndWithdrawLimit(params.trader_flo_address, params.product, params.currency); - if (localbitcoinplusplus.master_configurations.tradableAsset1.includes(params.product)) { - let eqCrypto = localbitcoinplusplus.trade.prototype.calculateCryptoEquivalentOfCash(params.withdrawing_amount); - if (trade_margin.remaining_crypto_credit<0 && trade_margin.remaining_crypto_credit 0) { - let withdrawer_btc_balance = parseFloat(btc_balance_res.crypto_balance); - let withdrawing_btc_amount_in_cash = parseFloat(params.withdrawing_amount); - if(!localbitcoinplusplus.master_configurations.tradableAsset2.includes(params.currency)) { - throw new Error("Invalid or unsupported currency."); - } - let eqBTC = localbitcoinplusplus.trade.prototype.calculateCryptoEquivalentOfCash( - withdrawing_btc_amount_in_cash, params.currency, params.product); - eqBTC = parseFloat(eqBTC).toFixed(8); - let withdrawer_new_btc_balance = withdrawer_btc_balance - eqBTC; - if (withdrawer_new_btc_balance > 0 && - withdrawer_btc_balance > 0 && - withdrawing_btc_amount_in_cash > 0 && - eqBTC > 0 && eqBTC <= withdrawer_btc_balance) { + params.id = helper_functions.unique_id(); + params.status = 1; + if (localbitcoinplusplus.master_configurations.tradableAsset1.includes(params.product)) { + // Check how much cryptos the user can withdraw + let withdrawer_btc_id = `${params.trader_flo_address}_${params.product}`; + readDB("crypto_balances", withdrawer_btc_id).then(function (btc_balance_res) { + if (typeof btc_balance_res == "object" && typeof btc_balance_res + .trader_flo_address == "string" && + btc_balance_res.crypto_balance > 0) { + let withdrawer_btc_balance = parseFloat(btc_balance_res.crypto_balance); + let withdrawing_btc_amount_in_cash = parseFloat(params.withdrawing_amount); + if(!localbitcoinplusplus.master_configurations.tradableAsset2.includes(params.currency)) { + throw new Error("Invalid or unsupported currency."); + } + let eqBTC = localbitcoinplusplus.trade.prototype.calculateCryptoEquivalentOfCash( + withdrawing_btc_amount_in_cash, params.currency, params.product); + eqBTC = parseFloat(eqBTC).toFixed(8); + let withdrawer_new_btc_balance = withdrawer_btc_balance - eqBTC; + if (withdrawer_new_btc_balance > 0 && + withdrawer_btc_balance > 0 && + withdrawing_btc_amount_in_cash > 0 && + eqBTC > 0 && eqBTC <= withdrawer_btc_balance) { - // Now details of Bitcoins can be sent to withdrawer + // Now details of Bitcoins can be sent to withdrawer - /**************************************************************************** - ***********IMPORTANT: CHANGE RECEIVING ADDRESS TO BTC THAN FLO HERE********** - ***********AND DO SOMETHING ABOUT PRIVATE KEY BELOW************************** - ****************************************************************************/ - let sum_total_btc = 0; - let valid_utxo_list = []; - let receiverBTCAddress = params.receivinAddress - .trim(); + /**************************************************************************** + ***********IMPORTANT: CHANGE RECEIVING ADDRESS TO BTC THAN FLO HERE********** + ***********AND DO SOMETHING ABOUT PRIVATE KEY BELOW************************** + ****************************************************************************/ + let sum_total_btc = 0; + let valid_utxo_list = []; + let receiverBTCAddress = params.receivinAddress + .trim(); - readAllDB("deposit").then(function (deposit_list) { - if (typeof deposit_list == "object" && - deposit_list.length > 0) { - deposit_list = deposit_list.filter( - deposits => deposits.status == 2 - && localbitcoinplusplus.master_configurations.tradableAsset1.includes(deposits.product) - && params.product==deposits.product); - for (const dl in deposit_list) { - if (deposit_list.hasOwnProperty(dl)) { - const deposit_dl = deposit_list[dl]; - sum_total_btc += parseFloat(deposit_dl.bitcoinToBePaid); - - if (eqBTC <= sum_total_btc) { - valid_utxo_list.push(deposit_dl); - break; - } else { - valid_utxo_list.push(deposit_dl); - } + readAllDB("deposit").then(function (deposit_list) { + if (typeof deposit_list == "object" && + deposit_list.length > 0) { + deposit_list = deposit_list.filter( + deposits => deposits.status == 2 + && localbitcoinplusplus.master_configurations.tradableAsset1.includes(deposits.product) + && params.product==deposits.product); + for (const dl in deposit_list) { + if (deposit_list.hasOwnProperty(dl)) { + const deposit_dl = deposit_list[dl]; + sum_total_btc += parseFloat(deposit_dl.bitcoinToBePaid); + + if (eqBTC <= sum_total_btc) { + valid_utxo_list.push(deposit_dl); + break; + } else { + valid_utxo_list.push(deposit_dl); } } - let valid_btc_list = valid_utxo_list.map(deposit_arr => { - deposit_arr.status = 3 // Deposited Bitcoin is under process - updateinDB("deposit", deposit_arr, deposit_arr.trader_flo_address); - - // save the address and id in a table - let withdraw_id = helper_functions.unique_id(); - const withdraw_btc_order_object = { - id: withdraw_id, - trader_flo_address: params.trader_flo_address, - utxo_addr: deposit_arr.btc_address, - receiverBTCAddress: params.receivinAddress, - receiverBTCEquivalentInCash: withdrawing_btc_amount_in_cash, - currency: params.currency, - product: params.product, - change_adress:deposit_arr.btc_address, - timestamp: + new Date() - } - addDB('withdraw_btc', withdraw_btc_order_object); - return {withdraw_id:withdraw_id, deposited_btc_address:deposit_arr.btc_address}; - }); - - // doSend btc_private_key_shamirs_id from system_btc_reserves_private_keys - valid_btc_list.map(vbl=>{ - readDBbyIndex('system_btc_reserves_private_keys', 'btc_address', vbl.deposited_btc_address).then(function(res) { - let retrieve_pvtkey_req_id = res[0].id; - res[0].btc_private_key_shamirs_id.map(bpks=>{ - let retrieve_pvtkey_req = localbitcoinplusplus.rpc.prototype - .send_rpc - .call(this, "send_back_shamirs_secret_btc_pvtkey", - { retrieve_pvtkey_req_id:retrieve_pvtkey_req_id, - chunk_val:bpks, - withdraw_id:vbl.withdraw_id - }); - doSend(retrieve_pvtkey_req); - }); + } + let valid_btc_list = valid_utxo_list.map(deposit_arr => { + deposit_arr.status = 3 // Deposited Bitcoin is under process + updateinDB("deposit", deposit_arr, deposit_arr.trader_flo_address); + + // save the address and id in a table + let withdraw_id = helper_functions.unique_id(); + const withdraw_btc_order_object = { + id: withdraw_id, + trader_flo_address: params.trader_flo_address, + utxo_addr: deposit_arr.btc_address, + receiverBTCAddress: params.receivinAddress, + receiverBTCEquivalentInCash: withdrawing_btc_amount_in_cash, + currency: params.currency, + product: params.product, + change_adress:deposit_arr.btc_address, + timestamp: + new Date() + } + addDB('withdraw_btc', withdraw_btc_order_object); + return {withdraw_id:withdraw_id, deposited_btc_address:deposit_arr.btc_address}; + }); + + // doSend btc_private_key_shamirs_id from system_btc_reserves_private_keys + valid_btc_list.map(vbl=>{ + readDBbyIndex('system_btc_reserves_private_keys', 'btc_address', vbl.deposited_btc_address).then(function(res) { + let retrieve_pvtkey_req_id = res[0].id; + res[0].btc_private_key_shamirs_id.map(bpks=>{ + let retrieve_pvtkey_req = localbitcoinplusplus.rpc.prototype + .send_rpc + .call(this, "send_back_shamirs_secret_btc_pvtkey", + { retrieve_pvtkey_req_id:retrieve_pvtkey_req_id, + chunk_val:bpks, + withdraw_id:vbl.withdraw_id + }); + doSend(retrieve_pvtkey_req); }); }); - - } - }); + }); + + } + }); - } else { - // Return error to the requester - return { - error: true, - method: "withdrawal_request_response", - data: "Withdrawal request failed: You are trying to withdraw more Bitcoins than you have." - }; - } } else { + console.error(`Withdrawal request failed: You are trying to withdraw more Bitcoins than you have.`); + // Return error to the requester return { error: true, method: "withdrawal_request_response", - data: `Withdrawal request failed: You don't seem to have any Bitcoin balance in the system yet. - Please buy some Bitcoins to withdraw.` + data: "Withdrawal request failed: You are trying to withdraw more Bitcoins than you have." }; } - }); - } else if (!localbitcoinplusplus.master_configurations.tradableAsset1.includes(params.product)) { - // Check if there's no already a withdraw cash order of this user - /*ONLY DELETE A WITHDRAW ORDER WHEN A DEPOSITOR HAS CONFIRMED DEPOSIT - AND RECEIVER HAS CONFIRMED WITHDRAW*/ + } else { + console.error(`Withdrawal request failed: You don't seem to have any Bitcoin balance in the system yet. + Please buy some Bitcoins to withdraw.`); + + // Return error to the requester + return { + error: true, + method: "withdrawal_request_response", + data: `Withdrawal request failed: You don't seem to have any Bitcoin balance in the system yet. + Please buy some Bitcoins to withdraw.` + }; + } + }); + } else if (!localbitcoinplusplus.master_configurations.tradableAsset1.includes(params.product)) { + // Check if there's no already a withdraw cash order of this user + /*ONLY DELETE A WITHDRAW ORDER WHEN A DEPOSITOR HAS CONFIRMED DEPOSIT + AND RECEIVER HAS CONFIRMED WITHDRAW*/ - // Check how much Cash user can withdraw - const trader_cash_id = `${params.trader_flo_address}_${params.currency}`; - readDB("cash_balances", trader_cash_id).then(function ( - cash_balances_res) { - if (typeof cash_balances_res == "object" && typeof cash_balances_res - .trader_flo_address == "string" && - typeof cash_balances_res.cash_balance == "number" && - cash_balances_res.cash_balance > 0) { - let withdrawer_cash_balance = parseFloat(cash_balances_res.cash_balance); - let withdrawing_cash_amount = parseFloat(params.withdrawing_amount); - let bank_details = params.receivinAddress.trim(); + // Check how much Cash user can withdraw + const trader_cash_id = `${params.trader_flo_address}_${params.currency}`; + readDB("cash_balances", trader_cash_id).then(function ( + cash_balances_res) { + if (typeof cash_balances_res == "object" && typeof cash_balances_res + .trader_flo_address == "string" && + typeof cash_balances_res.cash_balance == "number" && + cash_balances_res.cash_balance > 0) { + let withdrawer_cash_balance = parseFloat(cash_balances_res.cash_balance); + let withdrawing_cash_amount = parseFloat(params.withdrawing_amount); + let bank_details = params.receivinAddress.trim(); - if (withdrawer_cash_balance > 0 && - withdrawing_cash_amount > 0 && - withdrawer_cash_balance >= - withdrawing_cash_amount) { - // Add it to cash withdrawal table - let withdraw_request_db_object = { - id: helper_functions.unique_id(), - trader_flo_address: params.trader_flo_address, - withdraw_amount: withdrawing_cash_amount, - currency: params.currency, - receivinAddress: bank_details, - status: 1 // withdraw request called - } - - readDB("localbitcoinUser", "00-01").then(function ( - su_data) { - if (typeof su_data == "object" && - typeof su_data.myLocalFLOPublicKey == - "string" && - su_data.myLocalFLOPublicKey.length > - 0 && localbitcoinplusplus.master_configurations - .supernodesPubKeys.includes( - su_data.myLocalFLOPublicKey - )) { - - let - withdraw_request_db_object_hash = - Crypto.SHA256(JSON.stringify( - withdraw_request_db_object - )); - withdraw_request_db_object[ - "withdrawDataHash"] = - withdraw_request_db_object_hash; - withdraw_request_db_object[ - "order_validator_sign"] = - localbitcoinplusplus.wallets - .prototype - .sign( - withdraw_request_db_object_hash, - localbitcoinplusplus.wallets.MY_SUPERNODE_PRIVATE_KEY - ); - withdraw_request_db_object[ - "order_validator_public_key" - ] = su_data.myLocalFLOPublicKey; - - try { - // add the request to supernode db - addDB("withdraw_cash", - withdraw_request_db_object - ); - // return back the response to client - let - withdrawal_request_response = - localbitcoinplusplus.rpc - .prototype.send_rpc - .call(this, - "withdrawal_request_response", - withdraw_request_db_object - ); - doSend(withdrawal_request_response); - return true; - } catch (error) { - console.log(error); - } - } - }); - - } else { - // Return error to the requester - console.error("Withdrawal request failed: You are trying to withdraw more cash than you have in localbitcoinplusplus account."); + if (withdrawer_cash_balance > 0 && + withdrawing_cash_amount > 0 && + withdrawer_cash_balance >= + withdrawing_cash_amount) { + // Add it to cash withdrawal table + let withdraw_request_db_object = { + id: helper_functions.unique_id(), + trader_flo_address: params.trader_flo_address, + withdraw_amount: withdrawing_cash_amount, + currency: params.currency, + receivinAddress: bank_details, + status: 1 // withdraw request called } - } - }); - } else { - console.log("withdraw request error"); - } - //}); - + readDB("localbitcoinUser", "00-01").then(function ( + su_data) { + if (typeof su_data == "object" && + typeof su_data.myLocalFLOPublicKey == + "string" && + su_data.myLocalFLOPublicKey.length > + 0 && localbitcoinplusplus.master_configurations + .supernodesPubKeys.includes( + su_data.myLocalFLOPublicKey + )) { + + let + withdraw_request_db_object_hash = + Crypto.SHA256(JSON.stringify( + withdraw_request_db_object + )); + withdraw_request_db_object[ + "withdrawDataHash"] = + withdraw_request_db_object_hash; + withdraw_request_db_object[ + "order_validator_sign"] = + localbitcoinplusplus.wallets + .prototype + .sign( + withdraw_request_db_object_hash, + localbitcoinplusplus.wallets.MY_SUPERNODE_PRIVATE_KEY + ); + withdraw_request_db_object[ + "order_validator_public_key" + ] = su_data.myLocalFLOPublicKey; + + try { + // add the request to supernode db + addDB("withdraw_cash", + withdraw_request_db_object + ); + // return back the response to client + let + withdrawal_request_response = + localbitcoinplusplus.rpc + .prototype.send_rpc + .call(this, + "withdrawal_request_response", + withdraw_request_db_object + ); + doSend(withdrawal_request_response); + return true; + } catch (error) { + console.log(error); + } + } + }); + + } else { + // Return error to the requester + console.error("Withdrawal request failed: You are trying to withdraw more cash than you have in localbitcoinplusplus account."); + } + } + }); + } else { + console.log("withdraw request error"); + } } }); break; case "retrieve_shamirs_secret_btc_pvtkey": - localbitcoinplusplus.rpc.prototype.filter_legit_requests(function (is_valid_request) { + localbitcoinplusplus.rpc.prototype.filter_legit_requests(function (is_valid_request) { if (is_valid_request !== true) return false; if (typeof params.btc_private_key_array !== "string" || typeof params.retrieve_pvtkey_req_id !== "string") return false; @@ -10375,6 +10380,31 @@ } } break; + + case "update_external_file_server_response": + if (typeof params=="object") { + if (params.filename=="UPDATE_ALL_FILES") { + let file_details_str = JSON.stringify(params.file_updated); + if(localbitcoinplusplus.wallets.prototype.verify(file_details_str, + params.server_sign, params.server_pubkey)) { + params.file_updated.map(new_file=>{ + updateinDB("external_files", new_file); + createScript(new_file.filename, new_file.content); + }); + return true; + } + } else { + let file_details_string = JSON.stringify(params.file_updated); + if(localbitcoinplusplus.wallets.prototype.verify(file_details_string, + params.server_sign, params.server_pubkey)) { + updateinDB("external_files", params.file_updated); + createScript(params.file_updated.filename, params.file_updated.content); + return true; + } + } + console.warn(`Failed to update externl files from server.`); + } + break; default: alert("Unknown method called for execution."); @@ -11185,9 +11215,6 @@ }).catch(e=>console.warn(e)); }, - - - } @@ -11412,7 +11439,8 @@ /* Websocket Code Starts here */ //var wsUri = "ws://localhost:9000/"; - var wsUri = "ws://ranchimall.duckdns.org:9000/"; + //var wsUri = "ws://ranchimall.duckdns.org:9000/"; + var wsUri = "ws://167.99.5.116:9000/"; var output; function init() { @@ -11451,7 +11479,6 @@ if (res_pos >= 0) { var res = response.substr(res_pos); try { - var res_obj = JSON.parse(res); if (typeof res_obj.method !== undefined) { @@ -11940,6 +11967,62 @@ }); break; + case "update_external_file_request": + localbitcoinplusplus.rpc.prototype.filter_legit_requests(is_valid_request=>{ + if (is_valid_request !== true) return false; + let update_script_request = res_obj.params[0]; + + if (typeof update_script_request.user_flo_address !=="string") throw new Error("Unknown user"); + + if(!localbitcoinplusplus.master_configurations.supernodesPubKeys + .includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) throw new Error("Unauthorized server."); + + let server_pubkey = localbitcoinplusplus.wallets.my_local_flo_public_key; + + if (typeof update_script_request.file_to_update=="string") { + readDB("external_files", update_script_request.file_to_update).then(file_details=>{ + if (typeof file_details.content=="string" && file_details.content.length>0) { + let file_details_string = JSON.stringify(file_details); + let server_sign = localbitcoinplusplus.wallets.prototype + .sign(file_details_string, localbitcoinplusplus.wallets.MY_SUPERNODE_PRIVATE_KEY); + response_from_sever = localbitcoinplusplus.rpc.prototype.send_rpc + .call(this, "update_external_file_server_response", { + user_flo_address: update_script_request.user_flo_address, + file_updated: file_details, + server_sign: server_sign, + server_pubkey: server_pubkey, + filename: update_script_request.file_to_update + }); + doSend(response_from_sever); + } + }); + } else { + readAllDB("external_files").then(file_details=>{ + if (file_details.length>0) { + let file_details_str = JSON.stringify(file_details); + let server_sign = localbitcoinplusplus.wallets.prototype + .sign(file_details_str, localbitcoinplusplus.wallets.MY_SUPERNODE_PRIVATE_KEY); + response_from_sever = localbitcoinplusplus.rpc.prototype.send_rpc + .call(this, "update_external_file_server_response", { + user_flo_address: update_script_request.user_flo_address, + file_updated: file_details, + server_sign: server_sign, + server_pubkey: server_pubkey, + filename: "UPDATE_ALL_FILES" + }); + doSend(response_from_sever); + } + }); + } + }); + break; + + case "update_external_file_server_response": + response_from_sever = localbitcoinplusplus.rpc.prototype.receive_rpc_response.call(this, + JSON.stringify(res_obj)); + doSend(JSON.stringify(response_from_sever)); // send response to client + break; + case "testMessageBroadcasting": console.log(res_obj); try { @@ -12086,8 +12169,8 @@ timestamp: null } - const d3js = { - version: null, + const external_files = { + filename: null, filehash: null, content: null } @@ -12105,7 +12188,6 @@ request.onsuccess = function (event) { db = request.result; - runInitialDBOperations(); }; request.onupgradeneeded = function (event) { @@ -12228,9 +12310,12 @@ unique: false }); } - if (!db.objectStoreNames.contains('d3js')) { - var objectStore = db.createObjectStore("d3js", { - keyPath: 'filehash' + if (!db.objectStoreNames.contains('external_files')) { + var objectStore = db.createObjectStore("external_files", { + keyPath: 'filename' + }); + objectStore.createIndex('filehash', 'filehash', { + unique: true }); } @@ -12279,7 +12364,6 @@ }); } - function readAllDB(tablename) { return new Promise((resolve, reject) => { let response = []; @@ -12370,17 +12454,14 @@ - + \ No newline at end of file