Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #138 from isocolsky/ref/bip32
Browse files Browse the repository at this point in the history
Ref/bip32
  • Loading branch information
matiu committed Mar 10, 2015
2 parents 181ba74 + e109a43 commit 22542ee
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 181 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ Returns:
Required Arguments:
* walletId: Id of the wallet to join
* name: Copayer Name
* xPubKey: Peer's extended public key
* xPubKeySignature: xPubKey signature with Wallet Creation private key
* xPubKey - Extended Public Key for this copayer.
* requestPubKey - Public Key used to check requests from this copayer.
* copayerSignature - Signature sed by other copayers to verify the that the copayer joining knows the wallet secret.

Returns:
* copayerId: Assigned ID of the copayer (to be used on x-identity header)
Expand Down
23 changes: 6 additions & 17 deletions lib/model/copayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ function Copayer() {
};

Copayer.create = function(opts) {
$.checkArgument(opts && opts.xPubKey, 'need to provide an xPubKey');
opts = opts || {};
$.checkArgument(opts.xPubKey, 'Missing extended public key');
$.checkArgument(opts.requestPubKey, 'Missing request public key');

opts.copayerIndex = opts.copayerIndex || 0;

Expand All @@ -27,8 +29,8 @@ Copayer.create = function(opts) {

x.id = WalletUtils.xPubToCopayerId(x.xPubKey);
x.name = opts.name;
x.xPubKeySignature = opts.xPubKeySignature; // So third parties can check independently
x.requestPubKey = x.getRequestPubKey();
x.signature = opts.signature; // So third parties can check independently
x.requestPubKey = opts.requestPubKey;
x.addressManager = AddressManager.create({
copayerIndex: opts.copayerIndex
});
Expand All @@ -43,25 +45,12 @@ Copayer.fromObj = function(obj) {
x.id = obj.id;
x.name = obj.name;
x.xPubKey = obj.xPubKey;
x.xPubKeySignature = obj.xPubKeySignature;
x.requestPubKey = obj.requestPubKey;
x.signature = obj.signature;
x.addressManager = AddressManager.fromObj(obj.addressManager);

return x;
};

Copayer.prototype.getPublicKey = function(path) {
return HDPublicKey
.fromString(this.xPubKey)
.derive(path)
.publicKey
.toString();
};

Copayer.prototype.getRequestPubKey = function() {
return this.getPublicKey('m/1/1');
};



module.exports = Copayer;
10 changes: 3 additions & 7 deletions lib/model/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ Wallet.prototype.addCopayer = function(copayer) {
if (this.copayers.length < this.n) return;

this.status = 'complete';
this.publicKeyRing = _.pluck(this.copayers, 'xPubKey');
this.publicKeyRing = _.map(this.copayers, function(copayer) {
return _.pick(copayer, ['xPubKey', 'requestPubKey']);
});
};

Wallet.prototype.getCopayer = function(copayerId) {
Expand All @@ -108,12 +110,6 @@ Wallet.prototype.getNetworkName = function() {
return this.network;
};


Wallet.prototype.getPublicKey = function(copayerId, path) {
var copayer = this.getCopayer(copayerId);
return copayer.getPublicKey(path);
};

Wallet.prototype.isComplete = function() {
return this.status == 'complete';
};
Expand Down
17 changes: 10 additions & 7 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,14 @@ WalletService.prototype._notify = function(type, data) {
* @param {Object} opts
* @param {string} opts.walletId - The wallet id.
* @param {string} opts.name - The copayer name.
* @param {number} opts.xPubKey - Extended Public Key for this copayer.
* @param {number} opts.xPubKeySignature - Signature of xPubKey using the wallet pubKey.
* @param {string} opts.xPubKey - Extended Public Key for this copayer.
* @param {string} opts.requestPubKey - Public Key used to check requests from this copayer.
* @param {string} opts.copayerSignature - S(name|xPubKey|requestPubKey). Used by other copayers to verify the that the copayer joining knows the wallet secret.
*/
WalletService.prototype.joinWallet = function(opts, cb) {
var self = this;

if (!Utils.checkRequired(opts, ['walletId', 'name', 'xPubKey', 'xPubKeySignature']))
if (!Utils.checkRequired(opts, ['walletId', 'name', 'xPubKey', 'requestPubKey', 'copayerSignature']))
return cb(new ClientError('Required argument missing'));

if (_.isEmpty(opts.name))
Expand All @@ -208,7 +209,8 @@ WalletService.prototype.joinWallet = function(opts, cb) {
if (err) return cb(err);
if (!wallet) return cb(new ClientError('Wallet not found'));

if (!self._verifySignature(opts.xPubKey, opts.xPubKeySignature, wallet.pubKey)) {
var hash = WalletUtils.getCopayerHash(opts.name, opts.xPubKey, opts.requestPubKey);
if (!self._verifySignature(hash, opts.copayerSignature, wallet.pubKey)) {
return cb(new ClientError());
}

Expand All @@ -221,9 +223,10 @@ WalletService.prototype.joinWallet = function(opts, cb) {

var copayer = Copayer.create({
name: opts.name,
xPubKey: opts.xPubKey,
xPubKeySignature: opts.xPubKeySignature,
copayerIndex: wallet.copayers.length,
xPubKey: opts.xPubKey,
requestPubKey: opts.requestPubKey,
signature: opts.copayerSignature,
});

self.storage.fetchCopayerLookup(copayer.id, function(err, res) {
Expand Down Expand Up @@ -497,7 +500,7 @@ WalletService.prototype._selectUtxos = function(txp, utxos) {
* @param {string} opts.toAddress - Destination address.
* @param {number} opts.amount - Amount to transfer in satoshi.
* @param {string} opts.message - A message to attach to this transaction.
* @param {string} opts.proposalSignature - S(toAddress + '|' + amount + '|' + message). Used by other copayers to verify the proposal. Optional in 1-of-1 wallets.
* @param {string} opts.proposalSignature - S(toAddress|amount|message). Used by other copayers to verify the proposal.
* @returns {TxProposal} Transaction proposal.
*/
WalletService.prototype.createTx = function(opts, cb) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "bitcore-wallet-service",
"description": "A service for Mutisig HD Bitcoin Wallets",
"author": "BitPay Inc",
"version": "0.0.3",
"version": "0.0.4",
"keywords": [
"bitcoin",
"copay",
Expand All @@ -20,7 +20,7 @@
"dependencies": {
"async": "^0.9.0",
"bitcore": "^0.11.2",
"bitcore-wallet-utils": "0.0.1",
"bitcore-wallet-utils": "0.0.2",
"bitcore-explorers": "^0.9.1",
"body-parser": "^1.11.0",
"coveralls": "^2.11.2",
Expand Down
Loading

0 comments on commit 22542ee

Please sign in to comment.