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

155 lines
3.4 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');
var $ = require('preconditions').singleton();
2015-02-07 08:13:29 -08:00
var Uuid = require('uuid');
2015-02-02 11:16:14 -08:00
var Address = require('./address');
2015-01-28 09:21:09 -08:00
var Copayer = require('./copayer');
2015-02-03 10:44:44 -08:00
var AddressManager = require('./addressmanager');
2015-03-04 07:44:28 -08:00
var WalletUtils = require('bitcore-wallet-utils');
2015-02-02 06:55:03 -08:00
2015-02-17 15:58:04 -08:00
function Wallet() {
2015-02-17 16:20:08 -08:00
this.version = '1.0.0';
2015-02-17 15:58:04 -08:00
};
Wallet.create = function(opts) {
opts = opts || {};
var x = new Wallet();
x.createdOn = Math.floor(Date.now() / 1000);
2015-03-31 13:28:01 -07:00
x.id = opts.id || Uuid.v4();
2015-02-17 15:58:04 -08:00
x.name = opts.name;
x.m = opts.m;
x.n = opts.n;
x.status = 'pending';
2015-04-02 07:18:39 -07:00
x.scanning = false;
2015-02-17 15:58:04 -08:00
x.publicKeyRing = [];
x.addressIndex = 0;
x.copayers = [];
x.pubKey = opts.pubKey;
x.network = opts.network;
2015-02-17 16:20:08 -08:00
x.addressManager = AddressManager.create();
2015-02-17 15:58:04 -08:00
return x;
};
Wallet.fromObj = function(obj) {
var x = new Wallet();
x.createdOn = obj.createdOn;
x.id = obj.id;
x.name = obj.name;
x.m = obj.m;
x.n = obj.n;
x.status = obj.status;
2015-04-02 07:18:39 -07:00
x.scanning = obj.scanning;
2015-02-17 15:58:04 -08:00
x.publicKeyRing = obj.publicKeyRing;
x.copayers = _.map(obj.copayers, function(copayer) {
return Copayer.fromObj(copayer);
});
x.pubKey = obj.pubKey;
x.network = obj.network;
x.addressManager = AddressManager.fromObj(obj.addressManager);
return x;
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-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 15:13:13 -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
};
2015-02-10 05:22:23 -08:00
Wallet.prototype.isShared = function() {
return this.n > 1;
};
2015-04-01 12:10:03 -07:00
Wallet.prototype._updatePublicKeyRing = function() {
this.publicKeyRing = _.map(this.copayers, function(copayer) {
return _.pick(copayer, ['xPubKey', 'requestPubKey', 'isTemporaryRequestKey']);
});
};
2015-02-02 15:13:13 -08:00
Wallet.prototype.addCopayer = function(copayer) {
2015-01-28 09:21:09 -08:00
2015-04-01 12:10:03 -07:00
this.copayers.push(copayer);
2015-02-02 12:07:18 -08:00
if (this.copayers.length < this.n) return;
2015-02-02 15:13:13 -08:00
2015-02-02 12:07:18 -08:00
this.status = 'complete';
2015-04-01 12:10:03 -07:00
this._updatePublicKeyRing();
2015-01-28 09:21:09 -08:00
};
2015-04-01 13:55:40 -07:00
Wallet.prototype.updateCopayerRequestKey = function(copayerId, requestPubKey, signature) {
2015-03-31 13:28:01 -07:00
$.checkState(this.copayers.length == this.n);
var c = _.find(this.copayers, {
id: copayerId,
});
$.checkState(c)
2015-04-01 12:10:03 -07:00
.checkState(c.isTemporaryRequestKey);
2015-03-31 13:28:01 -07:00
c.requestPubKey = requestPubKey;
c.isTemporaryRequestKey = false;
2015-04-01 13:55:40 -07:00
c.signature = signature;
2015-04-01 12:10:03 -07:00
this._updatePublicKeyRing();
2015-03-31 13:28:01 -07:00
};
2015-02-02 15:13:13 -08:00
Wallet.prototype.getCopayer = function(copayerId) {
return _.find(this.copayers, {
id: copayerId
});
2015-01-28 09:21:09 -08:00
};
2015-02-05 12:22:38 -08:00
Wallet.prototype.getNetworkName = function() {
2015-02-16 06:17:44 -08:00
return this.network;
2015-02-02 11:16:14 -08:00
};
Wallet.prototype.isComplete = function() {
return this.status == 'complete';
};
2015-04-02 07:18:39 -07:00
Wallet.prototype.isScanning = function() {
return this.scanning;
};
2015-02-03 10:44:44 -08:00
Wallet.prototype.createAddress = function(isChange) {
$.checkState(this.isComplete());
2015-02-03 10:44:44 -08:00
var path = this.addressManager.getNewAddressPath(isChange);
2015-02-21 14:13:15 -08:00
var address = Address.create(WalletUtils.deriveAddress(this.publicKeyRing, path, this.m, this.network));
address.isChange = isChange;
return address;
2015-02-02 11:16:14 -08:00
};
2015-01-27 05:18:45 -08:00
module.exports = Wallet;