2015-01-27 05:18:45 -08:00
|
|
|
'use strict';
|
|
|
|
|
2015-02-17 12:36:45 -08:00
|
|
|
var $ = require('preconditions').singleton();
|
2015-01-27 05:18:45 -08:00
|
|
|
var _ = require('lodash');
|
2015-02-02 06:55:03 -08:00
|
|
|
var util = require('util');
|
2015-02-07 07:48:57 -08:00
|
|
|
var Uuid = require('uuid');
|
2015-10-30 11:24:47 -07:00
|
|
|
var sjcl = require('sjcl');
|
2015-03-31 13:01:26 -07:00
|
|
|
|
|
|
|
var Address = require('./address');
|
2015-02-03 10:44:44 -08:00
|
|
|
var AddressManager = require('./addressmanager');
|
2015-10-30 11:24:47 -07:00
|
|
|
var Bitcore = require('bitcore-lib');
|
2015-10-30 13:16:20 -07:00
|
|
|
|
2015-11-03 07:21:31 -08:00
|
|
|
var Constants = require('../common/constants');
|
2015-02-02 06:55:03 -08:00
|
|
|
|
2015-08-27 06:47:32 -07:00
|
|
|
function Copayer() {};
|
2015-08-04 07:19:08 -07:00
|
|
|
|
2015-10-30 11:24:47 -07:00
|
|
|
Copayer._xPubToCopayerId = function(xpub) {
|
|
|
|
var hash = sjcl.hash.sha256.hash(xpub);
|
|
|
|
return sjcl.codec.hex.fromBits(hash);
|
|
|
|
};
|
|
|
|
|
2015-02-17 15:26:58 -08:00
|
|
|
Copayer.create = function(opts) {
|
2015-03-09 14:11:25 -07:00
|
|
|
opts = opts || {};
|
2015-08-14 11:52:46 -07:00
|
|
|
$.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');
|
2015-02-17 15:26:58 -08:00
|
|
|
|
2015-02-02 15:13:13 -08:00
|
|
|
opts.copayerIndex = opts.copayerIndex || 0;
|
2015-01-27 05:18:45 -08:00
|
|
|
|
2015-02-17 15:26:58 -08:00
|
|
|
var x = new Copayer();
|
2015-02-17 12:36:45 -08:00
|
|
|
|
2015-08-27 06:47:32 -07:00
|
|
|
x.version = 2;
|
|
|
|
x.createdOn = Math.floor(Date.now() / 1000);
|
2015-02-17 15:26:58 -08:00
|
|
|
x.xPubKey = opts.xPubKey;
|
2015-10-30 11:24:47 -07:00
|
|
|
x.id = Copayer._xPubToCopayerId(x.xPubKey);
|
2015-02-17 15:26:58 -08:00
|
|
|
x.name = opts.name;
|
2015-08-05 16:32:20 -07:00
|
|
|
x.requestPubKey = opts.requestPubKey;
|
|
|
|
x.signature = opts.signature;
|
2015-08-14 11:52:46 -07:00
|
|
|
x.requestPubKeys = [{
|
|
|
|
key: opts.requestPubKey,
|
|
|
|
signature: opts.signature,
|
|
|
|
}];
|
|
|
|
|
2015-11-03 07:21:31 -08:00
|
|
|
var derivationStrategy = opts.derivationStrategy || Constants.DERIVATION_STRATEGIES.BIP45;
|
2015-08-31 13:38:39 -07:00
|
|
|
if (AddressManager.supportsCopayerBranches(derivationStrategy)) {
|
|
|
|
x.addressManager = AddressManager.create({
|
|
|
|
derivationStrategy: derivationStrategy,
|
|
|
|
copayerIndex: opts.copayerIndex,
|
|
|
|
});
|
|
|
|
}
|
2015-02-05 10:50:18 -08:00
|
|
|
|
2015-08-25 12:12:47 -07:00
|
|
|
x.customData = opts.customData;
|
|
|
|
|
2015-02-17 15:26:58 -08:00
|
|
|
return x;
|
2015-01-27 05:18:45 -08:00
|
|
|
};
|
|
|
|
|
2015-02-02 15:13:13 -08:00
|
|
|
Copayer.fromObj = function(obj) {
|
2015-02-17 15:26:58 -08:00
|
|
|
var x = new Copayer();
|
2015-01-27 05:18:45 -08:00
|
|
|
|
2015-08-27 06:47:32 -07:00
|
|
|
x.version = obj.version;
|
2015-02-02 12:07:18 -08:00
|
|
|
x.createdOn = obj.createdOn;
|
|
|
|
x.id = obj.id;
|
|
|
|
x.name = obj.name;
|
2015-02-17 15:26:58 -08:00
|
|
|
x.xPubKey = obj.xPubKey;
|
2015-08-05 16:32:20 -07:00
|
|
|
x.requestPubKey = obj.requestPubKey;
|
|
|
|
x.signature = obj.signature;
|
|
|
|
|
2015-08-27 06:47:32 -07:00
|
|
|
if (parseInt(x.version) == 1) {
|
2015-08-14 11:52:46 -07:00
|
|
|
x.requestPubKeys = [{
|
|
|
|
key: x.requestPubKey,
|
|
|
|
signature: x.signature,
|
|
|
|
}];
|
|
|
|
x.version = 2;
|
|
|
|
} else {
|
|
|
|
x.requestPubKeys = obj.requestPubKeys;
|
|
|
|
}
|
|
|
|
|
2015-08-31 13:38:39 -07:00
|
|
|
if (obj.addressManager) {
|
|
|
|
x.addressManager = AddressManager.fromObj(obj.addressManager);
|
|
|
|
}
|
2015-08-25 12:12:47 -07:00
|
|
|
x.customData = obj.customData;
|
|
|
|
|
2015-02-02 06:55:03 -08:00
|
|
|
return x;
|
|
|
|
};
|
2015-01-27 05:18:45 -08:00
|
|
|
|
2015-03-31 13:01:26 -07:00
|
|
|
Copayer.prototype.createAddress = function(wallet, isChange) {
|
|
|
|
$.checkState(wallet.isComplete());
|
|
|
|
|
|
|
|
var path = this.addressManager.getNewAddressPath(isChange);
|
2015-11-03 07:21:31 -08:00
|
|
|
var address = Address.derive(wallet.id, wallet.addressType, wallet.publicKeyRing, path, wallet.m, wallet.network, isChange);
|
2015-03-31 13:01:26 -07:00
|
|
|
return address;
|
|
|
|
};
|
2015-02-21 14:29:42 -08:00
|
|
|
|
2015-01-27 05:18:45 -08:00
|
|
|
module.exports = Copayer;
|