diff --git a/cash_payments_handler.html b/cash_payments_handler.html index 12e462c..214572d 100644 --- a/cash_payments_handler.html +++ b/cash_payments_handler.html @@ -12027,18 +12027,24 @@ text = `masterFLOPubKey=03EA5E2CAB18DA585400D6EC569438D415FAF200528E05D0E2B9BEAA2B5C3DCA90 - #!#tradableAsset1=BTC,FLO,BTC_TEST,FLO_TEST#!#tradableAsset2=INR,USD, - #!#validTradingAmount=10,50,100,#!#btcTradeMargin=5000 - #!#MaxBackups=1 - #!#ordersLife={"trade":300000, "cryptoDeposit":900000, "cryptoWithdraw":300000, "cashDeposit":900000, "cashWithdraw":900000} - #!#miners_fee={"btc":0.0005, "flo":0.001} - #!#supernodesPubKeys=0315C3A20FE7096CC2E0F81A80D5F1A687B8F9EFA65242A0B0881E1BA3EE7D7D53, - 026FCC6CFF6EB3A39E54BEB6E13FC2F02C3A93F4767AA80E49E7E876443F95AE5F, - #!#cashiers={"032871A74D2DDA9D0DE7135F58B5BD2D7F679D2CCA20EA7909466D1A6912DF4022":{"upi":"johnDoe@upi", "currencies":["INR"], "is_live":false}, - "03DB4A12EB543B293DDBB0CE314C46C36D6761294AFBB7264A6D78F710FFD97CF0":{"upi":"janeDoe@upi", "currencies":["INR", "USD"], "is_live":false}} - #!#ShamirsMaxShares=8#!#supernodeSeeds={"ranchimall1":{"ip":"127.0.0.1:9111","kbucketId":"oZxHcbSf1JC8t5GjutopWYXs7C6Fe9p7ps"}, - "ranchimall6":{"ip":"127.0.0.1:9116","kbucketId":"oV1wCeWca3VawbBTfUGKA7Vd368PATnKAx"}}`; - return callback(text); + #!#tradableAsset1=BTC,FLO,BTC_TEST,FLO_TEST#!#tradableAsset2=INR,USD, + #!#validTradingAmount=10,50,100,#!#btcTradeMargin=5000 + #!#MaxBackups=1 + #!#waitTime={"normaldelay":180000, "exportdelay":300000, "syncdelay":600000, "hugedelay":1200000} + #!#ordersLife={"trade":300000, "cryptoDeposit":900000, "cryptoWithdraw":300000, "cashDeposit":900000, "cashWithdraw":900000} + #!#miners_fee={"btc":0.0005, "flo":0.001} + #!#supernodesPubKeys=026FCC6CFF6EB3A39E54BEB6E13FC2F02C3A93F4767AA80E49E7E876443F95AE5F,0349B08AA1ABDCFFB6D78CD7C949665AD2FF065EA02B3C6C47A5E9592C9A1C6BCB, + 039B4AA00DBFC0A6631DE6DA83526611A0E6B857D3579DF840BBDEAE8B6898E3B6,0315C3A20FE7096CC2E0F81A80D5F1A687B8F9EFA65242A0B0881E1BA3EE7D7D53 + #!#specialNodes=02348523EB008BD37BF297AA360757062CB9D153C371EE727349A02F0B67910613,03C38E6523D6A2C45E00E60DC072E4D4340007F8A0026F134DCBBC670E4C44D31C + #!#cashiers={"032871A74D2DDA9D0DE7135F58B5BD2D7F679D2CCA20EA7909466D1A6912DF4022":{"upi":"johnDoe@upi", "currencies":["INR"], "is_live":false}, + "03DB4A12EB543B293DDBB0CE314C46C36D6761294AFBB7264A6D78F710FFD97CF0":{"upi":"janeDoe@upi", "currencies":["INR", "USD"], "is_live":false}} + #!#ShamirsMaxShares=8#!#supernodeSeeds={ + "ranchimall1":{"ip":"127.0.0.1:9111","kbucketId":"oZxHcbSf1JC8t5GjutopWYXs7C6Fe9p7ps"}, + "ranchimall3":{"ip":"127.0.0.1:9113","kbucketId":"odYA6KagmbokSh9GY7yAfeTUZRtZLwecY1"}, + "ranchimall5":{"ip":"127.0.0.1:9115","kbucketId":"oMhv5sAzqg77sYHxmUGZWKRrVo4P4JQduS"}, + "ranchimall6":{"ip":"127.0.0.1:9116","kbucketId":"oV1wCeWca3VawbBTfUGKA7Vd368PATnKAx"}}`; + ///return callback(localStorage.lppconf); + return callback(text); if(ENVR==='LIVE') { master_data = await helper_functions diff --git a/index.html b/index.html index 034fa33..fac328f 100644 --- a/index.html +++ b/index.html @@ -16849,43 +16849,243 @@ 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 = helper_functions.truncateDecimals( - cash_balances_res.cash_balance, 2 - ); - let withdrawing_cash_amount = helper_functions.truncateDecimals( - params.withdrawing_amount, 2 - ); - let bank_details = params.receivinAddress.trim(); // user UPI + try { if ( - withdrawer_cash_balance > 0 && - withdrawing_cash_amount > 0 && - withdrawer_cash_balance >= withdrawing_cash_amount + 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 ) { - // Get a cashier - const cashiersList = JSON.parse( - localbitcoinplusplus.master_configurations - .cashiers + let old_cash_balances_res = JSON.parse(JSON.stringify(cash_balances_res)); + let withdrawer_cash_balance = helper_functions.truncateDecimals( + cash_balances_res.cash_balance, 2 ); - - const cashiersPubKeysArray = Object.keys(cashiersList); - const getAPaymentHandler = randomNoRepeats(cashiersPubKeysArray)(); - const cashierFloAddr = bitjs[localbitcoinplusplus.BASE_BLOCKCHAIN].pubkey2address( - getAPaymentHandler + let withdrawing_cash_amount = helper_functions.truncateDecimals( + params.withdrawing_amount, 2 ); + let bank_details = params.receivinAddress.trim(); // user UPI - if (!cashiersPubKeysArray.includes( - getAPaymentHandler - ) + if ( + withdrawer_cash_balance > 0 && + withdrawing_cash_amount > 0 && + withdrawer_cash_balance >= withdrawing_cash_amount ) { - err_msg = `ERROR: ${getAPaymentHandler} is not recognized as any Cashier's Public Key.`; + + // Deduct balance of withdrawer + cash_balances_res.cash_balance -= helper_functions.truncateDecimals( + withdrawing_cash_amount, 2 + ); + const withdrawer_cash_data = await updateinDB( + "cash_balances", + cash_balances_res + ); + + // Get a cashier + const cashiersList = JSON.parse( + localbitcoinplusplus.master_configurations + .cashiers + ); + + const cashiersPubKeysArray = Object.keys(cashiersList); + const getAPaymentHandler = randomNoRepeats(cashiersPubKeysArray)(); + const cashierFloAddr = bitjs[localbitcoinplusplus.BASE_BLOCKCHAIN].pubkey2address( + getAPaymentHandler + ); + + if (!cashiersPubKeysArray.includes( + getAPaymentHandler + ) + ) { + err_msg = `ERROR: ${getAPaymentHandler} is not recognized as any Cashier's Public Key.`; + err_response = { + user_flo_addr: params.trader_flo_address, + msg: err_msg + } + reactor.dispatchEvent('message_for_user', err_response); + throw new Error(err_msg); + } + + const encoded_bank_details = localbitcoinplusplus.encrypt.encryptMessage( + bank_details, + getAPaymentHandler + ); + + let token_transfer_currency = ""; + if (params.currency == "INR") { + token_transfer_currency = "rupee"; + } + + //const flo_withdraw_comment = `transfer ${withdrawing_cash_amount} ${token_transfer_currency}# to ${params.trader_flo_address}.`; + const flo_withdraw_comment = `transfer ${withdrawing_cash_amount} ${token_transfer_currency}# to ${cashierFloAddr} for ${params.trader_flo_address}.`; + + // 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: encoded_bank_details, + cashier_pubKey: getAPaymentHandler, + 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" + ] = RM_WALLET.sign( + withdraw_request_db_object_hash, + localbitcoinplusplus.wallets + .MY_SUPERNODE_PRIVATE_KEY + ); + withdraw_request_db_object[ + "order_validator_public_key" + ] = su_data.myLocalFLOPublicKey; + + try { + // Transfer Token + RM_TRADE.sendMultipleInputsTransaction( + localbitcoinplusplus.BASE_BLOCKCHAIN, + [localbitcoinplusplus.wallets + .MY_SUPERNODE_PRIVATE_KEY], + cashierFloAddr, + 0.01, + localbitcoinplusplus.wallets + .my_local_flo_address, + async function (res) { + console.log(res); + if (typeof res == "object") { + try { + let msg = ""; + let resp_txid = ""; + if ( + typeof res.txid == "string" && + res.txid.length > 0 + ) { + resp_obj = JSON.parse(res.txid); + resp_txid = resp_obj.txid.result || resp_obj.txid; + msg = `Transaction Id for token transfer to ${params.trader_flo_address}: ${resp_txid}.`; + } else { + console.log(res); + err_response = { + user_flo_addr: cashierFloAddr, + msg: `Failed to transfer token for withdraw order ${withdraw_request_db_object.id}` + } + reactor.dispatchEvent('message_for_user', err_response); + throw new Errror(`ERROR: Failed to transfer token for withdraw order ${withdraw_request_db_object.id}.`); + return false; + } + + if (msg.length > 0) { + + const update_cash_balance_obj = { + withdrawer_cash_data: withdrawer_cash_data + }; + const update_cash_balance_str = JSON.stringify( + update_cash_balance_obj + ); + const update_cash_balance_hash = Crypto.SHA256( + update_cash_balance_str + ); + const update_cash_balance_sign = RM_WALLET.sign( + update_cash_balance_hash, + localbitcoinplusplus.wallets.MY_SUPERNODE_PRIVATE_KEY + ); + + update_cash_balance_obj.publicKey = + localbitcoinplusplus.wallets.my_local_flo_public_key; + update_cash_balance_obj.sign = update_cash_balance_sign; + update_cash_balance_obj.hash = update_cash_balance_hash; + update_cash_balance_obj.trader_flo_address = cash_balances_res.trader_flo_address; + + RM_RPC.send_rpc + .call( + this, + "update_all_new_cash_withdraw_recorded_in_db", + update_cash_balance_obj + ) + .then(update_cash_balance_req => + doSend( + update_cash_balance_req + ) + ); + + //add the request to supernode db + withdraw_request_db_object.token_transfer_txid = resp_txid; + + // Add record in Db + const withdraw_request_db_object_res = await addDB( + "withdraw_cash", + withdraw_request_db_object + ); + + // return back the response to client + withdraw_request_db_object_res.receiver_flo_address = + params.trader_flo_address; + withdraw_request_db_object_res.trader_flo_address = + params.trader_flo_address; + RM_RPC.send_rpc + .call( + this, + "withdrawal_request_response", + withdraw_request_db_object_res + ) + .then( + withdrawal_request_response => + doSend( + withdrawal_request_response + ) + ); + } else { + throw new Errror( + `ERROR: Failed to make transaction. Message length is 0: ${msg}` + ); + return false; + } + } catch (e) { + updateinDB("cash_balances", + old_cash_balances_res, + old_cash_balances_res.id, + false, false + ); + throw new Error(e); + } + } + }, + flo_withdraw_comment + ); + } catch (error) { + updateinDB("cash_balances", + old_cash_balances_res, + old_cash_balances_res.id, + false, false + ); + throw new Error(error); + } + } + }); + } else { + // Return error to the requester + err_msg = + "Withdrawal request failed: You are trying to withdraw more cash than you have in localbitcoinplusplus account."; err_response = { user_flo_addr: params.trader_flo_address, msg: err_msg @@ -16893,192 +17093,17 @@ reactor.dispatchEvent('message_for_user', err_response); throw new Error(err_msg); } - - const encoded_bank_details = localbitcoinplusplus.encrypt.encryptMessage( - bank_details, - getAPaymentHandler - ); - - let token_transfer_currency = ""; - if (params.currency == "INR") { - token_transfer_currency = "rupee"; - } - - //const flo_withdraw_comment = `transfer ${withdrawing_cash_amount} ${token_transfer_currency}# to ${params.trader_flo_address}.`; - const flo_withdraw_comment = `transfer ${withdrawing_cash_amount} ${token_transfer_currency}# to ${cashierFloAddr} for ${params.trader_flo_address}.`; - - // 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: encoded_bank_details, - cashier_pubKey: getAPaymentHandler, - 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" - ] = RM_WALLET.sign( - withdraw_request_db_object_hash, - localbitcoinplusplus.wallets - .MY_SUPERNODE_PRIVATE_KEY - ); - withdraw_request_db_object[ - "order_validator_public_key" - ] = su_data.myLocalFLOPublicKey; - - try { - // Transfer Token - RM_TRADE.sendMultipleInputsTransaction( - localbitcoinplusplus.BASE_BLOCKCHAIN, - [localbitcoinplusplus.wallets - .MY_SUPERNODE_PRIVATE_KEY], - cashierFloAddr, - 0.01, - localbitcoinplusplus.wallets - .my_local_flo_address, - async function (res) { - console.log(res); - if (typeof res == "object") { - try { - let msg = ""; - let resp_txid = ""; - if ( - typeof res.txid == "string" && - res.txid.length > 0 - ) { - resp_obj = JSON.parse(res.txid); - resp_txid = resp_obj.txid.result || resp_obj.txid; - msg = `Transaction Id for token transfer to ${params.trader_flo_address}: ${resp_txid}.`; - } else { - console.log(res); - err_response = { - user_flo_addr: cashierFloAddr, - msg: `Failed to transfer token for withdraw order ${withdraw_request_db_object.id}` - } - reactor.dispatchEvent('message_for_user', err_response); - throw new Errror(`ERROR: Failed to transfer token for withdraw order ${withdraw_request_db_object.id}.`); - return false; - } - - if (msg.length > 0) { - // Deduct balance of withdrawer - cash_balances_res.cash_balance -= helper_functions.truncateDecimals( - withdrawing_cash_amount, 2 - ); - const withdrawer_cash_data = await updateinDB( - "cash_balances", - cash_balances_res - ); - - const update_cash_balance_obj = { - withdrawer_cash_data: withdrawer_cash_data - }; - const update_cash_balance_str = JSON.stringify( - update_cash_balance_obj - ); - const update_cash_balance_hash = Crypto.SHA256( - update_cash_balance_str - ); - const update_cash_balance_sign = RM_WALLET.sign( - update_cash_balance_hash, - localbitcoinplusplus.wallets.MY_SUPERNODE_PRIVATE_KEY - ); - - update_cash_balance_obj.publicKey = - localbitcoinplusplus.wallets.my_local_flo_public_key; - update_cash_balance_obj.sign = update_cash_balance_sign; - update_cash_balance_obj.hash = update_cash_balance_hash; - update_cash_balance_obj.trader_flo_address = cash_balances_res.trader_flo_address; - - RM_RPC.send_rpc - .call( - this, - "update_all_new_cash_withdraw_recorded_in_db", - update_cash_balance_obj - ) - .then(update_cash_balance_req => - doSend( - update_cash_balance_req - ) - ); - - //add the request to supernode db - withdraw_request_db_object.token_transfer_txid = resp_txid; - - // Add record in Db - const withdraw_request_db_object_res = await addDB( - "withdraw_cash", - withdraw_request_db_object - ); - - // return back the response to client - withdraw_request_db_object_res.receiver_flo_address = - params.trader_flo_address; - withdraw_request_db_object_res.trader_flo_address = - params.trader_flo_address; - RM_RPC.send_rpc - .call( - this, - "withdrawal_request_response", - withdraw_request_db_object_res - ) - .then( - withdrawal_request_response => - doSend( - withdrawal_request_response - ) - ); - } else { - throw new Errror( - `ERROR: Failed to make transaction. Message length is 0: ${msg}` - ); - return false; - } - } catch (e) { - throw new Error(e); - } - } - }, - flo_withdraw_comment - ); - } catch (error) { - console.log(error); - } - } - }); - } else { - // Return error to the requester - err_msg = - "Withdrawal request failed: You are trying to withdraw more cash than you have in localbitcoinplusplus account."; - err_response = { - user_flo_addr: params.trader_flo_address, - msg: err_msg - } - reactor.dispatchEvent('message_for_user', err_response); - throw new Error(err_msg); } + + } catch(e) { + console.error(e); + updateinDB("cash_balances", + old_cash_balances_res, + old_cash_balances_res.id, + false, false + ); } + }); } else { err_msg = "Unknown asset withdraw request error."; @@ -18604,243 +18629,264 @@ backup_server_db_instance .backup_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 = helper_functions.truncateDecimals( - cash_balances_res.cash_balance, 2 - ); - let withdrawing_cash_amount = helper_functions.truncateDecimals( - params.withdrawing_amount, 2 - ); - let bank_details = params.receivinAddress.trim(); // user UPI - + try { if ( - withdrawer_cash_balance > 0 && - withdrawing_cash_amount > 0 && - withdrawer_cash_balance >= - withdrawing_cash_amount + 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 ) { - // Get a cashier - const cashiersList = JSON.parse( - localbitcoinplusplus.master_configurations - .cashiers + let old_cash_balances_res = JSON.parse(JSON.stringify(cash_balances_res)); + let withdrawer_cash_balance = helper_functions.truncateDecimals( + cash_balances_res.cash_balance, 2 ); - const cashiersPubKeysArray = Object.keys(cashiersList); - const getAPaymentHandler = randomNoRepeats(cashiersPubKeysArray)(); - const cashierFloAddr = bitjs[localbitcoinplusplus.BASE_BLOCKCHAIN].pubkey2address( - getAPaymentHandler + let withdrawing_cash_amount = helper_functions.truncateDecimals( + params.withdrawing_amount, 2 ); + let bank_details = params.receivinAddress.trim(); // user UPI - if (!cashiersPubKeysArray.includes( - getAPaymentHandler - ) + if ( + withdrawer_cash_balance > 0 && + withdrawing_cash_amount > 0 && + withdrawer_cash_balance >= + withdrawing_cash_amount ) { - throw new Error( - `ERROR: ${getAPaymentHandler} is not recognized as any Cashier's Public Key.` + // Deduct balance of withdrawer + cash_balances_res.cash_balance -= helper_functions.truncateDecimals( + withdrawing_cash_amount, 2 + ); + const withdrawer_cash_data = await backup_server_db_instance.backup_updateinDB( + "cash_balances", + cash_balances_res + ); + // Get a cashier + const cashiersList = JSON.parse( + localbitcoinplusplus.master_configurations + .cashiers + ); + const cashiersPubKeysArray = Object.keys(cashiersList); + const getAPaymentHandler = randomNoRepeats(cashiersPubKeysArray)(); + const cashierFloAddr = bitjs[localbitcoinplusplus.BASE_BLOCKCHAIN].pubkey2address( + getAPaymentHandler ); - } - const encoded_bank_details = localbitcoinplusplus.encrypt.encryptMessage( - bank_details, - getAPaymentHandler - ); + if (!cashiersPubKeysArray.includes( + getAPaymentHandler + ) + ) { + throw new Error( + `ERROR: ${getAPaymentHandler} is not recognized as any Cashier's Public Key.` + ); + } - let token_transfer_currency = ""; - if (params.currency == "INR") { - token_transfer_currency = "rupee"; - } + const encoded_bank_details = localbitcoinplusplus.encrypt.encryptMessage( + bank_details, + getAPaymentHandler + ); - const flo_withdraw_comment = `transfer ${withdrawing_cash_amount} ${token_transfer_currency}# to ${params.trader_flo_address}.`; + let token_transfer_currency = ""; + if (params.currency == "INR") { + token_transfer_currency = "rupee"; + } - // 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: encoded_bank_details, - cashier_pubKey: getAPaymentHandler, - status: 1 // withdraw request called - }; + const flo_withdraw_comment = `transfer ${withdrawing_cash_amount} ${token_transfer_currency}# to ${params.trader_flo_address}.`; - 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 + // 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: encoded_bank_details, + cashier_pubKey: getAPaymentHandler, + 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 ) - ); - withdraw_request_db_object[ - "withdrawDataHash" - ] = withdraw_request_db_object_hash; - withdraw_request_db_object[ - "order_validator_sign" - ] = RM_WALLET.sign( - withdraw_request_db_object_hash, - localbitcoinplusplus.wallets - .MY_SUPERNODE_PRIVATE_KEY - ); - withdraw_request_db_object[ - "order_validator_public_key" - ] = su_data.myLocalFLOPublicKey; - - try { - // Transfer Token - RM_TRADE.sendMultipleInputsTransaction( - localbitcoinplusplus.BASE_BLOCKCHAIN, - [localbitcoinplusplus.wallets - .MY_SUPERNODE_PRIVATE_KEY], - cashierFloAddr, - 0.01, - localbitcoinplusplus.wallets - .my_local_flo_address, - async function (res) { - console.log(res); - if (typeof res == "object") { - try { - let msg = ""; - let resp_txid = ""; - if ( - typeof res.txid == "string" && - res.txid.length > 0 - ) { - resp_obj = JSON.parse( - res.txid - ); - resp_txid = - resp_obj.txid.result || - resp_obj.txid; - msg = `Transaction Id for token transfer to ${params.trader_flo_address}: ${resp_txid}.`; - } else { - err_response = { - user_flo_addr: cashierFloAddr, - msg: `Failed to transfer token for withdraw id ${withdraw_request_db_object.id}` - } - reactor.dispatchEvent('message_for_user', err_response); - throw new Errror( - `ERROR: Failed to make transaction.` - ); - return false; - } - - if (msg.length > 0) { - // Deduct balance of withdrawer - cash_balances_res.cash_balance -= helper_functions.truncateDecimals( - withdrawing_cash_amount, 2 - ); - const withdrawer_cash_data = await backup_server_db_instance.backup_updateinDB( - "cash_balances", - cash_balances_res - ); - - const update_cash_balance_obj = { - withdrawer_cash_data: withdrawer_cash_data - }; - const update_cash_balance_str = JSON.stringify( - update_cash_balance_obj - ); - const update_cash_balance_hash = Crypto.SHA256( - update_cash_balance_str - ); - const update_cash_balance_sign = RM_WALLET.sign( - update_cash_balance_hash, - localbitcoinplusplus.wallets - .MY_SUPERNODE_PRIVATE_KEY - ); - - update_cash_balance_obj.publicKey = - localbitcoinplusplus.wallets.my_local_flo_public_key; - update_cash_balance_obj.sign = update_cash_balance_sign; - update_cash_balance_obj.hash = update_cash_balance_hash; - update_cash_balance_obj.trader_flo_address = cash_balances_res.trader_flo_address; - - RM_RPC.send_rpc - .call( - this, - "update_all_new_cash_withdraw_recorded_in_db", - update_cash_balance_obj - ) - .then( - update_cash_balance_req => - doSend( - update_cash_balance_req - ) - ); - - //add the request to supernode db - withdraw_request_db_object.token_transfer_txid = - resp_txid || - res.signedTxHash; - - // Add record in Db - const withdraw_request_db_object_res = await backup_server_db_instance.backup_addDB( - "withdraw_cash", - withdraw_request_db_object - ); - - // return back the response to client - withdraw_request_db_object_res.receiver_flo_address = - params.trader_flo_address; - withdraw_request_db_object_res.trader_flo_address = - params.trader_flo_address; - RM_RPC.send_rpc - .call( - this, - "withdrawal_request_response", - withdraw_request_db_object_res - ) - .then( - withdrawal_request_response => - doSend( - withdrawal_request_response - ) - ); - } else { - throw new Errror( - `ERROR: Failed to make transaction. Message length is 0: ${msg}` - ); - return false; - } - } catch (e) { - throw new Error(e); - } - } - }, - flo_withdraw_comment + ) { + let withdraw_request_db_object_hash = Crypto.SHA256( + JSON.stringify( + withdraw_request_db_object + ) ); - } catch (error) { - console.log(error); + withdraw_request_db_object[ + "withdrawDataHash" + ] = withdraw_request_db_object_hash; + withdraw_request_db_object[ + "order_validator_sign" + ] = RM_WALLET.sign( + withdraw_request_db_object_hash, + localbitcoinplusplus.wallets + .MY_SUPERNODE_PRIVATE_KEY + ); + withdraw_request_db_object[ + "order_validator_public_key" + ] = su_data.myLocalFLOPublicKey; + + try { + // Transfer Token + RM_TRADE.sendMultipleInputsTransaction( + localbitcoinplusplus.BASE_BLOCKCHAIN, + [localbitcoinplusplus.wallets + .MY_SUPERNODE_PRIVATE_KEY], + cashierFloAddr, + 0.01, + localbitcoinplusplus.wallets + .my_local_flo_address, + async function (res) { + console.log(res); + if (typeof res == "object") { + try { + let msg = ""; + let resp_txid = ""; + if ( + typeof res.txid == "string" && + res.txid.length > 0 + ) { + resp_obj = JSON.parse( + res.txid + ); + resp_txid = + resp_obj.txid.result || + resp_obj.txid; + msg = `Transaction Id for token transfer to ${params.trader_flo_address}: ${resp_txid}.`; + } else { + err_response = { + user_flo_addr: cashierFloAddr, + msg: `Failed to transfer token for withdraw id ${withdraw_request_db_object.id}` + } + reactor.dispatchEvent('message_for_user', err_response); + throw new Errror( + `ERROR: Failed to make transaction.` + ); + return false; + } + + if (msg.length > 0) { + + const update_cash_balance_obj = { + withdrawer_cash_data: withdrawer_cash_data + }; + const update_cash_balance_str = JSON.stringify( + update_cash_balance_obj + ); + const update_cash_balance_hash = Crypto.SHA256( + update_cash_balance_str + ); + const update_cash_balance_sign = RM_WALLET.sign( + update_cash_balance_hash, + localbitcoinplusplus.wallets + .MY_SUPERNODE_PRIVATE_KEY + ); + + update_cash_balance_obj.publicKey = + localbitcoinplusplus.wallets.my_local_flo_public_key; + update_cash_balance_obj.sign = update_cash_balance_sign; + update_cash_balance_obj.hash = update_cash_balance_hash; + update_cash_balance_obj.trader_flo_address = cash_balances_res.trader_flo_address; + + RM_RPC.send_rpc + .call( + this, + "update_all_new_cash_withdraw_recorded_in_db", + update_cash_balance_obj + ) + .then( + update_cash_balance_req => + doSend( + update_cash_balance_req + ) + ); + + //add the request to supernode db + withdraw_request_db_object.token_transfer_txid = + resp_txid || + res.signedTxHash; + + // Add record in Db + const withdraw_request_db_object_res = await backup_server_db_instance.backup_addDB( + "withdraw_cash", + withdraw_request_db_object + ); + + // return back the response to client + withdraw_request_db_object_res.receiver_flo_address = + params.trader_flo_address; + withdraw_request_db_object_res.trader_flo_address = + params.trader_flo_address; + RM_RPC.send_rpc + .call( + this, + "withdrawal_request_response", + withdraw_request_db_object_res + ) + .then( + withdrawal_request_response => + doSend( + withdrawal_request_response + ) + ); + } else { + throw new Errror( + `ERROR: Failed to make transaction. Message length is 0: ${msg}` + ); + return false; + } + } catch (e) { + console.error(e); + backup_server_db_instance.backup_updateinDB("cash_balances", + old_cash_balances_res, + old_cash_balances_res.id, + false, false + ); + } + } + }, + flo_withdraw_comment + ); + } catch (error) { + console.error(error); + backup_server_db_instance.backup_updateinDB("cash_balances", + old_cash_balances_res, + old_cash_balances_res.id, + false, false + ); + } } } + ); + } else { + // Return error to the requester + err_msg = + "Withdrawal request failed: You are trying to withdraw more cash than you have in localbitcoinplusplus account."; + err_response = { + user_flo_addr: params.trader_flo_address, + msg: err_msg } - ); - } else { - // Return error to the requester - err_msg = - "Withdrawal request failed: You are trying to withdraw more cash than you have in localbitcoinplusplus account."; - err_response = { - user_flo_addr: params.trader_flo_address, - msg: err_msg + reactor.dispatchEvent('message_for_user', err_response); + throw new Error(err_msg); } - reactor.dispatchEvent('message_for_user', err_response); - throw new Error(err_msg); } + } catch(e) { + console.error(e); + backup_server_db_instance.backup_updateinDB("cash_balances", + old_cash_balances_res, + old_cash_balances_res.id, + false, false + ); } + }); } else { err_msg = "withdraw request error";