@electrum-cash/network 3.2.0-development.5350390388-r2-development.5350390388
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 +21 -0
- package/README.md +150 -0
- package/dist/index.cjs +1623 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +605 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +1597 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +78 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 GeneralProtocols
|
|
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
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# electrum-cash
|
|
2
|
+
|
|
3
|
+
Electrum-cash is a lightweight `JavaScript` library that lets you connect with one or more `Electrum` servers.
|
|
4
|
+
It offers encrypted connections by default,
|
|
5
|
+
performs the expected protocol version negotiation and
|
|
6
|
+
automatically keeps your connection alive until your close it.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
Install the library with NPM:
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
# npm install electrum-cash
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
### Load library
|
|
19
|
+
|
|
20
|
+
Before you can use the library you need to include it in your project.
|
|
21
|
+
|
|
22
|
+
If you only want to use a **single server**, load the `ElectrumClient` module:
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
// Load the electrum library.
|
|
26
|
+
const { ElectrumClient } = require('electrum-cash');
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
If you want to use **multiple servers**, load the `ElectrumCluster` module:
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
// Load the electrum library.
|
|
33
|
+
const { ElectrumCluster } = require('electrum-cash');
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Connect to servers
|
|
37
|
+
|
|
38
|
+
After you have loaded the appropriate module you need to initialize the module by configuring your **application identifier** and **protocol version**.
|
|
39
|
+
|
|
40
|
+
If you only want to use a single server, initialize an `ElectrumClient` and connect to the server:
|
|
41
|
+
```js
|
|
42
|
+
// Initialize an electrum client.
|
|
43
|
+
const electrum = new ElectrumClient('Electrum client example', '1.4.1', 'bch.imaginary.cash');
|
|
44
|
+
|
|
45
|
+
// Wait for the client to connect
|
|
46
|
+
await electrum.connect();
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
If you want to use multiple servers, initialize an `ElectrumCluster` and add some servers:
|
|
50
|
+
|
|
51
|
+
*For more information on various cluster configurations, read the [cluster documentation](https://read.cash/@JonathanSilverblood/electrum-cash-strategic-use-of-clusters-83743111).*
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
// Initialize an electrum cluster where 2 out of 3 needs to be consistent, polled randomly with fail-over (default).
|
|
55
|
+
const electrum = new ElectrumCluster('Electrum cluster example', '1.4.1', 2, 3);
|
|
56
|
+
|
|
57
|
+
// Add some servers to the cluster.
|
|
58
|
+
electrum.addServer('bch.imaginary.cash');
|
|
59
|
+
electrum.addServer('electroncash.de');
|
|
60
|
+
electrum.addServer('electroncash.dk');
|
|
61
|
+
electrum.addServer('electron.jochen-hoenicke.de', 51002);
|
|
62
|
+
electrum.addServer('electrum.imaginary.cash');
|
|
63
|
+
|
|
64
|
+
// Wait for enough connections to be available.
|
|
65
|
+
await electrum.ready();
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Request information
|
|
69
|
+
|
|
70
|
+
Once your `ElectrumClient` or `ElectrumCluster` is connected and ready, you can call methods:
|
|
71
|
+
|
|
72
|
+
*For a list of methods you can use, refer to the [Electrum Cash documentation](https://bitcoincash.network/electrum/).*
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
// Declare an example transaction ID.
|
|
76
|
+
const transactionID = '4db095f34d632a4daf942142c291f1f2abb5ba2e1ccac919d85bdc2f671fb251';
|
|
77
|
+
|
|
78
|
+
// Request the full transaction hex for the transaction ID.
|
|
79
|
+
const transactionHex = await electrum.request('blockchain.transaction.get', transactionID);
|
|
80
|
+
|
|
81
|
+
// Print out the transaction hex.
|
|
82
|
+
console.log(transactionHex);
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Subscribe to notifications.
|
|
86
|
+
|
|
87
|
+
Once your `ElectrumClient` or `ElectrumCluster` is connected and ready, you can set up subscriptions to get notifications on events:
|
|
88
|
+
|
|
89
|
+
*For a list of methods you can subscribe to, refer to the [Electrum Cash documentation](https://bitcoincash.network/electrum/).*
|
|
90
|
+
|
|
91
|
+
```js
|
|
92
|
+
// Set up a callback function to handle new blocks.
|
|
93
|
+
const handleNotifications = function(data)
|
|
94
|
+
{
|
|
95
|
+
if(data.method === 'blockchain.headers.subscribe')
|
|
96
|
+
{
|
|
97
|
+
// Print out the block information.
|
|
98
|
+
// {
|
|
99
|
+
// jsonrpc: '2.0',
|
|
100
|
+
// method: 'blockchain.headers.subscribe',
|
|
101
|
+
// params:
|
|
102
|
+
// [
|
|
103
|
+
// {
|
|
104
|
+
// height: 797111,
|
|
105
|
+
// hex: '002001202a6b1367f68201ad957e95bec9bda3f132ca2fcb75c0c000000000000000000074befba60bd8615d87ddb636aa99bc032cec8db3adb0d915d45391bc811c1e9ceacf89647a60051819a79559'
|
|
106
|
+
// }
|
|
107
|
+
// ]
|
|
108
|
+
// }
|
|
109
|
+
console.log(data);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Listen for notifications.
|
|
114
|
+
electrum.on('notification', handleNotifications);
|
|
115
|
+
|
|
116
|
+
// Set up a subscription for new block headers.
|
|
117
|
+
await electrum.subscribe('blockchain.headers.subscribe');
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Shutting down
|
|
121
|
+
|
|
122
|
+
When you're done and don't want to be connected anymore you can disconnect the server(s).
|
|
123
|
+
|
|
124
|
+
If you're using a single `ElectrumClient`, call the `disconnect()` function:
|
|
125
|
+
|
|
126
|
+
```js
|
|
127
|
+
// Close the connection.
|
|
128
|
+
await electrum.disconnect();
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
If you're using a `ElectrumCluster` with multiple servers, call the `shutdown()` function.
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
// Close all connections.
|
|
135
|
+
await electrum.shutdown();
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Documentation
|
|
139
|
+
|
|
140
|
+
For a complete list of methods and parameters, read the [API documentation](https://generalprotocols.gitlab.io/electrum-cash/library/).
|
|
141
|
+
|
|
142
|
+
## Support and communication
|
|
143
|
+
|
|
144
|
+
If you need help with how to use the library or just want to talk about electrum-cash, you can find us on [Telegram](https://t.me/electrumcash) and [Discord](https://discord.gg/ZjXQzew).
|
|
145
|
+
|
|
146
|
+
You can also read our tutorials on [read.cash](https://read.cash/c/electrum-cash-f45e), or share your own.
|
|
147
|
+
|
|
148
|
+
## Notes
|
|
149
|
+
|
|
150
|
+
The keep-alive functionality of this library only works when the protocol version is 1.2 or higher.
|