change passphrase.

This commit is contained in:
Christopher Jeffrey 2016-05-31 06:29:25 -07:00
parent 4058bba907
commit 2e5df5514a
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 69 additions and 21 deletions

View File

@ -179,9 +179,6 @@ Wallet.prototype.init = function init(callback) {
self.account = account;
if (Buffer.isBuffer(options.passphrase))
options.passphrase.fill(0);
options.passphrase = null;
return callback();
@ -264,6 +261,39 @@ Wallet.prototype.removeKey = function removeKey(account, key, callback) {
});
};
/**
* Change or set master key's passphrase.
* @param {(String|Buffer)?} old
* @param {(String|Buffer)?} new_
* @param {Function} callback
*/
Wallet.prototype.setPassphrase = function setPassphrase(old, new_, callback) {
if (typeof new_ === 'function') {
callback = new_;
new_ = old;
old = null;
}
if (old) {
try {
this.master.decrypt(old);
} catch (e) {
return callback(e);
}
}
if (new_) {
try {
this.master.encrypt(new_);
} catch (e) {
return callback(e);
}
}
return this.save(callback);
};
/**
* Generate the wallet ID if none was passed in.
* It is represented as `m/44'` (public) hashed
@ -300,7 +330,7 @@ Wallet.prototype.createAccount = function createAccount(options, callback) {
var master, key;
try {
master = this.master.decrypt(options.passphrase);
master = this.master.toKey(options.passphrase);
} catch (e) {
return callback(e);
}
@ -977,7 +1007,7 @@ Wallet.prototype.sign = function sign(tx, options, callback) {
return callback(err);
try {
master = self.master.decrypt(options.passphrase);
master = self.master.toKey(options.passphrase);
} catch (e) {
return callback(null, 0);
}
@ -2335,7 +2365,7 @@ function MasterKey(options) {
this.key = options.key || null;
}
MasterKey.prototype.decrypt = function decrypt(passphrase) {
MasterKey.prototype.toKey = function toKey(passphrase) {
var xprivkey;
if (this.key)
@ -2351,6 +2381,23 @@ MasterKey.prototype.decrypt = function decrypt(passphrase) {
return bcoin.hd.PrivateKey.fromRaw(xprivkey);
};
MasterKey.prototype.decrypt = function decrypt(passphrase) {
if (!this.encrypted)
return;
assert(passphrase, 'Passphrase is required.');
this.encrypted = false;
this.xprivkey = utils.decrypt(this.xprivkey, passphrase);
if (this.phrase) {
this.phrase = utils.decrypt(this.phrase, passphrase);
this.passphrase = utils.decrypt(this.passphrase, passphrase);
}
this.key = this.toKey();
};
MasterKey.prototype.encrypt = function encrypt(passphrase) {
if (this.encrypted)
return;

View File

@ -327,25 +327,26 @@ describe('Wallet', function() {
w1.fill(t2, { rate: 10000, round: true }, function(err) {
assert.ifError(err);
w1.sign(t2, function(err) {
assert.ifError(err);
assert(t2.verify());
assert.ifError(err);
assert.equal(t2.getInputValue(), 16380);
// If change < dust and is added to outputs:
// assert.equal(t2.getOutputValue(), 6380);
// If change > dust and is added to fee:
assert.equal(t2.getOutputValue(), 5460);
assert.equal(t2.getFee(), 10920);
assert(t2.verify());
// Create new transaction
var t3 = bcoin.mtx().addOutput(w2, 15000);
w1.fill(t3, { rate: 10000, round: true }, function(err) {
assert(err);
assert.equal(err.requiredFunds, 25000);
cb();
assert.equal(t2.getInputValue(), 16380);
// If change < dust and is added to outputs:
// assert.equal(t2.getOutputValue(), 6380);
// If change > dust and is added to fee:
assert.equal(t2.getOutputValue(), 5460);
assert.equal(t2.getFee(), 10920);
// Create new transaction
var t3 = bcoin.mtx().addOutput(w2, 15000);
w1.fill(t3, { rate: 10000, round: true }, function(err) {
assert(err);
assert.equal(err.requiredFunds, 25000);
cb();
});
});
});
});
});
});
});