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

57 lines
1.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');
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-02 06:55:03 -08:00
2015-02-01 11:50:58 -08:00
var VERSION = '1.0.0';
var MESSAGE_SIGNING_PATH = "m/1/0";
2015-01-27 05:18:45 -08:00
function Copayer(opts) {
2015-02-02 12:07:18 -08:00
opts = opts || {};
2015-02-02 15:13:13 -08:00
opts.copayerIndex = opts.copayerIndex || 0;
2015-01-27 05:18:45 -08:00
2015-02-07 07:48:57 -08:00
this.id = Uuid.v4();
2015-02-02 10:29:14 -08:00
this.version = VERSION;
2015-02-02 15:13:13 -08:00
this.createdOn = Math.floor(Date.now() / 1000);
this.name = opts.name;
this.xPubKey = opts.xPubKey;
this.xPubKeySignature = opts.xPubKeySignature; // So third parties can check independently
2015-02-07 07:48:57 -08:00
if (this.xPubKey)
this.signingPubKey = this.getSigningPubKey();
2015-02-03 10:44:44 -08:00
this.addressManager = new AddressManager({ copayerIndex: opts.copayerIndex });
2015-02-01 11:50:58 -08:00
};
2015-02-05 10:50:18 -08:00
Copayer.prototype.getPublicKey = function(path) {
return HDPublicKey
.fromString(this.xPubKey)
.derive(path)
.publicKey
.toString();
};
2015-02-02 15:13:13 -08:00
Copayer.prototype.getSigningPubKey = function() {
2015-02-05 10:50:18 -08:00
return this.getPublicKey(MESSAGE_SIGNING_PATH);
2015-01-27 05:18:45 -08:00
};
2015-02-02 15:13:13 -08:00
Copayer.fromObj = function(obj) {
2015-02-02 12:07:18 -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;
x.xPubKey = obj.xPubKey;
x.xPubKeySignature = obj.xPubKeySignature;
2015-02-02 10:29:14 -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
module.exports = Copayer;