From 1d8574bdbd0d854d4f9083f1e3aaf10253f9ed03 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Thu, 8 May 2014 13:50:11 +0400 Subject: [PATCH] wallet: allow export/import without pub key --- lib/bcoin/chain.js | 4 ++-- lib/bcoin/pool.js | 9 +++++--- lib/bcoin/tx-pool.js | 7 ++++++ lib/bcoin/wallet.js | 54 ++++++++++++++++++++++++++++++++------------ 4 files changed, 55 insertions(+), 19 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index baa46ae5..9b97b133 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -227,9 +227,9 @@ Chain.prototype.get = function get(hash, cb) { }; Chain.prototype.isFull = function isFull() { - // < 10m since last block + // < 40m since last block return !this.request.count && - (+new Date / 1000) - this.index.ts[this.index.ts.length - 1] < 10 * 60; + (+new Date / 1000) - this.index.ts[this.index.ts.length - 1] < 40 * 60; }; Chain.prototype.hashesInRange = function hashesInRange(start, end) { diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index fc760921..ca240600 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -18,7 +18,7 @@ function Pool(options) { this.parallel = options.parallel || 2000; this.redundancy = options.redundancy || 2; this.load = { - timeout: options.loadTimeout || 5000, + timeout: options.loadTimeout || 3000, interval: options.loadInterval || 5000, window: options.loadWindow || 250, lastRange: null, @@ -80,6 +80,7 @@ Pool.prototype._init = function _init() { this._addLoader(); for (var i = 0; i < this.size; i++) this._addPeer(); + this._load(); var self = this; this.chain.on('missing', function(hash, preload) { @@ -126,10 +127,12 @@ Pool.prototype._addLoader = function _addLoader() { function destroy() { // Chain is full and up-to-date - if (self.block.lastHash === null && self.chain.isFull()) { + if (self.block.lastHash !== null && self.chain.isFull()) { clearTimeout(timer); self._removePeer(peer); self.emit('full'); + self.block.lastHash = null; + return; } peer.destroy(); @@ -164,7 +167,7 @@ Pool.prototype._addLoader = function _addLoader() { clearTimeout(timer); // Reinstantiate timeout - if (hashes.length === 500 && self._load()) + if (self._load()) timer = setTimeout(destroy, self.load.timeout); }); }; diff --git a/lib/bcoin/tx-pool.js b/lib/bcoin/tx-pool.js index 42f31c7a..5eab2cda 100644 --- a/lib/bcoin/tx-pool.js +++ b/lib/bcoin/tx-pool.js @@ -1,16 +1,21 @@ var assert = require('assert'); var bn = require('bn.js'); +var util = require('util'); var bcoin = require('../bcoin'); +var EventEmitter = require('events').EventEmitter; function TXPool(wallet) { if (!(this instanceof TXPool)) return new TXPool(wallet); + EventEmitter.call(this); + this._wallet = wallet; this._all = {}; this._unspent = {}; this._orphans = {}; } +util.inherits(TXPool, EventEmitter); module.exports = TXPool; TXPool.prototype.add = function add(tx) { @@ -61,6 +66,8 @@ TXPool.prototype.add = function add(tx) { orphan.tx.input(tx, orphan.index); } + this.emit('tx', tx); + return true; }; diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 4d96626b..6def3143 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -1,12 +1,16 @@ var assert = require('assert'); var bcoin = require('../bcoin'); var hash = require('hash.js'); +var util = require('util'); +var EventEmitter = require('events').EventEmitter; var utils = bcoin.utils; function Wallet(options, passphrase) { if (!(this instanceof Wallet)) return new Wallet(options, passphrase); + EventEmitter.call(this); + // bcoin.wallet('scope', 'password') if (typeof options === 'string' && typeof passphrase === 'string') { options = { @@ -31,11 +35,22 @@ function Wallet(options, passphrase) { } else { this.key = bcoin.ecdsa.genKeyPair(); } + + // Notify owners about new accepted transactions + var self = this; + this.tx.on('tx', function(tx) { + self.emit('tx', tx); + }); } +util.inherits(Wallet, EventEmitter); module.exports = Wallet; Wallet.prototype.getPrivateKey = function getPrivateKey(enc) { - var priv = this.key.getPrivate().toArray(); + var priv = this.key.getPrivate(); + if (priv) + priv = priv.toArray(); + else + return; if (!enc) return priv; @@ -56,8 +71,12 @@ Wallet.prototype.getPrivateKey = function getPrivateKey(enc) { } }; -Wallet.prototype.getPublicKey = function getPublicKey() { - return this.key.getPublic(this.compressed, 'array'); +Wallet.prototype.getPublicKey = function getPublicKey(enc) { + var pub = this.key.getPublic(this.compressed, 'array'); + if (enc === 'base58') + return utils.toBase58(pub); + else + return pub; }; Wallet.prototype.getHash = function getHash() { @@ -169,7 +188,8 @@ Wallet.prototype.toJSON = function toJSON() { return { v: 1, type: 'wallet', - key: this.getPrivateKey('base58'), + pub: this.getPublicKey('base58'), + priv: this.getPrivateKey('base58'), tx: this.tx.toJSON() }; }; @@ -178,18 +198,24 @@ Wallet.prototype.fromJSON = function fromJSON(json) { assert.equal(json.v, 1); assert.equal(json.type, 'wallet'); - var key = bcoin.utils.fromBase58(json.key); - assert(utils.isEqual(key.slice(-4), utils.checksum(key.slice(0, -4)))); - assert.equal(key[0], 128); + if (json.priv) { + var key = bcoin.utils.fromBase58(json.priv); + assert(utils.isEqual(key.slice(-4), utils.checksum(key.slice(0, -4)))); + assert.equal(key[0], 128); - key = key.slice(0, -4); - if (key.length === 34) { - assert.equal(key[33], 1); - this.key = bcoin.ecdsa.keyPair(key.slice(1, -1)); - this.compressed = true; + key = key.slice(0, -4); + if (key.length === 34) { + assert.equal(key[33], 1); + this.key = bcoin.ecdsa.keyPair(key.slice(1, -1)); + this.compressed = true; + } else { + this.key = bcoin.ecdsa.keyPair(key.slice(1)); + this.compressed = false; + } } else { - this.key = bcoin.ecdsa.keyPair(key.slice(1)); - this.compressed = false; + var key = bcoin.utils.fromBase58(json.pub); + this.compressed = key[0] !== 0x04; + this.key = bcoin.ecdsa.keyPair(key, 'hex'); } this.tx.fromJSON(json.tx);