wallet: allow export/import without pub key

This commit is contained in:
Fedor Indutny 2014-05-08 13:50:11 +04:00
parent a77083ce66
commit 1d8574bdbd
4 changed files with 55 additions and 19 deletions

View File

@ -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) {

View File

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

View File

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

View File

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