diff --git a/supernode/index.html b/supernode/index.html index fa97922..8a5abaf 100644 --- a/supernode/index.html +++ b/supernode/index.html @@ -12568,6 +12568,12 @@ if (typeof params == "object" && typeof method == "string") { + if (typeof params.receiver_flo_address=="string" + && params.receiver_flo_address.length>0) { + if(params.receiver_flo_address + !== localbitcoinplusplus.wallets.my_local_flo_address) return; + } + const RM_WALLET = new localbitcoinplusplus.wallets; const RM_TRADE = new localbitcoinplusplus.trade; const RM_RPC = new localbitcoinplusplus.rpc; @@ -12577,7 +12583,7 @@ request.response = {}; let err_msg; - if(typeof params.trader_flo_address !="string") return; + if(typeof params.trader_flo_address !=="string") return; const my_closest_su_list = await localbitcoinplusplus.kademlia.determineClosestSupernode(params.trader_flo_address); const primarySupernodeOfThisUser = my_closest_su_list[0].data.id; @@ -12659,15 +12665,17 @@ } let backup_server_db_instance; - if (typeof localbitcoinplusplus.newBackupDatabase.db[primarySupernodeOfThisUser] == "object") { - backup_server_db_instance = localbitcoinplusplus.newBackupDatabase.db[primarySupernodeOfThisUser]; - } + if (method !== "retrieve_shamirs_secret_btc_pvtkey") { + if (typeof localbitcoinplusplus.newBackupDatabase.db[primarySupernodeOfThisUser] == "object") { + backup_server_db_instance = localbitcoinplusplus.newBackupDatabase.db[primarySupernodeOfThisUser]; + } - if(typeof backup_server_db_instance !== "object") { - let backup_db_error_msg = `WARNING: Unknown DB instance. DB Backup failed.`; - showMessage(backup_db_error_msg); - throw new Error(backup_db_error_msg); - }; + if(typeof backup_server_db_instance !== "object") { + let backup_db_error_msg = `WARNING: Unknown DB instance. DB Backup failed.`; + showMessage(backup_db_error_msg); + throw new Error(backup_db_error_msg); + }; + } RM_RPC.filter_legit_backup_requests(params.trader_flo_address, async function (is_valid_request) { if (is_valid_request !== true) return false; @@ -13429,7 +13437,8 @@ "send_back_shamirs_secret_btc_pvtkey", { retrieve_pvtkey_req_id: retrieve_pvtkey_req_id, chunk_val: bpks, - withdraw_id: vbl.withdraw_id + withdraw_id: vbl.withdraw_id, + db_inst: primarySupernodeForThisUser } ).then(retrieve_pvtkey_req=> doSend(retrieve_pvtkey_req)); @@ -13593,6 +13602,10 @@ if (typeof params.btc_private_key_array !== "string" || typeof params.retrieve_pvtkey_req_id !== "string") return false; + backup_server_db_instance = localbitcoinplusplus.newBackupDatabase.db[params.db_inst]; + + if (typeof backup_server_db_instance !== "object") return; + let btc_private_key_str = params.btc_private_key_array; let retrieve_pvtkey_req_id = params.retrieve_pvtkey_req_id; let withdraw_id = params.withdraw_id; @@ -13621,7 +13634,7 @@ await RM_TRADE.resolve_current_crypto_price_in_fiat( withdraw_res.product, withdraw_res.currency); - const EqCryptoWd = RM_TRADE.calculateCryptoEquivalentOfCash( + let EqCryptoWd = RM_TRADE.calculateCryptoEquivalentOfCash( withdraw_res.receiverBTCEquivalentInCash, withdraw_res.currency, withdraw_res.product); @@ -13670,22 +13683,17 @@ msg = `Transaction Id for your withdrawn crypto asset: ${resp_txid}`; + let withdrawer_crypto_bal_id = `${withdraw_res.trader_flo_address}_${withdraw_res.product}`; backup_server_db_instance.backup_readDB ( 'crypto_balances', - withdraw_res - .id + withdrawer_crypto_bal_id ) .then( res_bal => { - // btc_eq_receiving_amount - // = - // Number(parseFloat( - // btc_eq_receiving_amount - // ) - // .toFixed( - // 8 - // )); + if (typeof res_bal !== "object") { + throw new Error(`FATAL ERROR: Failed to subtract balance of id ${withdrawer_crypto_bal_id} by ${EqCryptoWd}. `); + } res_bal .crypto_balance -= EqCryptoWd; @@ -15351,11 +15359,14 @@ if (res_pos >= 0) { var res = response.substr(res_pos); let res_obj = JSON.parse(res); - if (res_obj.method==="add_user_public_data") { + if (res_obj.method==="add_user_public_data" + || res_obj.method==="retrieve_shamirs_secret_btc_pvtkey" + ) { handle_backup_server_messages(response); + return; } if (res_obj.method==="sync_backup_supernode_from_backup_supernode" - || res_obj.method==="sync_primary_supernode_from_backup_supernode_response") { + || res_obj.method==="sync_primary_supernode_from_backup_supernode_response") { onMessage(response); return; } @@ -15604,6 +15615,7 @@ processBackupUserOnMesssageRequest(response); return; } + handle_backup_server_messages(response); return; } @@ -15923,7 +15935,7 @@ private_key_chunk: res, withdraw_id: res_obj.params[0].withdraw_id, receiver_flo_address: res_obj.globalParams.senderFloId - }).then(send_pvtkey_req=>doSend(send_pvtkey_req, res_obj.globalParams.senderFloId)); + }).then(send_pvtkey_req=>doSend(send_pvtkey_req)); }); } break; @@ -16810,6 +16822,8 @@ && res_obj.method !== "link_My_Local_IP_To_My_Flo_Id" && res_obj.method !== "link_Others_Local_IP_To_Their_Flo_Id" && res_obj.method !== "send_back_shamirs_secret_btc_pvtkey" + && res_obj.method !== "send_back_shamirs_secret_supernode_pvtkey" + && res_obj.method !== "store_shamirs_secret_pvtkey_shares" ) { if (localbitcoinplusplus.master_configurations.supernodesPubKeys .includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) { @@ -17124,20 +17138,21 @@ if (localbitcoinplusplus.master_configurations.supernodesPubKeys .includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) { if (typeof res_obj.params == "object" && typeof res_obj.params[0] == "object") { - if(typeof res_obj.params[0].trader_flo_address !="string") return; - localbitcoinplusplus.kademlia.determineClosestSupernode(res_obj.params[0].trader_flo_address) - .then(my_closest_su_list=>{ - const primarySupernodeOfThisUser = my_closest_su_list[0].data.id; - const backup_server_db_instance = localbitcoinplusplus.newBackupDatabase.db[primarySupernodeOfThisUser]; + // if(typeof res_obj.params[0].trader_flo_address !="string") return; + // localbitcoinplusplus.kademlia.determineClosestSupernode(res_obj.params[0].trader_flo_address) + // .then(my_closest_su_list=>{ + // const primarySupernodeOfThisUser = my_closest_su_list[0].data.id; + // const backup_server_db_instance = localbitcoinplusplus.newBackupDatabase.db[primarySupernodeOfThisUser]; - if(typeof backup_server_db_instance !== "object") { - let backup_db_error_msg = `WARNING: Unknown DB instance. DB Backup failed.`; - showMessage(backup_db_error_msg); - throw new Error(backup_db_error_msg); - }; - delete res_obj.params[0].trader_flo_address; - backup_server_db_instance.backup_addDB("supernode_private_key_chunks", res_obj.params[0]); - }); + // if(typeof backup_server_db_instance !== "object") { + // let backup_db_error_msg = `WARNING: Unknown DB instance. DB Backup failed.`; + // showMessage(backup_db_error_msg); + // throw new Error(backup_db_error_msg); + // }; + + // }); + delete res_obj.params[0].trader_flo_address; + addDB("supernode_private_key_chunks", res_obj.params[0]); } } break; @@ -17214,29 +17229,43 @@ case "send_back_shamirs_secret_btc_pvtkey": if (typeof res_obj.params == "object" && typeof res_obj.params[0] == "object") { - if(typeof res_obj.globalParams.primarySupernode !="string") return; - localbitcoinplusplus.kademlia.determineClosestSupernode(res_obj.globalParams.primarySupernode) - .then(my_closest_su_list=>{ - const primarySupernodeOfThisUser = my_closest_su_list[0].data.id; - const backup_server_db_instance = localbitcoinplusplus.newBackupDatabase.db[primarySupernodeOfThisUser]; + if(typeof res_obj.globalParams.primarySupernode !="string") return; + + // localbitcoinplusplus.kademlia.determineClosestSupernode(res_obj.globalParams.primarySupernode) + // .then(my_closest_su_list=>{ + // const primarySupernodeOfThisUser = my_closest_su_list[0].data.id; + // const backup_server_db_instance = localbitcoinplusplus.newBackupDatabase.db[primarySupernodeOfThisUser]; - if(typeof backup_server_db_instance !== "object") { - let backup_db_error_msg = `WARNING: Unknown DB instance. DB Backup failed.`; - showMessage(backup_db_error_msg); - throw new Error(backup_db_error_msg); - }; + // if(typeof backup_server_db_instance !== "object") { + // let backup_db_error_msg = `WARNING: Unknown DB instance. DB Backup failed.`; + // showMessage(backup_db_error_msg); + // throw new Error(backup_db_error_msg); + // }; - backup_server_db_instance.backup_readDB("supernode_private_key_chunks", res_obj.params[0].chunk_val).then(function ( - res) { - RM_RPC - .send_rpc - .call(this, "retrieve_shamirs_secret_btc_pvtkey", { - retrieve_pvtkey_req_id: res_obj.params[0].retrieve_pvtkey_req_id, - private_key_chunk: res, - withdraw_id: res_obj.params[0].withdraw_id, - receiver_flo_address: res_obj.globalParams.senderFloId, - }).then(send_pvtkey_req=>doSend(send_pvtkey_req, res_obj.globalParams.senderFloId)); - }); + // backup_server_db_instance.backup_readDB("supernode_private_key_chunks", res_obj.params[0].chunk_val).then(function ( + // res) { + // RM_RPC + // .send_rpc + // .call(this, "retrieve_shamirs_secret_btc_pvtkey", { + // retrieve_pvtkey_req_id: res_obj.params[0].retrieve_pvtkey_req_id, + // private_key_chunk: res, + // withdraw_id: res_obj.params[0].withdraw_id, + // receiver_flo_address: res_obj.globalParams.senderFloId, + // }).then(send_pvtkey_req=>doSend(send_pvtkey_req, res_obj.globalParams.senderFloId)); + // }); + // }); + + readDB("supernode_private_key_chunks", res_obj.params[0].chunk_val).then(function ( + res) { + RM_RPC + .send_rpc + .call(this, "retrieve_shamirs_secret_btc_pvtkey", { + retrieve_pvtkey_req_id: res_obj.params[0].retrieve_pvtkey_req_id, + private_key_chunk: res, + withdraw_id: res_obj.params[0].withdraw_id, + db_inst: res_obj.params[0].db_inst, + receiver_flo_address: res_obj.globalParams.senderFloId, + }).then(send_pvtkey_req=>doSend(send_pvtkey_req)); }); } break; @@ -18732,28 +18761,42 @@ if (typeof res_obj.params == "object" && typeof res_obj.params[0] == "object") { if(typeof res_obj.globalParams.senderFloId !="string") return; - localbitcoinplusplus.kademlia.determineClosestSupernode(res_obj.globalParams.senderFloId) - .then(my_closest_su_list=>{ - const primarySupernodeOfThisUser = my_closest_su_list[0].data.id; - const backup_server_db_instance = localbitcoinplusplus.newBackupDatabase.db[primarySupernodeOfThisUser]; + + // localbitcoinplusplus.kademlia.determineClosestSupernode(res_obj.globalParams.senderFloId) + // .then(my_closest_su_list=>{ + // const primarySupernodeOfThisUser = my_closest_su_list[0].data.id; + // const backup_server_db_instance = localbitcoinplusplus.newBackupDatabase.db[primarySupernodeOfThisUser]; - if(typeof backup_server_db_instance !== "object") { - let backup_db_error_msg = `WARNING: Unknown DB instance. DB Backup failed.`; - showMessage(backup_db_error_msg); - throw new Error(backup_db_error_msg); - }; + // if(typeof backup_server_db_instance !== "object") { + // let backup_db_error_msg = `WARNING: Unknown DB instance. DB Backup failed.`; + // showMessage(backup_db_error_msg); + // throw new Error(backup_db_error_msg); + // }; - backup_server_db_instance.backup_readDB("supernode_private_key_chunks", res_obj.params[0].chunk_val).then(function ( - res) { - RM_RPC - .send_rpc - .call(this, "retrieve_shamirs_secret_btc_pvtkey", { - retrieve_pvtkey_req_id: res_obj.params[0].retrieve_pvtkey_req_id, - private_key_chunk: res, - withdraw_id: res_obj.params[0].withdraw_id, - receiver_flo_address: res_obj.globalParams.senderFloId, - }).then(send_pvtkey_req=>doSend(send_pvtkey_req, res_obj.globalParams.senderFloId)); - }); + // backup_server_db_instance.backup_readDB("supernode_private_key_chunks", res_obj.params[0].chunk_val).then(function ( + // res) { + // RM_RPC + // .send_rpc + // .call(this, "retrieve_shamirs_secret_btc_pvtkey", { + // retrieve_pvtkey_req_id: res_obj.params[0].retrieve_pvtkey_req_id, + // private_key_chunk: res, + // withdraw_id: res_obj.params[0].withdraw_id, + // receiver_flo_address: res_obj.globalParams.senderFloId, + // }).then(send_pvtkey_req=>doSend(send_pvtkey_req, res_obj.globalParams.senderFloId)); + // }); + // }); + + readDB("supernode_private_key_chunks", res_obj.params[0].chunk_val).then(function ( + res) { + RM_RPC + .send_rpc + .call(this, "retrieve_shamirs_secret_btc_pvtkey", { + retrieve_pvtkey_req_id: res_obj.params[0].retrieve_pvtkey_req_id, + private_key_chunk: res, + withdraw_id: res_obj.params[0].withdraw_id, + receiver_flo_address: res_obj.globalParams.senderFloId, + db_inst: res_obj.params[0].db_inst + }).then(send_pvtkey_req=>doSend(send_pvtkey_req)); }); } break; @@ -18761,23 +18804,50 @@ case "store_shamirs_secret_pvtkey_shares": if (typeof res_obj.params == "object" && typeof res_obj.params[0] == "object") { - if(typeof res_obj.params[0].trader_flo_address !="string") return; - localbitcoinplusplus.kademlia.determineClosestSupernode(res_obj.params[0].trader_flo_address) - .then(my_closest_su_list=>{ - const primarySupernodeOfThisUser = my_closest_su_list[0].data.id; - const backup_server_db_instance = localbitcoinplusplus.newBackupDatabase.db[primarySupernodeOfThisUser]; + // if(typeof res_obj.params[0].trader_flo_address !="string") return; + // localbitcoinplusplus.kademlia.determineClosestSupernode(res_obj.params[0].trader_flo_address) + // .then(my_closest_su_list=>{ + // const primarySupernodeOfThisUser = my_closest_su_list[0].data.id; + // const backup_server_db_instance = localbitcoinplusplus.newBackupDatabase.db[primarySupernodeOfThisUser]; - if(typeof backup_server_db_instance !== "object") { - let backup_db_error_msg = `WARNING: Unknown DB instance. DB Backup failed.`; - showMessage(backup_db_error_msg); - throw new Error(backup_db_error_msg); - }; - delete res_obj.params[0].trader_flo_address; - backup_server_db_instance.backup_addDB("supernode_private_key_chunks", res_obj.params[0]); - }); + // if(typeof backup_server_db_instance !== "object") { + // let backup_db_error_msg = `WARNING: Unknown DB instance. DB Backup failed.`; + // showMessage(backup_db_error_msg); + // throw new Error(backup_db_error_msg); + // }; + // delete res_obj.params[0].trader_flo_address; + // backup_server_db_instance.backup_addDB("supernode_private_key_chunks", res_obj.params[0]); + // }); + delete res_obj.params[0].trader_flo_address; + addDB("supernode_private_key_chunks", res_obj.params[0]); + } break; + case "retrieve_shamirs_secret_btc_pvtkey": + if (typeof res_obj.params == "object" && typeof res_obj.params[0] == "object" && + typeof res_obj.params[0].private_key_chunk == "object" && + typeof res_obj.params[0].retrieve_pvtkey_req_id == "string" && + typeof res_obj.params[0].withdraw_id == "string") { + let shamirs_shares_response = res_obj.params[0]; + let retrieve_pvtkey_req_id = res_obj.params[0].retrieve_pvtkey_req_id; + let withdraw_id = res_obj.params[0].withdraw_id; + if (typeof btc_pvt_arr !== "object") btc_pvt_arr = []; + if (typeof btc_pvt_arr[retrieve_pvtkey_req_id] == "undefined") btc_pvt_arr[ + retrieve_pvtkey_req_id] = []; + btc_pvt_arr[retrieve_pvtkey_req_id].push(shamirs_shares_response); + if (btc_pvt_arr[retrieve_pvtkey_req_id].length === localbitcoinplusplus.master_configurations + .ShamirsMaxShares) { + delete res_obj.params[0].private_key_chunk; + res_obj.params[0].btc_private_key_array = JSON.stringify(btc_pvt_arr[ + retrieve_pvtkey_req_id]); + res_obj.params[0].trader_flo_address = localbitcoinplusplus.wallets.my_local_flo_address; + RM_RPC.backup_receive_rpc_response.call(this, JSON.stringify(res_obj)); + btc_pvt_arr[retrieve_pvtkey_req_id] = []; // Unset the object + } + } + break; + case "updateUserCryptoBalanceRequest": if (localbitcoinplusplus.master_configurations.supernodesPubKeys .includes(res_obj.nodePubKey)) {