@instadapp/interop-x 0.0.0-dev.0b0cc3f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +1 -0
- package/.github/workflows/publish-dev.yml +30 -0
- package/README.md +21 -0
- package/bin/interop-x +2 -0
- package/dist/abi/erc20.json +350 -0
- package/dist/abi/gnosisSafe.json +747 -0
- package/dist/abi/index.js +15 -0
- package/dist/abi/interopBridgeToken.json +286 -0
- package/dist/abi/interopXGateway.json +184 -0
- package/dist/config/index.js +17 -0
- package/dist/constants/addresses.js +28 -0
- package/dist/constants/index.js +17 -0
- package/dist/db/index.js +17 -0
- package/dist/db/models/index.js +17 -0
- package/dist/db/models/transaction.js +52 -0
- package/dist/db/sequelize.js +22 -0
- package/dist/index.js +43 -0
- package/dist/logger/index.js +138 -0
- package/dist/net/index.js +19 -0
- package/dist/net/peer/index.js +104 -0
- package/dist/net/pool/index.js +107 -0
- package/dist/net/protocol/dial/BaseDialProtocol.js +106 -0
- package/dist/net/protocol/dial/SignatureDialProtocol.js +46 -0
- package/dist/net/protocol/index.js +92 -0
- package/dist/tasks/BaseTask.js +61 -0
- package/dist/tasks/InteropXGateway/SyncDepositEvents.js +79 -0
- package/dist/tasks/index.js +27 -0
- package/dist/typechain/Erc20.js +2 -0
- package/dist/typechain/GnosisSafe.js +2 -0
- package/dist/typechain/InteropBridgeToken.js +2 -0
- package/dist/typechain/InteropXGateway.js +2 -0
- package/dist/typechain/common.js +2 -0
- package/dist/typechain/factories/Erc20__factory.js +367 -0
- package/dist/typechain/factories/GnosisSafe__factory.js +1174 -0
- package/dist/typechain/factories/InteropBridgeToken__factory.js +459 -0
- package/dist/typechain/factories/InteropXGateway__factory.js +265 -0
- package/dist/typechain/factories/index.js +14 -0
- package/dist/typechain/index.js +35 -0
- package/dist/types.js +21 -0
- package/dist/utils/index.js +101 -0
- package/package.json +79 -0
- package/src/abi/erc20.json +350 -0
- package/src/abi/gnosisSafe.json +747 -0
- package/src/abi/index.ts +11 -0
- package/src/abi/interopBridgeToken.json +286 -0
- package/src/abi/interopXGateway.json +184 -0
- package/src/config/index.ts +24 -0
- package/src/constants/addresses.ts +25 -0
- package/src/constants/index.ts +1 -0
- package/src/db/index.ts +1 -0
- package/src/db/models/index.ts +1 -0
- package/src/db/models/transaction.ts +92 -0
- package/src/db/sequelize.ts +21 -0
- package/src/index.ts +50 -0
- package/src/logger/index.ts +157 -0
- package/src/net/index.ts +3 -0
- package/src/net/peer/index.ts +119 -0
- package/src/net/pool/index.ts +128 -0
- package/src/net/protocol/dial/BaseDialProtocol.ts +104 -0
- package/src/net/protocol/dial/SignatureDialProtocol.ts +59 -0
- package/src/net/protocol/index.ts +138 -0
- package/src/tasks/BaseTask.ts +75 -0
- package/src/tasks/InteropXGateway/SyncDepositEvents.ts +114 -0
- package/src/tasks/index.ts +21 -0
- package/src/typechain/Erc20.ts +491 -0
- package/src/typechain/GnosisSafe.ts +1728 -0
- package/src/typechain/InteropBridgeToken.ts +686 -0
- package/src/typechain/InteropXGateway.ts +407 -0
- package/src/typechain/common.ts +44 -0
- package/src/typechain/factories/Erc20__factory.ts +368 -0
- package/src/typechain/factories/GnosisSafe__factory.ts +1178 -0
- package/src/typechain/factories/InteropBridgeToken__factory.ts +466 -0
- package/src/typechain/factories/InteropXGateway__factory.ts +272 -0
- package/src/typechain/factories/index.ts +7 -0
- package/src/typechain/index.ts +12 -0
- package/src/types.ts +39 -0
- package/src/utils/index.ts +129 -0
- package/tsconfig.json +30 -0
package/dist/index.js
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
require("module-alias/register");
|
7
|
+
const assert_1 = __importDefault(require("assert"));
|
8
|
+
const dotenv_1 = __importDefault(require("dotenv"));
|
9
|
+
const ethers_1 = require("ethers");
|
10
|
+
dotenv_1.default.config();
|
11
|
+
const logger_1 = __importDefault(require("@/logger"));
|
12
|
+
const logger = new logger_1.default('Process');
|
13
|
+
(0, assert_1.default)(process.env.PRIVATE_KEY, "PRIVATE_KEY is not defined");
|
14
|
+
try {
|
15
|
+
new ethers_1.ethers.Wallet(process.env.PRIVATE_KEY);
|
16
|
+
}
|
17
|
+
catch (e) {
|
18
|
+
logger.error('Invalid private key');
|
19
|
+
process.exit(1);
|
20
|
+
}
|
21
|
+
const tasks_1 = require("@/tasks");
|
22
|
+
const net_1 = require("@/net");
|
23
|
+
async function main() {
|
24
|
+
(0, net_1.startPeer)({});
|
25
|
+
const tasks = new tasks_1.Tasks();
|
26
|
+
tasks.start();
|
27
|
+
}
|
28
|
+
main()
|
29
|
+
.then(() => {
|
30
|
+
}).catch(err => {
|
31
|
+
console.error(err);
|
32
|
+
});
|
33
|
+
process.on('SIGINT', () => {
|
34
|
+
logger.debug('received SIGINT signal. exiting.');
|
35
|
+
process.exit(0);
|
36
|
+
});
|
37
|
+
process.on('SIGTERM', () => {
|
38
|
+
logger.debug('received SIGTERM signal. exiting.');
|
39
|
+
process.exit(0);
|
40
|
+
});
|
41
|
+
process.on('unhandledRejection', (reason, p) => {
|
42
|
+
logger.error('unhandled rejection: promise:', p, 'reason:', reason);
|
43
|
+
});
|
@@ -0,0 +1,138 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.setLogLevel = exports.LogLevels = void 0;
|
7
|
+
const chalk_1 = __importDefault(require("chalk"));
|
8
|
+
const luxon_1 = require("luxon");
|
9
|
+
var LogLevels;
|
10
|
+
(function (LogLevels) {
|
11
|
+
LogLevels[LogLevels["Critical"] = 0] = "Critical";
|
12
|
+
LogLevels[LogLevels["Error"] = 1] = "Error";
|
13
|
+
LogLevels[LogLevels["Warn"] = 2] = "Warn";
|
14
|
+
LogLevels[LogLevels["Info"] = 3] = "Info";
|
15
|
+
LogLevels[LogLevels["Log"] = 4] = "Log";
|
16
|
+
LogLevels[LogLevels["Debug"] = 5] = "Debug";
|
17
|
+
})(LogLevels = exports.LogLevels || (exports.LogLevels = {}));
|
18
|
+
const logLevelColors = {
|
19
|
+
[LogLevels.Critical]: 'red',
|
20
|
+
[LogLevels.Error]: 'red',
|
21
|
+
[LogLevels.Warn]: 'yellow',
|
22
|
+
[LogLevels.Info]: 'blue',
|
23
|
+
[LogLevels.Log]: 'white',
|
24
|
+
[LogLevels.Debug]: 'white'
|
25
|
+
};
|
26
|
+
let logLevel = LogLevels.Debug;
|
27
|
+
const setLogLevel = (_logLevel) => {
|
28
|
+
if (typeof _logLevel === 'string') {
|
29
|
+
const mapping = {
|
30
|
+
error: LogLevels.Error,
|
31
|
+
warn: LogLevels.Warn,
|
32
|
+
info: LogLevels.Info,
|
33
|
+
debug: LogLevels.Debug
|
34
|
+
};
|
35
|
+
_logLevel = mapping[_logLevel];
|
36
|
+
}
|
37
|
+
logLevel = _logLevel;
|
38
|
+
};
|
39
|
+
exports.setLogLevel = setLogLevel;
|
40
|
+
class Logger {
|
41
|
+
constructor(tag = '', opts = {
|
42
|
+
color: 'white'
|
43
|
+
}) {
|
44
|
+
var _a;
|
45
|
+
this.tag = '';
|
46
|
+
this.prefix = '';
|
47
|
+
this.options = {};
|
48
|
+
this.enabled = true;
|
49
|
+
this.critical = (...input) => {
|
50
|
+
if (!this.enabled)
|
51
|
+
return;
|
52
|
+
console.error(...this.headers(LogLevels.Critical), ...input);
|
53
|
+
};
|
54
|
+
this.debug = (...input) => {
|
55
|
+
if (!this.enabled)
|
56
|
+
return;
|
57
|
+
if (logLevel !== LogLevels.Debug) {
|
58
|
+
return;
|
59
|
+
}
|
60
|
+
console.debug(...this.headers(LogLevels.Debug), ...input);
|
61
|
+
};
|
62
|
+
this.error = (...input) => {
|
63
|
+
if (!this.enabled)
|
64
|
+
return;
|
65
|
+
console.error(...this.headers(LogLevels.Error), ...input);
|
66
|
+
};
|
67
|
+
this.info = (...input) => {
|
68
|
+
if (!this.enabled)
|
69
|
+
return;
|
70
|
+
if (!(logLevel === LogLevels.Debug || logLevel === LogLevels.Info)) {
|
71
|
+
return;
|
72
|
+
}
|
73
|
+
console.info(...this.headers(LogLevels.Info), ...input);
|
74
|
+
};
|
75
|
+
this.log = (...input) => {
|
76
|
+
if (!this.enabled)
|
77
|
+
return;
|
78
|
+
if (logLevel < LogLevels.Info) {
|
79
|
+
return;
|
80
|
+
}
|
81
|
+
console.log(...this.headers(LogLevels.Log), ...input);
|
82
|
+
};
|
83
|
+
this.warn = (...input) => {
|
84
|
+
if (!this.enabled)
|
85
|
+
return;
|
86
|
+
if (logLevel < LogLevels.Warn) {
|
87
|
+
return;
|
88
|
+
}
|
89
|
+
console.warn(...this.headers(LogLevels.Warn), ...input);
|
90
|
+
};
|
91
|
+
if (tag instanceof Object) {
|
92
|
+
opts = tag;
|
93
|
+
tag = (_a = opts.tag) !== null && _a !== void 0 ? _a : '';
|
94
|
+
}
|
95
|
+
if (opts.prefix) {
|
96
|
+
this.prefix = `<${opts.prefix}>`;
|
97
|
+
}
|
98
|
+
if (tag) {
|
99
|
+
if (opts.color) {
|
100
|
+
//@ts-ignore
|
101
|
+
this.tag = chalk_1.default[opts.color](`[${tag}]`);
|
102
|
+
}
|
103
|
+
else {
|
104
|
+
this.tag = `[${tag}]`;
|
105
|
+
}
|
106
|
+
}
|
107
|
+
if (process.env.DISABLE_LOGGER) {
|
108
|
+
this.enabled = false;
|
109
|
+
}
|
110
|
+
this.options = opts;
|
111
|
+
}
|
112
|
+
setEnabled(enabled) {
|
113
|
+
this.enabled = enabled;
|
114
|
+
}
|
115
|
+
create(additionalDataLabel) {
|
116
|
+
let label;
|
117
|
+
if (additionalDataLabel.id) {
|
118
|
+
label = `id: ${additionalDataLabel.id}`;
|
119
|
+
}
|
120
|
+
else {
|
121
|
+
label = `root: ${additionalDataLabel.root}`;
|
122
|
+
}
|
123
|
+
return new Logger(this.options.tag, Object.assign({}, this.options, {
|
124
|
+
prefix: `${this.options.prefix ? `${this.options.prefix} ` : ''}${label}`
|
125
|
+
}));
|
126
|
+
}
|
127
|
+
get timestamp() {
|
128
|
+
return luxon_1.DateTime.now().toISO();
|
129
|
+
}
|
130
|
+
headers(logLevelEnum) {
|
131
|
+
const keys = Object.keys(LogLevels);
|
132
|
+
const logLevelName = keys[logLevelEnum + keys.length / 2].toUpperCase();
|
133
|
+
//@ts-ignore
|
134
|
+
const coloredLogLevel = chalk_1.default[logLevelColors[logLevelEnum]](logLevelName.padEnd(5, ' '));
|
135
|
+
return [this.timestamp, coloredLogLevel, this.tag, this.prefix];
|
136
|
+
}
|
137
|
+
}
|
138
|
+
exports.default = Logger;
|
@@ -0,0 +1,19 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./peer"), exports);
|
18
|
+
__exportStar(require("./pool"), exports);
|
19
|
+
__exportStar(require("./protocol"), exports);
|
@@ -0,0 +1,104 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.stopPeer = exports.startPeer = void 0;
|
7
|
+
const libp2p_1 = __importDefault(require("libp2p"));
|
8
|
+
const libp2p_tcp_1 = __importDefault(require("libp2p-tcp"));
|
9
|
+
//@ts-ignore
|
10
|
+
const libp2p_websockets_1 = __importDefault(require("libp2p-websockets"));
|
11
|
+
//@ts-ignore
|
12
|
+
const libp2p_mplex_1 = __importDefault(require("libp2p-mplex"));
|
13
|
+
const libp2p_noise_1 = require("libp2p-noise");
|
14
|
+
const logger_1 = __importDefault(require("@/logger"));
|
15
|
+
const libp2p_bootstrap_1 = __importDefault(require("libp2p-bootstrap"));
|
16
|
+
const waait_1 = __importDefault(require("waait"));
|
17
|
+
const libp2p_gossipsub_1 = __importDefault(require("@achingbrain/libp2p-gossipsub"));
|
18
|
+
//@ts-ignore
|
19
|
+
const libp2p_mdns_1 = __importDefault(require("libp2p-mdns"));
|
20
|
+
//@ts-ignore
|
21
|
+
const libp2p_kad_dht_1 = __importDefault(require("libp2p-kad-dht"));
|
22
|
+
//@ts-ignore
|
23
|
+
const libp2p_pubsub_peer_discovery_1 = __importDefault(require("libp2p-pubsub-peer-discovery"));
|
24
|
+
const net_1 = require("@/net");
|
25
|
+
const config_1 = __importDefault(require("@/config"));
|
26
|
+
const logger = new logger_1.default("Peer");
|
27
|
+
let node;
|
28
|
+
// Known peers addresses
|
29
|
+
const bootstrapMultiaddrs = [
|
30
|
+
'/ip4/164.92.249.133/tcp/15001/ws/p2p/QmVGmkqRn8FEw4PQz83L8Mun24ALxXB2fEyS5aEfT2gGqz',
|
31
|
+
'/ip4/164.92.249.133/tcp/15000/p2p/QmVGmkqRn8FEw4PQz83L8Mun24ALxXB2fEyS5aEfT2gGqz',
|
32
|
+
];
|
33
|
+
const startPeer = async ({}) => {
|
34
|
+
node = await libp2p_1.default.create({
|
35
|
+
addresses: {
|
36
|
+
listen: ["/ip4/0.0.0.0/tcp/0", "/ip4/0.0.0.0/tcp/0/ws"],
|
37
|
+
},
|
38
|
+
modules: {
|
39
|
+
transport: [libp2p_tcp_1.default, libp2p_websockets_1.default],
|
40
|
+
streamMuxer: [libp2p_mplex_1.default],
|
41
|
+
connEncryption: [libp2p_noise_1.NOISE],
|
42
|
+
peerDiscovery: [libp2p_mdns_1.default, libp2p_bootstrap_1.default, libp2p_pubsub_peer_discovery_1.default],
|
43
|
+
pubsub: libp2p_gossipsub_1.default,
|
44
|
+
dht: libp2p_kad_dht_1.default,
|
45
|
+
},
|
46
|
+
config: {
|
47
|
+
dht: {
|
48
|
+
enabled: true,
|
49
|
+
},
|
50
|
+
peerDiscovery: {
|
51
|
+
autoDial: true,
|
52
|
+
// The `tag` property will be searched when creating the instance of your Peer Discovery service.
|
53
|
+
// The associated object, will be passed to the service when it is instantiated.
|
54
|
+
[libp2p_bootstrap_1.default.tag]: {
|
55
|
+
enabled: true,
|
56
|
+
interval: 60e3,
|
57
|
+
list: bootstrapMultiaddrs, // provide array of multiaddrs
|
58
|
+
},
|
59
|
+
[libp2p_mdns_1.default.tag]: {
|
60
|
+
interval: 20e3,
|
61
|
+
enabled: true,
|
62
|
+
},
|
63
|
+
[libp2p_pubsub_peer_discovery_1.default.tag]: {
|
64
|
+
interval: 1000,
|
65
|
+
enabled: true,
|
66
|
+
},
|
67
|
+
},
|
68
|
+
relay: {
|
69
|
+
enabled: true,
|
70
|
+
autoRelay: {
|
71
|
+
enabled: true,
|
72
|
+
maxListeners: 10,
|
73
|
+
},
|
74
|
+
},
|
75
|
+
},
|
76
|
+
peerStore: {
|
77
|
+
persistence: true,
|
78
|
+
},
|
79
|
+
});
|
80
|
+
logger.info("Peer ID:", node.peerId.toB58String());
|
81
|
+
await node.start();
|
82
|
+
net_1.protocol.start({
|
83
|
+
libp2p: node
|
84
|
+
});
|
85
|
+
node.on("peer:discovery", (peer) => logger.log(`Discovered peer ${peer}`)); // peer disc.
|
86
|
+
node.connectionManager.on("peer:connect", (connection) => logger.log(`Connected to ${connection.remotePeer.toB58String()}`));
|
87
|
+
logger.log("Peer discovery started");
|
88
|
+
await (0, waait_1.default)(1000);
|
89
|
+
setInterval(() => net_1.protocol.sendPeerInfo({
|
90
|
+
publicAddress: config_1.default.wallet.address,
|
91
|
+
}), 5000);
|
92
|
+
};
|
93
|
+
exports.startPeer = startPeer;
|
94
|
+
const stopPeer = async () => {
|
95
|
+
if (node) {
|
96
|
+
// stop libp2p
|
97
|
+
await node.stop();
|
98
|
+
logger.log("libp2p has stopped");
|
99
|
+
}
|
100
|
+
process.exit(0);
|
101
|
+
};
|
102
|
+
exports.stopPeer = stopPeer;
|
103
|
+
process.on("SIGTERM", exports.stopPeer);
|
104
|
+
process.on("SIGINT", exports.stopPeer);
|
@@ -0,0 +1,107 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.peerPool = exports.PeerPool = void 0;
|
7
|
+
const types_1 = require("@/types");
|
8
|
+
const config_1 = __importDefault(require("@/config"));
|
9
|
+
class PeerPool {
|
10
|
+
constructor() {
|
11
|
+
this.PEERS_CLEANUP_TIME_LIMIT = 1;
|
12
|
+
this.pool = new Map();
|
13
|
+
config_1.default.events.on(types_1.Event.PEER_CONNECTED, (peer) => {
|
14
|
+
this.connected(peer);
|
15
|
+
});
|
16
|
+
config_1.default.events.on(types_1.Event.PEER_DISCONNECTED, (peer) => {
|
17
|
+
this.disconnected(peer);
|
18
|
+
});
|
19
|
+
}
|
20
|
+
/**
|
21
|
+
* Connected peers
|
22
|
+
*/
|
23
|
+
get peers() {
|
24
|
+
const connectedPeers = Array.from(this.pool.values());
|
25
|
+
return connectedPeers;
|
26
|
+
}
|
27
|
+
/**
|
28
|
+
* Number of peers in pool
|
29
|
+
*/
|
30
|
+
get size() {
|
31
|
+
return this.peers.length;
|
32
|
+
}
|
33
|
+
/**
|
34
|
+
* Return true if pool contains the specified peer
|
35
|
+
* @param peer peer object or id
|
36
|
+
*/
|
37
|
+
contains(peer) {
|
38
|
+
if (typeof peer !== 'string') {
|
39
|
+
peer = peer.id;
|
40
|
+
}
|
41
|
+
return !!this.pool.get(peer);
|
42
|
+
}
|
43
|
+
/**
|
44
|
+
* Handler for peer connections
|
45
|
+
* @param peer peer
|
46
|
+
*/
|
47
|
+
connected(peer) {
|
48
|
+
if (this.size >= config_1.default.maxPeers)
|
49
|
+
return;
|
50
|
+
this.add(peer);
|
51
|
+
}
|
52
|
+
/**
|
53
|
+
* Handler for peer disconnections
|
54
|
+
* @param peer peer
|
55
|
+
*/
|
56
|
+
disconnected(peer) {
|
57
|
+
this.remove(peer);
|
58
|
+
}
|
59
|
+
/**
|
60
|
+
* Add peer to pool
|
61
|
+
* @param peer peer
|
62
|
+
* @emits {@link Event.POOL_PEER_ADDED}
|
63
|
+
*/
|
64
|
+
add(peer) {
|
65
|
+
if (peer && peer.id && !this.pool.get(peer.id)) {
|
66
|
+
this.pool.set(peer.id, peer);
|
67
|
+
peer.pooled = true;
|
68
|
+
config_1.default.events.emit(types_1.Event.POOL_PEER_ADDED, peer);
|
69
|
+
}
|
70
|
+
}
|
71
|
+
/**
|
72
|
+
* Remove peer from pool
|
73
|
+
* @param peer peer
|
74
|
+
* @emits {@link Event.POOL_PEER_REMOVED}
|
75
|
+
*/
|
76
|
+
remove(peer) {
|
77
|
+
if (peer && peer.id) {
|
78
|
+
if (this.pool.delete(peer.id)) {
|
79
|
+
peer.pooled = false;
|
80
|
+
config_1.default.events.emit(types_1.Event.POOL_PEER_REMOVED, peer);
|
81
|
+
}
|
82
|
+
}
|
83
|
+
}
|
84
|
+
get activePeers() {
|
85
|
+
this.cleanup();
|
86
|
+
return this.peers.filter((p) => {
|
87
|
+
if (!p.pooled)
|
88
|
+
return false;
|
89
|
+
const now = new Date();
|
90
|
+
return (now.getTime() - p.updated.getTime()) < 1000 * 10; // 10 seconds
|
91
|
+
});
|
92
|
+
}
|
93
|
+
get activePeerIds() {
|
94
|
+
return this.activePeers.map((p) => p.id);
|
95
|
+
}
|
96
|
+
cleanup() {
|
97
|
+
let compDate = Date.now() - this.PEERS_CLEANUP_TIME_LIMIT * 60;
|
98
|
+
this.peers.forEach((peerInfo) => {
|
99
|
+
if (peerInfo.updated.getTime() < compDate) {
|
100
|
+
console.log(`Peer ${peerInfo.id} idle for ${this.PEERS_CLEANUP_TIME_LIMIT} minutes`);
|
101
|
+
this.remove(peerInfo);
|
102
|
+
}
|
103
|
+
});
|
104
|
+
}
|
105
|
+
}
|
106
|
+
exports.PeerPool = PeerPool;
|
107
|
+
exports.peerPool = new PeerPool();
|
@@ -0,0 +1,106 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
3
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
4
|
+
var m = o[Symbol.asyncIterator], i;
|
5
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
6
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
7
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
8
|
+
};
|
9
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
10
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
11
|
+
};
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
13
|
+
exports.BaseDialProtocol = void 0;
|
14
|
+
const it_pipe_1 = __importDefault(require("it-pipe"));
|
15
|
+
const peer_id_1 = __importDefault(require("peer-id"));
|
16
|
+
const utils_1 = require("@/utils");
|
17
|
+
const waait_1 = __importDefault(require("waait"));
|
18
|
+
class BaseDialProtocol {
|
19
|
+
constructor(libp2p, protocol) {
|
20
|
+
this.timeout = 20000;
|
21
|
+
this.libp2p = libp2p;
|
22
|
+
this.protocol = protocol;
|
23
|
+
this.libp2p.handle(this.protocol, this.handle.bind(this));
|
24
|
+
}
|
25
|
+
encode(data) {
|
26
|
+
return JSON.stringify(data);
|
27
|
+
}
|
28
|
+
encodeResponse(data) {
|
29
|
+
return JSON.stringify(data);
|
30
|
+
}
|
31
|
+
decode(data) {
|
32
|
+
return JSON.parse(data.toString());
|
33
|
+
}
|
34
|
+
decodeResponse(data) {
|
35
|
+
return JSON.parse(data.toString());
|
36
|
+
}
|
37
|
+
async response(data) {
|
38
|
+
return data;
|
39
|
+
}
|
40
|
+
async handle({ connection, stream }) {
|
41
|
+
const instance = this;
|
42
|
+
try {
|
43
|
+
await (0, it_pipe_1.default)(stream, async function (source) {
|
44
|
+
var e_1, _a;
|
45
|
+
try {
|
46
|
+
for (var source_1 = __asyncValues(source), source_1_1; source_1_1 = await source_1.next(), !source_1_1.done;) {
|
47
|
+
const message = source_1_1.value;
|
48
|
+
const response = instance.encodeResponse(await instance.response(instance.decode(message)));
|
49
|
+
await (0, it_pipe_1.default)([response], stream);
|
50
|
+
}
|
51
|
+
}
|
52
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
53
|
+
finally {
|
54
|
+
try {
|
55
|
+
if (source_1_1 && !source_1_1.done && (_a = source_1.return)) await _a.call(source_1);
|
56
|
+
}
|
57
|
+
finally { if (e_1) throw e_1.error; }
|
58
|
+
}
|
59
|
+
});
|
60
|
+
}
|
61
|
+
catch (err) {
|
62
|
+
console.error(err);
|
63
|
+
}
|
64
|
+
}
|
65
|
+
async send(data, peerId) {
|
66
|
+
return await (0, utils_1.asyncCallWithTimeout)(new Promise(async (resolve, reject) => {
|
67
|
+
try {
|
68
|
+
let connection = this.libp2p.connectionManager.get(peer_id_1.default.createFromB58String(peerId));
|
69
|
+
if (!connection) {
|
70
|
+
await (0, waait_1.default)(5000);
|
71
|
+
connection = this.libp2p.connectionManager.get(peer_id_1.default.createFromB58String(peerId));
|
72
|
+
if (!connection) {
|
73
|
+
await (0, waait_1.default)(3000);
|
74
|
+
connection = this.libp2p.connectionManager.get(peer_id_1.default.createFromB58String(peerId));
|
75
|
+
if (!connection) {
|
76
|
+
throw new Error('No connection available');
|
77
|
+
}
|
78
|
+
}
|
79
|
+
}
|
80
|
+
const { stream } = await connection.newStream([this.protocol]);
|
81
|
+
const instance = this;
|
82
|
+
(0, it_pipe_1.default)([this.encode(data)], stream, async function (source) {
|
83
|
+
var e_2, _a;
|
84
|
+
try {
|
85
|
+
for (var source_2 = __asyncValues(source), source_2_1; source_2_1 = await source_2.next(), !source_2_1.done;) {
|
86
|
+
const message = source_2_1.value;
|
87
|
+
resolve(instance.decodeResponse(message));
|
88
|
+
stream.close();
|
89
|
+
}
|
90
|
+
}
|
91
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
92
|
+
finally {
|
93
|
+
try {
|
94
|
+
if (source_2_1 && !source_2_1.done && (_a = source_2.return)) await _a.call(source_2);
|
95
|
+
}
|
96
|
+
finally { if (e_2) throw e_2.error; }
|
97
|
+
}
|
98
|
+
});
|
99
|
+
}
|
100
|
+
catch (error) {
|
101
|
+
reject(error);
|
102
|
+
}
|
103
|
+
}), this.timeout);
|
104
|
+
}
|
105
|
+
}
|
106
|
+
exports.BaseDialProtocol = BaseDialProtocol;
|
@@ -0,0 +1,46 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.SignatureDialProtocol = void 0;
|
7
|
+
const BaseDialProtocol_1 = require("./BaseDialProtocol");
|
8
|
+
const waait_1 = __importDefault(require("waait"));
|
9
|
+
const config_1 = __importDefault(require("@/config"));
|
10
|
+
const db_1 = require("@/db");
|
11
|
+
class SignatureDialProtocol extends BaseDialProtocol_1.BaseDialProtocol {
|
12
|
+
constructor(libp2p) {
|
13
|
+
super(libp2p, '/interop-x/signatures');
|
14
|
+
this.timeout = 30000;
|
15
|
+
}
|
16
|
+
async response(data) {
|
17
|
+
const signer = config_1.default.wallet;
|
18
|
+
let event;
|
19
|
+
let maxTimeout = 20000;
|
20
|
+
do {
|
21
|
+
event = await db_1.Transaction.findOne({ where: { transactionHash: data.transactionHash } });
|
22
|
+
if (!event) {
|
23
|
+
await (0, waait_1.default)(1000);
|
24
|
+
maxTimeout -= 1000;
|
25
|
+
}
|
26
|
+
} while (!event && maxTimeout > 0);
|
27
|
+
if (!event) {
|
28
|
+
return {
|
29
|
+
signer: signer.address,
|
30
|
+
data: null,
|
31
|
+
error: 'Event not found'
|
32
|
+
};
|
33
|
+
}
|
34
|
+
// const signedData = await signGnosisSafeTx({
|
35
|
+
// to: addresses[event.chainId as ChainId].multisend,
|
36
|
+
// data: 'TODO',
|
37
|
+
// chainId: event.chainId as ChainId,
|
38
|
+
// safeTxGas: data.safeTxGas,
|
39
|
+
// }, { signer });
|
40
|
+
return {
|
41
|
+
signer: signer.address,
|
42
|
+
data: null, //signedData,
|
43
|
+
};
|
44
|
+
}
|
45
|
+
}
|
46
|
+
exports.SignatureDialProtocol = SignatureDialProtocol;
|
@@ -0,0 +1,92 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.protocol = void 0;
|
7
|
+
const stream_1 = require("stream");
|
8
|
+
const ethereumjs_util_1 = require("ethereumjs-util");
|
9
|
+
const SignatureDialProtocol_1 = require("./dial/SignatureDialProtocol");
|
10
|
+
const __1 = require("..");
|
11
|
+
const config_1 = __importDefault(require("@/config"));
|
12
|
+
const types_1 = require("@/types");
|
13
|
+
class Protocol extends stream_1.EventEmitter {
|
14
|
+
constructor() {
|
15
|
+
super(...arguments);
|
16
|
+
this.protocolMessages = [
|
17
|
+
{
|
18
|
+
name: 'PeerInfo',
|
19
|
+
code: 0x09,
|
20
|
+
encode: (info) => [
|
21
|
+
Buffer.from(info.publicAddress),
|
22
|
+
],
|
23
|
+
decode: ([publicAddress]) => ({
|
24
|
+
publicAddress: publicAddress.toString(),
|
25
|
+
}),
|
26
|
+
},
|
27
|
+
];
|
28
|
+
}
|
29
|
+
start({ libp2p, topic = null, }) {
|
30
|
+
this.libp2p = libp2p;
|
31
|
+
this.topic = topic || 'itnerop-x-protocol';
|
32
|
+
if (this.libp2p.isStarted())
|
33
|
+
this.init();
|
34
|
+
this.on('PeerInfo', (payload) => {
|
35
|
+
config_1.default.events.emit(types_1.Event.PEER_CONNECTED, {
|
36
|
+
id: payload.peerId,
|
37
|
+
publicAddress: payload.data.publicAddress,
|
38
|
+
pooled: false,
|
39
|
+
updated: new Date(),
|
40
|
+
});
|
41
|
+
});
|
42
|
+
this.signature = new SignatureDialProtocol_1.SignatureDialProtocol(this.libp2p);
|
43
|
+
}
|
44
|
+
init() {
|
45
|
+
this.libp2p.pubsub.subscribe(this.topic);
|
46
|
+
this.libp2p.pubsub.on(this.topic, (message) => {
|
47
|
+
try {
|
48
|
+
const [codeBuf, payload] = ethereumjs_util_1.rlp.decode(message.data);
|
49
|
+
const code = (0, ethereumjs_util_1.bufferToInt)(codeBuf);
|
50
|
+
const protocolMessage = this.protocolMessages.find((m) => m.code === code);
|
51
|
+
if (!protocolMessage) {
|
52
|
+
return;
|
53
|
+
}
|
54
|
+
const decoded = protocolMessage.decode(payload);
|
55
|
+
this.emit(protocolMessage.name, {
|
56
|
+
peerId: message.from,
|
57
|
+
name: protocolMessage.name,
|
58
|
+
code: protocolMessage.code,
|
59
|
+
data: decoded
|
60
|
+
});
|
61
|
+
this.emit('all', {
|
62
|
+
peerId: message.from,
|
63
|
+
name: protocolMessage.name,
|
64
|
+
code: protocolMessage.code,
|
65
|
+
data: decoded,
|
66
|
+
});
|
67
|
+
}
|
68
|
+
catch (err) {
|
69
|
+
console.error(err);
|
70
|
+
}
|
71
|
+
});
|
72
|
+
}
|
73
|
+
sendPeerInfo(data) {
|
74
|
+
const message = this.protocolMessages.find((m) => m.name === 'PeerInfo');
|
75
|
+
const encoded = ethereumjs_util_1.rlp.encode([message.code, message.encode(data)]);
|
76
|
+
this.libp2p.pubsub.publish(this.topic, encoded);
|
77
|
+
}
|
78
|
+
async requestSignatures(data, peerIds) {
|
79
|
+
try {
|
80
|
+
peerIds = peerIds || __1.peerPool.activePeerIds;
|
81
|
+
const promises = peerIds.map((peerId) => this.signature.send(data, peerId));
|
82
|
+
return (await Promise.allSettled(promises))
|
83
|
+
.map((p) => p.status === 'fulfilled' ? p.value : null)
|
84
|
+
.filter(Boolean);
|
85
|
+
}
|
86
|
+
catch (error) {
|
87
|
+
console.log(error);
|
88
|
+
return [];
|
89
|
+
}
|
90
|
+
}
|
91
|
+
}
|
92
|
+
exports.protocol = new Protocol();
|