wallet: preliminary storage support

This commit is contained in:
Fedor Indutny 2014-05-08 14:09:11 +04:00
parent 1d8574bdbd
commit 688b16eb98
3 changed files with 67 additions and 17 deletions

View File

@ -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;

View File

@ -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;
};

View File

@ -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');
});
});