wallet: allow export/import without pub key
This commit is contained in:
parent
a77083ce66
commit
1d8574bdbd
@ -227,9 +227,9 @@ Chain.prototype.get = function get(hash, cb) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Chain.prototype.isFull = function isFull() {
|
Chain.prototype.isFull = function isFull() {
|
||||||
// < 10m since last block
|
// < 40m since last block
|
||||||
return !this.request.count &&
|
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) {
|
Chain.prototype.hashesInRange = function hashesInRange(start, end) {
|
||||||
|
|||||||
@ -18,7 +18,7 @@ function Pool(options) {
|
|||||||
this.parallel = options.parallel || 2000;
|
this.parallel = options.parallel || 2000;
|
||||||
this.redundancy = options.redundancy || 2;
|
this.redundancy = options.redundancy || 2;
|
||||||
this.load = {
|
this.load = {
|
||||||
timeout: options.loadTimeout || 5000,
|
timeout: options.loadTimeout || 3000,
|
||||||
interval: options.loadInterval || 5000,
|
interval: options.loadInterval || 5000,
|
||||||
window: options.loadWindow || 250,
|
window: options.loadWindow || 250,
|
||||||
lastRange: null,
|
lastRange: null,
|
||||||
@ -80,6 +80,7 @@ Pool.prototype._init = function _init() {
|
|||||||
this._addLoader();
|
this._addLoader();
|
||||||
for (var i = 0; i < this.size; i++)
|
for (var i = 0; i < this.size; i++)
|
||||||
this._addPeer();
|
this._addPeer();
|
||||||
|
this._load();
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
this.chain.on('missing', function(hash, preload) {
|
this.chain.on('missing', function(hash, preload) {
|
||||||
@ -126,10 +127,12 @@ Pool.prototype._addLoader = function _addLoader() {
|
|||||||
|
|
||||||
function destroy() {
|
function destroy() {
|
||||||
// Chain is full and up-to-date
|
// 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);
|
clearTimeout(timer);
|
||||||
self._removePeer(peer);
|
self._removePeer(peer);
|
||||||
self.emit('full');
|
self.emit('full');
|
||||||
|
self.block.lastHash = null;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
peer.destroy();
|
peer.destroy();
|
||||||
@ -164,7 +167,7 @@ Pool.prototype._addLoader = function _addLoader() {
|
|||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
|
|
||||||
// Reinstantiate timeout
|
// Reinstantiate timeout
|
||||||
if (hashes.length === 500 && self._load())
|
if (self._load())
|
||||||
timer = setTimeout(destroy, self.load.timeout);
|
timer = setTimeout(destroy, self.load.timeout);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,16 +1,21 @@
|
|||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
var bn = require('bn.js');
|
var bn = require('bn.js');
|
||||||
|
var util = require('util');
|
||||||
var bcoin = require('../bcoin');
|
var bcoin = require('../bcoin');
|
||||||
|
var EventEmitter = require('events').EventEmitter;
|
||||||
|
|
||||||
function TXPool(wallet) {
|
function TXPool(wallet) {
|
||||||
if (!(this instanceof TXPool))
|
if (!(this instanceof TXPool))
|
||||||
return new TXPool(wallet);
|
return new TXPool(wallet);
|
||||||
|
|
||||||
|
EventEmitter.call(this);
|
||||||
|
|
||||||
this._wallet = wallet;
|
this._wallet = wallet;
|
||||||
this._all = {};
|
this._all = {};
|
||||||
this._unspent = {};
|
this._unspent = {};
|
||||||
this._orphans = {};
|
this._orphans = {};
|
||||||
}
|
}
|
||||||
|
util.inherits(TXPool, EventEmitter);
|
||||||
module.exports = TXPool;
|
module.exports = TXPool;
|
||||||
|
|
||||||
TXPool.prototype.add = function add(tx) {
|
TXPool.prototype.add = function add(tx) {
|
||||||
@ -61,6 +66,8 @@ TXPool.prototype.add = function add(tx) {
|
|||||||
orphan.tx.input(tx, orphan.index);
|
orphan.tx.input(tx, orphan.index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.emit('tx', tx);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,12 +1,16 @@
|
|||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
var bcoin = require('../bcoin');
|
var bcoin = require('../bcoin');
|
||||||
var hash = require('hash.js');
|
var hash = require('hash.js');
|
||||||
|
var util = require('util');
|
||||||
|
var EventEmitter = require('events').EventEmitter;
|
||||||
var utils = bcoin.utils;
|
var utils = bcoin.utils;
|
||||||
|
|
||||||
function Wallet(options, passphrase) {
|
function Wallet(options, passphrase) {
|
||||||
if (!(this instanceof Wallet))
|
if (!(this instanceof Wallet))
|
||||||
return new Wallet(options, passphrase);
|
return new Wallet(options, passphrase);
|
||||||
|
|
||||||
|
EventEmitter.call(this);
|
||||||
|
|
||||||
// bcoin.wallet('scope', 'password')
|
// bcoin.wallet('scope', 'password')
|
||||||
if (typeof options === 'string' && typeof passphrase === 'string') {
|
if (typeof options === 'string' && typeof passphrase === 'string') {
|
||||||
options = {
|
options = {
|
||||||
@ -31,11 +35,22 @@ function Wallet(options, passphrase) {
|
|||||||
} else {
|
} else {
|
||||||
this.key = bcoin.ecdsa.genKeyPair();
|
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;
|
module.exports = Wallet;
|
||||||
|
|
||||||
Wallet.prototype.getPrivateKey = function getPrivateKey(enc) {
|
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)
|
if (!enc)
|
||||||
return priv;
|
return priv;
|
||||||
|
|
||||||
@ -56,8 +71,12 @@ Wallet.prototype.getPrivateKey = function getPrivateKey(enc) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype.getPublicKey = function getPublicKey() {
|
Wallet.prototype.getPublicKey = function getPublicKey(enc) {
|
||||||
return this.key.getPublic(this.compressed, 'array');
|
var pub = this.key.getPublic(this.compressed, 'array');
|
||||||
|
if (enc === 'base58')
|
||||||
|
return utils.toBase58(pub);
|
||||||
|
else
|
||||||
|
return pub;
|
||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype.getHash = function getHash() {
|
Wallet.prototype.getHash = function getHash() {
|
||||||
@ -169,7 +188,8 @@ Wallet.prototype.toJSON = function toJSON() {
|
|||||||
return {
|
return {
|
||||||
v: 1,
|
v: 1,
|
||||||
type: 'wallet',
|
type: 'wallet',
|
||||||
key: this.getPrivateKey('base58'),
|
pub: this.getPublicKey('base58'),
|
||||||
|
priv: this.getPrivateKey('base58'),
|
||||||
tx: this.tx.toJSON()
|
tx: this.tx.toJSON()
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@ -178,18 +198,24 @@ Wallet.prototype.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 key = bcoin.utils.fromBase58(json.key);
|
if (json.priv) {
|
||||||
assert(utils.isEqual(key.slice(-4), utils.checksum(key.slice(0, -4))));
|
var key = bcoin.utils.fromBase58(json.priv);
|
||||||
assert.equal(key[0], 128);
|
assert(utils.isEqual(key.slice(-4), utils.checksum(key.slice(0, -4))));
|
||||||
|
assert.equal(key[0], 128);
|
||||||
|
|
||||||
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));
|
this.key = bcoin.ecdsa.keyPair(key.slice(1, -1));
|
||||||
this.compressed = true;
|
this.compressed = true;
|
||||||
|
} else {
|
||||||
|
this.key = bcoin.ecdsa.keyPair(key.slice(1));
|
||||||
|
this.compressed = false;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
this.key = bcoin.ecdsa.keyPair(key.slice(1));
|
var key = bcoin.utils.fromBase58(json.pub);
|
||||||
this.compressed = false;
|
this.compressed = key[0] !== 0x04;
|
||||||
|
this.key = bcoin.ecdsa.keyPair(key, 'hex');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.tx.fromJSON(json.tx);
|
this.tx.fromJSON(json.tx);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user