bitcore-wallet-service/lib/model/wallet.js

92 lines
2.0 KiB
JavaScript
Raw Normal View History

2015-01-27 05:18:45 -08:00
'use strict';
var _ = require('lodash');
2015-02-02 06:55:03 -08:00
var util = require('util');
2015-01-27 05:18:45 -08:00
2015-01-28 09:21:09 -08:00
var Copayer = require('./copayer');
2015-02-02 06:55:03 -08:00
var Addressable = require('./Addressable');
2015-02-02 10:29:14 -08:00
var VERSION = '1.0.0';
2015-01-28 09:21:09 -08:00
2015-01-27 05:18:45 -08:00
function Wallet(opts) {
2015-02-02 06:55:03 -08:00
Wallet.super_.apply(this, arguments);
2015-02-02 12:07:18 -08:00
opts = opts || {};
2015-01-27 05:18:45 -08:00
2015-02-02 10:29:14 -08:00
this.version = VERSION;
2015-02-02 12:07:18 -08:00
this.createdOn = Math.floor(Date.now() / 1000);
this.id = opts.id;
this.name = opts.name;
this.m = opts.m;
this.n = opts.n;
this.status = 'pending';
this.publicKeyRing = [];
this.addressIndex = 0;
this.copayers = [];
2015-02-02 04:15:50 -08:00
this.pubKey = opts.pubKey;
2015-01-27 05:18:45 -08:00
};
2015-02-02 10:29:14 -08:00
/* For compressed keys, m*73 + n*34 <= 496 */
Wallet.COPAYER_PAIR_LIMITS = {
1: 1,
2: 2,
3: 3,
4: 4,
5: 4,
6: 4,
7: 3,
8: 3,
9: 2,
10: 2,
11: 1,
12: 1,
};
2015-02-02 06:55:03 -08:00
util.inherits(Wallet, Addressable);
2015-02-01 11:50:58 -08:00
2015-02-02 10:29:14 -08:00
/**
* Get the maximum allowed number of required copayers.
* This is a limit imposed by the maximum allowed size of the scriptSig.
* @param {number} totalCopayers - the total number of copayers
* @return {number}
*/
Wallet.getMaxRequiredCopayers = function(totalCopayers) {
return Wallet.COPAYER_PAIR_LIMITS[totalCopayers];
};
2015-02-01 06:41:16 -08:00
2015-02-02 10:29:14 -08:00
Wallet.verifyCopayerLimits = function (m, n) {
2015-02-02 12:07:18 -08:00
return (n >= 1 && n <= 12) && (m >= 1 && m <= Wallet.COPAYER_PAIR_LIMITS[n]);
2015-02-01 11:50:58 -08:00
};
Wallet.fromObj = function (obj) {
2015-02-02 12:07:18 -08:00
var x = new Wallet();
2015-02-01 11:50:58 -08:00
2015-02-02 12:07:18 -08:00
x.createdOn = obj.createdOn;
x.id = obj.id;
x.name = obj.name;
x.m = obj.m;
x.n = obj.n;
x.status = obj.status;
x.publicKeyRing = obj.publicKeyRing;
x.copayers = _.map(obj.copayers, function (copayer) {
return new Copayer(copayer);
});
2015-02-02 04:15:50 -08:00
x.pubKey = obj.pubKey;
2015-02-01 06:41:16 -08:00
2015-02-02 06:55:03 -08:00
Wallet.super_.prototype.fromObj.apply(this, [obj]);
2015-02-02 12:07:18 -08:00
return x;
2015-01-27 05:18:45 -08:00
};
2015-01-28 09:21:09 -08:00
Wallet.prototype.addCopayer = function (copayer) {
2015-02-02 12:07:18 -08:00
this.copayers.push(copayer);
2015-01-28 09:21:09 -08:00
2015-02-02 12:07:18 -08:00
if (this.copayers.length < this.n) return;
this.status = 'complete';
this.publicKeyRing = _.pluck(this.copayers, 'xPubKey');
2015-01-28 09:21:09 -08:00
};
Wallet.prototype.getCopayer = function (copayerId) {
2015-02-02 12:07:18 -08:00
return _.find(this.copayers, { id: copayerId });
2015-01-28 09:21:09 -08:00
};
2015-01-27 05:18:45 -08:00
module.exports = Wallet;