-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwalletCore.js
executable file
·120 lines (116 loc) · 4.51 KB
/
walletCore.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
"use strict";
let databaseGroup = require('./wanchaindb/index.js').databaseGroup;
let sendTransaction = require('./cross_send/sendTransaction.js');
let crosschain = require('./dbDefine/crossTransDefine.js');
let sendFromSocket = require("./wanchainsender/index.js").SendFromSocket;
const sendFromWeb3 = require("./wanchainsender/index.js").SendFromWeb3;
const wanchaintrans = require("./wanchaintrans/index.js");
const NormalSend = wanchaintrans.NormalSend;
let keystoreDir = require('wanchain-keystore').keystoreDir;
let messageFactory = require('./webSocket/messageFactory.js');
let socketServer = require("./wanchainsender/index.js").socketServer;
const mr = require('./monitor.js').MonitorRecord;
const be = require('./ccUtil.js').Backend;
let montimer;
async function recordMonitor(config, ethSend,wanSend){
await mr.init(config, ethSend,wanSend);
if(montimer){
clearInterval(montimer);
}
montimer = setInterval(function(){
mr.monitorTask();
}, 6000);
}
class walletcore {
constructor(config){
this.socketUrl = config.socketUrl;
global.getLogger = config.getLogger;
this.wanSend = new sendFromSocket(null,'WAN');
this.ethSend = new sendFromSocket(null,'ETH');
this.EthKeyStoreDir = new keystoreDir(config.ethKeyStorePath);
this.WanKeyStoreDir = new keystoreDir(config.wanKeyStorePath);
this.databaseGroup = databaseGroup;
databaseGroup.useDatabase(config.databasePath,[crosschain]);
this.be = be;
this.sendFromSocket = sendFromSocket;
this.wanchaintrans = wanchaintrans;
}
close(){
clearInterval(montimer);
for (var key in databaseGroup.databaseAry) {
databaseGroup.databaseAry[key].db.close();
}
this.wanSend.socket.connection.close();
}
async reinit(config){
this.wanSend.socket.connection.close();
let newWebSocket = new socketServer(config.socketUrl,messageFactory);
this.wanSend.socket = newWebSocket;
this.ethSend.socket = newWebSocket;
let self = this;
return new Promise(function(resolve, fail){
newWebSocket.connection.on('error', function _cb(err){
fail(err);
});
newWebSocket.connection.on('open', function _cb(){
recordMonitor(config,self.ethSend, self.wanSend);
be.init(config, self.ethSend, self.wanSend,function(){
resolve();
});
})
});
}
async init(config){
global.getCollection = this.getCollection;
global.getCollectionCb = this.getCollectionCb;
global.EthKeyStoreDir = this.EthKeyStoreDir;
global.WanKeyStoreDir = this.WanKeyStoreDir;
for (var key in databaseGroup.databaseAry) {
await databaseGroup.databaseAry[key].init();
}
databaseGroup.inited = true;
let newWebSocket = new socketServer(config.socketUrl,messageFactory);
this.wanSend.socket = newWebSocket;
this.ethSend.socket = newWebSocket;
let self = this;
return new Promise(function(resolve, fail){
newWebSocket.connection.on('error', function _cb(err){
fail(err);
});
newWebSocket.connection.on('open', function _cb(){
recordMonitor(config,self.ethSend, self.wanSend);
be.init(config, self.ethSend, self.wanSend,function(){
resolve();
});
})
});
}
createSendTransaction(ChainType){
let sendGroup = ChainType == 'ETH' ? this.ethSend : this.wanSend;
return new sendTransaction(sendGroup);
}
getCollection(dbName,collectionName){
return databaseGroup.getCollection(dbName,collectionName);
}
loadDatabase(dbName, cb){
return databaseGroup.databaseAry[dbName].db.loadDatabase({},cb);
}
async getCollectionCb(dbName,collectionName, cb){
if(!databaseGroup.inited){
for (var key in databaseGroup.databaseAry) {
await databaseGroup.databaseAry[key].init();
}
}
let collection = databaseGroup.getCollection(dbName,collectionName);
cb(collection);
}
CreaterSender(ChainType) {
if(this.socketUrl){
return new sendFromSocket(new socketServer(this.socketUrl,messageFactory),ChainType);
}
}
CreaterWeb3Sender(url) {
return new sendFromWeb3(url);
}
}
module.exports = global.walletcore = walletcore;