From 688b16eb986079195359d8f2a6d789d1ba01bd6f Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Thu, 8 May 2014 14:09:11 +0400 Subject: [PATCH] wallet: preliminary storage support --- lib/bcoin/tx-pool.js | 33 ++++++++++++++++++++++++++++- lib/bcoin/wallet.js | 49 ++++++++++++++++++++++++++++++-------------- test/wallet-test.js | 2 +- 3 files changed, 67 insertions(+), 17 deletions(-) diff --git a/lib/bcoin/tx-pool.js b/lib/bcoin/tx-pool.js index 5eab2cda..f649c091 100644 --- a/lib/bcoin/tx-pool.js +++ b/lib/bcoin/tx-pool.js @@ -11,14 +11,37 @@ function TXPool(wallet) { EventEmitter.call(this); this._wallet = wallet; + this._storage = wallet.storage; + this._prefix = 'bt/' + wallet.getAddress() + '/tx/'; this._all = {}; this._unspent = {}; this._orphans = {}; + + // Load TXs from storage + this._init(); } util.inherits(TXPool, EventEmitter); module.exports = TXPool; -TXPool.prototype.add = function add(tx) { +TXPool.prototype._init = function init() { + if (!this._storage) + return; + + var self = this; + var s = this._storage.createReadStream({ + keys: false, + start: this._prefix, + end: this._prefix + 'z' + }) + s.on('data', function(data) { + self.add(bcoin.tx.fromJSON(data.value), true); + }); + s.on('error', function(err) { + self.emit('error', err); + }); +}; + +TXPool.prototype.add = function add(tx, noWrite) { var hash = tx.hash('hex'); if (!this._wallet.own(tx)) @@ -66,6 +89,14 @@ TXPool.prototype.add = function add(tx) { orphan.tx.input(tx, orphan.index); } + if (!noWrite && this._storage) { + var self = this; + this._storage.put(this._prefix + hash, tx.toJSON(), function(err) { + if (err) + self.emit('error', err); + }); + } + this.emit('tx', tx); return true; diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 6def3143..b8fa1207 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -21,8 +21,9 @@ function Wallet(options, passphrase) { if (!options) options = {}; - this.compressed = true; - this.tx = new bcoin.txPool(this); + this.compressed = typeof options.compressed !== 'undefined' ? + options.compressed : true; + this.storage = options.storage; this.key = null; if (options.passphrase) { @@ -36,14 +37,23 @@ function Wallet(options, passphrase) { this.key = bcoin.ecdsa.genKeyPair(); } + this.tx = new bcoin.txPool(this); + this._init(); +} +util.inherits(Wallet, EventEmitter); +module.exports = Wallet; + +Wallet.prototype._init = function init() { // 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; + + this.tx.on('error', function(err) { + self.emit('error', err); + }); +}; Wallet.prototype.getPrivateKey = function getPrivateKey(enc) { var priv = this.key.getPrivate(); @@ -194,10 +204,14 @@ Wallet.prototype.toJSON = function toJSON() { }; }; -Wallet.prototype.fromJSON = function fromJSON(json) { +Wallet.fromJSON = function fromJSON(json) { assert.equal(json.v, 1); assert.equal(json.type, 'wallet'); + var priv; + var pub; + var compressed; + if (json.priv) { var key = bcoin.utils.fromBase58(json.priv); assert(utils.isEqual(key.slice(-4), utils.checksum(key.slice(0, -4)))); @@ -206,19 +220,24 @@ Wallet.prototype.fromJSON = function fromJSON(json) { 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; + priv = key.slice(1, -1); + compressed = true; } else { - this.key = bcoin.ecdsa.keyPair(key.slice(1)); - this.compressed = false; + priv = key.slice(1); + compressed = false; } } else { - var key = bcoin.utils.fromBase58(json.pub); - this.compressed = key[0] !== 0x04; - this.key = bcoin.ecdsa.keyPair(key, 'hex'); + pub = bcoin.utils.fromBase58(json.pub); + compressed = pub[0] !== 0x04; } - this.tx.fromJSON(json.tx); + var w = new Wallet({ + priv: priv, + pub: pub, + compressed: compressed + }); - return this; + w.tx.fromJSON(json.tx); + + return w; }; diff --git a/test/wallet-test.js b/test/wallet-test.js index e7b4a793..72992549 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -94,7 +94,7 @@ describe('Wallet', function() { w.addTX(t3); assert.equal(w.balance().toString(10), '22000'); - var w2 = bcoin.wallet().fromJSON(w.toJSON()); + var w2 = bcoin.wallet.fromJSON(w.toJSON()); assert.equal(w2.balance().toString(10), '22000'); }); });