Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const redis = require('../redis'),
- constants = require('../constants'),
- satoshi = constants.satoshi,
- contract = constants.contract,
- minimalWithdraw = constants.minimalWithdraw,
- maxWithdrawNoKyc = constants.maxWithdrawNoKyc,
- bitcoinIP = require('../constants').bitcoinIP,
- bitcoinPort = require('../constants').bitcoinPort,
- bitcoinUser = require('../constants').bitcoinUser,
- bitcoinPassword = require('../constants').bitcoinPassword,
- { spawn } = require('child_process'),
- ObjectID = require('mongodb').ObjectID,
- mongo = require('../mongo');
- const withdraw = function withdraw(id, cluster, json, ip, callback){
- json['ad'] = String(json['ad']);
- json['q'] = parseFloat(String(json['q']));
- if(checkBTC(json['ad']) == false){
- callback(JSON.stringify({error : true, message : "A valid address must be provided.", my_id : json['my_id']}));
- return;
- }
- if(isNaN(json['q']) || json['q'] <= 0){
- callback(JSON.stringify({error : true, message : "Quantity must be a number", my_id : json['my_id']}));
- return;
- }
- if(json['q'] < minimalWithdraw){
- callback(JSON.stringify({error : true, message : "Quantity must be a number greater or equal to "+minimalWithdraw+" BTC", my_id : json['my_id']}));
- return;
- }
- redis[cluster].hmget(id, 'free', 'locked', function(err, result){
- if(err){
- callback(JSON.stringify({error : true, message : "The operation failed, please try again.", my_id : json['my_id']}));
- return;
- }
- let free = Number(result[0]), locked = Number(result[1]);
- free -= json['q'] * satoshi;
- if(free < 0){
- callback(JSON.stringify({error : true, message : "Not enough funds.", my_id : json['my_id']}));
- return;
- }
- if(free < locked){
- callback(JSON.stringify({error : true, message : "You must keep funds to reduce your positions.", my_id : json['my_id']}));
- return;
- }
- withdrawBTC(id, cluster, json, ip, callback, result, free, locked, result['kyc'], cpt_withdraw);
- });
- }
- function withdrawBTC(id, cluster, json, ip, callback, result, free, locked, kyc, cpt_withdraw){
- try{
- const address = json['ad'], qte = json['q'], now = Date.now()
- const mult = [
- ['hincrby', id, 'free', qte * satoshi * -1],
- ['hincrby', id, 'cpt_withdraw', qte * satoshi],
- ['lpush', id+'l', JSON.stringify({d:now, l:'Withdraw of '+Number(qte).toFixed(8)+' BTC requested', ip:ip})]
- ];
- if(now - json['t'] > 2000){
- callback(JSON.stringify({error : true, message : "The operation failed, please try again.", my_id : json['my_id']}));
- return;
- }
- redis[cluster].multi(mult).exec(function(err, result){
- if(err){
- callback(JSON.stringify({error : true, message : "The operation failed, please try again.", my_id : json['my_id']}));
- return;
- }
- mongo.insert(cluster, {id:id, d:now, l:'Withdraw of '+Number(qte).toFixed(8)+' BTC to '+address+' requested', ip:ip}, 'logs');
- const sendtoaddress = spawn('bitcoin-cli', ['-rpcconnect='+bitcoinIP, '-rpcport='+bitcoinPort, '-rpcuser='+bitcoinUser, '-rpcpassword='+bitcoinPassword, 'sendtoaddress', address, qte, "", id, true]);
- let result_command = "";
- sendtoaddress.stdout.on('data', (data) => {result_command+=data;});
- sendtoaddress.stderr.on('data', (data) => {});
- sendtoaddress.on('error', (code) => {});
- sendtoaddress.on('close', (code) => {
- if(code != 0){
- callback(JSON.stringify({error : true, message : "Please contact the support.", my_id : json['my_id'], free:(free / satoshi).toFixed(8)}));
- mongo.insert(cluster, {id:id, d:now, l:'Withdraw of '+Number(qte).toFixed(8)+' BTC to '+address+' failed', ip:ip}, 'logs');
- redis[cluster].lpush(id+'l', JSON.stringify({d:now, l:'Withdraw of '+Number(qte).toFixed(8)+' BTC failed', ip:ip}));
- }else{
- result_command = result_command.substr(0, result_command.length - 1);
- const bal = [now, "BTC Withdraw to "+address, qte * -1];
- const mult = [
- ['lpush', id+'b', JSON.stringify(bal)],
- ['lpush', id+'l', JSON.stringify({d:now, l:'Withdraw of '+Number(qte).toFixed(8)+' BTC confirmed', ip:ip})]
- ];
- redis[cluster].multi(mult).exec(function(err, result){
- if(!err){
- mongo.insert(cluster, {id:id, d:now, l:'Withdraw of '+Number(qte).toFixed(8)+' BTC to '+address+' confirmed', ip:ip}, 'logs');
- mongo.insert(cluster, {id:id, d:now, l:'BTC Withdraw to '+address, q:bal[2], txid:result_command}, 'balance');
- callback(JSON.stringify({error: false, message : "Withdraw confirmed txid:"+result_command, txid:result_command, my_id : json['my_id'], free:(free / satoshi).toFixed(8)}));
- }else{
- mongo.insert(cluster, {id:id, d:now, l:'Withdraw of '+Number(qte).toFixed(8)+' BTC to '+address+' failed', ip:ip}, 'logs');
- redis[cluster].lpush(id+'l', JSON.stringify({d:now, l:'Withdraw of '+Number(qte).toFixed(8)+' BTC failed', ip:ip}));
- callback(JSON.stringify({error: true, message : "Please contact the support.", my_id : json['my_id'], free:(free / satoshi).toFixed(8)}));
- }
- });
- }
- });
- });
- }catch(e){
- console.log(e);
- callback(JSON.stringify({error : true, message : "The operation failed, please try again.", my_id : json['my_id']}));
- }
- }
- function isChecksumAddress (address) {
- address = address.replace('0x','');
- var addressHash = sha3(address.toLowerCase());
- for (var i = 0; i < 40; i++ ) {
- if ((parseInt(addressHash[i], 16) > 7 && address[i].toUpperCase() !== address[i]) || (parseInt(addressHash[i], 16) <= 7 && address[i].toLowerCase() !== address[i])) {
- return false;
- }
- }
- return true;
- };
- function checkBTC(address) {
- var decoded = base58_decode(address);
- if (decoded.length != 25) return false;
- var cksum = decoded.substr(decoded.length - 4);
- var rest = decoded.substr(0, decoded.length - 4);
- const firsthex2a = hex2a(crypto.createHash('sha256').update(rest, 'ascii').digest('hex'));
- var good_cksum = hex2a(crypto.createHash('sha256').update(firsthex2a, 'ascii').digest('hex')).substr(0, 4);
- if (cksum != good_cksum) return false;
- return true;
- }
- var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
- var ALPHABET_MAP = {}
- for(var i = 0; i < ALPHABET.length; i++) {
- ALPHABET_MAP[ALPHABET.charAt(i)] = i
- }
- var BASE = 58
- function base58_decode(string) {
- if (string.length === 0) return []
- var i, j, bytes = [0]
- for (i = 0; i < string.length; i++) {
- var c = string[i]
- if (!(c in ALPHABET_MAP)) return "";
- for (j = 0; j < bytes.length; j++) bytes[j] *= BASE
- bytes[0] += ALPHABET_MAP[c]
- var carry = 0
- for (j = 0; j < bytes.length; ++j) {
- bytes[j] += carry
- carry = bytes[j] >> 8
- bytes[j] &= 0xff
- }
- while (carry) {
- bytes.push(carry & 0xff)
- carry >>= 8
- }
- }
- // deal with leading zeros
- for (i = 0; string[i] === '1' && i < string.length - 1; i++) bytes.push(0)
- bytes = bytes.reverse()
- output = '';
- for (i=0; i<bytes.length; i++) {
- output += String.fromCharCode(bytes[i]);
- }
- return output;
- }
- function hex2a(hex) {
- var str = '';
- for (var i = 0; i < hex.length; i += 2)
- str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
- return str;
- }
- function a2hex(str) {
- var aHex = "0123456789abcdef";
- var l = str.length;
- var nBuf;
- var strBuf;
- var strOut = "";
- for (var i = 0; i < l; i++) {
- nBuf = str.charCodeAt(i);
- strBuf = aHex[Math.floor(nBuf/16)];
- strBuf += aHex[nBuf % 16];
- strOut += strBuf;
- }
- return strOut;
- }
- function pow(big, exp) {
- if (exp == 0) return int2bigInt(1, 1, 0);
- var i;
- var newbig = big;
- for (i = 1; i < exp; i++) {
- newbig = mult(newbig, big);
- }
- return newbig;
- }
- function repeat(s, n){
- var a = [];
- while(a.length < n){
- a.push(s);
- }
- return a.join('');
- }
- module.exports.withdraw = withdraw;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement