@leofcoin/launch-chain 0.2.4 → 0.2.6
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/LICENSE +20 -20
- package/index.d.ts +38 -30
- package/index.js +137 -128
- package/package.json +2 -1
- package/rollup.config.js +12 -12
- package/src/index.ts +195 -177
- package/test.js +10 -6
- package/tsconfig.json +13 -13
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 vandeurenglenn
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 vandeurenglenn
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
package/index.d.ts
CHANGED
|
@@ -1,30 +1,38 @@
|
|
|
1
|
-
import Chain from '@leofcoin/chain/chain';
|
|
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
|
-
|
|
1
|
+
import Chain from '@leofcoin/chain/chain';
|
|
2
|
+
import WSClient from '@leofcoin/endpoint-clients/ws';
|
|
3
|
+
import HttpClient from '@leofcoin/endpoint-clients/http';
|
|
4
|
+
type launchMode = 'direct' | 'remote';
|
|
5
|
+
type endpointReturns = {
|
|
6
|
+
http?: string[];
|
|
7
|
+
ws?: string[];
|
|
8
|
+
};
|
|
9
|
+
type clientReturns = {
|
|
10
|
+
http?: HttpClient[];
|
|
11
|
+
ws?: WSClient[];
|
|
12
|
+
};
|
|
13
|
+
type launchReturn = {
|
|
14
|
+
chain: Chain;
|
|
15
|
+
mode: launchMode;
|
|
16
|
+
endpoints: endpointReturns;
|
|
17
|
+
clients: clientReturns;
|
|
18
|
+
};
|
|
19
|
+
type endpointOptions = {
|
|
20
|
+
port: number;
|
|
21
|
+
url?: string;
|
|
22
|
+
};
|
|
23
|
+
type launchOptions = {
|
|
24
|
+
network?: string;
|
|
25
|
+
networkVersion?: string;
|
|
26
|
+
stars: string[];
|
|
27
|
+
forceRemote: boolean;
|
|
28
|
+
mode?: launchMode;
|
|
29
|
+
ws?: endpointOptions[] | undefined;
|
|
30
|
+
http?: endpointOptions[] | undefined;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
*
|
|
34
|
+
* @param {object} options { ws: boolean || {url: string, port: number}, http: boolean || {url: string, port: number}, network}
|
|
35
|
+
* @returns '{ mode: string, endpoints: object, chain}'
|
|
36
|
+
*/
|
|
37
|
+
declare const launch: (options: launchOptions, password: string) => Promise<launchReturn>;
|
|
38
|
+
export { launch as default };
|
package/index.js
CHANGED
|
@@ -1,135 +1,144 @@
|
|
|
1
1
|
import Node from '@leofcoin/chain/node';
|
|
2
2
|
import Chain from '@leofcoin/chain/chain';
|
|
3
3
|
import nodeConfig from '@leofcoin/lib/node-config';
|
|
4
|
+
import WSClient from '@leofcoin/endpoint-clients/ws';
|
|
5
|
+
import HttpClient from '@leofcoin/endpoint-clients/http';
|
|
4
6
|
|
|
5
|
-
const defaultOptions = {
|
|
6
|
-
network: 'leofcoin:peach',
|
|
7
|
-
networkVersion: 'peach',
|
|
8
|
-
stars: ['wss://peach.leofcoin.org'],
|
|
9
|
-
forceRemote: false,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
*
|
|
20
|
-
* @param {string}
|
|
21
|
-
* @
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
resolve(
|
|
39
|
-
}
|
|
40
|
-
catch (error) {
|
|
41
|
-
reject(error);
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
/**
|
|
45
|
-
*
|
|
46
|
-
* @param {string} url
|
|
47
|
-
* @param {string} networkVersion network/testnet-network sepperate by -
|
|
48
|
-
* @returns Promise(boolean)
|
|
49
|
-
*/
|
|
50
|
-
const
|
|
51
|
-
try {
|
|
52
|
-
await tryWs(url, networkVersion);
|
|
53
|
-
return
|
|
54
|
-
}
|
|
55
|
-
catch (error) {
|
|
56
|
-
return
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
// chain is undefined when mode is remote
|
|
60
|
-
// endpoints contain urls to connect to the desired remote
|
|
61
|
-
// when mode is remote means an instance is already running
|
|
62
|
-
// when mode is direct means chain is directly available and no endpoint is needed to interact with it
|
|
63
|
-
/**
|
|
64
|
-
*
|
|
65
|
-
* @param {object} options { ws: boolean || {url: string, port: number}, http: boolean || {url: string, port: number}, network}
|
|
66
|
-
* @returns '{ mode: string, endpoints: object, chain}'
|
|
67
|
-
*/
|
|
68
|
-
const launch = async (options) => {
|
|
69
|
-
if (!options)
|
|
70
|
-
options = defaultOptions;
|
|
71
|
-
else
|
|
72
|
-
options = { ...defaultOptions, ...options };
|
|
73
|
-
const availableEndpoints = {
|
|
74
|
-
http: [],
|
|
75
|
-
ws: []
|
|
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
|
-
if (options.
|
|
121
|
-
const importee = await import('@leofcoin/endpoints/
|
|
122
|
-
const
|
|
123
|
-
await
|
|
124
|
-
endpoints.
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
7
|
+
const defaultOptions = {
|
|
8
|
+
network: 'leofcoin:peach',
|
|
9
|
+
networkVersion: 'peach',
|
|
10
|
+
stars: ['wss://peach.leofcoin.org'],
|
|
11
|
+
forceRemote: false,
|
|
12
|
+
mode: 'direct',
|
|
13
|
+
ws: [{
|
|
14
|
+
port: 4040
|
|
15
|
+
}],
|
|
16
|
+
http: [{
|
|
17
|
+
port: 8080
|
|
18
|
+
}]
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
*
|
|
22
|
+
* @param {string} url
|
|
23
|
+
* @param {string} networkVersion network/testnet-network sepperate by -
|
|
24
|
+
* @returns Promise(boolean)
|
|
25
|
+
*/
|
|
26
|
+
const getHttp = async (url, networkVersion) => {
|
|
27
|
+
try {
|
|
28
|
+
const client = new HttpClient(url, networkVersion);
|
|
29
|
+
await client.network();
|
|
30
|
+
return client;
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const tryWs = (url, networkVersion) => new Promise(async (resolve, reject) => {
|
|
37
|
+
try {
|
|
38
|
+
const socket = await new WSClient(url, networkVersion);
|
|
39
|
+
await socket.init();
|
|
40
|
+
resolve(socket);
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
reject(error);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
/**
|
|
47
|
+
*
|
|
48
|
+
* @param {string} url
|
|
49
|
+
* @param {string} networkVersion network/testnet-network sepperate by -
|
|
50
|
+
* @returns Promise(boolean)
|
|
51
|
+
*/
|
|
52
|
+
const getWS = async (url, networkVersion) => {
|
|
53
|
+
try {
|
|
54
|
+
const ws = await tryWs(url, networkVersion);
|
|
55
|
+
return ws;
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
// chain is undefined when mode is remote
|
|
62
|
+
// endpoints contain urls to connect to the desired remote
|
|
63
|
+
// when mode is remote means an instance is already running
|
|
64
|
+
// when mode is direct means chain is directly available and no endpoint is needed to interact with it
|
|
65
|
+
/**
|
|
66
|
+
*
|
|
67
|
+
* @param {object} options { ws: boolean || {url: string, port: number}, http: boolean || {url: string, port: number}, network}
|
|
68
|
+
* @returns '{ mode: string, endpoints: object, chain}'
|
|
69
|
+
*/
|
|
70
|
+
const launch = async (options, password) => {
|
|
71
|
+
if (!options)
|
|
72
|
+
options = defaultOptions;
|
|
73
|
+
else
|
|
74
|
+
options = { ...defaultOptions, ...options };
|
|
75
|
+
const availableEndpoints = {
|
|
76
|
+
http: [],
|
|
77
|
+
ws: []
|
|
78
|
+
};
|
|
79
|
+
const availableClients = {
|
|
80
|
+
http: [],
|
|
81
|
+
ws: []
|
|
82
|
+
};
|
|
83
|
+
if (options.http) {
|
|
84
|
+
for (const endpoint of options.http) {
|
|
85
|
+
if (endpoint.port && !endpoint.url)
|
|
86
|
+
endpoint.url = `http://localhost:${endpoint.port}`;
|
|
87
|
+
const client = await getHttp(endpoint.url, options.networkVersion);
|
|
88
|
+
if (client)
|
|
89
|
+
availableEndpoints.http.push(endpoint.url) && availableClients.http.push({ url: endpoint.url, client });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (options.ws) {
|
|
93
|
+
for (const endpoint of options.ws) {
|
|
94
|
+
if (endpoint.port && !endpoint.url)
|
|
95
|
+
endpoint.url = `ws://localhost:${endpoint.port}`;
|
|
96
|
+
const client = await getWS(endpoint.url, options.networkVersion);
|
|
97
|
+
if (client)
|
|
98
|
+
availableEndpoints.ws.push(endpoint.url) && availableClients.ws.push({ url: endpoint.url, client });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const endpoints = {
|
|
102
|
+
http: [],
|
|
103
|
+
ws: []
|
|
104
|
+
};
|
|
105
|
+
let chain;
|
|
106
|
+
let mode;
|
|
107
|
+
if (availableEndpoints.http.length > 0 || availableEndpoints.ws.length > 0) {
|
|
108
|
+
availableEndpoints.http.forEach(endpoint => {
|
|
109
|
+
endpoints.http.push(endpoint);
|
|
110
|
+
});
|
|
111
|
+
availableEndpoints.ws.forEach(endpoint => {
|
|
112
|
+
endpoints.ws.push(endpoint);
|
|
113
|
+
});
|
|
114
|
+
mode = 'remote';
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
if (options.forceRemote)
|
|
118
|
+
throw new Error(`forceRemote was set but no remotes connected`);
|
|
119
|
+
await new Node({ network: options.network, stars: options.stars, networkVersion: options.networkVersion }, password);
|
|
120
|
+
await nodeConfig({ network: options.network, stars: options.stars, networkVersion: options.networkVersion });
|
|
121
|
+
chain = await new Chain();
|
|
122
|
+
if (options.ws) {
|
|
123
|
+
const importee = await import('@leofcoin/endpoints/ws');
|
|
124
|
+
const wsServer = importee.default;
|
|
125
|
+
await wsServer(chain, options.ws[0].port, options.networkVersion);
|
|
126
|
+
endpoints.ws.push(options.ws[0].url);
|
|
127
|
+
}
|
|
128
|
+
if (options.http) {
|
|
129
|
+
const importee = await import('@leofcoin/endpoints/http');
|
|
130
|
+
const httpServer = importee.default;
|
|
131
|
+
await httpServer(chain, options.http[0].port, options.networkVersion);
|
|
132
|
+
endpoints.http.push(options.http[0].url);
|
|
133
|
+
}
|
|
134
|
+
mode = 'direct';
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
chain,
|
|
138
|
+
mode,
|
|
139
|
+
endpoints,
|
|
140
|
+
clients: availableClients
|
|
141
|
+
};
|
|
133
142
|
};
|
|
134
143
|
|
|
135
144
|
export { launch as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/launch-chain",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./index.js"
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"homepage": "https://github.com/leofcoin/launch-chain#readme",
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@leofcoin/chain": "^1.4.8",
|
|
27
|
+
"@leofcoin/endpoint-clients": "^0.2.3",
|
|
27
28
|
"@leofcoin/endpoints": "^0.2.0"
|
|
28
29
|
},
|
|
29
30
|
"devDependencies": {
|
package/rollup.config.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import typescript from '@rollup/plugin-typescript';
|
|
2
|
-
import tsconfig from './tsconfig.json' assert { type: 'json'};
|
|
3
|
-
|
|
4
|
-
export default [{
|
|
5
|
-
input: ['./src/index.ts'],
|
|
6
|
-
output: {
|
|
7
|
-
format: 'es',
|
|
8
|
-
dir: './'
|
|
9
|
-
},
|
|
10
|
-
plugins: [
|
|
11
|
-
typescript(tsconfig)
|
|
12
|
-
]
|
|
1
|
+
import typescript from '@rollup/plugin-typescript';
|
|
2
|
+
import tsconfig from './tsconfig.json' assert { type: 'json'};
|
|
3
|
+
|
|
4
|
+
export default [{
|
|
5
|
+
input: ['./src/index.ts'],
|
|
6
|
+
output: {
|
|
7
|
+
format: 'es',
|
|
8
|
+
dir: './'
|
|
9
|
+
},
|
|
10
|
+
plugins: [
|
|
11
|
+
typescript(tsconfig)
|
|
12
|
+
]
|
|
13
13
|
}]
|
package/src/index.ts
CHANGED
|
@@ -1,178 +1,196 @@
|
|
|
1
|
-
import Node from '@leofcoin/chain/node'
|
|
2
|
-
import Chain from '@leofcoin/chain/chain'
|
|
3
|
-
import nodeConfig from '@leofcoin/lib/node-config'
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
type
|
|
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
|
-
ws
|
|
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
|
-
} catch (error) {
|
|
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
|
-
chain
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
1
|
+
import Node from '@leofcoin/chain/node'
|
|
2
|
+
import Chain from '@leofcoin/chain/chain'
|
|
3
|
+
import nodeConfig from '@leofcoin/lib/node-config'
|
|
4
|
+
import WSClient from '@leofcoin/endpoint-clients/ws'
|
|
5
|
+
import HttpClient from '@leofcoin/endpoint-clients/http'
|
|
6
|
+
|
|
7
|
+
type launchMode = 'direct' | 'remote'
|
|
8
|
+
|
|
9
|
+
type endpointReturns = {
|
|
10
|
+
http?: string[],
|
|
11
|
+
ws?: string[],
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type clientReturns = {
|
|
15
|
+
http?: HttpClient[],
|
|
16
|
+
ws?: WSClient[],
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type launchReturn = {
|
|
20
|
+
chain: Chain,
|
|
21
|
+
mode: launchMode,
|
|
22
|
+
endpoints: endpointReturns,
|
|
23
|
+
clients: clientReturns
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type endpointOptions = {
|
|
27
|
+
port: number,
|
|
28
|
+
url?: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
type launchOptions = {
|
|
32
|
+
network?: string,
|
|
33
|
+
networkVersion?: string,
|
|
34
|
+
stars: string[],
|
|
35
|
+
forceRemote: boolean,
|
|
36
|
+
mode?: launchMode,
|
|
37
|
+
ws?: endpointOptions[] | undefined,
|
|
38
|
+
http?: endpointOptions[] | undefined,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const defaultOptions:launchOptions = {
|
|
42
|
+
network: 'leofcoin:peach',
|
|
43
|
+
networkVersion: 'peach',
|
|
44
|
+
stars: ['wss://peach.leofcoin.org'],
|
|
45
|
+
forceRemote: false,
|
|
46
|
+
mode: 'direct',
|
|
47
|
+
ws: [{
|
|
48
|
+
port: 4040
|
|
49
|
+
}],
|
|
50
|
+
http: [{
|
|
51
|
+
port: 8080
|
|
52
|
+
}]
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
*
|
|
57
|
+
* @param {string} url
|
|
58
|
+
* @param {string} networkVersion network/testnet-network sepperate by -
|
|
59
|
+
* @returns Promise(boolean)
|
|
60
|
+
*/
|
|
61
|
+
const getHttp = async (url: string, networkVersion: string): Promise<undefined | HttpClient> => {
|
|
62
|
+
try {
|
|
63
|
+
const client = new HttpClient(url, networkVersion)
|
|
64
|
+
await client.network()
|
|
65
|
+
return client
|
|
66
|
+
} catch (error) {
|
|
67
|
+
return undefined
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const tryWs = (url: string, networkVersion: string): Promise<WSClient> => new Promise(async (resolve, reject) => {
|
|
72
|
+
try {
|
|
73
|
+
const socket = await new WSClient(url, networkVersion)
|
|
74
|
+
await socket.init()
|
|
75
|
+
resolve(socket)
|
|
76
|
+
} catch (error) {
|
|
77
|
+
reject(error)
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
*
|
|
83
|
+
* @param {string} url
|
|
84
|
+
* @param {string} networkVersion network/testnet-network sepperate by -
|
|
85
|
+
* @returns Promise(boolean)
|
|
86
|
+
*/
|
|
87
|
+
const getWS = async (url: string, networkVersion: string): Promise<WSClient> => {
|
|
88
|
+
try {
|
|
89
|
+
const ws = await tryWs(url, networkVersion)
|
|
90
|
+
return ws
|
|
91
|
+
} catch (error) {
|
|
92
|
+
return undefined
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
*
|
|
98
|
+
* @param {string} httpURL
|
|
99
|
+
* @param {string} wsURL
|
|
100
|
+
* @param {string} networkVersion
|
|
101
|
+
* @returns Promise({http: boolean, ws: boolean})
|
|
102
|
+
*/
|
|
103
|
+
const hasClient = async (httpURL: string, wsURL: string, networkVersion: string) => {
|
|
104
|
+
const ws = await getWS(wsURL, networkVersion)
|
|
105
|
+
const http = await getHttp(httpURL, networkVersion)
|
|
106
|
+
return {http, ws}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// chain is undefined when mode is remote
|
|
110
|
+
// endpoints contain urls to connect to the desired remote
|
|
111
|
+
// when mode is remote means an instance is already running
|
|
112
|
+
// when mode is direct means chain is directly available and no endpoint is needed to interact with it
|
|
113
|
+
/**
|
|
114
|
+
*
|
|
115
|
+
* @param {object} options { ws: boolean || {url: string, port: number}, http: boolean || {url: string, port: number}, network}
|
|
116
|
+
* @returns '{ mode: string, endpoints: object, chain}'
|
|
117
|
+
*/
|
|
118
|
+
const launch = async (options: launchOptions, password: string): Promise<launchReturn> => {
|
|
119
|
+
if (!options) options = defaultOptions
|
|
120
|
+
else options = {...defaultOptions, ...options }
|
|
121
|
+
|
|
122
|
+
const availableEndpoints: endpointReturns = {
|
|
123
|
+
http: [],
|
|
124
|
+
ws: []
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const availableClients: clientReturns = {
|
|
128
|
+
http: [],
|
|
129
|
+
ws: []
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (options.http) {
|
|
133
|
+
for (const endpoint of options.http) {
|
|
134
|
+
if (endpoint.port && !endpoint.url) endpoint.url = `http://localhost:${endpoint.port}`
|
|
135
|
+
const client = await getHttp(endpoint.url, options.networkVersion)
|
|
136
|
+
if (client) availableEndpoints.http.push(endpoint.url) && availableClients.http.push({url: endpoint.url, client})
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (options.ws) {
|
|
141
|
+
for (const endpoint of options.ws) {
|
|
142
|
+
if (endpoint.port && !endpoint.url) endpoint.url = `ws://localhost:${endpoint.port}`
|
|
143
|
+
const client = await getWS(endpoint.url, options.networkVersion)
|
|
144
|
+
if (client) availableEndpoints.ws.push(endpoint.url) && availableClients.ws.push({url: endpoint.url, client})
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const endpoints: endpointReturns = {
|
|
149
|
+
http: [],
|
|
150
|
+
ws: []
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
let chain: Chain
|
|
154
|
+
let mode: launchMode
|
|
155
|
+
|
|
156
|
+
if (availableEndpoints.http.length > 0 || availableEndpoints.ws.length > 0) {
|
|
157
|
+
availableEndpoints.http.forEach(endpoint => {
|
|
158
|
+
endpoints.http.push(endpoint)
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
availableEndpoints.ws.forEach(endpoint => {
|
|
162
|
+
endpoints.ws.push(endpoint)
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
mode = 'remote'
|
|
166
|
+
} else {
|
|
167
|
+
if (options.forceRemote) throw new Error(`forceRemote was set but no remotes connected`)
|
|
168
|
+
|
|
169
|
+
await new Node({ network: options.network, stars: options.stars, networkVersion: options.networkVersion }, password)
|
|
170
|
+
await nodeConfig({ network: options.network, stars: options.stars, networkVersion: options.networkVersion })
|
|
171
|
+
|
|
172
|
+
chain = await new Chain()
|
|
173
|
+
|
|
174
|
+
if (options.ws) {
|
|
175
|
+
const importee = await import('@leofcoin/endpoints/ws')
|
|
176
|
+
const wsServer = importee.default
|
|
177
|
+
await wsServer(chain, options.ws[0].port, options.networkVersion)
|
|
178
|
+
endpoints.ws.push(options.ws[0].url)
|
|
179
|
+
}
|
|
180
|
+
if (options.http) {
|
|
181
|
+
const importee = await import('@leofcoin/endpoints/http')
|
|
182
|
+
const httpServer = importee.default
|
|
183
|
+
await httpServer(chain, options.http[0].port, options.networkVersion)
|
|
184
|
+
endpoints.http.push(options.http[0].url)
|
|
185
|
+
}
|
|
186
|
+
mode = 'direct'
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
chain,
|
|
190
|
+
mode,
|
|
191
|
+
endpoints,
|
|
192
|
+
clients: availableClients
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
178
196
|
export { launch as default}
|
package/test.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import launch from './index.js'
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
network: 'leofcoin:peach',
|
|
5
|
-
networkVersion: 'peach'
|
|
6
|
-
|
|
1
|
+
import launch from './index.js'
|
|
2
|
+
|
|
3
|
+
const {chain, endpoints, clients, mode } = await launch({
|
|
4
|
+
network: 'leofcoin:peach',
|
|
5
|
+
networkVersion: 'peach',
|
|
6
|
+
forceRemote: true,
|
|
7
|
+
ws: [{ url:'wss://ws-remote.leofcoin.org'}],
|
|
8
|
+
http: [{ url: 'https://remote.leofcoin.org' }]
|
|
9
|
+
})
|
|
10
|
+
console.log(await clients.ws[0].client.networkStats());
|
package/tsconfig.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "
|
|
4
|
-
"module": "
|
|
5
|
-
"outDir": "./",
|
|
6
|
-
"moduleResolution":"
|
|
7
|
-
"allowJs": true,
|
|
8
|
-
"declaration": true
|
|
9
|
-
},
|
|
10
|
-
"include": [
|
|
11
|
-
"./src/**/*"
|
|
12
|
-
],
|
|
13
|
-
"exclude": ["./node_modules"]
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ESNext",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"outDir": "./",
|
|
6
|
+
"moduleResolution":"nodenext",
|
|
7
|
+
"allowJs": true,
|
|
8
|
+
"declaration": true
|
|
9
|
+
},
|
|
10
|
+
"include": [
|
|
11
|
+
"./src/**/*"
|
|
12
|
+
],
|
|
13
|
+
"exclude": ["./node_modules"]
|
|
14
14
|
}
|