improved retrieve_shamirs_secret_btc_pvtkey logic
This commit is contained in:
parent
aabc8dbc77
commit
2e19df47bc
466
index.html
466
index.html
@ -14267,6 +14267,7 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
reactor.addEventListener('createClosestSupernodesObject', async function(getClosestSuList=[]) {
|
reactor.addEventListener('createClosestSupernodesObject', async function(getClosestSuList=[]) {
|
||||||
|
|
||||||
if (typeof localbitcoinplusplus.myClosestSupernodes === "object"
|
if (typeof localbitcoinplusplus.myClosestSupernodes === "object"
|
||||||
@ -14352,7 +14353,7 @@
|
|||||||
/* Send user a message */
|
/* Send user a message */
|
||||||
reactor.addEventListener("message_for_user", function(response={}) {
|
reactor.addEventListener("message_for_user", function(response={}) {
|
||||||
|
|
||||||
if(localbitcoinplusplus.master_configurations.supernodesPubKeys
|
if(!localbitcoinplusplus.master_configurations.supernodesPubKeys
|
||||||
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) return;
|
.includes(localbitcoinplusplus.wallets.my_local_flo_public_key)) return;
|
||||||
|
|
||||||
const RM_RPC = new localbitcoinplusplus.rpc();
|
const RM_RPC = new localbitcoinplusplus.rpc();
|
||||||
@ -15857,7 +15858,7 @@
|
|||||||
error: false,
|
error: false,
|
||||||
method:
|
method:
|
||||||
"deposit_asset_request_response",
|
"deposit_asset_request_response",
|
||||||
msg: `Please send ${params.product} ${params.bitcoinToBePaid} to the following addres: ${generate_btc_keys_for_requester.address}.`,
|
msg: `Please send ${params.product} ${params.bitcoinToBePaid} to the following addres: ${generate_btc_keys_for_requester.address}`,
|
||||||
data: deposit_res
|
data: deposit_res
|
||||||
};
|
};
|
||||||
deposit_response_object.receiver_flo_address =
|
deposit_response_object.receiver_flo_address =
|
||||||
@ -16572,7 +16573,7 @@
|
|||||||
case "retrieve_shamirs_secret_btc_pvtkey":
|
case "retrieve_shamirs_secret_btc_pvtkey":
|
||||||
RM_RPC.filter_legit_requests(
|
RM_RPC.filter_legit_requests(
|
||||||
params.trader_flo_address,
|
params.trader_flo_address,
|
||||||
function(is_valid_request) {
|
async function (is_valid_request) {
|
||||||
if (is_valid_request !== true) return false;
|
if (is_valid_request !== true) return false;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@ -16596,17 +16597,14 @@
|
|||||||
if (
|
if (
|
||||||
params.db_inst !==
|
params.db_inst !==
|
||||||
localbitcoinplusplus.wallets.my_local_flo_address
|
localbitcoinplusplus.wallets.my_local_flo_address
|
||||||
)
|
) return;
|
||||||
return;
|
|
||||||
|
|
||||||
let btc_private_key_str = params.btc_private_key_array;
|
let btc_private_key_str = params.btc_private_key_array;
|
||||||
let retrieve_pvtkey_req_id = params.retrieve_pvtkey_req_id;
|
let retrieve_pvtkey_req_id = params.retrieve_pvtkey_req_id;
|
||||||
let withdraw_id = params.withdraw_id;
|
let withdraw_id = params.withdraw_id;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let btc_private_key_object = JSON.parse(
|
let btc_private_key_object = JSON.parse(btc_private_key_str);
|
||||||
btc_private_key_str
|
|
||||||
);
|
|
||||||
let btc_pk_shares_array = btc_private_key_object
|
let btc_pk_shares_array = btc_private_key_object
|
||||||
.map(pkChunks => {
|
.map(pkChunks => {
|
||||||
if (typeof pkChunks.private_key_chunk !== "undefined")
|
if (typeof pkChunks.private_key_chunk !== "undefined")
|
||||||
@ -16615,14 +16613,21 @@
|
|||||||
.filter(val => val !== undefined);
|
.filter(val => val !== undefined);
|
||||||
console.log(btc_pk_shares_array);
|
console.log(btc_pk_shares_array);
|
||||||
|
|
||||||
readDB("withdraw_btc", withdraw_id).then(function(
|
const withdraw_res = await readDB("withdraw_btc", withdraw_id);
|
||||||
withdraw_res
|
|
||||||
) {
|
|
||||||
if (typeof withdraw_res == "object") {
|
if (typeof withdraw_res == "object") {
|
||||||
readDB(
|
const withdrawer_crypto_bal_id = `${withdraw_res.trader_flo_address}_${withdraw_res.product}`;
|
||||||
"system_btc_reserves_private_keys",
|
const withdrawer_crypto_bal_response = await readDB("crypto_balances", withdrawer_crypto_bal_id);
|
||||||
retrieve_pvtkey_req_id
|
|
||||||
).then(async function(btc_reserves) {
|
if (typeof withdrawer_crypto_bal_response !== "object"
|
||||||
|
|| typeof withdrawer_crypto_bal_response.crypto_balance !== "number") {
|
||||||
|
err_response = {
|
||||||
|
user_flo_addr: params.trader_flo_address,
|
||||||
|
msg: `You do not have any balance to withdraw ${withdraw_res.product}.`
|
||||||
|
}
|
||||||
|
reactor.dispatchEvent('message_for_user', err_response);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const btc_reserves = await readDB("system_btc_reserves_private_keys", retrieve_pvtkey_req_id);
|
||||||
if (typeof btc_reserves == "object") {
|
if (typeof btc_reserves == "object") {
|
||||||
// Ideally this line should never run.
|
// Ideally this line should never run.
|
||||||
if (btc_reserves.product !== withdraw_res.product)
|
if (btc_reserves.product !== withdraw_res.product)
|
||||||
@ -16634,10 +16639,18 @@
|
|||||||
withdraw_res.product,
|
withdraw_res.product,
|
||||||
withdraw_res.currency
|
withdraw_res.currency
|
||||||
);
|
);
|
||||||
const EqCryptoWd = withdraw_res.receivingBTC;
|
const EqCryptoWd = helper_functions.truncateDecimals(withdraw_res.receivingBTC);
|
||||||
|
|
||||||
let transaction_key =
|
if (EqCryptoWd > withdrawer_crypto_bal_response.crypto_balance) {
|
||||||
btc_reserves.supernode_transaction_key;
|
err_response = {
|
||||||
|
user_flo_addr: params.trader_flo_address,
|
||||||
|
msg: `You are withdrawing ${withdraw_res.product} more than your balance.`
|
||||||
|
}
|
||||||
|
reactor.dispatchEvent('message_for_user', err_response);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let transaction_key = btc_reserves.supernode_transaction_key;
|
||||||
if (transaction_key.length > 0) {
|
if (transaction_key.length > 0) {
|
||||||
let btc_private_key = RM_WALLET.rebuild_private_key(
|
let btc_private_key = RM_WALLET.rebuild_private_key(
|
||||||
btc_pk_shares_array,
|
btc_pk_shares_array,
|
||||||
@ -16666,20 +16679,7 @@
|
|||||||
resp_obj.txid.result ||
|
resp_obj.txid.result ||
|
||||||
resp_obj.txid;
|
resp_obj.txid;
|
||||||
msg = `Transaction Id for your withdrawn crypto asset: ${resp_txid}.`;
|
msg = `Transaction Id for your withdrawn crypto asset: ${resp_txid}.`;
|
||||||
} else {
|
|
||||||
console.log(res);
|
|
||||||
err_response = {
|
|
||||||
user_flo_addr: params.trader_flo_address,
|
|
||||||
msg: `Withdraw of crypto failed. Please try again later.`
|
|
||||||
}
|
|
||||||
reactor.dispatchEvent('message_for_user', err_response);
|
|
||||||
throw new Errror(
|
|
||||||
`ERROR: Failed to make transaction.`
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (msg.length > 0) {
|
|
||||||
const RM_RPC = new localbitcoinplusplus.rpc();
|
const RM_RPC = new localbitcoinplusplus.rpc();
|
||||||
RM_RPC.send_rpc
|
RM_RPC.send_rpc
|
||||||
.call(this, "supernode_message", {
|
.call(this, "supernode_message", {
|
||||||
@ -16692,42 +16692,25 @@
|
|||||||
.then(server_response =>
|
.then(server_response =>
|
||||||
doSend(server_response)
|
doSend(server_response)
|
||||||
);
|
);
|
||||||
} else return;
|
|
||||||
const withdrawer_crypto_bal_id = `${withdraw_res.trader_flo_address}_${withdraw_res.product}`;
|
withdrawer_crypto_bal_response.crypto_balance -= EqCryptoWd;
|
||||||
readDB(
|
const updated_crypto_balance = await updateinDB(
|
||||||
"crypto_balances",
|
"crypto_balances",
|
||||||
|
withdrawer_crypto_bal_response,
|
||||||
withdrawer_crypto_bal_id
|
withdrawer_crypto_bal_id
|
||||||
).then(res_bal => {
|
|
||||||
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;
|
|
||||||
updateinDB(
|
|
||||||
"crypto_balances",
|
|
||||||
res_bal,
|
|
||||||
withdrawer_crypto_bal_id
|
|
||||||
).then(res_obj => {
|
|
||||||
const res_obj_str = JSON.stringify(
|
|
||||||
res_obj
|
|
||||||
);
|
|
||||||
const res_obj_hash = Crypto.SHA256(
|
|
||||||
res_obj_str
|
|
||||||
);
|
);
|
||||||
|
const res_obj_str = JSON.stringify(updated_crypto_balance);
|
||||||
|
const res_obj_hash = Crypto.SHA256(res_obj_str);
|
||||||
const res_obj_sign = RM_WALLET.sign(
|
const res_obj_sign = RM_WALLET.sign(
|
||||||
res_obj_hash,
|
res_obj_hash,
|
||||||
localbitcoinplusplus.wallets
|
localbitcoinplusplus.wallets.MY_SUPERNODE_PRIVATE_KEY
|
||||||
.MY_SUPERNODE_PRIVATE_KEY
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const updateUserCryptoBalanceObject = {
|
const updateUserCryptoBalanceObject = {
|
||||||
updatedBTCBalanceObject: res_obj,
|
updatedBTCBalanceObject: updated_crypto_balance,
|
||||||
updatedBTCBalanceObjectSign: res_obj_sign,
|
updatedBTCBalanceObjectSign: res_obj_sign,
|
||||||
trader_flo_address:
|
trader_flo_address: withdraw_res.trader_flo_address,
|
||||||
withdraw_res.trader_flo_address,
|
receiver_flo_address: withdraw_res.trader_flo_address
|
||||||
receiver_flo_address:
|
|
||||||
withdraw_res.trader_flo_address
|
|
||||||
};
|
};
|
||||||
|
|
||||||
RM_RPC.send_rpc(
|
RM_RPC.send_rpc(
|
||||||
@ -16735,39 +16718,8 @@
|
|||||||
updateUserCryptoBalanceObject
|
updateUserCryptoBalanceObject
|
||||||
).then(
|
).then(
|
||||||
updateUserCryptoBalanceRequestObject =>
|
updateUserCryptoBalanceRequestObject =>
|
||||||
doSend(
|
doSend(updateUserCryptoBalanceRequestObject)
|
||||||
updateUserCryptoBalanceRequestObject
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
});
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.warn(error);
|
|
||||||
// Do not delete these data instantly as the data
|
|
||||||
// may be required by a follow-up withdraw request
|
|
||||||
localbitcoinplusplus.actions
|
|
||||||
.delay(900000)
|
|
||||||
.then(() => {
|
|
||||||
removeinDB("withdraw_btc", withdraw_id);
|
|
||||||
// update deposits status back to 2 in db
|
|
||||||
readDBbyIndex(
|
|
||||||
"deposit",
|
|
||||||
"btc_address",
|
|
||||||
withdraw_res.utxo_addr
|
|
||||||
).then(deposit_arr_resp => {
|
|
||||||
if (typeof deposit_arr_resp[0] == "object") {
|
|
||||||
deposit_arr_resp[0].status = 2; // UTXO ready to be used again
|
|
||||||
updateinDB(
|
|
||||||
"deposit",
|
|
||||||
deposit_arr_resp[0],
|
|
||||||
deposit_arr_resp[0].id
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if there's BTC left in deposited BTC. If yes update its status to 2 else delete it
|
// Check if there's BTC left in deposited BTC. If yes update its status to 2 else delete it
|
||||||
|
|
||||||
@ -16775,11 +16727,11 @@
|
|||||||
*******************CHECK ACTUAL BTC BALANCE HERE THROUGH AN API AND UPDATE DEPOSIT TABLE****************************************************
|
*******************CHECK ACTUAL BTC BALANCE HERE THROUGH AN API AND UPDATE DEPOSIT TABLE****************************************************
|
||||||
************************************************************************************************************************************/
|
************************************************************************************************************************************/
|
||||||
|
|
||||||
readDBbyIndex(
|
const deposit_arr_resp = await readDBbyIndex(
|
||||||
"deposit",
|
"deposit",
|
||||||
"btc_address",
|
"btc_address",
|
||||||
withdraw_res.utxo_addr
|
withdraw_res.utxo_addr
|
||||||
).then(deposit_arr_resp => {
|
);
|
||||||
if (typeof deposit_arr_resp == "object") {
|
if (typeof deposit_arr_resp == "object") {
|
||||||
deposit_arr_resp.map(
|
deposit_arr_resp.map(
|
||||||
async deposit_arr => {
|
async deposit_arr => {
|
||||||
@ -16822,9 +16774,7 @@
|
|||||||
const bal_url = `${explorer}/api/addr/${withdraw_res.utxo_addr}/balance`;
|
const bal_url = `${explorer}/api/addr/${withdraw_res.utxo_addr}/balance`;
|
||||||
console.log(bal_url);
|
console.log(bal_url);
|
||||||
|
|
||||||
let current_balance = await helper_functions.ajaxGet(
|
let current_balance = await helper_functions.ajaxGet(bal_url);
|
||||||
bal_url
|
|
||||||
);
|
|
||||||
if (
|
if (
|
||||||
!isNaN(current_balance) &&
|
!isNaN(current_balance) &&
|
||||||
parseFloat(current_balance) > 0
|
parseFloat(current_balance) > 0
|
||||||
@ -16842,6 +16792,10 @@
|
|||||||
} else {
|
} else {
|
||||||
deposit_arr.bitcoinToBePaid -= EqCryptoWd;
|
deposit_arr.bitcoinToBePaid -= EqCryptoWd;
|
||||||
btc_reserves.balance -= EqCryptoWd;
|
btc_reserves.balance -= EqCryptoWd;
|
||||||
|
// Tx is not registered in Blocckhain yet. Refresh balance after 30 minutes
|
||||||
|
localbitcoinplusplus.actions.delay(1800000).then(() =>
|
||||||
|
reactor.dispatchEvent("refresh_reserved_crypto_balances", params.trader_flo_address)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@ -16877,13 +16831,8 @@
|
|||||||
|
|
||||||
// Do not delete these data instantly as the data
|
// Do not delete these data instantly as the data
|
||||||
// may be required by a follow-up withdraw request
|
// may be required by a follow-up withdraw request
|
||||||
localbitcoinplusplus.actions
|
await localbitcoinplusplus.actions.delay(180000)
|
||||||
.delay(900000)
|
await removeinDB("withdraw_btc", withdraw_id);
|
||||||
.then(() => {
|
|
||||||
removeinDB(
|
|
||||||
"withdraw_btc",
|
|
||||||
withdraw_id
|
|
||||||
);
|
|
||||||
|
|
||||||
RM_RPC.send_rpc(
|
RM_RPC.send_rpc(
|
||||||
"delete_deposited_crypto_instance",
|
"delete_deposited_crypto_instance",
|
||||||
@ -16896,28 +16845,18 @@
|
|||||||
).then(delRequestObject =>
|
).then(delRequestObject =>
|
||||||
doSend(delRequestObject)
|
doSend(delRequestObject)
|
||||||
);
|
);
|
||||||
});
|
|
||||||
x
|
|
||||||
// AND DO THE SAME ABOVE 2 IN BACKUP RECEIVE RPC
|
// AND DO THE SAME ABOVE 2 IN BACKUP RECEIVE RPC
|
||||||
} else {
|
} else {
|
||||||
// Do not delete these data instantly as the data
|
// Do not delete these data instantly as the data
|
||||||
// may be required by a follow-up withdraw request
|
// may be required by a follow-up withdraw request
|
||||||
localbitcoinplusplus.actions
|
await localbitcoinplusplus.actions.delay(180000);
|
||||||
.delay(900000)
|
|
||||||
.then(() => {
|
let p1 = removeinDB("deposit", deposit_arr.id);
|
||||||
// delete entry in deposits in db
|
let p2 = removeinDB("system_btc_reserves_private_keys", retrieve_pvtkey_req_id);
|
||||||
removeinDB(
|
let p3 = removeinDB("withdraw_btc", withdraw_id);
|
||||||
"deposit",
|
|
||||||
deposit_arr.id
|
await Promise.all([p1, p2, p3]);
|
||||||
);
|
|
||||||
removeinDB(
|
|
||||||
"system_btc_reserves_private_keys",
|
|
||||||
retrieve_pvtkey_req_id
|
|
||||||
);
|
|
||||||
removeinDB(
|
|
||||||
"withdraw_btc",
|
|
||||||
withdraw_id
|
|
||||||
);
|
|
||||||
|
|
||||||
RM_RPC.send_rpc(
|
RM_RPC.send_rpc(
|
||||||
"delete_deposited_crypto_instance",
|
"delete_deposited_crypto_instance",
|
||||||
@ -16933,20 +16872,55 @@ x
|
|||||||
).then(delRequestObject =>
|
).then(delRequestObject =>
|
||||||
doSend(delRequestObject)
|
doSend(delRequestObject)
|
||||||
);
|
);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
} else {
|
||||||
|
console.log(res);
|
||||||
|
throw new Errror(`ERROR: Failed to make transaction.`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.warn(error);
|
||||||
|
err_response = {
|
||||||
|
user_flo_addr: params.trader_flo_address,
|
||||||
|
msg: `Withdraw of crypto failed. Network issue:- Please try again after 30 minutes.`
|
||||||
|
}
|
||||||
|
reactor.dispatchEvent('message_for_user', err_response);
|
||||||
|
// Do not delete these data instantly as the data
|
||||||
|
// may be required by a follow-up withdraw request
|
||||||
|
await localbitcoinplusplus.actions.delay(180000);
|
||||||
|
|
||||||
|
await removeinDB("withdraw_btc", withdraw_id);
|
||||||
|
// update deposits status back to 2 in db
|
||||||
|
const deposit_arr_resp = await readDBbyIndex(
|
||||||
|
"deposit",
|
||||||
|
"btc_address",
|
||||||
|
withdraw_res.utxo_addr
|
||||||
|
)
|
||||||
|
if (typeof deposit_arr_resp[0] == "object") {
|
||||||
|
deposit_arr_resp[0].status = 2; // UTXO ready to be used again
|
||||||
|
await updateinDB(
|
||||||
|
"deposit",
|
||||||
|
deposit_arr_resp[0],
|
||||||
|
deposit_arr_resp[0].id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(error);
|
throw new Error(error);
|
||||||
}
|
}
|
||||||
@ -17518,7 +17492,7 @@ x
|
|||||||
let deposit_response_object = {
|
let deposit_response_object = {
|
||||||
error: false,
|
error: false,
|
||||||
method: "deposit_asset_request_response",
|
method: "deposit_asset_request_response",
|
||||||
msg: `Please send ${params.product} ${params.bitcoinToBePaid} to the following addres: ${generate_btc_keys_for_requester.address}.`,
|
msg: `Please send ${params.product} ${params.bitcoinToBePaid} to the following addres: ${generate_btc_keys_for_requester.address}`,
|
||||||
data: deposit_res
|
data: deposit_res
|
||||||
};
|
};
|
||||||
deposit_response_object.receiver_flo_address =
|
deposit_response_object.receiver_flo_address =
|
||||||
@ -18242,7 +18216,7 @@ x
|
|||||||
case "retrieve_shamirs_secret_btc_pvtkey":
|
case "retrieve_shamirs_secret_btc_pvtkey":
|
||||||
RM_RPC.filter_legit_backup_requests(
|
RM_RPC.filter_legit_backup_requests(
|
||||||
params.trader_flo_address,
|
params.trader_flo_address,
|
||||||
function(is_valid_request) {
|
async function (is_valid_request) {
|
||||||
if (is_valid_request !== true) return false;
|
if (is_valid_request !== true) return false;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@ -18290,16 +18264,27 @@ x
|
|||||||
.filter(val => val !== undefined);
|
.filter(val => val !== undefined);
|
||||||
console.log(btc_pk_shares_array);
|
console.log(btc_pk_shares_array);
|
||||||
|
|
||||||
backup_server_db_instance
|
const withdraw_res = await backup_server_db_instance
|
||||||
.backup_readDB("withdraw_btc", withdraw_id)
|
.backup_readDB("withdraw_btc", withdraw_id);
|
||||||
.then(function(withdraw_res) {
|
|
||||||
if (typeof withdraw_res == "object") {
|
if (typeof withdraw_res == "object") {
|
||||||
backup_server_db_instance
|
const withdrawer_crypto_bal_id = `${withdraw_res.trader_flo_address}_${withdraw_res.product}`;
|
||||||
.backup_readDB(
|
const withdrawer_crypto_bal_response = await backup_server_db_instance
|
||||||
"system_btc_reserves_private_keys",
|
.backup_readDB("crypto_balances", withdrawer_crypto_bal_id);
|
||||||
retrieve_pvtkey_req_id
|
|
||||||
)
|
if (typeof withdrawer_crypto_bal_response !== "object"
|
||||||
.then(async function(btc_reserves) {
|
|| typeof withdrawer_crypto_bal_response.crypto_balance !== "number") {
|
||||||
|
err_response = {
|
||||||
|
user_flo_addr: params.trader_flo_address,
|
||||||
|
msg: `You do not have any balance to withdraw ${withdraw_res.product}.`
|
||||||
|
}
|
||||||
|
reactor.dispatchEvent('message_for_user', err_response);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const btc_reserves = await backup_server_db_instance
|
||||||
|
.backup_readDB("system_btc_reserves_private_keys", retrieve_pvtkey_req_id);
|
||||||
|
|
||||||
if (typeof btc_reserves == "object") {
|
if (typeof btc_reserves == "object") {
|
||||||
// Ideally this line should never run.
|
// Ideally this line should never run.
|
||||||
if (
|
if (
|
||||||
@ -18314,12 +18299,18 @@ x
|
|||||||
withdraw_res.product,
|
withdraw_res.product,
|
||||||
withdraw_res.currency
|
withdraw_res.currency
|
||||||
);
|
);
|
||||||
let EqCryptoWd = helper_functions.truncateDecimals(
|
const EqCryptoWd = helper_functions.truncateDecimals(withdraw_res.receivingBTC);
|
||||||
withdraw_res.receivingBTC
|
|
||||||
);
|
|
||||||
|
|
||||||
let transaction_key =
|
if (EqCryptoWd > withdrawer_crypto_bal_response.crypto_balance) {
|
||||||
btc_reserves.supernode_transaction_key;
|
err_response = {
|
||||||
|
user_flo_addr: params.trader_flo_address,
|
||||||
|
msg: `You are withdrawing ${withdraw_res.product} more than your balance.`
|
||||||
|
}
|
||||||
|
reactor.dispatchEvent('message_for_user', err_response);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let transaction_key = btc_reserves.supernode_transaction_key;
|
||||||
if (transaction_key.length > 0) {
|
if (transaction_key.length > 0) {
|
||||||
let btc_private_key = RM_WALLET.rebuild_private_key(
|
let btc_private_key = RM_WALLET.rebuild_private_key(
|
||||||
btc_pk_shares_array,
|
btc_pk_shares_array,
|
||||||
@ -18349,20 +18340,7 @@ x
|
|||||||
resp_obj.txid.result ||
|
resp_obj.txid.result ||
|
||||||
resp_obj.txid;
|
resp_obj.txid;
|
||||||
msg = `Transaction Id for your withdrawn crypto asset: ${resp_txid}.`;
|
msg = `Transaction Id for your withdrawn crypto asset: ${resp_txid}.`;
|
||||||
} else {
|
|
||||||
console.log(res);
|
|
||||||
err_response = {
|
|
||||||
user_flo_addr: params.trader_flo_address,
|
|
||||||
msg: `Withdraw crypto request failed. Please try again later.`
|
|
||||||
}
|
|
||||||
reactor.dispatchEvent('message_for_user', err_response);
|
|
||||||
throw new Errror(
|
|
||||||
`ERROR: Failed to make transaction.`
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (msg.length > 0) {
|
|
||||||
const RM_RPC = new localbitcoinplusplus.rpc();
|
const RM_RPC = new localbitcoinplusplus.rpc();
|
||||||
RM_RPC.send_rpc
|
RM_RPC.send_rpc
|
||||||
.call(
|
.call(
|
||||||
@ -18379,32 +18357,18 @@ x
|
|||||||
.then(server_response =>
|
.then(server_response =>
|
||||||
doSend(server_response)
|
doSend(server_response)
|
||||||
);
|
);
|
||||||
} else return;
|
|
||||||
|
|
||||||
let withdrawer_crypto_bal_id = `${withdraw_res.trader_flo_address}_${withdraw_res.product}`;
|
let withdrawer_crypto_bal_id = `${withdraw_res.trader_flo_address}_${withdraw_res.product}`;
|
||||||
backup_server_db_instance
|
|
||||||
.backup_readDB(
|
withdrawer_crypto_bal_response.crypto_balance -= EqCryptoWd;
|
||||||
"crypto_balances",
|
const updated_crypto_balance = await backup_server_db_instance
|
||||||
withdrawer_crypto_bal_id
|
|
||||||
)
|
|
||||||
.then(res_bal => {
|
|
||||||
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;
|
|
||||||
backup_server_db_instance
|
|
||||||
.backup_updateinDB(
|
.backup_updateinDB(
|
||||||
"crypto_balances",
|
"crypto_balances",
|
||||||
res_bal,
|
withdrawer_crypto_bal_response,
|
||||||
withdrawer_crypto_bal_id
|
withdrawer_crypto_bal_id
|
||||||
)
|
);
|
||||||
.then(res_obj => {
|
|
||||||
const res_obj_str = JSON.stringify(
|
const res_obj_str = JSON.stringify(
|
||||||
res_obj
|
updated_crypto_balance
|
||||||
);
|
);
|
||||||
const res_obj_hash = Crypto.SHA256(
|
const res_obj_hash = Crypto.SHA256(
|
||||||
res_obj_str
|
res_obj_str
|
||||||
@ -18417,7 +18381,7 @@ x
|
|||||||
);
|
);
|
||||||
|
|
||||||
const updateUserCryptoBalanceObject = {
|
const updateUserCryptoBalanceObject = {
|
||||||
updatedBTCBalanceObject: res_obj,
|
updatedBTCBalanceObject: updated_crypto_balance,
|
||||||
updatedBTCBalanceObjectSign: res_obj_sign,
|
updatedBTCBalanceObjectSign: res_obj_sign,
|
||||||
trader_flo_address:
|
trader_flo_address:
|
||||||
withdraw_res.trader_flo_address,
|
withdraw_res.trader_flo_address,
|
||||||
@ -18434,51 +18398,17 @@ x
|
|||||||
updateUserCryptoBalanceRequestObject
|
updateUserCryptoBalanceRequestObject
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
});
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.warn(error);
|
|
||||||
// Do not delete these data instantly as the data
|
|
||||||
// may be required by a follow-up withdraw request
|
|
||||||
localbitcoinplusplus.actions
|
|
||||||
.delay(900000)
|
|
||||||
.then(() => {
|
|
||||||
backup_server_db_instance
|
|
||||||
.backup_removeinDB("withdraw_btc", withdraw_id);
|
|
||||||
// update deposits status back to 2 in db
|
|
||||||
backup_server_db_instance
|
|
||||||
.backup_readDBbyIndex(
|
|
||||||
"deposit",
|
|
||||||
"btc_address",
|
|
||||||
withdraw_res.utxo_addr
|
|
||||||
).then(deposit_arr_resp => {
|
|
||||||
if (typeof deposit_arr_resp[0] == "object") {
|
|
||||||
deposit_arr_resp[0].status = 2; // UTXO ready to be used again
|
|
||||||
backup_server_db_instance
|
|
||||||
.backup_updateinDB(
|
|
||||||
"deposit",
|
|
||||||
deposit_arr_resp[0],
|
|
||||||
deposit_arr_resp[0].id
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if there's BTC left in deposited BTC. If yes update its status to 2 else delete it
|
// Check if there's BTC left in deposited BTC. If yes update its status to 2 else delete it
|
||||||
|
|
||||||
/***********************************************************************************************************************************
|
/***********************************************************************************************************************************
|
||||||
*******************CHECK ACTUAL BTC BALANCE HERE THROUGH AN API AND UPDATE DEPOSIT TABLE****************************************************
|
*******************CHECK ACTUAL BTC BALANCE HERE THROUGH AN API AND UPDATE DEPOSIT TABLE****************************************************
|
||||||
************************************************************************************************************************************/
|
************************************************************************************************************************************/
|
||||||
|
|
||||||
backup_server_db_instance
|
const deposit_arr_resp = await backup_server_db_instance
|
||||||
.backup_readDBbyIndex(
|
.backup_readDBbyIndex(
|
||||||
"deposit",
|
"deposit",
|
||||||
"btc_address",
|
"btc_address",
|
||||||
withdraw_res.utxo_addr
|
withdraw_res.utxo_addr
|
||||||
)
|
);
|
||||||
.then(function(deposit_arr_resp) {
|
|
||||||
if (
|
if (
|
||||||
typeof deposit_arr_resp ==
|
typeof deposit_arr_resp ==
|
||||||
"object"
|
"object"
|
||||||
@ -18545,8 +18475,14 @@ x
|
|||||||
"number"
|
"number"
|
||||||
) {
|
) {
|
||||||
deposit_arr.bitcoinToBePaid = current_balance;
|
deposit_arr.bitcoinToBePaid = current_balance;
|
||||||
|
btc_reserves.balance = current_balance;
|
||||||
} else {
|
} else {
|
||||||
deposit_arr.bitcoinToBePaid -= EqCryptoWd;
|
deposit_arr.bitcoinToBePaid -= EqCryptoWd;
|
||||||
|
btc_reserves.balance -= EqCryptoWd;
|
||||||
|
// Tx is not registered in Blocckhain yet. Refresh balance after 30 minutes
|
||||||
|
localbitcoinplusplus.actions.delay(1800000).then(() =>
|
||||||
|
reactor.dispatchEvent("refresh_reserved_crypto_balances", params.trader_flo_address)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@ -18582,10 +18518,9 @@ x
|
|||||||
doSend(delRequestObject)
|
doSend(delRequestObject)
|
||||||
);
|
);
|
||||||
|
|
||||||
localbitcoinplusplus.actions
|
await localbitcoinplusplus.actions.delay(180000);
|
||||||
.delay(900000)
|
|
||||||
.then(() => {
|
await backup_server_db_instance.backup_removeinDB(
|
||||||
backup_server_db_instance.backup_removeinDB(
|
|
||||||
"withdraw_btc",
|
"withdraw_btc",
|
||||||
withdraw_id
|
withdraw_id
|
||||||
);
|
);
|
||||||
@ -18605,27 +18540,28 @@ x
|
|||||||
delRequestObject
|
delRequestObject
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
localbitcoinplusplus.actions
|
await localbitcoinplusplus.actions.delay(180000)
|
||||||
.delay(900000)
|
|
||||||
.then(() => {
|
|
||||||
// delete entry in deposits in db
|
// delete entry in deposits in db
|
||||||
backup_server_db_instance.backup_removeinDB(
|
let p1 = backup_server_db_instance.backup_removeinDB(
|
||||||
"deposit",
|
"deposit",
|
||||||
deposit_arr.id
|
deposit_arr.id
|
||||||
);
|
);
|
||||||
|
|
||||||
backup_server_db_instance.backup_removeinDB(
|
let p2 = backup_server_db_instance.backup_removeinDB(
|
||||||
"system_btc_reserves_private_keys",
|
"system_btc_reserves_private_keys",
|
||||||
retrieve_pvtkey_req_id
|
retrieve_pvtkey_req_id
|
||||||
);
|
);
|
||||||
|
|
||||||
backup_server_db_instance.backup_removeinDB(
|
let p3 = backup_server_db_instance.backup_removeinDB(
|
||||||
"withdraw_btc",
|
"withdraw_btc",
|
||||||
withdraw_id
|
withdraw_id
|
||||||
);
|
);
|
||||||
|
|
||||||
|
await Promise.all([p1, p2, p3]);
|
||||||
|
|
||||||
RM_RPC.send_rpc(
|
RM_RPC.send_rpc(
|
||||||
"delete_deposited_crypto_instance",
|
"delete_deposited_crypto_instance",
|
||||||
{
|
{
|
||||||
@ -18644,20 +18580,61 @@ x
|
|||||||
delRequestObject
|
delRequestObject
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
} else {
|
||||||
|
console.log(res);
|
||||||
|
throw new Errror(
|
||||||
|
`ERROR: Failed to make transaction.`
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.warn(error);
|
||||||
|
err_response = {
|
||||||
|
user_flo_addr: params.trader_flo_address,
|
||||||
|
msg: `Withdraw crypto request failed. Please try again later.`
|
||||||
|
}
|
||||||
|
reactor.dispatchEvent('message_for_user', err_response);
|
||||||
|
|
||||||
|
// Do not delete these data instantly as the data
|
||||||
|
// may be required by a follow-up withdraw request
|
||||||
|
await localbitcoinplusplus.actions.delay(180000);
|
||||||
|
|
||||||
|
await backup_server_db_instance
|
||||||
|
.backup_removeinDB("withdraw_btc", withdraw_id);
|
||||||
|
// update deposits status back to 2 in db
|
||||||
|
const deposit_arr_resp = await backup_server_db_instance
|
||||||
|
.backup_readDBbyIndex(
|
||||||
|
"deposit",
|
||||||
|
"btc_address",
|
||||||
|
withdraw_res.utxo_addr
|
||||||
|
)
|
||||||
|
if (typeof deposit_arr_resp[0] == "object") {
|
||||||
|
deposit_arr_resp[0].status = 2; // UTXO ready to be used again
|
||||||
|
backup_server_db_instance
|
||||||
|
.backup_updateinDB(
|
||||||
|
"deposit",
|
||||||
|
deposit_arr_resp[0],
|
||||||
|
deposit_arr_resp[0].id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(error);
|
throw new Error(error);
|
||||||
}
|
}
|
||||||
@ -19700,7 +19677,7 @@ x
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
helper_functions
|
helper_functions
|
||||||
.ajaxGet(
|
.ajaxGet(
|
||||||
"https://ranchimallflo.duckdns.org/api/v1.0/getPrices"
|
`${localbitcoinplusplus.server.flo_api_mainnet}/api/v1.0/getPrices`
|
||||||
)
|
)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
if (
|
if (
|
||||||
@ -19794,7 +19771,7 @@ x
|
|||||||
crypto_code = "FLO";
|
crypto_code = "FLO";
|
||||||
}
|
}
|
||||||
helper_functions
|
helper_functions
|
||||||
.ajaxGet(`https://ranchimallflo.duckdns.org/api/v1.0/getPrices`)
|
.ajaxGet(`${localbitcoinplusplus.server.flo_api_mainnet}/api/v1.0/getPrices`)
|
||||||
.then(async res => {
|
.then(async res => {
|
||||||
if (
|
if (
|
||||||
typeof res == "object" &&
|
typeof res == "object" &&
|
||||||
@ -20463,7 +20440,7 @@ x
|
|||||||
},
|
},
|
||||||
cancelTrade(trade_id, trader_flo_address, trade_type) {
|
cancelTrade(trade_id, trader_flo_address, trade_type) {
|
||||||
if (typeof trade_id !== "string") {
|
if (typeof trade_id !== "string") {
|
||||||
showMessage("WARNING: Failed to cancel the trade.");
|
showMessage("WARNING: Failed to cancel the order.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const RM_WALLET = new localbitcoinplusplus.wallets();
|
const RM_WALLET = new localbitcoinplusplus.wallets();
|
||||||
@ -22085,7 +22062,7 @@ x
|
|||||||
id => {
|
id => {
|
||||||
msg_response = {
|
msg_response = {
|
||||||
user_flo_addr: params.trader_flo_address,
|
user_flo_addr: params.trader_flo_address,
|
||||||
msg: `Failed to verify trade for trade id ${cancel_request.trade_id}`
|
msg: `Failed to verify order for order id ${cancel_request.trade_id}`
|
||||||
}
|
}
|
||||||
reactor.dispatchEvent('message_for_user', msg_response);
|
reactor.dispatchEvent('message_for_user', msg_response);
|
||||||
}
|
}
|
||||||
@ -22093,12 +22070,12 @@ x
|
|||||||
} else {
|
} else {
|
||||||
err_response = {
|
err_response = {
|
||||||
user_flo_addr: params.trader_flo_address,
|
user_flo_addr: params.trader_flo_address,
|
||||||
msg: `Failed to verify trade for trade id ${cancel_request.trade_id}`
|
msg: `Failed to verify order for order id ${cancel_request.trade_id}`
|
||||||
}
|
}
|
||||||
reactor.dispatchEvent('message_for_user', err_response);
|
reactor.dispatchEvent('message_for_user', err_response);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
showMessage("Failed to cancel trade.");
|
showMessage("Failed to cancel order.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -24034,7 +24011,7 @@ x
|
|||||||
"string"
|
"string"
|
||||||
) {
|
) {
|
||||||
err_msg =
|
err_msg =
|
||||||
"ERROR: Failed to cancel the trade. User is unknown.";
|
"ERROR: Failed to cancel the order. User is unknown.";
|
||||||
showMessage(err_msg);
|
showMessage(err_msg);
|
||||||
throw new Error(err_msg);
|
throw new Error(err_msg);
|
||||||
}
|
}
|
||||||
@ -24058,7 +24035,7 @@ x
|
|||||||
{
|
{
|
||||||
err_response = {
|
err_response = {
|
||||||
user_flo_addr: params.trader_flo_address,
|
user_flo_addr: params.trader_flo_address,
|
||||||
msg: `Trade Id ${id} deleted.`
|
msg: `Order Id ${id} deleted.`
|
||||||
}
|
}
|
||||||
reactor.dispatchEvent('message_for_user', err_response);
|
reactor.dispatchEvent('message_for_user', err_response);
|
||||||
}
|
}
|
||||||
@ -24067,7 +24044,7 @@ x
|
|||||||
} else {
|
} else {
|
||||||
err_response = {
|
err_response = {
|
||||||
user_flo_addr: params.trader_flo_address,
|
user_flo_addr: params.trader_flo_address,
|
||||||
msg: `Failed to verify trade for trade id ${cancel_request.trade_id}`
|
msg: `Failed to verify order for order id ${cancel_request.trade_id}`
|
||||||
}
|
}
|
||||||
reactor.dispatchEvent('message_for_user', err_response);
|
reactor.dispatchEvent('message_for_user', err_response);
|
||||||
}
|
}
|
||||||
@ -24076,7 +24053,7 @@ x
|
|||||||
} else {
|
} else {
|
||||||
err_response = {
|
err_response = {
|
||||||
user_flo_addr: params.trader_flo_address,
|
user_flo_addr: params.trader_flo_address,
|
||||||
msg: `Failed to cancel trade for trade id ${cancel_request.trade_id}`
|
msg: `Failed to cancel order for order id ${cancel_request.trade_id}`
|
||||||
}
|
}
|
||||||
reactor.dispatchEvent('message_for_user', err_response);
|
reactor.dispatchEvent('message_for_user', err_response);
|
||||||
}
|
}
|
||||||
@ -26074,7 +26051,7 @@ x
|
|||||||
"string"
|
"string"
|
||||||
) {
|
) {
|
||||||
err_msg =
|
err_msg =
|
||||||
"ERROR: Failed to cancel the trade. User is unknown.";
|
"ERROR: Failed to cancel the order. User is unknown.";
|
||||||
showMessage(err_msg);
|
showMessage(err_msg);
|
||||||
throw new Error(err_msg);
|
throw new Error(err_msg);
|
||||||
}
|
}
|
||||||
@ -26095,16 +26072,16 @@ x
|
|||||||
cancel_request.trade_id
|
cancel_request.trade_id
|
||||||
)
|
)
|
||||||
.then(id =>
|
.then(id =>
|
||||||
showMessage(`Trade Id ${id} deleted.`)
|
showMessage(`Order Id ${id} deleted.`)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
showMessage(
|
showMessage(
|
||||||
`Failed to verify trade for trade id ${cancel_request.trade_id}`
|
`Failed to verify order for order id ${cancel_request.trade_id}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
showMessage("Failed to cancel trade.");
|
showMessage("Failed to cancel order.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -29715,7 +29692,7 @@ x
|
|||||||
<li>Price: ${price}</li>
|
<li>Price: ${price}</li>
|
||||||
<li>Currency: ${currency}</li>
|
<li>Currency: ${currency}</li>
|
||||||
<li><button class='button bg-red fs-16 mg-5 CANCEL_TRADE'
|
<li><button class='button bg-red fs-16 mg-5 CANCEL_TRADE'
|
||||||
id='${trade_id}-${flo_id}-${order_type}'> Cancel Trade</button></li>
|
id='${trade_id}-${flo_id}-${order_type}'> Cancel Order</button></li>
|
||||||
<li><hr></li>
|
<li><hr></li>
|
||||||
`;
|
`;
|
||||||
});
|
});
|
||||||
@ -29737,6 +29714,15 @@ x
|
|||||||
|
|
||||||
<!-- Balances Functions-->
|
<!-- Balances Functions-->
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
|
const displayLivePrices = async()=>{
|
||||||
|
try {
|
||||||
|
//
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const displayBalances = flo_id => {
|
const displayBalances = flo_id => {
|
||||||
if (typeof flo_id !== "string") return;
|
if (typeof flo_id !== "string") return;
|
||||||
showMessage(`Loading your balances.`);
|
showMessage(`Loading your balances.`);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user