• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

handshake-org / hsd / 3953908740

pending completion
3953908740

push

github

Nodari Chkuaselidze
pkg: Release v5.0.0

7392 of 12654 branches covered (58.42%)

Branch coverage included in aggregate %.

23565 of 32581 relevant lines covered (72.33%)

31610.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.38
/lib/wallet/nodeclient.js
1
/*!
2
 * nodeclient.js - node client for hsd
3
 * Copyright (c) 2017-2018, Christopher Jeffrey (MIT License).
4
 * https://github.com/handshake-org/hsd
5
 */
6

7
'use strict';
8

9
const assert = require('bsert');
1✔
10
const AsyncEmitter = require('bevent');
1✔
11

12
/**
13
 * Node Client
14
 * @alias module:node.NodeClient
15
 */
16

17
class NodeClient extends AsyncEmitter {
18
  /**
19
   * Create a node client.
20
   * @constructor
21
   */
22

23
  constructor(node) {
24
    super();
23✔
25

26
    this.node = node;
23✔
27
    this.network = node.network;
23✔
28
    this.filter = null;
23✔
29
    this.opened = false;
23✔
30

31
    this.init();
23✔
32
  }
33

34
  /**
35
   * Initialize the client.
36
   */
37

38
  init() {
39
    this.node.chain.on('connect', async (entry, block) => {
23✔
40
      if (!this.opened)
2,265!
41
        return;
×
42

43
      await this.emitAsync('block connect', entry, block.txs);
2,265✔
44
    });
45

46
    this.node.chain.on('disconnect', async (entry, block) => {
23✔
47
      if (!this.opened)
33!
48
        return;
×
49

50
      await this.emitAsync('block disconnect', entry);
33✔
51
    });
52

53
    this.node.on('tx', (tx) => {
23✔
54
      if (!this.opened)
324!
55
        return;
×
56

57
      this.emit('tx', tx);
324✔
58
    });
59

60
    this.node.on('reset', (tip) => {
23✔
61
      if (!this.opened)
3!
62
        return;
×
63

64
      this.emit('chain reset', tip);
3✔
65
    });
66
  }
67

68
  /**
69
   * Open the client.
70
   * @returns {Promise}
71
   */
72

73
  async open(options) {
74
    assert(!this.opened, 'NodeClient is already open.');
23✔
75
    this.opened = true;
23✔
76
    setImmediate(() => this.emit('connect'));
23✔
77
  }
78

79
  /**
80
   * Close the client.
81
   * @returns {Promise}
82
   */
83

84
  async close() {
85
    assert(this.opened, 'NodeClient is not open.');
23✔
86
    this.opened = false;
23✔
87
    setImmediate(() => this.emit('disconnect'));
23✔
88
  }
89

90
  /**
91
   * Add a listener.
92
   * @param {String} type
93
   * @param {Function} handler
94
   */
95

96
  bind(type, handler) {
97
    return this.on(type, handler);
92✔
98
  }
99

100
  /**
101
   * Add a listener.
102
   * @param {String} type
103
   * @param {Function} handler
104
   */
105

106
  hook(type, handler) {
107
    return this.on(type, handler);
23✔
108
  }
109

110
  /**
111
   * Get chain tip.
112
   * @returns {Promise}
113
   */
114

115
  async getTip() {
116
    return this.node.chain.tip;
×
117
  }
118

119
  /**
120
   * Get chain entry.
121
   * @param {Hash} hash
122
   * @returns {Promise}
123
   */
124

125
  async getEntry(hash) {
126
    const entry = await this.node.chain.getEntry(hash);
23✔
127

128
    if (!entry)
23!
129
      return null;
×
130

131
    if (!await this.node.chain.isMainChain(entry))
23!
132
      return null;
×
133

134
    return entry;
23✔
135
  }
136

137
  /**
138
   * Send a transaction. Do not wait for promise.
139
   * @param {TX} tx
140
   * @returns {Promise}
141
   */
142

143
  async send(tx) {
144
    this.node.relay(tx);
330✔
145
  }
146

147
  /**
148
   * Send a claim. Do not wait for promise.
149
   * @param {Claim} claim
150
   * @returns {Promise}
151
   */
152

153
  async sendClaim(claim) {
154
    this.node.relayClaim(claim);
4✔
155
  }
156

157
  /**
158
   * Set bloom filter.
159
   * @param {Bloom} filter
160
   * @returns {Promise}
161
   */
162

163
  async setFilter(filter) {
164
    this.filter = filter;
23✔
165
    this.node.pool.setFilter(filter);
23✔
166
  }
167

168
  /**
169
   * Add data to filter.
170
   * @param {Buffer} data
171
   * @returns {Promise}
172
   */
173

174
  async addFilter(data) {
175
    // `data` is ignored because pool.spvFilter === walletDB.filter
176
    // and therefore is already updated.
177
    // Argument is kept here to be consistent with API in
178
    // wallet/client.js (hs-client NodeClient) and wallet/nullclient.js
179
    this.node.pool.queueFilterLoad();
31,634✔
180
  }
181

182
  /**
183
   * Reset filter.
184
   * @returns {Promise}
185
   */
186

187
  async resetFilter() {
188
    this.node.pool.queueFilterLoad();
×
189
  }
190

191
  /**
192
   * Esimate smart fee.
193
   * @param {Number?} blocks
194
   * @returns {Promise}
195
   */
196

197
  async estimateFee(blocks) {
198
    if (!this.node.fees)
373!
199
      return this.network.feeRate;
×
200

201
    return this.node.fees.estimateFee(blocks);
373✔
202
  }
203

204
  /**
205
   * Get hash range.
206
   * @param {Number} start
207
   * @param {Number} end
208
   * @returns {Promise}
209
   */
210

211
  async getHashes(start = -1, end = -1) {
46✔
212
    return this.node.chain.getHashes(start, end);
23✔
213
  }
214

215
  /**
216
   * Rescan for any missed transactions.
217
   * @param {Number|Hash} start - Start block.
218
   * @param {Bloom} filter
219
   * @param {Function} iter - Iterator.
220
   * @returns {Promise}
221
   */
222

223
  async rescan(start) {
224
    if (this.node.spv)
55✔
225
      return this.node.chain.reset(start);
2✔
226

227
    return this.node.chain.scan(start, this.filter, (entry, txs) => {
53✔
228
      return this.emitAsync('block rescan', entry, txs);
1,212✔
229
    });
230
  }
231

232
  /**
233
   * Get name state.
234
   * @param {Buffer} nameHash
235
   * @returns {Object}
236
   */
237

238
  async getNameStatus(nameHash) {
239
    return this.node.getNameStatus(nameHash);
75✔
240
  }
241

242
  /**
243
   * Get UTXO.
244
   * @param {Hash} hash
245
   * @param {Number} index
246
   * @returns {Object}
247
   */
248

249
  async getCoin(hash, index) {
250
    return this.node.getCoin(hash, index);
3✔
251
  }
252
}
253

254
/*
255
 * Expose
256
 */
257

258
module.exports = NodeClient;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc