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

68 lines
1.5 KiB
JavaScript
Raw Normal View History

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-01 11:50:58 -08:00
var Bitcore = require('bitcore');
var HDPublicKey = Bitcore.HDPublicKey;
2015-02-07 07:48:57 -08:00
var Uuid = require('uuid');
2015-02-03 10:44:44 -08:00
var AddressManager = require('./addressmanager');
2015-02-17 12:51:35 -08:00
var Utils = require('../walletutils');
2015-02-02 06:55:03 -08:00
2015-02-01 11:50:58 -08:00
var MESSAGE_SIGNING_PATH = "m/1/0";
2015-01-27 05:18:45 -08:00
2015-02-17 15:26:58 -08:00
function Copayer() {
2015-02-17 16:20:08 -08:00
this.version = '1.0.0';
2015-02-17 15:26:58 -08:00
};
Copayer.create = function(opts) {
2015-02-17 12:36:45 -08:00
$.checkArgument(opts && opts.xPubKey, 'need to provide an xPubKey');
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();
x.createdOn = Math.floor(Date.now() / 1000);
2015-02-17 12:36:45 -08:00
2015-02-17 15:26:58 -08:00
x.xPubKey = opts.xPubKey;
2015-02-01 11:50:58 -08:00
2015-02-17 15:26:58 -08:00
x.id = Utils.xPubToCopayerId(x.xPubKey);
x.name = opts.name;
x.xPubKeySignature = opts.xPubKeySignature; // So third parties can check independently
x.signingPubKey = x.getSigningPubKey();
2015-02-17 16:20:08 -08:00
x.addressManager = AddressManager.create({
2015-02-17 15:26:58 -08:00
copayerIndex: opts.copayerIndex
});
2015-02-05 10:50:18 -08:00
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-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-02-02 12:07:18 -08:00
x.xPubKeySignature = obj.xPubKeySignature;
2015-02-17 15:26:58 -08:00
x.signingPubKey = obj.signingPubKey;
2015-02-03 10:44:44 -08:00
x.addressManager = AddressManager.fromObj(obj.addressManager);
2015-01-27 05:18:45 -08:00
2015-02-02 06:55:03 -08:00
return x;
};
2015-01-27 05:18:45 -08:00
2015-02-17 15:26:58 -08:00
Copayer.prototype.getPublicKey = function(path) {
return HDPublicKey
.fromString(this.xPubKey)
.derive(path)
.publicKey
.toString();
};
Copayer.prototype.getSigningPubKey = function() {
return this.getPublicKey(MESSAGE_SIGNING_PATH);
};
2015-01-27 05:18:45 -08:00
module.exports = Copayer;