wallet: refactor passphrase options.
This commit is contained in:
parent
25ac6b70de
commit
a864ec1552
@ -757,6 +757,7 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
// Create account
|
// Create account
|
||||||
this.post('/wallet/:id/account/:account?', con(function* (req, res, send, next) {
|
this.post('/wallet/:id/account/:account?', con(function* (req, res, send, next) {
|
||||||
var options = req.options;
|
var options = req.options;
|
||||||
|
var passphrase = options.passphrase;
|
||||||
var account;
|
var account;
|
||||||
|
|
||||||
if (typeof options.account === 'string') {
|
if (typeof options.account === 'string') {
|
||||||
@ -764,7 +765,7 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
options.account = null;
|
options.account = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
account = yield req.wallet.createAccount(req.options);
|
account = yield req.wallet.createAccount(options, passphrase);
|
||||||
|
|
||||||
if (!account)
|
if (!account)
|
||||||
return send(404);
|
return send(404);
|
||||||
@ -830,24 +831,27 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
// Send TX
|
// Send TX
|
||||||
this.post('/wallet/:id/send', con(function* (req, res, send, next) {
|
this.post('/wallet/:id/send', con(function* (req, res, send, next) {
|
||||||
var options = req.options;
|
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());
|
send(200, tx.toJSON());
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// Create TX
|
// Create TX
|
||||||
this.post('/wallet/:id/create', con(function* (req, res, send, next) {
|
this.post('/wallet/:id/create', con(function* (req, res, send, next) {
|
||||||
var options = req.options;
|
var options = req.options;
|
||||||
|
var passphrase = options.passphrase;
|
||||||
var tx = yield req.wallet.createTX(options);
|
var tx = yield req.wallet.createTX(options);
|
||||||
yield req.wallet.sign(tx, options);
|
yield req.wallet.sign(tx, passphrase);
|
||||||
send(200, tx.toJSON());
|
send(200, tx.toJSON());
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// Sign TX
|
// Sign TX
|
||||||
this.post('/wallet/:id/sign', con(function* (req, res, send, next) {
|
this.post('/wallet/:id/sign', con(function* (req, res, send, next) {
|
||||||
var options = req.options;
|
var options = req.options;
|
||||||
|
var passphrase = options.passphrase;
|
||||||
var tx = req.options.tx;
|
var tx = req.options.tx;
|
||||||
enforce(tx, 'TX is required.');
|
enforce(tx, 'TX is required.');
|
||||||
yield req.wallet.sign(tx, options);
|
yield req.wallet.sign(tx, passphrase);
|
||||||
send(200, tx.toJSON());
|
send(200, tx.toJSON());
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|||||||
@ -188,15 +188,16 @@ Wallet.fromOptions = function fromOptions(db, options) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Wallet.prototype.init = co(function* init(options) {
|
Wallet.prototype.init = co(function* init(options) {
|
||||||
|
var passphrase = options.passphrase;
|
||||||
var account;
|
var account;
|
||||||
|
|
||||||
assert(!this.initialized);
|
assert(!this.initialized);
|
||||||
this.initialized = true;
|
this.initialized = true;
|
||||||
|
|
||||||
if (options.passphrase)
|
if (passphrase)
|
||||||
yield this.master.encrypt(options.passphrase);
|
yield this.master.encrypt(passphrase);
|
||||||
|
|
||||||
account = yield this._createAccount(options);
|
account = yield this._createAccount(options, passphrase);
|
||||||
assert(account);
|
assert(account);
|
||||||
|
|
||||||
this.account = account;
|
this.account = account;
|
||||||
@ -667,10 +668,10 @@ Wallet.prototype.getToken = function getToken(master, nonce) {
|
|||||||
* @returns {Promise} - Returns {@link Account}.
|
* @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();
|
var unlock = yield this.writeLock.lock();
|
||||||
try {
|
try {
|
||||||
return yield this._createAccount(options);
|
return yield this._createAccount(options, passphrase);
|
||||||
} finally {
|
} finally {
|
||||||
unlock();
|
unlock();
|
||||||
}
|
}
|
||||||
@ -682,9 +683,7 @@ Wallet.prototype.createAccount = co(function* createAccount(options) {
|
|||||||
* @returns {Promise} - Returns {@link Account}.
|
* @returns {Promise} - Returns {@link Account}.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Wallet.prototype._createAccount = co(function* createAccount(options) {
|
Wallet.prototype._createAccount = co(function* createAccount(options, passphrase) {
|
||||||
var passphrase = options.passphrase;
|
|
||||||
var timeout = options.timeout;
|
|
||||||
var name = options.name;
|
var name = options.name;
|
||||||
var key, master, account, exists;
|
var key, master, account, exists;
|
||||||
|
|
||||||
@ -696,7 +695,7 @@ Wallet.prototype._createAccount = co(function* createAccount(options) {
|
|||||||
if (exists)
|
if (exists)
|
||||||
throw new Error('Account already exists.');
|
throw new Error('Account already exists.');
|
||||||
|
|
||||||
master = yield this.unlock(passphrase, timeout);
|
master = yield this.unlock(passphrase);
|
||||||
|
|
||||||
if (this.watchOnly && options.accountKey) {
|
if (this.watchOnly && options.accountKey) {
|
||||||
key = options.accountKey;
|
key = options.accountKey;
|
||||||
@ -758,14 +757,14 @@ Wallet.prototype._createAccount = co(function* createAccount(options) {
|
|||||||
* @returns {Promise} - Returns {@link Account}.
|
* @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 name = options.name;
|
||||||
var account = yield this.getAccount(name);
|
var account = yield this.getAccount(name);
|
||||||
|
|
||||||
if (account)
|
if (account)
|
||||||
return 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}.
|
* @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();
|
var unlock = yield this.fundLock.lock();
|
||||||
try {
|
try {
|
||||||
return yield this._send(options);
|
return yield this._send(options, passphrase);
|
||||||
} finally {
|
} finally {
|
||||||
unlock();
|
unlock();
|
||||||
}
|
}
|
||||||
@ -1459,10 +1458,10 @@ Wallet.prototype.send = co(function* send(options) {
|
|||||||
* @returns {Promise} - Returns {@link TX}.
|
* @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);
|
var tx = yield this.createTX(options, true);
|
||||||
|
|
||||||
yield this.sign(tx, options);
|
yield this.sign(tx, passphrase);
|
||||||
|
|
||||||
if (!tx.isSigned())
|
if (!tx.isSigned())
|
||||||
throw new Error('TX could not be fully signed.');
|
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).
|
* of inputs scripts built and signed).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Wallet.prototype.sign = co(function* sign(tx, options) {
|
Wallet.prototype.sign = co(function* sign(tx, passphrase) {
|
||||||
var rings;
|
var rings;
|
||||||
|
|
||||||
if (!options)
|
|
||||||
options = {};
|
|
||||||
|
|
||||||
if (typeof options === 'string' || Buffer.isBuffer(options))
|
|
||||||
options = { passphrase: options };
|
|
||||||
|
|
||||||
if (this.watchOnly)
|
if (this.watchOnly)
|
||||||
throw new Error('Cannot sign from a watch-only wallet.');
|
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);
|
rings = yield this.deriveInputs(tx);
|
||||||
|
|
||||||
|
|||||||
@ -787,7 +787,7 @@ describe('Wallet', function() {
|
|||||||
|
|
||||||
// Should fail
|
// Should fail
|
||||||
try {
|
try {
|
||||||
yield w.sign(t2, { passphrase: 'bar' });
|
yield w.sign(t2, 'bar');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
err = e;
|
err = e;
|
||||||
}
|
}
|
||||||
@ -796,7 +796,7 @@ describe('Wallet', function() {
|
|||||||
assert(!t2.verify());
|
assert(!t2.verify());
|
||||||
|
|
||||||
// Should succeed
|
// Should succeed
|
||||||
yield w.sign(t2, { passphrase: 'foo' });
|
yield w.sign(t2, 'foo');
|
||||||
assert(t2.verify());
|
assert(t2.verify());
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user