|
|
@ -80,6 +80,19 @@ CopayServer.prototype.createWallet = function (opts, cb) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
CopayServer.prototype.runLocked = function (walletId, cb, task) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
Lock.get(walletId, function (lock) { |
|
|
|
var _cb = function () { |
|
|
|
cb.apply(null, arguments); |
|
|
|
lock.free(); |
|
|
|
}; |
|
|
|
task(_cb); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Joins a wallet in creation. |
|
|
|
* @param {Object} opts |
|
|
@ -92,17 +105,12 @@ CopayServer.prototype.createWallet = function (opts, cb) { |
|
|
|
CopayServer.prototype.joinWallet = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
Lock.get(opts.walletId, function (lock) { |
|
|
|
var _cb = function (err, res) { |
|
|
|
cb(err, res); |
|
|
|
lock.free(); |
|
|
|
}; |
|
|
|
|
|
|
|
self.runLocked(opts.walletId, cb, function (cb) { |
|
|
|
self.getWallet({ id: opts.walletId, includeCopayers: true }, function (err, wallet) { |
|
|
|
if (err) return _cb(err); |
|
|
|
if (!wallet) return _cb('Wallet not found'); |
|
|
|
if (_.find(wallet.copayers, { xPubKey: opts.xPubKey })) return _cb('Copayer already in wallet'); |
|
|
|
if (wallet.copayers.length == wallet.n) return _cb('Wallet full'); |
|
|
|
if (err) return cb(err); |
|
|
|
if (!wallet) return cb('Wallet not found'); |
|
|
|
if (_.find(wallet.copayers, { xPubKey: opts.xPubKey })) return cb('Copayer already in wallet'); |
|
|
|
if (wallet.copayers.length == wallet.n) return cb('Wallet full'); |
|
|
|
|
|
|
|
// TODO: validate copayer's extended public key using the public key from this wallet
|
|
|
|
// Note: use Bitcore.crypto.ecdsa .verify()
|
|
|
@ -115,16 +123,15 @@ CopayServer.prototype.createWallet = function (opts, cb) { |
|
|
|
}); |
|
|
|
|
|
|
|
self.storage.storeCopayer(wallet.id, copayer, function (err) { |
|
|
|
if (err) return _cb(err); |
|
|
|
if ((wallet.copayers.length + 1) < wallet.n) return _cb(); |
|
|
|
if (err) return cb(err); |
|
|
|
if ((wallet.copayers.length + 1) < wallet.n) return cb(); |
|
|
|
|
|
|
|
wallet.status = 'complete'; |
|
|
|
wallet.publicKeyRing = _.pluck(wallet.copayers, 'xPubKey'); |
|
|
|
wallet.publicKeyRing.push(copayer.xPubKey); |
|
|
|
self.storage.storeWallet(wallet, _cb); |
|
|
|
self.storage.storeWallet(wallet, cb); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|