walletclient: cleanup.
This commit is contained in:
parent
649f8cc171
commit
5ed8c0f4fe
@ -30,10 +30,6 @@ class HTTPWallet extends Client {
|
||||
async init() {
|
||||
await super.open();
|
||||
|
||||
this.on('error', (err) => {
|
||||
this.emit('error', err);
|
||||
});
|
||||
|
||||
this.listen('wallet tx', (details) => {
|
||||
this.emit('tx', details);
|
||||
});
|
||||
|
||||
@ -7,36 +7,19 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
const {Client} = require('bcurl');
|
||||
const HTTPClient = require('../http/client');
|
||||
const TX = require('../primitives/tx');
|
||||
const Headers = require('../primitives/headers');
|
||||
const digest = require('../crypto/digest');
|
||||
const util = require('../utils/util');
|
||||
const BufferReader = require('../utils/reader');
|
||||
|
||||
class WalletClient extends Client {
|
||||
/**
|
||||
* Bcoin HTTP client.
|
||||
* @alias module:wallet.WalletClient
|
||||
* @constructor
|
||||
* @param {Object|String} options
|
||||
*/
|
||||
|
||||
class WalletClient extends HTTPClient {
|
||||
constructor(options) {
|
||||
super(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Open the client, wait for socket to connect.
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
async open() {
|
||||
await super.open();
|
||||
|
||||
this.on('error', (err) => {
|
||||
this.emit('error', err);
|
||||
});
|
||||
|
||||
this.listen('block connect', (entry, txs) => {
|
||||
this.emit('block connect', ...parseBlock(entry, txs));
|
||||
});
|
||||
@ -56,150 +39,32 @@ class WalletClient extends Client {
|
||||
this.listen('tx', (tx) => {
|
||||
this.emit('tx', TX.fromRaw(tx));
|
||||
});
|
||||
|
||||
await this.watchChain();
|
||||
await this.watchMempool();
|
||||
}
|
||||
|
||||
/**
|
||||
* Auth with server.
|
||||
* @private
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
auth() {
|
||||
return this.call('auth', this.password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make an RPC call.
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
execute(name, params) {
|
||||
return super.execute('/', name, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Watch the blockchain.
|
||||
* @private
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
watchChain() {
|
||||
return this.call('watch chain');
|
||||
}
|
||||
|
||||
/**
|
||||
* Watch the blockchain.
|
||||
* @private
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
watchMempool() {
|
||||
return this.call('watch mempool');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get chain tip.
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
async getTip() {
|
||||
const raw = await this.call('get tip');
|
||||
return parseEntry(raw);
|
||||
return parseEntry(await super.getTip());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get chain entry.
|
||||
* @param {Hash} hash
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
async getEntry(block) {
|
||||
if (typeof block === 'string')
|
||||
block = util.revHex(block);
|
||||
|
||||
const raw = await this.call('get entry', block);
|
||||
return parseEntry(raw);
|
||||
return parseEntry(await super.getEntry(block));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get hashes.
|
||||
* @param {Number} [start=-1]
|
||||
* @param {Number} [end=-1]
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
getHashes(start = -1, end = -1) {
|
||||
return this.call('get hashes', start, end);
|
||||
async send(tx) {
|
||||
return super.send(tx.toRaw());
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a transaction. Do not wait for promise.
|
||||
* @param {TX} tx
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
send(tx) {
|
||||
return this.call('send', tx.toRaw());
|
||||
async setFilter(filter) {
|
||||
return super.setFilter(filter.toRaw());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set bloom filter.
|
||||
* @param {Bloom} filter
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
setFilter(filter) {
|
||||
return this.call('set filter', filter.toRaw());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add data to filter.
|
||||
* @param {Buffer} data
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
addFilter(chunks) {
|
||||
if (!Array.isArray(chunks))
|
||||
chunks = [chunks];
|
||||
|
||||
return this.call('add filter', chunks);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset filter.
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
resetFilter() {
|
||||
return this.call('reset filter');
|
||||
}
|
||||
|
||||
/**
|
||||
* Esimate smart fee.
|
||||
* @param {Number?} blocks
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
estimateFee(blocks) {
|
||||
return this.call('estimate fee', blocks);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rescan for any missed transactions.
|
||||
* @param {Number|Hash} start - Start block.
|
||||
* @param {Bloom} filter
|
||||
* @param {Function} iter - Iterator.
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
rescan(start) {
|
||||
async rescan(start) {
|
||||
if (typeof start === 'string')
|
||||
start = util.revHex(start);
|
||||
|
||||
return this.call('rescan', start);
|
||||
return super.rescan(start);
|
||||
}
|
||||
}
|
||||
|
||||
@ -208,15 +73,16 @@ class WalletClient extends Client {
|
||||
*/
|
||||
|
||||
function parseEntry(data) {
|
||||
const block = Headers.fromHead(data);
|
||||
assert(Buffer.isBuffer(data));
|
||||
assert(data.length >= 84);
|
||||
|
||||
const br = new BufferReader(data);
|
||||
br.seek(80);
|
||||
const h = digest.hash256(data.slice(0, 80));
|
||||
|
||||
const height = br.readU32();
|
||||
const hash = block.hash('hex');
|
||||
|
||||
return { hash, height, time: block.time };
|
||||
return {
|
||||
hash: h.toString('hex'),
|
||||
height: data.readUInt32LE(80, true),
|
||||
time: data.readUInt32LE(68, true)
|
||||
};
|
||||
}
|
||||
|
||||
function parseBlock(entry, txs) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user