Skip to content

Commit

Permalink
new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
onsightit committed Jul 23, 2017
1 parent bc362ab commit 67a2f50
Show file tree
Hide file tree
Showing 19 changed files with 298 additions and 328 deletions.
59 changes: 22 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# Web-Wallet

Web-Wallet for altcoin daemons.
A Web-Wallet for altcoin daemons.


## Prerequisites:

A running RPC coin daemon. See: https://github.com/YourApp/YourCoin
A running RPC coin daemon. See: https://github.com/Web-Wallet/YourCoin

Mongo DB for storing account info. See: https://www.mongodb.com/

Create DB and user:
> use database-name
> db.createUser( { user: "{user}", pwd: "{password}", roles: [ { role: "readWrite" } ] } )
Node.js 6.x for running YourApp. For debian installations:
Node.js 6.x for running Web-Wallet. For debian installations:

If running 4.x:
> sudo apt-get purge nodejs npm
Expand All @@ -21,7 +22,7 @@ Node.js 6.x for running YourApp. For debian installations:
> curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
> sudo apt-get install -y nodejs
If YourApp is not running locally, https is the default protocol. To set up a self-signed SSL certificate in debian/apache2 environments, run:
If Web-Wallet is not running locally, https is the default protocol. To set up a self-signed SSL certificate in debian/apache2 environments, run:

> sudo mkdir /etc/apache2/certs
> sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/certs/{domain name}.key -out /etc/apache2/certs/{domain name}.crt
Expand All @@ -31,11 +32,11 @@ If YourApp is not running locally, https is the default protocol. To set up a s

## Configuring:

Configure the database connection parameters, localizations and features of YourApp by copying 'settings.json.template' to 'settings.json' and making your changes in 'settings.json'.
Configure the database connection parameters, localizations and features of Web-Wallet by copying 'settings.json.template' to 'settings.json' and making your changes in 'settings.json'.

Configure YourApp and daemon for local or non-local operation, or a combination of both. For instance:
Configure Web-Wallet and daemon for local or non-local operation, or a combination of both. For instance:

If the coin daemon is running on the same machine as YourApp, the daemon's config file will be used. It can be found at:
If the coin daemon is running on the same machine as Web-Wallet, the daemon's config file will be used. It can be found at:

// Mac OS
'$HOME/Library/Application Support/YourCoin/yourcoin.conf'
Expand All @@ -48,55 +49,39 @@ If the coin daemon is running on the same machine as YourApp, the daemon's confi

(Substitute your coin's name for 'YourCoin' above.)

If the daemon is running on another machine, you will need to configure node.js's coin.conf file to match the daemon's config file. coin.conf is found in: lib/coin.conf

Either way, the config file will need at a minimum the following parameters:
You will need to configure wallet params in settings.conf to match the wallet daemon's config file. The wallet daemon's config file will need the following parameters:

> rpcuser=rpcuser
> rpcuser=rpcuser # Change me.
> rpcpassword=password # Change me!
> rpcconnect=localhost # RPC daemon
> rpcport=18181
> server=1 # If not running a daemon
Local vs Not-Local configuration:

The config file parameter 'rpcconnect' determines whether the daemon is local or not-local, even if the daemon and YourApp are both running on the same machine. YourApp's boolean flag 'isLocal' is determined to be true if 'rpcconnect' is one of the following:

> rpcconnect=127.0.0.1
> rpcconnect=localhost
> rpcconnect=192.168.x.x
> rpcconnect=hostname_with_no_tld
The last two examples allow for YourApp to be considered 'local', even though the node and daemon may be running on different machines on the same local network.

If the daemon and YourApp are both running on the same machine, you can still define YourApp as NOT-local by setting the 'rpcconnect' parameter to a fully qualified domain name (i.e. myhost.homelan.net), which requirs a simple modification to the machine's hosts file. (e.g. 192.168.1.246 myhost.homelan.net)

If 'isLocal' is true, more control over YourApp is allowed. (i.e. encrypting the wallet, locking/unlocking the wallet for sending/staking, and more wallet stats and features are available.)

[See 'rpcconnect' in the coin's source code, init.cpp, for more information.]
> rpcport=19184
> server=1
> listen=1
> daemon=1
> staking=1
> rpcallowip=<your IP address>
If you need to run YourApp app from a "sub directory" of the main web-site (e.g. https://example.com/wallet/), change the settings.json parameter, chRoot to: "".
If you need to run Web-Wallet app from a "sub directory" of the main web-site (e.g. https://example.com/web-wallet/), change the settings.json parameter, chRoot to: "".


## Running:

Windows:

> YourApp.bat
> Web-Wallet.bat
(If supervisor is not installed, run 'npm install supervisor'.)

Linux:

> YourApp.sh
> Web-Wallet.sh
(If 'daemon' is not installed, please consult your Linux distro's documentation for installing 'daemon'.)

YourApp has an admin account pre-defined which you can login with:
Web-Wallet has an admin account pre-defined which you can login with:

> Login: MASTER_ACCOUNT
> Password: password (you will be required to change this)
The MASTER_ACOUNT always sees YourApp as 'local' and has views into the wallet as if you were running a Qt wallet (i.e. the full wallet balance).
The MASTER_ACOUNT always sees Web-Wallet as 'local' and has views into the wallet as if you were running a Qt wallet (i.e. the full wallet balance).

To setup individual accounts, use YourApp's Signup page, or login with a social media account.
To setup individual accounts, use Web-Wallet's Signup page, or login with a social media account.
4 changes: 0 additions & 4 deletions coin.conf.template

This file was deleted.

8 changes: 5 additions & 3 deletions lib/coin-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var api = require('./commands'),
function Client(options) {
this.opts = {
host: 'localhost',
port: 18181,
port: 19184,
method: 'POST',
user: 'rpcuser',
pass: 'rpcpass',
Expand All @@ -16,7 +16,8 @@ function Client(options) {
'Authorization': ''
},
https: false,
rejectUnauthorized: true,
strictSSL: false,
rejectUnauthorized: false,
ca: null,
passphrasecallback: null,
account: ''
Expand Down Expand Up @@ -75,7 +76,7 @@ Client.prototype = {
data = JSON.parse(data);
} catch(exception) {
var errMsg = (res.statusCode !== 200 ? 'Invalid params ' + res.statusCode : 'Failed to parse JSON');
// statusCode 403 is rpc auth failed. check local coin.conf params.
// statusCode 403 is rpc auth failed. Check local wallet params in settings.conf.
errMsg += ' : '+JSON.stringify(data);
return fn(new Error(errMsg));
}
Expand Down Expand Up @@ -162,6 +163,7 @@ Client.prototype = {
} else if (key === 'passphrasecallback' ||
key === 'account' ||
key === 'https' ||
key === 'strictSSL' ||
key === 'rejectUnauthorized' ||
key === 'ca') {
opts[key] = v;
Expand Down
8 changes: 0 additions & 8 deletions lib/coin.conf

This file was deleted.

108 changes: 13 additions & 95 deletions lib/coinapi.js
Original file line number Diff line number Diff line change
@@ -1,104 +1,22 @@
fs = require('fs-extra');

// Get the user defined application settings.
settings = require('./settings');

// You will need to configure the config file found in: coin.conf
// Otherwise, if the daemon is running on the same machine as the node, its config file will be used.
// Get the user defined application / database / wallet configurable parameters from settings.json.
// See README.md for more information.
filepath = 'coin.conf';
console.log("Reading " + filepath + " parameters...");
if (fs.existsSync(filepath) === false){
console.log("WARNING: " + filepath + " does not exists. Using the coin's config file...");
if (process.platform == 'darwin'){
// Mac OS
filepath = process.env.HOME + '/Library/Application Support/' + settings.coinName + '/' + settings.coinName + '.conf';
} else if (process.platform == 'linux'){
// Linux
filepath = process.env.HOME + '/.' + settings.coinName.toLowerCase() + '/' + settings.coinName.toLowerCase() + '.conf';
} else {
// Windows
filepath = process.env.APPDATA + '/' + settings.coinName + '/' + settings.coinName + '.conf';
}
if (fs.existsSync(filepath) === false){
console.log("ERROR: " + filepath + " does not exists. Exiting.");
process.exit(3);
}
}

conf_data = fs.readFileSync(filepath, 'utf8', function (err) {
if (err) {
return console.log(err);
}
});

function wordTrim(str){
str.trim();
var idx = str.search(/\s/); // look for whitespace after first word (ie. comments)
if (idx !== -1){
str = str.substring(0, idx);
}
return str;
}

arrayFromConf = conf_data.match(/[^\r\n]+/g); // Turn lines into array

var rpcUser = "";
var rpcPass = "";
var rpcHost = "";
var rpcPort = "";

for (var k in arrayFromConf){
if (arrayFromConf.hasOwnProperty(k)){
// Get specific parm and value before and after '='
var p = wordTrim(arrayFromConf[k].substring(0, arrayFromConf[k].indexOf("=")));
var v = wordTrim(arrayFromConf[k].substring(arrayFromConf[k].indexOf("=") + 1));
switch(p){
case ("rpcuser"):
rpcUser = v;
break;
case ("rpcpassword"):
rpcPass = v;
break;
case ("rpcconnect"):
rpcHost = v.toLowerCase();
break;
case ("rpcport"):
rpcPort = v;
break;
default:
break;
}
}
}
// Validation checks
if (rpcHost === "")
console.log("WARNING: " + filepath + " does not contain rpcconnect setting!");
if (rpcPort === "")
console.log("WARNING: " + filepath + " does not contain rpcport setting!");
if (rpcUser === "")
console.log("WARNING: " + filepath + " does not contain rpcuser setting!");
if (rpcPass === "")
console.log("WARNING: " + filepath + " does not contain rpcpassword setting!");

var isLocal = false;
if (rpcHost === "localhost" || rpcHost === "127.0.0.1" || rpcHost.indexOf("192.168.") === 0 || rpcHost.indexOf(".") === -1){
isLocal = true;
}
var settings = require('./settings');

var Client = require('./coin-client');
var client = new Client();
client.set('host', rpcHost);
client.set('port', rpcPort);
client.set('user', rpcUser);
client.set('pass', rpcPass);
if (!isLocal) client.set('https', true);
if (!isLocal) client.set('rejectUnauthorized', false); // Use false if using a self-signed certificate.
client.set('user', settings.wallet.rpcuser);
client.set('pass', settings.wallet.rpcpassword);
client.set('host', settings.wallet.rpchost);
client.set('port', settings.wallet.rpcport);
client.set('https', settings.wallet.ssl); // Use true if wallet is not local.
client.set('strictSSL', settings.wallet.strictSSL); // Use false if using a self-signed certificate.
client.set('rejectUnauthorized', settings.wallet.rejectUnauthorized); // Use false if using a self-signed certificate.

client.auth();

// Clear from settings which gets passed to the client.
settings.wallet.rpcuser = "XXXXXXXX";
settings.wallet.rpcpassword = "XXXXXXXX";

module.exports.api = client;
module.exports.settings = settings;
module.exports.isLocal = isLocal;
module.exports.rpcHost = rpcHost;
module.exports.rpcPort = rpcPort;
37 changes: 20 additions & 17 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@ var opts = {server: {
};

mongoose.Promise = global.Promise;
mongoose.connection.on('close', function (){
mongoose.connection.on('close', function () {
console.log('Database closed.');
});
mongoose.connection.on('error', function (err){
mongoose.connection.on('error', function (err) {
mongoose.disconnect();
console.log('Database error:' + err);
process.emit('database_error', "Database error.");
});
mongoose.connection.on('disconnected', function (){
mongoose.connection.on('disconnected', function () {
process.emit('database_disconnected', "Database disconnected.");
});
mongoose.connection.on('reconnected', function (){ // If: auto_reconnect === true
mongoose.connection.on('reconnected', function () { // If: auto_reconnect === true
process.emit('database_reconnected', "Database reconnected.");
});
mongoose.connection.on('connected', function (){ // If: auto_reconnect === false
mongoose.connection.on('connected', function () { // If: auto_reconnect === false
process.emit('database_connected', "Database connected.");
});

module.exports = {
// Save the user's profile
saveUserProfile: function(id, profile, cb) {
//console.log("DEBUG: " + JSON.stringify(profile));
User.findOne({'_id': id}, function(err, user){
if (user){
User.findOne({'_id': id}, function(err, user) {
if (user) {
user.profile = profile;
user.save(function(err){
user.save(function(err) {
if(err)
cb(err, "Save Error!");
else
Expand All @@ -50,14 +50,17 @@ module.exports = {

// Save the user's wallet
saveUserWallet: function(id, node_id, account, addresses, cb) {
User.findOne({'_id': id}, function(err, user){
if (user){
for (var i = 0; i < user.wallet.length; i++){
if (user.wallet[i].node_id === node_id && user.wallet[i].account === account){
user.wallet[i].addresses = addresses;
User.findOne({'_id': id}, function(err, user) {
if (user) {
for (var i = 0; i < user.wallet.length; i++) {
if (user.wallet[i].node_id === node_id && user.wallet[i].account === account) {
user.wallet[i].addresses = []; // Re-init addresses
for (var j = 0; j < addresses.length; j++) {
user.wallet[i].addresses.push(addresses[j].address);
}
}
}
user.save(function(err){
user.save(function(err) {
if(err)
cb(err, "Save Error!");
else
Expand All @@ -83,9 +86,9 @@ module.exports = {
// Close DB
close: function(cb) {
// mongoose.connection.readyState: 0 = disconnected, 1 = connected, 2 = connecting, 3 = disconnecting
if (mongoose.connection && mongoose.connection.readyState && mongoose.connection.readyState < 3){
mongoose.connection.close(function (){
mongoose.disconnect(function (){
if (mongoose.connection && mongoose.connection.readyState && mongoose.connection.readyState < 3) {
mongoose.connection.close(function () {
mongoose.disconnect(function () {
return cb();
}).catch(function (e) {
console.log("Unable to disconnect from database: " + e);
Expand Down
Loading

0 comments on commit 67a2f50

Please sign in to comment.