This commit is contained in:
Matias Alejo Garcia 2015-02-01 11:41:16 -03:00
parent 8876f697a8
commit a91af80235
3 changed files with 24 additions and 4 deletions

View File

@ -5,6 +5,7 @@ var _ = require('lodash');
var Copayer = require('./copayer'); var Copayer = require('./copayer');
var Bitcore = require('bitcore'); var Bitcore = require('bitcore');
var PublicKey = Bitcore.PublicKey; var PublicKey = Bitcore.PublicKey;
var WALLET_VERSION = '1.0.0';
function Wallet(opts) { function Wallet(opts) {
opts = opts || {}; opts = opts || {};
@ -18,6 +19,7 @@ function Wallet(opts) {
this.publicKeyRing = []; this.publicKeyRing = [];
this.addressIndex = 0; this.addressIndex = 0;
this.copayers = []; this.copayers = [];
this.version = WALLET_VERSION;
if (opts.pubKey) if (opts.pubKey)
this.pubKey = new PublicKey(opts.pubKey); this.pubKey = new PublicKey(opts.pubKey);
@ -26,6 +28,10 @@ function Wallet(opts) {
Wallet.fromObj = function (obj) { Wallet.fromObj = function (obj) {
var x = new Wallet(); var x = new Wallet();
// TODO add sanity checks OR migration steps?
if (!obj.pubKey || !obj.m || !obj.n)
return cb('Wallet corrupted');
x.createdOn = obj.createdOn; x.createdOn = obj.createdOn;
x.id = obj.id; x.id = obj.id;
x.name = obj.name; x.name = obj.name;
@ -39,6 +45,7 @@ Wallet.fromObj = function (obj) {
}); });
x.pubKey = new PublicKey(obj.pubKey); x.pubKey = new PublicKey(obj.pubKey);
return x; return x;
}; };

View File

@ -11,6 +11,7 @@ var Explorers = require('bitcore-explorers');
var Lock = require('./lock'); var Lock = require('./lock');
var Storage = require('./storage'); var Storage = require('./storage');
var SignUtils = require('./signutils');
var Wallet = require('./model/wallet'); var Wallet = require('./model/wallet');
var Copayer = require('./model/copayer'); var Copayer = require('./model/copayer');
@ -77,7 +78,6 @@ CopayServer.prototype.createWallet = function (opts, cb) {
self.storage.fetchWallet(opts.id, function (err, wallet) { self.storage.fetchWallet(opts.id, 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');
return cb(null, wallet); return cb(null, wallet);
}); });
}; };
@ -95,6 +95,16 @@ CopayServer.prototype._runLocked = function (walletId, cb, task) {
}); });
}; };
/**
* Verifies a signature
* @param text
* @param signature
* @param pubKey
*/
CopayServer.prototype._verifySignature = function (text, signature, pubKey) {
return SignUtils.verify( text, signature, pubKey);
};
/** /**
* Joins a wallet in creation. * Joins a wallet in creation.
* @param {Object} opts * @param {Object} opts
@ -110,11 +120,13 @@ CopayServer.prototype._runLocked = function (walletId, cb, task) {
self._runLocked(opts.walletId, cb, function (cb) { self._runLocked(opts.walletId, cb, function (cb) {
self.getWallet({ id: opts.walletId }, function (err, wallet) { self.getWallet({ id: opts.walletId }, function (err, wallet) {
if (err) return cb(err); if (err) return cb(err);
if (!self._verifySignature(opts.xPubKey, opts.xPubKeySignature, wallet.pubKey)) {
return cb('Bad request');
}
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
// Note: use Bitcore.crypto.ecdsa .verify()
var copayer = new Copayer({ var copayer = new Copayer({
id: opts.id, id: opts.id,
name: opts.name, name: opts.name,

View File

@ -203,6 +203,7 @@ describe('Copay server', function() {
server = new CopayServer({ server = new CopayServer({
storage: storage, storage: storage,
}); });
server._verifySignature = sinon.stub().returns(true);
}); });
it('should join existing wallet', function (done) { it('should join existing wallet', function (done) {