Skip to content

Commit 6bdc216

Browse files
authored
Merge pull request #1 from nkuba/websocket
- implemented web socket client - configured linter - fixed protocol methods - added simple tests - refactored and cleaned up code - commented out persistency strategy due to instability on testing
2 parents 01b4dd6 + 84afedc commit 6bdc216

28 files changed

+5673
-649
lines changed

.eslintrc.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
'env': {
3+
'browser': true,
4+
'es6': true,
5+
'node': true,
6+
},
7+
'extends': [
8+
'google',
9+
],
10+
'globals': {
11+
'Atomics': 'readonly',
12+
'SharedArrayBuffer': 'readonly',
13+
},
14+
'parserOptions': {
15+
'ecmaVersion': 2018,
16+
'sourceType': 'module',
17+
},
18+
'rules': {
19+
'indent': ['error', 2, { "SwitchCase": 1 }],
20+
'max-len': 0,
21+
'require-jsdoc': 0, // Added just temporarily
22+
'camelcase': 0,
23+
'semi': ["error", "never"],
24+
},
25+
};

README.md

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,61 @@
1-
# node-electrum-client
1+
# electrum-client-js
22

3-
Electrum Protocol Client for Node.js
3+
JavaScript implementation of [Electrum Protocol] Client.
44

5-
## Continuous Integration
5+
This is a library that can communicate with the [ElectrumX Server]
6+
on `tcp`, `ssl`, `ws` and `wss` protocols.
67

7-
Latest [CircleCI](.circleci/) build status:
8+
Works in node.js and browser.
9+
10+
Implements methods described in [Electrum Protocol methods] documentation.
811

9-
[![CircleCI](https://circleci.com/gh/nkuba/electrum-client-js.svg?style=svg)](https://circleci.com/gh/nkuba/electrum-client-js)
12+
Subscriptions and notifications are also supported, please see [example](example/subscribe.js).
13+
14+
## Continuous Integration
1015

11-
## what is this
16+
Latest [CircleCI](.circleci/) build status:
1217

13-
https://electrum.org/
18+
[![CircleCI build status](https://circleci.com/gh/nkuba/electrum-client-js.svg?style=svg)](https://circleci.com/gh/nkuba/electrum-client-js)
1419

15-
electrum is bitcoin wallet service.
16-
This is a library of Node.js that can communicate with the electrum(x) server.
1720

18-
## install
21+
## Install
1922

2023
```
21-
npm i electrum-client
24+
npm install --save nkuba/electrum-client-js
2225
```
2326

24-
## spec
27+
## Usage
2528

26-
* TCP / TLS
27-
* JSON-RPC
28-
* Subscribe Message
29-
* High Performance Message
30-
* no dependency for other library
29+
```js
30+
const ElectrumClient = require('electrum-client-js')
3131

32-
## protocol spec
32+
async function main() {
33+
const client = new ElectrumClient(
34+
'fortress.qtornado.com',
35+
50002,
36+
'ssl'
37+
)
3338

34-
* https://electrumx.readthedocs.io/en/latest/PROTOCOL.html
39+
try {
40+
await client.connect(
41+
'electrum-client-js', // optional client name
42+
'1.4.2' // optional protocol version
43+
)
3544

36-
## usage
45+
const header = await client.blockchain_headers_subscribe()
46+
console.log('Current header:', header)
3747

38-
```
39-
const ElectrumCli = require('electrum-client')
40-
const main = async () => {
41-
const ecl = new ElectrumCli(995, 'btc.smsys.me', 'tls') // tcp or tls
42-
await ecl.connect() // connect(promise)
43-
ecl.subscribe.on('blockchain.headers.subscribe', (v) => console.log(v)) // subscribe message(EventEmitter)
44-
try{
45-
const ver = await ecl.server_version("2.7.11", "1.0") // json-rpc(promise)
46-
console.log(ver)
47-
}catch(e){
48-
console.log(e)
49-
}
50-
await ecl.close() // disconnect(promise)
48+
await client.close()
49+
} catch (err) {
50+
console.error(err)
51+
}
5152
}
53+
5254
main()
5355
```
56+
See more [examples](example/).
57+
58+
59+
[Electrum Protocol]: https://electrumx.readthedocs.io/en/latest/protocol.html
60+
[Electrum Protocol methods]: https://electrumx.readthedocs.io/en/latest/protocol-methods.html
61+
[ElectrumX Server]: https://electrumx.readthedocs.io/en/latest/

example/bitcoin.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
1-
const ElectrumClient = require("..")
1+
const ElectrumClient = require('..')
22

3-
4-
const peers = require('electrum-host-parse').getDefaultPeers("BitcoinSegwit").filter(v => v.ssl)
5-
const getRandomPeer = () => peers[peers.length * Math.random() | 0]
3+
const config = {
4+
host: 'fortress.qtornado.com',
5+
port: 50002,
6+
protocol: 'ssl',
7+
}
68

79
const main = async () => {
8-
const peer = getRandomPeer()
9-
console.log('begin connection: ' + JSON.stringify(peer))
10-
const ecl = new ElectrumClient(peer.ssl, peer.host, 'ssl')
11-
await ecl.connect()
12-
try{
13-
const ver = await ecl.server_version("2.7.11", "1.0")
14-
console.log(ver)
15-
const balance = await ecl.blockchainAddress_getBalance("12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX")
16-
console.log(balance)
17-
const unspent = await ecl.blockchainAddress_listunspent("12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX")
18-
console.log(unspent)
19-
}catch(e){
20-
console.log(e)
21-
}
22-
await ecl.close()
10+
console.log('Connecting...')
11+
const client = new ElectrumClient(config.host, config.port, config.protocol)
12+
13+
await client.connect()
14+
15+
try {
16+
const ver = await client.server_version('electrum-client-js', '1.4')
17+
console.log('Negotiated version:', ver)
18+
19+
const balance = await client.blockchain_scripthash_getBalance('740485f380ff6379d11ef6fe7d7cdd68aea7f8bd0d953d9fdf3531fb7d531833')
20+
console.log('Balance:', balance)
21+
22+
const unspent = await client.blockchain_scripthash_listunspent('740485f380ff6379d11ef6fe7d7cdd68aea7f8bd0d953d9fdf3531fb7d531833')
23+
console.log('Unspent:', unspent)
24+
} catch (e) {
25+
console.error(e)
26+
}
27+
28+
await client.close()
2329
}
24-
main().catch(console.log)
30+
31+
main().catch(console.error)

example/ex.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

example/example.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const ElectrumClient = require('..')
2+
3+
async function main() {
4+
const client = new ElectrumClient(
5+
'fortress.qtornado.com',
6+
50002,
7+
'ssl'
8+
)
9+
10+
try {
11+
await client.connect(
12+
'electrum-client-js', // optional client name
13+
'1.4.2' // optional protocol version
14+
)
15+
16+
const header = await client.blockchain_headers_subscribe()
17+
console.log('Current header:', header)
18+
19+
await client.close()
20+
} catch (err) {
21+
console.error(err)
22+
}
23+
}
24+
25+
main()
Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,46 @@
1-
'use strict';
2-
const ElectrumClient = require('..');
1+
'use strict'
2+
const ElectrumClient = require('..')
33

44
const createRaiiClient = (port, host, protocol, options) => {
5-
return (params, promise) => {
6-
const name = params.join(':')
7-
const client = new ElectrumClient(port, host, protocol, options)
8-
console.time(name)
9-
return client.connect().then( () => {
10-
return promise(client)
11-
}).catch( e => {
12-
client.close()
13-
console.timeEnd(name)
14-
throw e
15-
}).then( res => {
16-
client.close()
17-
console.timeEnd(name)
18-
return res
19-
})
20-
}
5+
return (params, promise) => {
6+
const name = params.join(':')
7+
const client = new ElectrumClient(port, host, protocol, options)
218

9+
console.time(name)
10+
11+
return client.connect()
12+
.then(() => {
13+
return promise(client)
14+
}).catch((e) => {
15+
client.close()
16+
console.timeEnd(name)
17+
throw e
18+
}).then((res) => {
19+
client.close()
20+
console.timeEnd(name)
21+
return res
22+
})
23+
}
2224
}
2325

24-
const main = async(hex) => {
25-
const hosts = ['electrum-mona.bitbank.cc', 'electrumx.tamami-foundation.org']
26-
const host = hosts[Math.floor(Math.random() * hosts.length)]
27-
const connect = createRaiiClient(50001, host, 'tcp')
28-
await connect(['blockchainTransaction_broadcast', hex], async(client) => {
29-
const ver = await client.server_version('2.7.11', '1.0')
30-
console.log(ver)
31-
const result = await client.blockchainTransaction_broadcast(hex)
32-
console.log(result)
33-
})
26+
const main = async (hex) => {
27+
const hosts = ['fortress.qtornado.com', 'electrumx.tamami-foundation.org']
28+
29+
const host = hosts[Math.floor(Math.random() * hosts.length)]
30+
31+
const connect = createRaiiClient(host, 50001, 'tcp')
32+
33+
await connect(['blockchain_transaction_broadcast', hex], async (client) => {
34+
const ver = await client.server_version('2.7.11', '1.4')
35+
console.log('Version:', ver)
36+
37+
const result = await client.blockchain_transaction_broadcast(hex)
38+
console.log('Result:', result)
39+
})
3440
}
3541

3642
const getopt = () => {
37-
return process.argv.slice(2)[0]
43+
return process.argv.slice(2)[0]
3844
}
3945

4046
main(getopt()).catch(console.log)

example/raii_timeout.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

example/scripthash.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
const ElectrumClient = require('..')
22

33
const main = async () => {
4-
const ecl = new ElectrumClient(50002, 'bitcoins.sk', 'tls')
5-
await ecl.connect()
6-
try{
7-
const ver = await ecl.server_version("3.0.5", "1.1")
8-
console.log(ver)
9-
const balance = await ecl.blockchainScripthash_getBalance("676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c")
10-
console.log(balance)
11-
const unspent = await ecl.blockchainScripthash_listunspent("676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c")
12-
console.log(unspent)
13-
const history = await ecl.blockchainScripthash_getHistory("676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c")
14-
console.log(history)
15-
const mempool = await ecl.blockchainScripthash_getMempool("676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c")
16-
console.log(mempool)
17-
}catch(e){
18-
console.log(e)
19-
}
20-
await ecl.close()
4+
const ecl = new ElectrumClient('fortress.qtornado.com', 50002, 'tls')
5+
await ecl.connect()
6+
try {
7+
const ver = await ecl.server_version('3.0.5', '1.4')
8+
console.log(ver)
9+
const balance = await ecl.blockchain_scripthash_getBalance('676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c')
10+
console.log(balance)
11+
const unspent = await ecl.blockchain_scripthash_listunspent('676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c')
12+
console.log(unspent)
13+
const history = await ecl.blockchain_scripthash_getHistory('676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c')
14+
console.log(history)
15+
const mempool = await ecl.blockchain_scripthash_getMempool('676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c')
16+
console.log(mempool)
17+
} catch (e) {
18+
console.log(e)
19+
}
20+
await ecl.close()
2121
}
2222
main().catch(console.log)

0 commit comments

Comments
 (0)