Skip to content

Commit

Permalink
Merge pull request #1 from nkuba/websocket
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
nkuba authored Aug 20, 2019
2 parents 01b4dd6 + 84afedc commit 6bdc216
Show file tree
Hide file tree
Showing 28 changed files with 5,673 additions and 649 deletions.
25 changes: 25 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
'env': {
'browser': true,
'es6': true,
'node': true,
},
'extends': [
'google',
],
'globals': {
'Atomics': 'readonly',
'SharedArrayBuffer': 'readonly',
},
'parserOptions': {
'ecmaVersion': 2018,
'sourceType': 'module',
},
'rules': {
'indent': ['error', 2, { "SwitchCase": 1 }],
'max-len': 0,
'require-jsdoc': 0, // Added just temporarily
'camelcase': 0,
'semi': ["error", "never"],
},
};
74 changes: 41 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,61 @@
# node-electrum-client
# electrum-client-js

Electrum Protocol Client for Node.js
JavaScript implementation of [Electrum Protocol] Client.

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

Latest [CircleCI](.circleci/) build status:
Works in node.js and browser.

Implements methods described in [Electrum Protocol methods] documentation.

[![CircleCI](https://circleci.com/gh/nkuba/electrum-client-js.svg?style=svg)](https://circleci.com/gh/nkuba/electrum-client-js)
Subscriptions and notifications are also supported, please see [example](example/subscribe.js).

## Continuous Integration

## what is this
Latest [CircleCI](.circleci/) build status:

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

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

## install
## Install

```
npm i electrum-client
npm install --save nkuba/electrum-client-js
```

## spec
## Usage

* TCP / TLS
* JSON-RPC
* Subscribe Message
* High Performance Message
* no dependency for other library
```js
const ElectrumClient = require('electrum-client-js')

## protocol spec
async function main() {
const client = new ElectrumClient(
'fortress.qtornado.com',
50002,
'ssl'
)

* https://electrumx.readthedocs.io/en/latest/PROTOCOL.html
try {
await client.connect(
'electrum-client-js', // optional client name
'1.4.2' // optional protocol version
)

## usage
const header = await client.blockchain_headers_subscribe()
console.log('Current header:', header)

```
const ElectrumCli = require('electrum-client')
const main = async () => {
const ecl = new ElectrumCli(995, 'btc.smsys.me', 'tls') // tcp or tls
await ecl.connect() // connect(promise)
ecl.subscribe.on('blockchain.headers.subscribe', (v) => console.log(v)) // subscribe message(EventEmitter)
try{
const ver = await ecl.server_version("2.7.11", "1.0") // json-rpc(promise)
console.log(ver)
}catch(e){
console.log(e)
}
await ecl.close() // disconnect(promise)
await client.close()
} catch (err) {
console.error(err)
}
}

main()
```
See more [examples](example/).


[Electrum Protocol]: https://electrumx.readthedocs.io/en/latest/protocol.html
[Electrum Protocol methods]: https://electrumx.readthedocs.io/en/latest/protocol-methods.html
[ElectrumX Server]: https://electrumx.readthedocs.io/en/latest/
47 changes: 27 additions & 20 deletions example/bitcoin.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
const ElectrumClient = require("..")
const ElectrumClient = require('..')


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

const main = async () => {
const peer = getRandomPeer()
console.log('begin connection: ' + JSON.stringify(peer))
const ecl = new ElectrumClient(peer.ssl, peer.host, 'ssl')
await ecl.connect()
try{
const ver = await ecl.server_version("2.7.11", "1.0")
console.log(ver)
const balance = await ecl.blockchainAddress_getBalance("12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX")
console.log(balance)
const unspent = await ecl.blockchainAddress_listunspent("12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX")
console.log(unspent)
}catch(e){
console.log(e)
}
await ecl.close()
console.log('Connecting...')
const client = new ElectrumClient(config.host, config.port, config.protocol)

await client.connect()

try {
const ver = await client.server_version('electrum-client-js', '1.4')
console.log('Negotiated version:', ver)

const balance = await client.blockchain_scripthash_getBalance('740485f380ff6379d11ef6fe7d7cdd68aea7f8bd0d953d9fdf3531fb7d531833')
console.log('Balance:', balance)

const unspent = await client.blockchain_scripthash_listunspent('740485f380ff6379d11ef6fe7d7cdd68aea7f8bd0d953d9fdf3531fb7d531833')
console.log('Unspent:', unspent)
} catch (e) {
console.error(e)
}

await client.close()
}
main().catch(console.log)

main().catch(console.error)
25 changes: 0 additions & 25 deletions example/ex.js

This file was deleted.

25 changes: 25 additions & 0 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const ElectrumClient = require('..')

async function main() {
const client = new ElectrumClient(
'fortress.qtornado.com',
50002,
'ssl'
)

try {
await client.connect(
'electrum-client-js', // optional client name
'1.4.2' // optional protocol version
)

const header = await client.blockchain_headers_subscribe()
console.log('Current header:', header)

await client.close()
} catch (err) {
console.error(err)
}
}

main()
64 changes: 35 additions & 29 deletions example/raii_client_transaction_send.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
'use strict';
const ElectrumClient = require('..');
'use strict'
const ElectrumClient = require('..')

const createRaiiClient = (port, host, protocol, options) => {
return (params, promise) => {
const name = params.join(':')
const client = new ElectrumClient(port, host, protocol, options)
console.time(name)
return client.connect().then( () => {
return promise(client)
}).catch( e => {
client.close()
console.timeEnd(name)
throw e
}).then( res => {
client.close()
console.timeEnd(name)
return res
})
}
return (params, promise) => {
const name = params.join(':')
const client = new ElectrumClient(port, host, protocol, options)

console.time(name)

return client.connect()
.then(() => {
return promise(client)
}).catch((e) => {
client.close()
console.timeEnd(name)
throw e
}).then((res) => {
client.close()
console.timeEnd(name)
return res
})
}
}

const main = async(hex) => {
const hosts = ['electrum-mona.bitbank.cc', 'electrumx.tamami-foundation.org']
const host = hosts[Math.floor(Math.random() * hosts.length)]
const connect = createRaiiClient(50001, host, 'tcp')
await connect(['blockchainTransaction_broadcast', hex], async(client) => {
const ver = await client.server_version('2.7.11', '1.0')
console.log(ver)
const result = await client.blockchainTransaction_broadcast(hex)
console.log(result)
})
const main = async (hex) => {
const hosts = ['fortress.qtornado.com', 'electrumx.tamami-foundation.org']

const host = hosts[Math.floor(Math.random() * hosts.length)]

const connect = createRaiiClient(host, 50001, 'tcp')

await connect(['blockchain_transaction_broadcast', hex], async (client) => {
const ver = await client.server_version('2.7.11', '1.4')
console.log('Version:', ver)

const result = await client.blockchain_transaction_broadcast(hex)
console.log('Result:', result)
})
}

const getopt = () => {
return process.argv.slice(2)[0]
return process.argv.slice(2)[0]
}

main(getopt()).catch(console.log)
40 changes: 0 additions & 40 deletions example/raii_timeout.js

This file was deleted.

34 changes: 17 additions & 17 deletions example/scripthash.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const ElectrumClient = require('..')

const main = async () => {
const ecl = new ElectrumClient(50002, 'bitcoins.sk', 'tls')
await ecl.connect()
try{
const ver = await ecl.server_version("3.0.5", "1.1")
console.log(ver)
const balance = await ecl.blockchainScripthash_getBalance("676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c")
console.log(balance)
const unspent = await ecl.blockchainScripthash_listunspent("676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c")
console.log(unspent)
const history = await ecl.blockchainScripthash_getHistory("676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c")
console.log(history)
const mempool = await ecl.blockchainScripthash_getMempool("676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c")
console.log(mempool)
}catch(e){
console.log(e)
}
await ecl.close()
const ecl = new ElectrumClient('fortress.qtornado.com', 50002, 'tls')
await ecl.connect()
try {
const ver = await ecl.server_version('3.0.5', '1.4')
console.log(ver)
const balance = await ecl.blockchain_scripthash_getBalance('676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c')
console.log(balance)
const unspent = await ecl.blockchain_scripthash_listunspent('676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c')
console.log(unspent)
const history = await ecl.blockchain_scripthash_getHistory('676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c')
console.log(history)
const mempool = await ecl.blockchain_scripthash_getMempool('676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c')
console.log(mempool)
} catch (e) {
console.log(e)
}
await ecl.close()
}
main().catch(console.log)
Loading

0 comments on commit 6bdc216

Please sign in to comment.