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

View File

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

View File

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

View File

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