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-03-31 13:01:26 -07:00
|
|
|
|
|
|
|
var Address = require('./address');
|
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-03-12 07:34:41 -07:00
|
|
|
var Bitcore = WalletUtils.Bitcore;
|
|
|
|
var HDPublicKey = Bitcore.HDPublicKey;
|
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-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-03-04 07:44:28 -08:00
|
|
|
x.id = WalletUtils.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-02-17 16:20:08 -08:00
|
|
|
x.addressManager = AddressManager.create({
|
2015-08-31 13:13:46 -07:00
|
|
|
derivationStrategy: opts.derivationStrategy || WalletUtils.DERIVATION_STRATEGIES.BIP45,
|
2015-08-27 13:14:33 -07:00
|
|
|
copayerIndex: opts.copayerIndex,
|
2015-02-17 15:26:58 -08:00
|
|
|
});
|
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-02-03 10:44:44 -08:00
|
|
|
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-04-20 13:46:40 -07:00
|
|
|
var raw = Address.create(WalletUtils.deriveAddress(wallet.publicKeyRing, path, wallet.m, wallet.network));
|
|
|
|
var address = Address.create(_.extend(raw, {
|
|
|
|
walletId: wallet.id,
|
|
|
|
}));
|
|
|
|
|
2015-03-31 13:01:26 -07:00
|
|
|
address.isChange = isChange;
|
|
|
|
return address;
|
|
|
|
};
|
2015-02-21 14:29:42 -08:00
|
|
|
|
2015-01-27 05:18:45 -08:00
|
|
|
module.exports = Copayer;
|