|
@ -19,10 +19,12 @@ var TxProposal = require('./model/txproposal'); |
|
|
/** |
|
|
/** |
|
|
* Creates an instance of the Copay server. |
|
|
* Creates an instance of the Copay server. |
|
|
* @constructor |
|
|
* @constructor |
|
|
|
|
|
* @param {Object} opts |
|
|
|
|
|
* @param {Storage} [opts.storage] - The storage provider. |
|
|
*/ |
|
|
*/ |
|
|
function CopayServer(opts) { |
|
|
function CopayServer(opts) { |
|
|
opts = opts || {}; |
|
|
opts = opts || {}; |
|
|
this.storage = new Storage(opts); |
|
|
this.storage = opts.storage || new Storage(); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -81,7 +83,7 @@ CopayServer.prototype.createWallet = function (opts, cb) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CopayServer.prototype.runLocked = function (walletId, cb, task) { |
|
|
CopayServer.prototype._runLocked = function (walletId, cb, task) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
Lock.get(walletId, function (lock) { |
|
|
Lock.get(walletId, function (lock) { |
|
@ -105,13 +107,12 @@ CopayServer.prototype.runLocked = function (walletId, cb, task) { |
|
|
CopayServer.prototype.joinWallet = function (opts, cb) { |
|
|
CopayServer.prototype.joinWallet = function (opts, cb) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
self.runLocked(opts.walletId, cb, function (cb) { |
|
|
self._runLocked(opts.walletId, cb, function (cb) { |
|
|
self.getWallet({ id: opts.walletId, includeCopayers: true }, function (err, wallet) { |
|
|
self.getWallet({ id: opts.walletId, includeCopayers: true }, function (err, wallet) { |
|
|
if (err) return cb(err); |
|
|
if (err) return cb(err); |
|
|
if (!wallet) return cb('Wallet not found'); |
|
|
if (!wallet) return cb('Wallet not found'); |
|
|
if (_.find(wallet.copayers, { xPubKey: opts.xPubKey })) return cb('Copayer already in wallet'); |
|
|
if (_.find(wallet.copayers, { xPubKey: opts.xPubKey })) return cb('Copayer already in wallet'); |
|
|
if (wallet.copayers.length == wallet.n) return cb('Wallet full'); |
|
|
if (wallet.copayers.length == wallet.n) return cb('Wallet full'); |
|
|
|
|
|
|
|
|
// TODO: validate copayer's extended public key using the public key from this wallet
|
|
|
// TODO: validate copayer's extended public key using the public key from this wallet
|
|
|
// Note: use Bitcore.crypto.ecdsa .verify()
|
|
|
// Note: use Bitcore.crypto.ecdsa .verify()
|
|
|
|
|
|
|
|
@ -149,19 +150,21 @@ CopayServer.prototype._doCreateAddress = function (pkr, index, isChange) { |
|
|
CopayServer.prototype.createAddress = function (opts, cb) { |
|
|
CopayServer.prototype.createAddress = function (opts, cb) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
self.getWallet({ id: opts.walletId }, function (err, wallet) { |
|
|
self._runLocked(opts.walletId, cb, function (cb) { |
|
|
if (err) return cb(err); |
|
|
self.getWallet({ id: opts.walletId }, function (err, wallet) { |
|
|
if (!wallet) return cb('Wallet not found'); |
|
|
|
|
|
|
|
|
|
|
|
var index = wallet.addressIndex++; |
|
|
|
|
|
self.storage.storeWallet(wallet, function (err) { |
|
|
|
|
|
if (err) return cb(err); |
|
|
if (err) return cb(err); |
|
|
|
|
|
if (!wallet) return cb('Wallet not found'); |
|
|
var address = self._doCreateAddress(wallet.publicKeyRing, index, opts.isChange); |
|
|
|
|
|
self.storage.storeAddress(opts.walletId, address, function (err) { |
|
|
var index = wallet.addressIndex++; |
|
|
|
|
|
self.storage.storeWallet(wallet, function (err) { |
|
|
if (err) return cb(err); |
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
|
return cb(null, address); |
|
|
var address = self._doCreateAddress(wallet.publicKeyRing, index, opts.isChange); |
|
|
|
|
|
self.storage.storeAddress(opts.walletId, address, function (err) { |
|
|
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
|
|
|
|
return cb(null, address); |
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|