walletclient: cleanup.
This commit is contained in:
parent
649f8cc171
commit
5ed8c0f4fe
@ -30,10 +30,6 @@ class HTTPWallet extends Client {
|
|||||||
async init() {
|
async init() {
|
||||||
await super.open();
|
await super.open();
|
||||||
|
|
||||||
this.on('error', (err) => {
|
|
||||||
this.emit('error', err);
|
|
||||||
});
|
|
||||||
|
|
||||||
this.listen('wallet tx', (details) => {
|
this.listen('wallet tx', (details) => {
|
||||||
this.emit('tx', details);
|
this.emit('tx', details);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -7,36 +7,19 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const {Client} = require('bcurl');
|
const HTTPClient = require('../http/client');
|
||||||
const TX = require('../primitives/tx');
|
const TX = require('../primitives/tx');
|
||||||
const Headers = require('../primitives/headers');
|
const digest = require('../crypto/digest');
|
||||||
const util = require('../utils/util');
|
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) {
|
constructor(options) {
|
||||||
super(options);
|
super(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Open the client, wait for socket to connect.
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
|
|
||||||
async open() {
|
async open() {
|
||||||
await super.open();
|
await super.open();
|
||||||
|
|
||||||
this.on('error', (err) => {
|
|
||||||
this.emit('error', err);
|
|
||||||
});
|
|
||||||
|
|
||||||
this.listen('block connect', (entry, txs) => {
|
this.listen('block connect', (entry, txs) => {
|
||||||
this.emit('block connect', ...parseBlock(entry, txs));
|
this.emit('block connect', ...parseBlock(entry, txs));
|
||||||
});
|
});
|
||||||
@ -56,150 +39,32 @@ class WalletClient extends Client {
|
|||||||
this.listen('tx', (tx) => {
|
this.listen('tx', (tx) => {
|
||||||
this.emit('tx', TX.fromRaw(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() {
|
async getTip() {
|
||||||
const raw = await this.call('get tip');
|
return parseEntry(await super.getTip());
|
||||||
return parseEntry(raw);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get chain entry.
|
|
||||||
* @param {Hash} hash
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
|
|
||||||
async getEntry(block) {
|
async getEntry(block) {
|
||||||
if (typeof block === 'string')
|
if (typeof block === 'string')
|
||||||
block = util.revHex(block);
|
block = util.revHex(block);
|
||||||
|
|
||||||
const raw = await this.call('get entry', block);
|
return parseEntry(await super.getEntry(block));
|
||||||
return parseEntry(raw);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async send(tx) {
|
||||||
* Get hashes.
|
return super.send(tx.toRaw());
|
||||||
* @param {Number} [start=-1]
|
|
||||||
* @param {Number} [end=-1]
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
|
|
||||||
getHashes(start = -1, end = -1) {
|
|
||||||
return this.call('get hashes', start, end);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async setFilter(filter) {
|
||||||
* Send a transaction. Do not wait for promise.
|
return super.setFilter(filter.toRaw());
|
||||||
* @param {TX} tx
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
|
|
||||||
send(tx) {
|
|
||||||
return this.call('send', tx.toRaw());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async rescan(start) {
|
||||||
* 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) {
|
|
||||||
if (typeof start === 'string')
|
if (typeof start === 'string')
|
||||||
start = util.revHex(start);
|
start = util.revHex(start);
|
||||||
|
|
||||||
return this.call('rescan', start);
|
return super.rescan(start);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,15 +73,16 @@ class WalletClient extends Client {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function parseEntry(data) {
|
function parseEntry(data) {
|
||||||
const block = Headers.fromHead(data);
|
assert(Buffer.isBuffer(data));
|
||||||
|
assert(data.length >= 84);
|
||||||
|
|
||||||
const br = new BufferReader(data);
|
const h = digest.hash256(data.slice(0, 80));
|
||||||
br.seek(80);
|
|
||||||
|
|
||||||
const height = br.readU32();
|
return {
|
||||||
const hash = block.hash('hex');
|
hash: h.toString('hex'),
|
||||||
|
height: data.readUInt32LE(80, true),
|
||||||
return { hash, height, time: block.time };
|
time: data.readUInt32LE(68, true)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseBlock(entry, txs) {
|
function parseBlock(entry, txs) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user