wallet: preliminary storage support
This commit is contained in:
parent
1d8574bdbd
commit
688b16eb98
@ -11,14 +11,37 @@ function TXPool(wallet) {
|
|||||||
EventEmitter.call(this);
|
EventEmitter.call(this);
|
||||||
|
|
||||||
this._wallet = wallet;
|
this._wallet = wallet;
|
||||||
|
this._storage = wallet.storage;
|
||||||
|
this._prefix = 'bt/' + wallet.getAddress() + '/tx/';
|
||||||
this._all = {};
|
this._all = {};
|
||||||
this._unspent = {};
|
this._unspent = {};
|
||||||
this._orphans = {};
|
this._orphans = {};
|
||||||
|
|
||||||
|
// Load TXs from storage
|
||||||
|
this._init();
|
||||||
}
|
}
|
||||||
util.inherits(TXPool, EventEmitter);
|
util.inherits(TXPool, EventEmitter);
|
||||||
module.exports = TXPool;
|
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');
|
var hash = tx.hash('hex');
|
||||||
|
|
||||||
if (!this._wallet.own(tx))
|
if (!this._wallet.own(tx))
|
||||||
@ -66,6 +89,14 @@ TXPool.prototype.add = function add(tx) {
|
|||||||
orphan.tx.input(tx, orphan.index);
|
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);
|
this.emit('tx', tx);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@ -21,8 +21,9 @@ function Wallet(options, passphrase) {
|
|||||||
if (!options)
|
if (!options)
|
||||||
options = {};
|
options = {};
|
||||||
|
|
||||||
this.compressed = true;
|
this.compressed = typeof options.compressed !== 'undefined' ?
|
||||||
this.tx = new bcoin.txPool(this);
|
options.compressed : true;
|
||||||
|
this.storage = options.storage;
|
||||||
this.key = null;
|
this.key = null;
|
||||||
|
|
||||||
if (options.passphrase) {
|
if (options.passphrase) {
|
||||||
@ -36,14 +37,23 @@ function Wallet(options, passphrase) {
|
|||||||
this.key = bcoin.ecdsa.genKeyPair();
|
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
|
// Notify owners about new accepted transactions
|
||||||
var self = this;
|
var self = this;
|
||||||
this.tx.on('tx', function(tx) {
|
this.tx.on('tx', function(tx) {
|
||||||
self.emit('tx', tx);
|
self.emit('tx', tx);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
util.inherits(Wallet, EventEmitter);
|
this.tx.on('error', function(err) {
|
||||||
module.exports = Wallet;
|
self.emit('error', err);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
Wallet.prototype.getPrivateKey = function getPrivateKey(enc) {
|
Wallet.prototype.getPrivateKey = function getPrivateKey(enc) {
|
||||||
var priv = this.key.getPrivate();
|
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.v, 1);
|
||||||
assert.equal(json.type, 'wallet');
|
assert.equal(json.type, 'wallet');
|
||||||
|
|
||||||
|
var priv;
|
||||||
|
var pub;
|
||||||
|
var compressed;
|
||||||
|
|
||||||
if (json.priv) {
|
if (json.priv) {
|
||||||
var key = bcoin.utils.fromBase58(json.priv);
|
var key = bcoin.utils.fromBase58(json.priv);
|
||||||
assert(utils.isEqual(key.slice(-4), utils.checksum(key.slice(0, -4))));
|
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);
|
key = key.slice(0, -4);
|
||||||
if (key.length === 34) {
|
if (key.length === 34) {
|
||||||
assert.equal(key[33], 1);
|
assert.equal(key[33], 1);
|
||||||
this.key = bcoin.ecdsa.keyPair(key.slice(1, -1));
|
priv = key.slice(1, -1);
|
||||||
this.compressed = true;
|
compressed = true;
|
||||||
} else {
|
} else {
|
||||||
this.key = bcoin.ecdsa.keyPair(key.slice(1));
|
priv = key.slice(1);
|
||||||
this.compressed = false;
|
compressed = false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var key = bcoin.utils.fromBase58(json.pub);
|
pub = bcoin.utils.fromBase58(json.pub);
|
||||||
this.compressed = key[0] !== 0x04;
|
compressed = pub[0] !== 0x04;
|
||||||
this.key = bcoin.ecdsa.keyPair(key, 'hex');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.tx.fromJSON(json.tx);
|
var w = new Wallet({
|
||||||
|
priv: priv,
|
||||||
|
pub: pub,
|
||||||
|
compressed: compressed
|
||||||
|
});
|
||||||
|
|
||||||
return this;
|
w.tx.fromJSON(json.tx);
|
||||||
|
|
||||||
|
return w;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -94,7 +94,7 @@ describe('Wallet', function() {
|
|||||||
w.addTX(t3);
|
w.addTX(t3);
|
||||||
assert.equal(w.balance().toString(10), '22000');
|
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');
|
assert.equal(w2.balance().toString(10), '22000');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user