Browse Source

refactor create/fromObj

activeAddress
Matias Alejo Garcia 10 years ago
parent
commit
637a1e2d8b
  1. 53
      lib/model/copayer.js
  2. 7
      lib/storage.js
  3. 1
      test/integration/server.js
  4. 1
      test/storage.js

53
lib/model/copayer.js

@ -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;
};

7
lib/storage.js

@ -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);
});

1
test/integration/server.js

@ -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);

1
test/storage.js

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

Loading…
Cancel
Save