Browse Source

adds pubKey validation

activeAddress
Matias Alejo Garcia 10 years ago
parent
commit
b1b29e7df1
  1. 6
      lib/model/wallet.js
  2. 11
      lib/server.js
  3. 26
      test/integration.js

6
lib/model/wallet.js

@ -3,6 +3,8 @@
var _ = require('lodash');
var Copayer = require('./copayer');
var Bitcore = require('bitcore');
var PublicKey = Bitcore.PublicKey;
function Wallet(opts) {
opts = opts || {};
@ -16,6 +18,9 @@ function Wallet(opts) {
this.publicKeyRing = [];
this.addressIndex = 0;
this.copayers = [];
if (opts.pubKey)
this.pubKey = new PublicKey(opts.pubKey);
};
Wallet.fromObj = function (obj) {
@ -32,6 +37,7 @@ Wallet.fromObj = function (obj) {
x.copayers = _.map(obj.copayers, function (copayer) {
return new Copayer(copayer);
});
x.pubKey = new PublicKey(obj.pubKey);
return x;
};

11
lib/server.js

@ -6,6 +6,7 @@ var async = require('async');
var log = require('npmlog');
log.debug = log.verbose;
var Bitcore = require('bitcore');
var PublicKey = Bitcore.PublicKey;
var Explorers = require('bitcore-explorers');
var Lock = require('./lock');
@ -39,9 +40,13 @@ function CopayServer(opts) {
* @param {string} [opts.network = 'livenet'] - The Bitcoin network for this wallet.
*/
CopayServer.prototype.createWallet = function (opts, cb) {
var self = this;
var self = this, pubKey;
// TODO: validate opts.pubKey is valid and belongs to opts.network
try {
pubKey = new PublicKey.fromString(opts.pubKey);
} catch (e) {
return cb(e.toString());
};
self.storage.fetchWallet(opts.id, function (err, wallet) {
if (err) return cb(err);
@ -53,7 +58,7 @@ CopayServer.prototype.createWallet = function (opts, cb) {
m: opts.m,
n: opts.n,
network: opts.network || 'livenet',
pubKey: opts.pubKey,
pubKey: pubKey,
});
self.storage.storeWallet(wallet, cb);

26
test/integration.js

@ -16,6 +16,7 @@ var Address = require('../lib/model/address');
var Copayer = require('../lib/model/copayer');
var CopayServer = require('../lib/server');
var aPubKey = '042F65F56A6C06C2B651C473AC221B2460DA57859AFB72564E9781B655EBC0AFAF322B9A732324ECC92A3319DFB1F0D53F0CB7E6620C98BD1EF53106A7CF3F6DB9';
var helpers = {};
helpers.createAndJoinWallet = function (id, m, n, cb) {
@ -24,7 +25,7 @@ helpers.createAndJoinWallet = function (id, m, n, cb) {
name: id + ' wallet',
m: m,
n: n,
pubKey: 'dummy',
pubKey: aPubKey,
};
server.createWallet(walletOpts, function(err) {
if (err) return cb(err);
@ -78,19 +79,21 @@ describe('Copay server', function() {
});
it('should get existing wallet', function (done) {
var w1 = new Wallet({
id: '123',
name: 'my wallet',
m: 2,
n: 3,
pubKey: 'dummy',
pubKey: aPubKey,
});
var w2 = new Wallet({
id: '234',
name: 'my wallet 2',
m: 3,
n: 4,
pubKey: 'dummy',
pubKey: aPubKey,
});
db.batch([{
@ -119,16 +122,15 @@ describe('Copay server', function() {
name: 'my wallet',
m: 2,
n: 3,
pubKey: 'dummy',
pubKey: aPubKey,
});
var w2 = new Wallet({
id: '234',
name: 'my wallet 2',
m: 3,
n: 4,
pubKey: 'dummy',
pubKey: aPubKey,
});
db.batch([{
type: 'put',
key: 'wallet-123',
@ -160,7 +162,7 @@ describe('Copay server', function() {
name: 'my wallet',
m: 2,
n: 3,
pubKey: 'dummy',
pubKey: aPubKey,
};
server.createWallet(opts, function(err) {
should.not.exist(err);
@ -179,7 +181,7 @@ describe('Copay server', function() {
name: 'my wallet',
m: 2,
n: 3,
pubKey: 'dummy',
pubKey: aPubKey,
};
server.createWallet(opts, function(err) {
should.not.exist(err);
@ -209,7 +211,7 @@ describe('Copay server', function() {
name: 'my wallet',
m: 2,
n: 3,
pubKey: 'dummy',
pubKey: aPubKey,
};
server.createWallet(walletOpts, function(err) {
should.not.exist(err);
@ -240,7 +242,7 @@ describe('Copay server', function() {
name: 'my wallet',
m: 2,
n: 3,
pubKey: 'dummy',
pubKey: aPubKey,
};
server.createWallet(walletOpts, function(err) {
should.not.exist(err);
@ -264,7 +266,7 @@ describe('Copay server', function() {
name: 'my wallet',
m: 1,
n: 1,
pubKey: 'dummy',
pubKey: aPubKey,
};
server.createWallet(walletOpts, function(err) {
should.not.exist(err);
@ -302,7 +304,7 @@ describe('Copay server', function() {
name: 'my wallet',
m: 1,
n: 1,
pubKey: 'dummy',
pubKey: aPubKey,
};
server.createWallet(walletOpts, function(err) {
should.not.exist(err);

Loading…
Cancel
Save