Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hs-client: move all websocket calls under .ws namespace. #886

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"env": {
"es2020": true,
"es2022": true,
"node": true
},
"extends": "eslint:recommended",
Expand Down Expand Up @@ -35,7 +35,7 @@
}
],
"parserOptions": {
"ecmaVersion": 11,
"ecmaVersion": 13,
"ecmaFeatures": {
"globalReturn": true
},
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ process and allows parallel rescans.
- expects ws hook for `block rescan interactive` params `rawEntry, rawTXs`
that returns scanAction object.
- expects ws hook for `block rescan interactive abort` param `message`.
- Move all `websocket` APIs under `.ws` namespace.

### Wallet Changes
#### Configuration
Expand All @@ -63,6 +64,8 @@ process and allows parallel rescans.
exact fee.
- All transaction sending endpoints now fundlock/queue tx creation. (no more
conflicting transactions)
- Move all `websocket` APIs under `.ws` namespace.


## v6.0.0

Expand Down
5 changes: 4 additions & 1 deletion jsdoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"includePattern": ".+\\.js(doc)?$",
"excludePattern": "(^|\\/|\\\\)_"
},
"plugins": ["plugins/markdown"],
"plugins": [
"plugins/markdown",
"plugins/rm-imports"
],
"templates": {
"cleverLinks": false,
"monospaceLinks": false
Expand Down
17 changes: 14 additions & 3 deletions lib/client/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

const assert = require('bsert');
const {Client} = require('bcurl');
const WSClient = require('./wsclient');

/**
* Node Client
Expand All @@ -26,6 +27,8 @@ class NodeClient extends Client {

constructor(options) {
super(options);

this.ws = new NodeWSClient(this);
}

/**
Expand All @@ -34,9 +37,9 @@ class NodeClient extends Client {
*/

async auth() {
await this.call('auth', this.password);
await this.watchChain();
await this.watchMempool();
await this.ws.call('auth', this.password);
await this.ws.watchChain();
await this.ws.watchMempool();
}

/**
Expand Down Expand Up @@ -192,7 +195,15 @@ class NodeClient extends Client {
reset(height) {
return this.post('/reset', { height });
}
}

/**
* Node WS Client
* @alias module:client.NodeWSClient
* @extends {WSClient}
*/

class NodeWSClient extends WSClient {
/**
* Watch the blockchain.
* @private
Expand Down
94 changes: 52 additions & 42 deletions lib/client/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
const assert = require('bsert');
const EventEmitter = require('events');
const {Client} = require('bcurl');
const WSClient = require('./wsclient');

/**
* Wallet Client
Expand All @@ -28,6 +29,7 @@ class WalletClient extends Client {
constructor(options) {
super(options);
this.wallets = new Map();
this.ws = new WalletWSClient(this);
}

/**
Expand All @@ -37,31 +39,31 @@ class WalletClient extends Client {
*/

init() {
this.bind('tx', (id, details) => {
this.ws.bind('tx', (id, details) => {
this.dispatch(id, 'tx', details);
});

this.bind('confirmed', (id, details) => {
this.ws.bind('confirmed', (id, details) => {
this.dispatch(id, 'confirmed', details);
});

this.bind('unconfirmed', (id, details) => {
this.ws.bind('unconfirmed', (id, details) => {
this.dispatch(id, 'unconfirmed', details);
});

this.bind('conflict', (id, details) => {
this.ws.bind('conflict', (id, details) => {
this.dispatch(id, 'conflict', details);
});

this.bind('updated', (id, details) => {
this.ws.bind('updated', (id, details) => {
this.dispatch(id, 'updated', details);
});

this.bind('address', (id, receive) => {
this.ws.bind('address', (id, receive) => {
this.dispatch(id, 'address', receive);
});

this.bind('balance', (id, balance) => {
this.ws.bind('balance', (id, balance) => {
this.dispatch(id, 'balance', balance);
});
}
Expand Down Expand Up @@ -104,7 +106,7 @@ class WalletClient extends Client {
*/

async auth() {
await this.call('auth', this.password);
await this.ws.call('auth', this.password);
}

/**
Expand All @@ -124,38 +126,6 @@ class WalletClient extends Client {
return new Wallet(this, id, token);
}

/**
* Join a wallet.
*/

all(token) {
return this.call('join', '*', token);
}

/**
* Leave a wallet.
*/

none() {
return this.call('leave', '*');
}

/**
* Join a wallet.
*/

join(id, token) {
return this.call('join', id, token);
}

/**
* Leave a wallet.
*/

leave(id) {
return this.call('leave', id);
}

/**
* Rescan the chain.
* @param {Number} height
Expand Down Expand Up @@ -913,7 +883,7 @@ class Wallet extends EventEmitter {
*/

async open() {
await this.parent.join(this.id, this.token);
await this.parent.ws.join(this.id, this.token);
this.parent.wallets.set(this.id, this);
}

Expand All @@ -923,7 +893,7 @@ class Wallet extends EventEmitter {
*/

async close() {
await this.parent.leave(this.id);
await this.parent.ws.leave(this.id);
this.parent.wallets.delete(this.id);
}

Expand Down Expand Up @@ -1577,6 +1547,46 @@ class Wallet extends EventEmitter {
}
}

/**
* Wallet WS Client
* @alias module:client.WalletClient
* @extends {WSClient}
*/

class WalletWSClient extends WSClient {
/**
* Join a wallet.
*/

all(token) {
return this.call('join', '*', token);
}

/**
* Leave a wallet.
*/

none() {
return this.call('leave', '*');
}

/**
* Join a wallet.
*/

join(id, token) {
return this.call('join', id, token);
}

/**
* Leave a wallet.
*/

leave(id) {
return this.call('leave', id);
}
}

/*
* Expose
*/
Expand Down
124 changes: 124 additions & 0 deletions lib/client/wsclient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*!
* wsclient.js - WS Client for Node and Wallet.
* Copyright (c) 2024, Nodari Chkuaselidze (MIT License)
*/

'use strict';

/** @typedef {import('bcurl').Client} Client */
/** @typedef {import('bsock').Socket} Socket */

/**
* Websocket Client
* @alias module:client.WSClient
*/

class WSClient {
/** @type {Client} */
client;

/** @type {Socket} */
socket;

/**
* @param {Client} client
*/

constructor(client) {
this.client = client;
this.socket = client.socket;
}

/**
* @returns {Boolean}
*/

get opened() {
return this.client.opened;
}

/**
* Open websocket.
* @returns {Promise}
*/

async open() {
return this.client.open();
}

/**
* Close websocket.
* @returns {Promise}
*/

async close() {
return this.client.close();
}

/**
* Alias for hook.
* @param {String} event
* @param {Function} handler
*/

hook(event, handler) {
return this.client.hook(event, handler);
}

/**
* Alias for unhook.
* @param {String} event
* @param {Function} handler
*/

unhook(event, handler) {
return this.client.unhook(event, handler);
}

/**
* Alias call.
* @param {String} event
* @param {...*} args
* @returns {Promise}
*/

async call(event, ...args) {
return this.client.call(event, ...args);
}

/**
* Add an event listener.
* @param {String} event
* @param {Function} handler
*/

bind(event, handler) {
return this.socket.bind(event, handler);
}

/**
* Remove an event listener.
* @param {String} event
* @param {Function} handler
*/

unbind(event, handler) {
return this.socket.unbind(event, handler);
}

/**
* Fire an event.
* @param {String} event
* @param {...*} args
*/

fire(event, ...args) {
return this.socket.fire(event, ...args);
}
}

/*
* Expose
*/

module.exports = WSClient;
Loading
Loading