-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
180 lines (159 loc) · 4.61 KB
/
server.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
var _ = require('lodash')
, express = require('express')
, app = express(app)
, server = require('http').createServer(app)
, ccWalletEngine = require('cc-wallet-engine')
, EurecaServer = require('eureca.io').EurecaServer
, eurecaServer = new EurecaServer({allow: ['renderAssets','paymentComplete']})
var wallet,
mnemonic = "you video scan pipe roof ridge comic cannon aspect girl hobby wait", // Create by wallet.generateMnemonic();
password = 'lorem ipsum', pin = '1234',
systemAssetDefinitions = [{
monikers: ['gold'],
colorDescs: ['epobc:b95323a763fa507110a89ab857af8e949810cf1e67e91104cd64222a04ccd0bb:0:180679']
}, {
// Asset definition from desktop chromawallet:
// {"color_set": ["epobc:0261b29b587020eeca15f831a5290a9d81038851da4365689be04e588ce58c66:0:303510"], "monikers": ["euro"], "unit": 100}
"colorDescs": ["epobc:0261b29b587020eeca15f831a5290a9d81038851da4365689be04e588ce58c66:0:303510"],
"monikers": ["euro"],
"unit": 100
}
],
walletOptions = {
testnet: true,
systemAssetDefinitions: systemAssetDefinitions
};
var objReg = {}, glbOid = 1, assetModels = [], clientIds = [];
var saveObj = function (obj, optOid) {
if (!optOid) {
glbOid = glbOid + 1
optOid = glbOid
}
objReg[optOid] = obj
return optOid
}
var getObj = function (oid) {
return objReg[oid]
}
var renderAssetModelsToClient = function (client) {
var serialized = assetModels.map(function (am) {
var name = am.getMoniker()
, oid = saveObj(am, name)
, data = {
name : name,
available : am.getAvailableBalance(),
unconfirmed : am.getUnconfirmedBalance(),
address : am.getAddress(),
oid: name
};
return data;
});
try {
client.renderAssets(serialized);
} catch (e) {
console.error(e);
}
//Send to client
}
//eurecaServer.exports.
eurecaServer.onConnect(function (connection) {
console.log('new Client', connection.id);
var id = connection.id;
clientIds.push(id);
var client = eurecaServer.getClient(id);
renderAssetModelsToClient(client);
});
function walletWasUpdated() {
console.log("Wallet was updated!");
assetModels = wallet.getAssetModels();
console.log('Assets: \n' + _.pluck(assetModels, 'props'));
console.log('Clients:', clientIds);
clientIds.forEach(function (id) {
console.log("Transmitting to client:" + id);
var client = eurecaServer.getClient(id);
renderAssetModelsToClient(client);
});
};
function createWallet() {
wallet = new ccWalletEngine(walletOptions);
if (!wallet.canResetSeed()) {
wallet.initialize(mnemonic, password, pin);
} else {
wallet.resetSeed(password);
}
wallet.setCallback(walletWasUpdated);
};
eurecaServer.attach(server)
createWallet();
eurecaServer.exports.hello = function () {
console.log('Hello from client');
}
eurecaServer.exports.scanAndSend = function (uri) {
var context = this,
response = {}
context.async = true
try {
wallet.makePaymentFromURI(uri, function (err, payment) {
if (err)
throw err
var onPaymentComplete = function (err, txid) {
if (err)
throw err
response.ok = "Sent ok"
context.return(null, response);
}
try {
payment.send(onPaymentComplete);
}
catch (e){
context.return(e.toString(), response);
}
});
} catch (e) {
console.log(e);
context.return(e.toString(), response);
}
}
eurecaServer.exports.send = function (oid, address, amount) {
try {
var am = getObj(oid)
, error
, progress
, paymentModel = am.makePayment()
if (!paymentModel.checkAddress(address)) {
error = 'Invalid address:' + address;
} else if (!paymentModel.checkAmount(amount)) {
error = 'Wrong amount:' + amount;
} else {
paymentModel.addRecipient(address, amount);
var connId = this.connection.id;
var onPaymentComplete = function () {
var client = eurecaServer.getClient(connId);
client.paymentComplete(oid);
};
paymentModel.send(onPaymentComplete);
}
return {
error: error
}
} catch (e) {
return {
error: e.msg
}
}
};
app.use('/bower_components',
express.static(__dirname + '/bower_components'))
app.get('/', function (req, res, next) {
res.sendFile(__dirname + '/index.html')
})
app.get('/logo.png', function (req, res, next) {
res.sendFile(__dirname + '/logo.png')
})
app.get('/faucet.css', function (req, res, next) {
res.sendFile(__dirname + '/faucet.css')
})
app.get('/faucet.js', function (req, res, next) {
res.sendFile(__dirname + '/faucet.js')
})
server.listen(8000)