wallet: refactor. add hd key destroy methods.
This commit is contained in:
parent
731e615b86
commit
2831af4300
@ -194,6 +194,21 @@ Mnemonic.fromOptions = function fromOptions(options) {
|
|||||||
return new Mnemonic().fromOptions(options);
|
return new Mnemonic().fromOptions(options);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroy the mnemonic (zeroes entropy).
|
||||||
|
*/
|
||||||
|
|
||||||
|
Mnemonic.prototype.destroy = function destroy() {
|
||||||
|
this.bits = constants.hd.MIN_ENTROPY;
|
||||||
|
this.language = 'english';
|
||||||
|
if (this.entropy) {
|
||||||
|
this.entropy.fill(0);
|
||||||
|
this.entropy = null;
|
||||||
|
}
|
||||||
|
this.phrase = null;
|
||||||
|
this.passphrase = '';
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate the seed.
|
* Generate the seed.
|
||||||
* @param {String?} passphrase
|
* @param {String?} passphrase
|
||||||
@ -932,6 +947,38 @@ HDPrivateKey.prototype.__defineGetter__('xpubkey', function() {
|
|||||||
return this.hdPublicKey.xpubkey;
|
return this.hdPublicKey.xpubkey;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroy the key (zeroes chain code, privkey, and pubkey).
|
||||||
|
* @param {Boolean} pub - Destroy hd public key as well.
|
||||||
|
*/
|
||||||
|
|
||||||
|
HDPrivateKey.prototype.destroy = function destroy(pub) {
|
||||||
|
this.depth = 0;
|
||||||
|
this.parentFingerPrint.fill(0);
|
||||||
|
this.childIndex = 0;
|
||||||
|
this.chainCode.fill(0);
|
||||||
|
this.privateKey.fill(0);
|
||||||
|
this.publicKey.fill(0);
|
||||||
|
|
||||||
|
if (this.fingerPrint) {
|
||||||
|
this.fingerPrint.fill(0);
|
||||||
|
this.fingerPrint = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this._hdPublicKey) {
|
||||||
|
if (pub)
|
||||||
|
this._hdPublicKey.destroy();
|
||||||
|
this._hdPublicKey = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._xprivkey = null;
|
||||||
|
|
||||||
|
if (this.mnemonic) {
|
||||||
|
this.mnemonic.destroy();
|
||||||
|
this.mnemonic = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Derive a child key.
|
* Derive a child key.
|
||||||
* @param {Number|String} - Child index or path.
|
* @param {Number|String} - Child index or path.
|
||||||
@ -1592,6 +1639,25 @@ HDPublicKey.prototype.__defineGetter__('xpubkey', function() {
|
|||||||
return this._xpubkey;
|
return this._xpubkey;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroy the key (zeroes chain code and pubkey).
|
||||||
|
*/
|
||||||
|
|
||||||
|
HDPublicKey.prototype.destroy = function destroy() {
|
||||||
|
this.depth = 0;
|
||||||
|
this.parentFingerPrint.fill(0);
|
||||||
|
this.childIndex = 0;
|
||||||
|
this.chainCode.fill(0);
|
||||||
|
this.publicKey.fill(0);
|
||||||
|
|
||||||
|
if (this.fingerPrint) {
|
||||||
|
this.fingerPrint.fill(0);
|
||||||
|
this.fingerPrint = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._xpubkey = null;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Derive a child key.
|
* Derive a child key.
|
||||||
* @param {Number|String} - Child index or path.
|
* @param {Number|String} - Child index or path.
|
||||||
|
|||||||
@ -1121,7 +1121,8 @@ TXDB.prototype._lazyRemove = function lazyRemove(tx, callback) {
|
|||||||
TXDB.prototype._remove = function remove(tx, info, callback) {
|
TXDB.prototype._remove = function remove(tx, info, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var hash = tx.hash('hex');
|
var hash = tx.hash('hex');
|
||||||
var i, path, account, key, address, input, output, coin;
|
var i, path, account, key, prevout;
|
||||||
|
var address, input, output, coin;
|
||||||
|
|
||||||
this.del(layout.t(hash));
|
this.del(layout.t(hash));
|
||||||
|
|
||||||
@ -1149,7 +1150,7 @@ TXDB.prototype._remove = function remove(tx, info, callback) {
|
|||||||
for (i = 0; i < tx.inputs.length; i++) {
|
for (i = 0; i < tx.inputs.length; i++) {
|
||||||
input = tx.inputs[i];
|
input = tx.inputs[i];
|
||||||
key = input.prevout.hash + input.prevout.index;
|
key = input.prevout.hash + input.prevout.index;
|
||||||
var prevout = input.prevout;
|
prevout = input.prevout;
|
||||||
address = input.getHash('hex');
|
address = input.getHash('hex');
|
||||||
|
|
||||||
if (tx.isCoinbase())
|
if (tx.isCoinbase())
|
||||||
|
|||||||
@ -2995,9 +2995,7 @@ MasterKey.prototype.destroy = function destroy() {
|
|||||||
this.stop();
|
this.stop();
|
||||||
|
|
||||||
if (this.key) {
|
if (this.key) {
|
||||||
this.key.chainCode.fill(0);
|
this.key.destroy(true);
|
||||||
this.key.privateKey.fill(0);
|
|
||||||
this.key.publicKey.fill(0);
|
|
||||||
this.key = null;
|
this.key = null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1525,14 +1525,20 @@ WalletDB.prototype.addTX = function addTX(tx, callback, force) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
WalletDB.prototype.getAddressPath = function getAddressPath(wid, hash, callback) {
|
WalletDB.prototype.getAddressPath = function getAddressPath(wid, hash, callback) {
|
||||||
|
var path;
|
||||||
this.getAddressPaths(hash, function(err, paths) {
|
this.getAddressPaths(hash, function(err, paths) {
|
||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
if (!paths || !paths[wid])
|
if (!paths)
|
||||||
return callback();
|
return callback();
|
||||||
|
|
||||||
callback(null, paths[wid]);
|
path = paths[wid];
|
||||||
|
|
||||||
|
if (!path)
|
||||||
|
return callback();
|
||||||
|
|
||||||
|
callback(null, path);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1699,7 +1705,7 @@ Path.prototype.fromJSON = function fromJSON(json) {
|
|||||||
var indexes = bcoin.hd.parsePath(json.path, constants.hd.MAX_INDEX);
|
var indexes = bcoin.hd.parsePath(json.path, constants.hd.MAX_INDEX);
|
||||||
|
|
||||||
assert(indexes.length === 3);
|
assert(indexes.length === 3);
|
||||||
assert(indexes[0] >= 0);
|
assert(indexes[0] >= constants.hd.HARDENED);
|
||||||
indexes[0] -= constants.hd.HARDENED;
|
indexes[0] -= constants.hd.HARDENED;
|
||||||
|
|
||||||
this.wid = json.wid;
|
this.wid = json.wid;
|
||||||
@ -1932,6 +1938,14 @@ function serializePaths(out) {
|
|||||||
return p.render();
|
return p.render();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parseWallets(data) {
|
||||||
|
var p = new BufferReader(data);
|
||||||
|
var wallets = [];
|
||||||
|
while (p.left())
|
||||||
|
wallets.push(p.readU32());
|
||||||
|
return wallets;
|
||||||
|
}
|
||||||
|
|
||||||
function serializeWallets(wallets) {
|
function serializeWallets(wallets) {
|
||||||
var p = new BufferWriter();
|
var p = new BufferWriter();
|
||||||
var i, info;
|
var i, info;
|
||||||
@ -1944,14 +1958,6 @@ function serializeWallets(wallets) {
|
|||||||
return p.render();
|
return p.render();
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseWallets(data) {
|
|
||||||
var p = new BufferReader(data);
|
|
||||||
var wallets = [];
|
|
||||||
while (p.left())
|
|
||||||
wallets.push(p.readU32());
|
|
||||||
return wallets;
|
|
||||||
}
|
|
||||||
|
|
||||||
function WalletBlock(hash, height) {
|
function WalletBlock(hash, height) {
|
||||||
if (!(this instanceof WalletBlock))
|
if (!(this instanceof WalletBlock))
|
||||||
return new WalletBlock(hash, height);
|
return new WalletBlock(hash, height);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user