Skip to content

Commit 3e274aa

Browse files
committed
WIP: adding account management.
1 parent 2006741 commit 3e274aa

File tree

8 files changed

+470
-4
lines changed

8 files changed

+470
-4
lines changed

AccountManagement/accountManagement.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var userRegistry = require('../DataAccess/userRegistry.js');
2+
13
var keythereum = require("keythereum");
24
var Tx = require('ethereumjs-tx');
35
var Web3 = require('web3');

Blockchain/pauseMining.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,4 @@ function checkWork() {
1414
eth.filter("latest", function(err, block) { checkWork(); });
1515
eth.filter("pending", function(err, block) { checkWork(); });
1616

17-
setTimeout(function(){
18-
checkWork();
19-
}, 1000);
17+
checkWork();

DataAccess/userRegistry.js

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
var mongojs = require('mongojs')
2+
3+
var config = require('../config.js');
4+
var db;
5+
6+
function logError(err){
7+
if(err){
8+
if(err.message == 'ns not found'){
9+
return;
10+
} else {
11+
console.log(err);
12+
}
13+
}
14+
}
15+
16+
function connectToDB(cb){
17+
var dbConnection = mongojs(config.dbConnectionString, config.dbCollections);
18+
db = dbConnection;
19+
cb();
20+
}
21+
22+
function getListOfUsers(cb){
23+
if(!db){
24+
connectToDB(function(){
25+
getListOfUsers(function(res){
26+
cb(res);
27+
});
28+
});
29+
} else {
30+
db.userRegistry.find(function(err, docs){
31+
logError(err);
32+
cb(docs);
33+
});
34+
}
35+
}
36+
37+
function getUser(username, cb){
38+
if(!db){
39+
connectToDB(function(){
40+
getUser(username, function(res){
41+
cb(res);
42+
});
43+
});
44+
} else {
45+
db.userRegistry.findOne({username: {$regex: new RegExp('^'+username+'$', 'i')}}, function(err, docs){
46+
logError(err);
47+
cb(docs);
48+
});
49+
}
50+
}
51+
52+
function getUserAndPassword(username, password, cb){
53+
if(!db){
54+
connectToDB(function(){
55+
getUserAndPassword(username, password, function(res){
56+
cb(res);
57+
});
58+
});
59+
} else {
60+
db.userRegistry.findOne({username: {$regex: new RegExp('^'+username+'$', 'i')}, password: password}, function(err, docs){
61+
logError(err);
62+
cb(docs);
63+
});
64+
}
65+
}
66+
67+
function getUserRegistry(cb){
68+
if(!db){
69+
connectToDB(function(){
70+
getUserRegistry(function(res){
71+
cb(res);
72+
});
73+
});
74+
} else {
75+
db.userRegistry.find({}, function(err, docs){
76+
logError(err);
77+
cb(docs);
78+
});
79+
}
80+
}
81+
82+
function findUserFromAddress(address, cb){
83+
if(!db){
84+
connectToDB(function(){
85+
findUserFromAddress(address, function(res){
86+
cb(res);
87+
});
88+
});
89+
} else {
90+
db.userRegistry.findOne({'accountAddressRepo.address': address}, function(err, docs){
91+
logError(err);
92+
cb(docs);
93+
});
94+
}
95+
}
96+
97+
function addUserToRegistry(entry, cb){
98+
if(!db){
99+
connectToDB(function(){
100+
addUserToRegistry(entry, function(res){
101+
cb(res);
102+
});
103+
});
104+
} else {
105+
db.userRegistry.insert(entry, function(err, docs){
106+
logError(err);
107+
cb(docs);
108+
});
109+
}
110+
}
111+
112+
function updateUser(entry, cb){
113+
if(!db){
114+
connectToDB(function(){
115+
updateUser(entry, function(res){
116+
cb(res);
117+
});
118+
});
119+
} else {
120+
db.userRegistry.save(entry, function(err, docs){
121+
logError(err);
122+
cb(docs);
123+
});
124+
}
125+
}
126+
127+
function closeDB(){
128+
if(db != null || db != undefined){
129+
db.close();
130+
}
131+
}
132+
133+
exports.GetUser = getUser;
134+
exports.GetListOfUsers = getListOfUsers;
135+
exports.GetUserRegistry = getUserRegistry;
136+
exports.AddUserToRegistry = addUserToRegistry;
137+
exports.CloseDB = closeDB;
138+
exports.GetUserAndPassword = getUserAndPassword;
139+
exports.UpdateUser = updateUser;
140+
exports.FindUserFromAddress = findUserFromAddress;

TerminalClient/user.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var accountManagement = require('../AccountManagement/accountManagement.js');
2+
var etherDistribution = require('../EtherDistribution/etherDistribution.js');
3+
4+
var Web3 = require('web3');
5+
var fs = require('fs');
6+
var web3 = new Web3();
7+
web3.setProvider(new web3.providers.HttpProvider('http://localhost:20000'));
8+
9+
const readline = require('readline');
10+
const rl = readline.createInterface({
11+
input: process.stdin,
12+
output: process.stdout
13+
});
14+
15+
16+
function run(){
17+
etherDistribution.StartEtherDistribution();
18+
rl.question('What would you like to do: '+
19+
'\n1) Register new user'+
20+
'\n0) Quit'+
21+
'\n> ', function(answer){
22+
if(answer == 0){
23+
console.log('Quiting');
24+
rl.close();
25+
return;
26+
} else if (answer == 1){ // Register new user
27+
28+
29+
} else {
30+
run();
31+
}
32+
});
33+
}
34+
35+
run();

User/userBalances.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
var async = require('async');
2+
3+
var cryptoZARRegistry = require('../DataAccess/cryptoZARRegistry.js');
4+
var cryptoZARIssuance = require('../Issuance/cryptoZARIssuance.js');
5+
var util = require('../Util/util.js');
6+
var eventEmitter = require('../BlockChainViewer/eventEmitter.js');
7+
var config = require('../config.js');
8+
9+
var Web3 = require('web3');
10+
var web3 = new Web3();
11+
web3.setProvider(new web3.providers.HttpProvider(config.rpcaddress));
12+
13+
var coinbasePassword = '1234';
14+
15+
function migrateBalance(token, accountAddress, cb){
16+
console.log('Migrating user balance for account:', accountAddress);
17+
web3.personal.unlockAccount(web3.eth.coinbase, coinbasePassword, 60*60*24*365, function(err, res){
18+
if (err) {console.log('ERROR in migrateBalance:', err);}
19+
var oldAccount = web3.eth.defaultAccount;
20+
web3.eth.defaultAccount = web3.eth.coinbase;
21+
if(token.migrateBalance){ // Checks that this function exists
22+
token.migrateBalance(accountAddress);
23+
}
24+
web3.eth.defaultAccount = oldAccount;
25+
eventEmitter.once('newBlock', function(blockHeight){
26+
var balanceInfo = token.balanceOf(accountAddress);
27+
var addressBalance = util.ConvertToUnitCurrency(util.SumBalance(balanceInfo));
28+
cb(addressBalance);
29+
});
30+
});
31+
}
32+
33+
function getUserCryptoZARBalance(accountAddressRepo, cb){
34+
cryptoZARIssuance.GetCryptoZARTokenInstance(function(token){
35+
if(token){
36+
async.mapSeries(accountAddressRepo, function(account, callback){
37+
if(token.balanceMigrated(account.address) == false){
38+
migrateBalance(token, account.address, function(addressBalance){
39+
callback(null, addressBalance);
40+
});
41+
} else {
42+
var balanceInfo = token.balanceOf(account.address);
43+
var addressBalance = util.ConvertToUnitCurrency(util.SumBalance(balanceInfo));
44+
callback(null, addressBalance);
45+
}
46+
}, function(err, results){
47+
if(err){
48+
console.log('ERROR getting balances:', err);
49+
cb(0);
50+
} else {
51+
cb(util.SumArrayValues(results));
52+
}
53+
})
54+
} else {
55+
cb(0);
56+
}
57+
});
58+
}
59+
60+
function getAddressCryptoZARBalance(accountAddress, cb){
61+
var list = [{address: accountAddress}];
62+
getUserCryptoZARBalance(list, function(balance){
63+
cb(balance);
64+
});
65+
}
66+
67+
exports.GetUserCryptoZARBalance = getUserCryptoZARBalance;
68+
exports.GetAddressCryptoZARBalance = getAddressCryptoZARBalance;
69+

0 commit comments

Comments
 (0)