-
Notifications
You must be signed in to change notification settings - Fork 2
/
account.js
69 lines (62 loc) · 2.42 KB
/
account.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var express = require('express');
var router = express.Router();
var sha3_224 = require('js-sha3').sha3_224;
var fs = require('fs');
var address = require('../models/postgres/address.js');
var account = require('../models/postgres/account.js');
var Web3 = require('web3');
var web3 = new Web3();
var router = express.Router();
web3.setProvider(new web3.providers.HttpProvider(process.env.ENODE_BASE || 'http://localhost:8545'));
/* POST create account, this will also create Ethereum address */
router.post('/create', function(req, res, next) {
if (req.body.account !== undefined && req.body.account !== null && req.body.account !== '' &&
req.body.password !== undefined && req.body.password !== null && req.body.password !== '') {
var userAccount = req.body.account;
var userPassword = req.body.password;
var accountEntity = {
'account': userAccount,
'password': userPassword
};
account.create(accountEntity).then(function(accountId){
// generate passphrase
// generate token
var passphrase = sha3_224(account + ':passphrase:' + Math.random());
var token = sha3_224(account + ':token:' + Math.random());
var ethAddress = web3.personal.newAccount(passphrase);
// TODO save address & passphrase
var addressEntity = {
'accountId': accountId,
'address': ethAddress,
'passphrase': passphrase, // hash
'token': token
}
address.create(addressEntity).then(function(addressResult){
var fileDir = process.env.KEYSTORE_PATH;
fs.readdir(fileDir, 'utf8', function(err, files) {
if (err) console.log(err);
files.forEach(function(file) {
if (file.indexOf(ethAddress.substring(2)) >= 0) {
fs.readFile(fileDir + '/' + file, 'utf8', function(err, data) {
res.json({'account': userAccount,
'address': ethAddress,
'keystore': JSON.parse(data),
'passphrase': passphrase,
'accessToken': token});
})
}
});
});
}).catch(function(err){
console.log(err.message, err.stack);
res.json({'error': {'code': 402, 'message': 'error on create eth address'}});
});
}).catch(function(err){
console.log(err.message, err.stack);
res.json({'error': {'code': 401, 'message': 'error on create account'}});
});
} else {
res.json({'error': {'message': 'must give account and password'}});
}
});
module.exports = router;