refactor create/fromObj

This commit is contained in:
Matias Alejo Garcia 2015-08-14 15:52:46 -03:00
parent 72ba0b3a58
commit 637a1e2d8b
4 changed files with 29 additions and 33 deletions

View File

@ -13,14 +13,18 @@ var Bitcore = WalletUtils.Bitcore;
var HDPublicKey = Bitcore.HDPublicKey; var HDPublicKey = Bitcore.HDPublicKey;
function Copayer() { function Copayer() {
this.version = '1.0.0'; this.version = '2';
}; };
Copayer.getVersion = function() {
return parseInt(this.version);
};
Copayer.create = function(opts) { Copayer.create = function(opts) {
opts = opts || {}; opts = opts || {};
$.checkArgument(opts.xPubKey, 'Missing copayer extended public key'); $.checkArgument(opts.xPubKey, 'Missing copayer extended public key')
$.checkArgument(opts.requestPubKey || opts.requestPubKeys, 'Missing copayer request public key'); .checkArgument(opts.requestPubKey, 'Missing copayer request public key')
.checkArgument(opts.signature, 'Missing copayer request public key signature');
opts.copayerIndex = opts.copayerIndex || 0; opts.copayerIndex = opts.copayerIndex || 0;
@ -32,19 +36,14 @@ Copayer.create = function(opts) {
x.id = WalletUtils.xPubToCopayerId(x.xPubKey); x.id = WalletUtils.xPubToCopayerId(x.xPubKey);
x.name = opts.name; x.name = opts.name;
if (!opts.requestPubKeys) {
x.requestPubKeys = [{
key: opts.requestPubKey,
signature: opts.signature,
}];
}
x.requestPubKey = opts.requestPubKey; x.requestPubKey = opts.requestPubKey;
x.signature = opts.signature; x.signature = opts.signature;
x.requestPubKeys = opts.requestPubKeys; x.requestPubKeys = [{
Copayer.expandForCompat(x); key: opts.requestPubKey,
signature: opts.signature,
}];
x.addressManager = AddressManager.create({ x.addressManager = AddressManager.create({
copayerIndex: opts.copayerIndex copayerIndex: opts.copayerIndex
}); });
@ -52,21 +51,6 @@ Copayer.create = function(opts) {
return x; return x;
}; };
Copayer.expandForCompat = function(x) {
if (!x.requestPubKeys) {
x.requestPubKeys = [{
key: x.requestPubKey,
signature: x.signature,
}];
}
// For backcompat
if (!x.requestPubKey) {
var l = _.last(x.requestPubKeys);
x.requestPubKey = l.key;
x.signature = l.signature;
}
};
Copayer.fromObj = function(obj) { Copayer.fromObj = function(obj) {
var x = new Copayer(); var x = new Copayer();
@ -78,9 +62,16 @@ Copayer.fromObj = function(obj) {
x.requestPubKey = obj.requestPubKey; x.requestPubKey = obj.requestPubKey;
x.signature = obj.signature; x.signature = obj.signature;
x.requestPubKeys = obj.requestPubKeys; if (this.getVersion() == 1) {
Copayer.expandForCompat(x); x.requestPubKeys = [{
key: x.requestPubKey,
signature: x.signature,
}];
x.version = 2;
} else {
x.requestPubKeys = obj.requestPubKeys;
}
x.addressManager = AddressManager.fromObj(obj.addressManager); x.addressManager = AddressManager.fromObj(obj.addressManager);
return x; return x;
}; };

View File

@ -141,7 +141,12 @@ Storage.prototype.fetchCopayerLookup = function(copayerId, cb) {
if (err) return cb(err); if (err) return cb(err);
if (!result) return cb(); if (!result) return cb();
Model.Copayer.expandForCompat(result); if (!result.requestPubKeys) {
result.requestPubKeys = [{
key: result.requestPubKey,
signature: result.signature,
}];
}
return cb(null, result); return cb(null, result);
}); });

View File

@ -113,7 +113,6 @@ helpers.createAndJoinWallet = function(m, n, opts, cb) {
}); });
}, function(err) { }, function(err) {
if (err) return new Error('Could not generate wallet'); if (err) return new Error('Could not generate wallet');
helpers.getAuthServer(copayerIds[0], function(s) { helpers.getAuthServer(copayerIds[0], function(s) {
s.getWallet({}, function(err, w) { s.getWallet({}, function(err, w) {
cb(s, w); cb(s, w);

View File

@ -132,6 +132,7 @@ describe('Storage', function() {
name: 'copayer ' + i, name: 'copayer ' + i,
xPubKey: 'xPubKey ' + i, xPubKey: 'xPubKey ' + i,
requestPubKey: 'requestPubKey ' + i, requestPubKey: 'requestPubKey ' + i,
signature: 'signarture ' + i,
}); });
wallet.addCopayer(copayer); wallet.addCopayer(copayer);
}); });