Пример #1
0
	callRpcMethod : function(method, params, callback) {
		this.writeLogEntry("Calling JSON-RPC method " + method + " with params " + JSON.stringify(params));

		var that = this;
		var client = new rpc.Client(this.rpcClientOptions);
		client.call(
			{"jsonrpc" : "2.0", "method" : method, "params" : params, "id" : 0},
			function (err, res) {
				if (err) {
					that.writeLogEntry("[" + method + "] Error: " + JSON.stringify(err));
					if (callback) callback(res);
					return;
				} else if (res.error) {
					that.writeLogEntry("[" + method + "] Error: " + JSON.stringify(res.error));
					if (callback) callback(res);
					return;
				}
				
				that.writeLogEntry("Called JSON-RPC method '" + method + "' with response: " + JSON.stringify(res.result));
				if (callback) {
					that.writeLogEntry('callback...');
					callback(err, res);
				}
			}
		);
	},
Пример #2
0
  socket.on('importprivkey', function(add) {
    rpc.call({'jsonrpc':'2.0', 'method':'importprivkey', 'params':[add.key, add.account], 'id':0}, function(error, response) {
      if (error || !response) {
        console.log('RPC importprivkey ERROR\n\n', error);
        return;
      }

      if (response.hasOwnProperty('error')) {
        /**
         * error_code_wallet_error = -4
         */
        if (response.error.code === -4) {
          socket.emit('alerts', "The private key you're trying to import is already in your wallet.");
        }

        /**
         * error_code_invalid_address_or_key = -5
         */
        if (response.error.code === -5) {
          socket.emit('alerts', "The private key you're trying to import is invalid.");
        }
      } else {
        socket.emit('alerts', 'Private key successfully imported.');
        socket.emit('importprivkey_response', true);
        rpc_listreceivedby();
      }
    });
  });
Пример #3
0
  socket.on('dumpprivkey', function(key) {
    rpc.call({'jsonrpc':'2.0', 'method':'dumpprivkey', 'params':[key], 'id':0}, function(error, response) {
      if (error || !response) {
        console.log('RPC dumpprivkey ERROR\n\n', error);
        return;
      }

      if (response.hasOwnProperty('error')) {
        /**
         * error_code_wallet_error = -4
         */
        if (response.error.code === -4) {
            socket.emit('alerts', "The address you've entered does not belong to this wallet.");
        }

        /**
         * error_code_invalid_address_or_key = -5
         */
        if (response.error.code === -5) {
            socket.emit('alerts', "The address you've entered is invalid.");
        }
      } else {
        socket.emit('alerts', 'Private key: ' + response.result);
      }
    });
  });
Пример #4
0
  socket.on('walletpassphrasechange', function(passphrase) {
    rpc.call({'jsonrpc':'2.0', 'method':'walletpassphrasechange', 'params':[passphrase.old, passphrase.new], 'id':0}, function(error, response) {
      if (error || !response) {
        console.log('RPC walletpassphrasechange ERROR\n\n', error);
        return;
      }

      if (response.hasOwnProperty('error')) {
        /**
         * error_code_wallet_passphrase_incorrect = -14
         */
        if (response.error.code === -14) {
          socket.emit('alerts', "You've entered an incorrect current passphrase.");
        }

        /**
         * error_code_wallet_wrong_enc_state = -15
         */
        if (response.error.code === -15) {
          socket.emit('alerts', 'Wallet is not encrypted.');
        }
      } else {
        socket.emit('alerts', 'Passphrase successfuly changed.');
      }
    });
  });
Пример #5
0
  socket.on('getnewaddress', function(account) {
    rpc.call({'jsonrpc':'2.0', 'method':'getnewaddress', 'params':[account], 'id':0}, function(error, response) {
      if (error || !response) {
        console.log('RPC getnewaddress ERROR\n\n', error);
        return;
      }

      rpc_listreceivedby();
    });
  });
Пример #6
0
  socket.on('dumpwallet', function() {
    rpc.call({'jsonrpc':'2.0', 'method':'dumpwallet', 'params':[], 'id':0}, function(error, response) {
      if (error || !response) {
        console.log('RPC dumpwallet ERROR\n\n', error);
        return;
      }

      socket.emit('alerts', 'Wallet.csv successfuly dumped in your .Vanillacoin/data directory.');
    });
  });
Пример #7
0
  /**
   * RPC method 'gettransaction'
   */
  function rpc_gettransaction(txid, callback) {
    rpc.call({'jsonrpc':'2.0', 'method':'gettransaction', 'params':[txid], 'id':0}, function(error, response) {
      if (error || !response) {
        console.log('RPC gettransaction ERROR\n\n', error);
        return;
      }

      callback(response.result, txid);
    });
  }
Пример #8
0
  socket.on('walletlock', function() {
    rpc.call({'jsonrpc':'2.0', 'method':'walletlock', 'params':[], 'id':0}, function(error, response) {
      if (error || !response) {
        console.log('RPC walletlock ERROR\n\n', error);
        return;
      }

      rpc_walletpassphrase();
    });
  });
Пример #9
0
  /**
   * RPC method 'walletpassphrase'
   */
  function rpc_walletpassphrase() {
    rpc.call({'jsonrpc':'2.0', 'method':'walletpassphrase', 'params':[], 'id':0}, function(error, response) {
      if (error || !response) {
        console.log('RPC walletpassphrase (state check) ERROR\n\n', error);
        return;
      }

      socket.emit('wallet_status', response.error);
    });
  }
Пример #10
0
  socket.on('encryptwallet', function(key) {
    rpc.call({'jsonrpc':'2.0', 'method':'encryptwallet', 'params':[key], 'id':0}, function(error, response) {
      if (error || !response) {
        console.log('RPC encryptwallet ERROR\n\n', error);
        return;
      }

      socket.emit('alerts', 'Wallet successfuly encrypted. Restart your main Vanilla wallet.');
    });
  });
Пример #11
0
  socket.on('repairwallet', function() {
    rpc.call({'jsonrpc':'2.0', 'method':'repairwallet', 'params':[], 'id':0}, function(error, response) {
      if (error || !response) {
        console.log('RPC repairwallet ERROR\n\n', error);
        return;
      }

      socket.emit('repairwallet_response', response.result);
    });
  });
Пример #12
0
 connect: function(zabbixUrl, username, password, callback) {
   client = new rpc.Client(urlToOptions(zabbixUrl));
   client.call(
     {
       jsonrpc: "2.0",
       method: "user.login",
       params: {
         user: username,
         password: password
       },
       id: 1
     },
     function(err, res) {
       if (err) {
         console.log(err);
       } else {
         auth_token = res.result;
         callback();
       }
     }
   );
 },
Пример #13
0
  /**
   * RPC method 'validateaddress'
   */
  function rpc_validateaddress(address, callback, passalong) {
    rpc.call({'jsonrpc':'2.0', 'method':'validateaddress', 'params':[address], 'id':0}, function(error, response) {
      if (error || !response) {
        console.log('RPC validateaddress ERROR\n\n', error);
        return;
      }

      if (passalong) {
        callback(response.result, passalong);
      } else {
        callback(response.result);
      }
    });
  }
Пример #14
0
  socket.on('walletpassphrase', function(passphrase) {
    rpc.call({'jsonrpc':'2.0', 'method':'walletpassphrase', 'params':[passphrase], 'id':0}, function(error, response) {
      if (error || !response) {
        console.log('RPC walletpassphrase (unlock) ERROR\n\n', error);
        return;
      }

      if (response.hasOwnProperty('result')) {
        rpc_walletpassphrase();
      } else {
        socket.emit('alerts', "The passphrase you've entered is incorrect.");                
      }
    });
  });
Пример #15
0
                          socket.on('validateaddress', function(params) {
                              var address = params[0];
                              var amount = params[1];

                              rpc.call({"jsonrpc": "2.0", "method": "validateaddress", "params": [address], "id": 0}, function(err, res) {
                                  if (err) { console.log(err); }

                                  if (res.result['isvalid']) {
                                      socket.emit('transfer_possible', [address, amount]);
                                  } else {
                                      socket.emit('alerts', "The address you're trying to send to is not valid.");
                                  }
                              });
                          });
Пример #16
0
  socket.on('chainblender', function(action) {
    rpc.call({'jsonrpc':'2.0', 'method':'chainblender', 'params':[action], 'id':0}, function(error, response) {
      if (error || !response) {
        console.log('RPC chainblender ERROR\n\n', error);
        return;
      }

      if (!response.error) {
        if (action === 'start') {
          socket.emit('alerts', 'Chainblender started.');
        } else {
          socket.emit('alerts', 'Chainblender stopped.')
        }
      } else {
        socket.emit('alerts', 'You have to unlock the wallet before using Chainblender.');
      }
    });
  });
Пример #17
0
  socket.on('backupwallet', function() {
    rpc.call({'jsonrpc':'2.0', 'method':'backupwallet', 'params':[""], 'id':0}, function(error, response) {
      if (error || !response) {
        console.log('RPC backupwallet ERROR\n\n', error);
        return;
      }

      if (response.hasOwnProperty('error')) {
        /**
         * error_code_wallet_error = -4
         */
        if (response.error.code === -4) {
          socket.emit('alerts', 'Backup failed.');
        }
      } else {
        socket.emit('alerts', 'Wallet successfuly backed up inside of your Vanilla executable directory.');
      }
    });
  });
Пример #18
0
                          socket.on('sendtoaddress', function(params) {
                              var address = params[0];
                              var amount = params[1];

                              rpc.call({"jsonrpc": "2.0", "method": "sendtoaddress", "params": [address, amount], "id": 0}, function(err, res) {
                                  if (err) { console.log(err); }

                                  if (res.error) {
                                      /**
                                       * error_code_type_error = -3, NaN or negative
                                       */
                                      if (res.error['code'] == -3) {
                                          socket.emit('alerts', "You're trying to send an invalid amount (" + amount + ").");
                                      }
                                      /**
                                       * error_code_wallet_error = -4, not enough funds
                                       */
                                      else if (res.error['code'] == -4) {
                                          socket.emit('alerts', "You have insufficient funds.");
                                      }
                                      /**
                                       * error_code_wallet_unlock_needed = -13, wallet locked
                                       */
                                      else if (res.error['code'] == -13) {
                                          socket.emit('alerts', 'Wallet is locked. Unlock it if you want to transfer funds.');
                                      }
                                      /**
                                       * error_code_amount_too_small = -101, amount too small
                                       */
                                      else if (res.error['code'] == -101) {
                                          socket.emit('alerts', 'Amount too small.');
                                      } else {
                                          console.log(res.error);
                                      }
                                  } else {
                                      socket.emit('alerts', "Sent " + amount + " XVC to " + address + " (txid: " + res.result + ").");

                                      rpc_listsinceblock();
                                  }
                              }); 
                          });
Пример #19
0
    (function update() {
      rpc.call([
          {'jsonrpc':'2.0', 'method':'getinfo', 'params':[], 'id':0},
          {'jsonrpc':'2.0', 'method':'getincentiveinfo', 'params':[], 'id':0},
          {'jsonrpc':'2.0', 'method':'chainblender', 'params':['info'], 'id':0}
        ], function(error, response) {

        if (error || !response) {
          console.log('RPC getinfo && getincentiveinfo ERROR\n\n', error);
          return;
        }

        cache.wallet_info = Object.assign({}, response[1].result, response[2].result);
        cache.wallet_info = Object.assign({}, cache.wallet_info, response[0].result);

        cache.wallet_info.version = cache.wallet_info.version.replace(':', ' ');
        socket.emit('wallet_info', cache.wallet_info);
      });

      setTimeout(update, 10000);
    })();
Пример #20
0
var rpc = require('node-json-rpc');

var options = {  
  port: 8000,  
  host: '127.0.0.1',  
  path: '/',  
  strict: false
};
 
var client = new rpc.Client(options);

client.call(
  {"jsonrpc": "2.0", "method": "receivedMessageCount", "params": [], "id": 0},
  function (err, res) {
    // Did it all work ? 
    if (err) { console.log(err); }
    else { console.log(res); }
  }
);

client.call(
  {"jsonrpc": "2.0", "method": "getMessageByNumber", "params": [10], "id": 0},
  function (err, res) {
    // Did it all work ? 
    if (err) { console.log(err); }
    else { console.log(res); }
  }
);

client.call(
  {"jsonrpc": "2.0", "method": "decryptMessageByNumber", "params": [10], "id": 0},
Пример #21
0
    (function update() {
      rpc.call([
          {'jsonrpc':'2.0', 'method':'getpeerinfo', 'params':[], 'id':0},
          {'jsonrpc':'2.0', 'method':'getnetworkinfo', 'params':[], 'id':0}
        ], function(error, response) {

        if (error || !response) {
          console.log('RPC getpeerinfo && getnetworkinfo ERROR\n\n', error);
          return;
        }

        var endpoints = [];
        var save = false;

        cache.wallet_info.udp_connections = response[1].result.udp.connections;
        socket.emit('wallet_info', cache.wallet_info);

        var connected_nodes = response[0].result.filter(function(peer) {
          return parseInt(peer.lastsend) !== 0;
        });

        connected_nodes.forEach(function(peer) {
          peer.group = 'Connected nodes';
          peer.subver_clean = peer.subver.replace('/', '').replace('/', '').replace(':',' ');

          /**
           * Convert to miliseconds
           */
          peer.lastsend *= 1000;
          peer.lastrecv *= 1000;
          peer.conntime *= 1000;

          /**
           * Check if there's geodata on IP, else request it
           */
          var ip = peer.addr.split(':')[0];

          if (cache.nodes.geodata[ip]) {
            peer.lon = cache.nodes.geodata[ip].lon;
            peer.lat = cache.nodes.geodata[ip].lat;
            peer.country = cache.nodes.geodata[ip].country;
          } else {
            https_getlocation(ip);
            save = true;
          }
        });

        response[1].result.endpoints.forEach(function(endpoint) {
          var ip = endpoint.split(':')[0];

          if (cache.nodes.geodata[ip]) {
            endpoints.push({
              'addr':endpoint,
              'group':'Network endpoints',
              'lon':cache.nodes.geodata[ip].lon,
              'lat':cache.nodes.geodata[ip].lat,
              'country':cache.nodes.geodata[ip].country
            });
          } else {
            https_getlocation(ip);
            save = true;
          }
        });

        cache.nodes.connected = connected_nodes;
        cache.nodes.endpoints = endpoints;

        socket.emit('nodes_geomap', cache.nodes.endpoints.concat(cache.nodes.connected));
        socket.emit('nodes_connected', cache.nodes.connected);

        if (save) {
          setTimeout(function() {
            fs.writeFile('data/nodes_geodata.json', JSON.stringify(cache.nodes.geodata), function(error) {
              if (error) {
                console.log('FS.WRITE data/nodes_geodata.json ERROR\n\n', error);
                return;
              }
            });
          }, 1000);
        }
      });

      setTimeout(update, 60000);
    })();
Пример #22
0
// https://www.npmjs.com/package/node-json-rpc
// npm install node-json-rpc
var rpc = require('node-json-rpc');

var client = new rpc.Client({
      port: 8080,
      host: '127.0.0.1',
      path: '/',
});

client.call({
    "jsonrpc": "2.0",
    "method": "add",
    "params": {
      a: 1,
      b: 3
    },
    "id": 0
  },
  function(err, res) {
    if (err) {
      console.log("Error add");
      console.log(err);
    } else {
      console.log("Success add");
      console.log(res);
    }
  }
);
Пример #23
0
  /**
   * Update transactions
   */
  function rpc_listsinceblock() {
    rpc.call({'jsonrpc':'2.0', 'method':'listsinceblock', 'params':[], 'id':0}, function(error, response) {
      if (error || !response) {
        console.log('RPC listsinceblock ERROR\n\n', error);
        return;
      }

      var length_before = Object.keys(cache.transactions.edits).length;
      cache.transactions.log = [];

      response.result.transactions.forEach(function(tx) {
        if (!tx.account) {
          tx.account = 'Default';
        }

        /**
         * Convert to miliseconds
         */
        tx.timereceived *= 1000;
        tx.blocktime *= 1000;
        tx.time *= 1000;

        /**
         * Check if txid exists in transactions_edits
         */
        if (cache.transactions.edits[tx.txid]) {
          if (tx.category === 'generate' || tx.category === 'immature') {
            if (cache.transactions.edits[tx.txid].pos) {
              tx.pos = true;
              tx.address = cache.transactions.edits[tx.txid].address;
              tx.amount = cache.transactions.edits[tx.txid].amount;

              if (tx.category === 'generate') {
                tx.category = 'PoS reward';
              }
            } else {
              tx.pos = false;

              if (tx.category === 'generate') {
                tx.category = 'Incentive reward';
              }
            }
          }

          if (tx.category === 'receive') {
            if (cache.transactions.edits[tx.txid]['self-send']) {
              tx.category = 'Self-send';
            }
          }

          /**
           * Exclude sends to self, push everything else
           */
          if (!(tx.category === 'send' && cache.transactions.edits[tx.txid]['self-send']) && tx.confirmations != -1) {
            cache.transactions.log.push(tx);
          }
        } else {
          if (tx.category === 'generate' || tx.category === 'immature') {
            rpc_gettransaction(tx.txid, function(response, txid) {
              if (response.vout[0].scriptPubKey.type === 'nonstandard') {
                if (response.amount < 0) {
                  response.amount = response.amount + response.details[0].amount;
                }

                cache.transactions.edits[txid] = {
                  'pos':true,
                  'address':response.vout[1].scriptPubKey.addresses[0],
                  'amount':response.amount
                };
              } else {
                cache.transactions.edits[txid] = {
                  'pos':false
                };
              }
            });
          }

          var is_mine = false;

          if (tx.category === 'send' && tx.blended) {
            tx.category = 'Blended';
            tx.amount = tx.amount + tx.fee;
          }

          if (tx.category === 'send' && !tx.blended && Math.abs(tx.fee.toFixed(0)) === Math.abs(tx.amount.toFixed(0))) {
            tx.category = 'Received';
            tx.amount = tx.amount + tx.fee;
          } else if (tx.category === 'send' && !tx.blended) {
            rpc_validateaddress(tx.address, function(address, txid) {
              if (address.ismine) {
                is_mine = true;

                cache.transactions.edits[txid] = {
                  'self-send':true
                };
              }
            }, tx.txid);
          }

          if (!is_mine && tx.confirmations != -1) {
            cache.transactions.log.push(tx);
          }
        }

        if (tx.category === 'receive') {
          if (tx.confirmations === 0) {
            tx.category = 'Receiving';
          } else {
            tx.category = 'Received';
          }
        }

        if (tx.category === 'send') {
          if (tx.confirmations === 0) {
            tx.category = 'Sending';
          } else {
            tx.category = 'Sent';
          }
        }

        if (tx.category === 'immature') {
          tx.category = 'Immature';
        }
      });

      setTimeout(function() {
        if (Object.keys(cache.transactions.edits).length > length_before) {
          fs.writeFile('data/transactions_edits.json', JSON.stringify(cache.transactions.edits), function(error) {
            if (error) {
              console.log('FS.WRITE data/transactions_edits.json ERROR\n\n', error);
              return;
            }
          });
        }
      }, 1000);

      socket.emit('transactions', cache.transactions.log);
    });
  }
Пример #24
0
  /**
   * Update account information
   */
  function rpc_listreceivedby() {
    rpc.call([
        {'jsonrpc':'2.0', 'method':'listreceivedbyaddress', 'params':{'minconf':1, 'includeempty':true}, 'id':0},
        {'jsonrpc':'2.0', 'method':'listreceivedbyaccount', 'params':{'minconf':1, 'includeempty':true}, 'id':0}
      ], function(error, response) {

      if (error || !response) {
        console.log('RPC listreceivedbyaddress && listreceivedbyaccount ERROR\n\n', error);
        return;
      }

      var accounts = {};

      /**
       * Set accounts
       */
      response[1].result.forEach(function(byaccount) {
        if (!byaccount.account) {
          byaccount.account = 'Default';
        }

        var hash = crypto.createHash('md5').update(byaccount.account).digest('hex');
        var hidden = true;

        /**
         * If hash is found in settings, set visibility accordingly
         */
        if (cache.settings.hidden.hasOwnProperty(hash)) {
          hidden = cache.settings.hidden[hash];
        }

        /**
         * If there's no settings entry, display Default account addresses
         */
        if (!cache.settings.hidden.hasOwnProperty(hash) && byaccount.account === 'Default') {
          hidden = false;
        }

        accounts[hash] = {
          'account':byaccount.account,
          'hidden':hidden,
          'received':byaccount.amount,
          'addresses':[]
        };

        cache.settings.hidden[hash] = hidden;
      });

      /**
       * Push addresses to accounts
       */
      response[0].result.forEach(function(byaddress) {
        if (!byaddress.account) {
          byaddress.account = 'Default';
        }

        var hash = crypto.createHash('md5').update(byaddress.account).digest('hex');

        accounts[hash].addresses.push({
          'address':byaddress.address,
          'received':byaddress.amount,
          'confirmations':byaddress.confirmations
        });
      });

      cache.accounts = accounts;
      socket.emit('accounts', cache.accounts);
    });
  }