wallet: refactor passphrase options.

This commit is contained in:
Christopher Jeffrey 2016-10-10 01:28:14 -07:00
parent 25ac6b70de
commit a864ec1552
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 26 additions and 29 deletions

View File

@ -757,6 +757,7 @@ HTTPServer.prototype._init = function _init() {
// Create account
this.post('/wallet/:id/account/:account?', con(function* (req, res, send, next) {
var options = req.options;
var passphrase = options.passphrase;
var account;
if (typeof options.account === 'string') {
@ -764,7 +765,7 @@ HTTPServer.prototype._init = function _init() {
options.account = null;
}
account = yield req.wallet.createAccount(req.options);
account = yield req.wallet.createAccount(options, passphrase);
if (!account)
return send(404);
@ -830,24 +831,27 @@ HTTPServer.prototype._init = function _init() {
// Send TX
this.post('/wallet/:id/send', con(function* (req, res, send, next) {
var options = req.options;
var tx = yield req.wallet.send(options);
var passphrase = options.passphrase;
var tx = yield req.wallet.send(options, passphrase);
send(200, tx.toJSON());
}));
// Create TX
this.post('/wallet/:id/create', con(function* (req, res, send, next) {
var options = req.options;
var passphrase = options.passphrase;
var tx = yield req.wallet.createTX(options);
yield req.wallet.sign(tx, options);
yield req.wallet.sign(tx, passphrase);
send(200, tx.toJSON());
}));
// Sign TX
this.post('/wallet/:id/sign', con(function* (req, res, send, next) {
var options = req.options;
var passphrase = options.passphrase;
var tx = req.options.tx;
enforce(tx, 'TX is required.');
yield req.wallet.sign(tx, options);
yield req.wallet.sign(tx, passphrase);
send(200, tx.toJSON());
}));

View File

@ -188,15 +188,16 @@ Wallet.fromOptions = function fromOptions(db, options) {
*/
Wallet.prototype.init = co(function* init(options) {
var passphrase = options.passphrase;
var account;
assert(!this.initialized);
this.initialized = true;
if (options.passphrase)
yield this.master.encrypt(options.passphrase);
if (passphrase)
yield this.master.encrypt(passphrase);
account = yield this._createAccount(options);
account = yield this._createAccount(options, passphrase);
assert(account);
this.account = account;
@ -667,10 +668,10 @@ Wallet.prototype.getToken = function getToken(master, nonce) {
* @returns {Promise} - Returns {@link Account}.
*/
Wallet.prototype.createAccount = co(function* createAccount(options) {
Wallet.prototype.createAccount = co(function* createAccount(options, passphrase) {
var unlock = yield this.writeLock.lock();
try {
return yield this._createAccount(options);
return yield this._createAccount(options, passphrase);
} finally {
unlock();
}
@ -682,9 +683,7 @@ Wallet.prototype.createAccount = co(function* createAccount(options) {
* @returns {Promise} - Returns {@link Account}.
*/
Wallet.prototype._createAccount = co(function* createAccount(options) {
var passphrase = options.passphrase;
var timeout = options.timeout;
Wallet.prototype._createAccount = co(function* createAccount(options, passphrase) {
var name = options.name;
var key, master, account, exists;
@ -696,7 +695,7 @@ Wallet.prototype._createAccount = co(function* createAccount(options) {
if (exists)
throw new Error('Account already exists.');
master = yield this.unlock(passphrase, timeout);
master = yield this.unlock(passphrase);
if (this.watchOnly && options.accountKey) {
key = options.accountKey;
@ -758,14 +757,14 @@ Wallet.prototype._createAccount = co(function* createAccount(options) {
* @returns {Promise} - Returns {@link Account}.
*/
Wallet.prototype.ensureAccount = co(function* ensureAccount(options) {
Wallet.prototype.ensureAccount = co(function* ensureAccount(options, passphrase) {
var name = options.name;
var account = yield this.getAccount(name);
if (account)
return account;
return yield this.createAccount(options);
return yield this.createAccount(options, passphrase);
});
/**
@ -1442,10 +1441,10 @@ Wallet.prototype.createTX = co(function* createTX(options, force) {
* @returns {Promise} - Returns {@link TX}.
*/
Wallet.prototype.send = co(function* send(options) {
Wallet.prototype.send = co(function* send(options, passphrase) {
var unlock = yield this.fundLock.lock();
try {
return yield this._send(options);
return yield this._send(options, passphrase);
} finally {
unlock();
}
@ -1459,10 +1458,10 @@ Wallet.prototype.send = co(function* send(options) {
* @returns {Promise} - Returns {@link TX}.
*/
Wallet.prototype._send = co(function* send(options) {
Wallet.prototype._send = co(function* send(options, passphrase) {
var tx = yield this.createTX(options, true);
yield this.sign(tx, options);
yield this.sign(tx, passphrase);
if (!tx.isSigned())
throw new Error('TX could not be fully signed.');
@ -1760,19 +1759,13 @@ Wallet.prototype.template = co(function* template(tx) {
* of inputs scripts built and signed).
*/
Wallet.prototype.sign = co(function* sign(tx, options) {
Wallet.prototype.sign = co(function* sign(tx, passphrase) {
var rings;
if (!options)
options = {};
if (typeof options === 'string' || Buffer.isBuffer(options))
options = { passphrase: options };
if (this.watchOnly)
throw new Error('Cannot sign from a watch-only wallet.');
yield this.unlock(options.passphrase, options.timeout);
yield this.unlock(passphrase);
rings = yield this.deriveInputs(tx);

View File

@ -787,7 +787,7 @@ describe('Wallet', function() {
// Should fail
try {
yield w.sign(t2, { passphrase: 'bar' });
yield w.sign(t2, 'bar');
} catch (e) {
err = e;
}
@ -796,7 +796,7 @@ describe('Wallet', function() {
assert(!t2.verify());
// Should succeed
yield w.sign(t2, { passphrase: 'foo' });
yield w.sign(t2, 'foo');
assert(t2.verify());
}));