bitcore-wallet-service/lib/client/api.js

424 lines
10 KiB
JavaScript
Raw Normal View History

2015-02-12 11:42:32 -08:00
'use strict';
var _ = require('lodash');
2015-02-16 14:54:38 -08:00
var $ = require('preconditions').singleton();
2015-02-15 08:14:36 -08:00
var util = require('util');
2015-02-12 11:42:32 -08:00
var async = require('async');
var log = require('npmlog');
var request = require('request')
log.debug = log.verbose;
var Bitcore = require('bitcore')
2015-02-15 06:33:04 -08:00
var SignUtils = require('../signutils');
2015-02-16 08:10:48 -08:00
var Verifier = require('./verifier');
2015-02-16 11:23:42 -08:00
var ServerCompromisedError = require('./servercompromisederror')
2015-02-12 11:42:32 -08:00
2015-02-13 11:07:47 -08:00
var BASE_URL = 'http://localhost:3001/copay/api';
2015-02-12 11:42:32 -08:00
2015-02-13 07:45:05 -08:00
function _createProposalOpts(opts, signingKey) {
var msg = opts.toAddress + '|' + opts.amount + '|' + opts.message;
opts.proposalSignature = SignUtils.sign(msg, signingKey);
return opts;
};
2015-02-12 11:50:10 -08:00
function _getUrl(path) {
2015-02-12 11:42:32 -08:00
return BASE_URL + path;
};
2015-02-12 19:00:54 -08:00
function _parseError(body) {
if (_.isString(body)) {
2015-02-13 07:45:05 -08:00
try {
2015-02-13 08:35:20 -08:00
body = JSON.parse(body);
2015-02-13 07:45:05 -08:00
} catch (e) {
2015-02-13 08:35:20 -08:00
body = {
error: body
};
2015-02-13 07:45:05 -08:00
}
2015-02-12 19:00:54 -08:00
}
var code = body.code || 'ERROR';
var message = body.error || 'There was an unknown error processing the request';
log.error(code, message);
};
2015-02-15 06:12:04 -08:00
function _signRequest(method, url, args, privKey) {
var message = method.toLowerCase() + '|' + url + '|' + JSON.stringify(args);
2015-02-12 19:00:54 -08:00
return SignUtils.sign(message, privKey);
};
2015-02-12 11:42:32 -08:00
2015-02-13 13:24:35 -08:00
function _createXPrivKey(network) {
return new Bitcore.HDPrivateKey(network).toString();
2015-02-12 19:00:54 -08:00
};
2015-02-12 11:42:32 -08:00
2015-02-15 06:33:04 -08:00
function API(opts) {
2015-02-15 08:14:36 -08:00
if (!opts.storage) {
throw new Error('Must provide storage option');
}
this.storage = opts.storage;
this.verbose = !!opts.verbose;
2015-02-16 10:02:20 -08:00
this.request = request || opts.request;
2015-02-15 08:14:36 -08:00
if (this.verbose) {
log.level = 'debug';
2015-02-12 18:57:16 -08:00
}
2015-02-12 11:42:32 -08:00
};
2015-02-16 16:10:14 -08:00
API.prototype._tryToComplete = function(data, cb) {
var self = this;
var isCorrupted;
var url = '/v1/wallets/';
self._doGetRequest(url, data, function(err, wallet) {
if (err) return cb(err);
if (wallet.n > 0 && wallet.status === 'complete' && !data.verified) {
var pubKey = Bitcore.PrivateKey.fromString(data.walletPrivKey).toPublicKey().toString();
var fake = [];
_.each(wallet.copayers, function(copayer) {
if (!SignUtils.verify(copayer.xPubKey, copayer.xPubKeySignature, pubKey)) {
fake.push(copayer);
}
});
if (fake.length > 0) {
isCorrupted = true;
data.verified = 'corrupt';
} else {
data.verified = 'ok';
}
self.storage.save(data, function(err) {
if (isCorrupted) {
return cb('Some copayers in the wallet could not be verified to have known the wallet secret');
}
return cb(err, data);
});
}
return cb(null, data);
});
};
API.prototype._loadAndCheck = function(cb) {
var self = this;
2015-02-16 15:23:25 -08:00
this.storage.load(function(err, data) {
if (err || !data) {
return cb(err || 'Wallet file not found.');
}
2015-02-16 14:54:38 -08:00
2015-02-16 15:23:25 -08:00
if (data.verified == 'corrupt') {
return cb('The wallet is tagged as corrupt. Some of the copayers cannot be verified to have known the wallet secret.');
2015-02-16 14:54:38 -08:00
}
2015-02-16 15:23:25 -08:00
if (data.n > 1) {
var pkrComplete = data.publicKeyRing && data.m && data.publicKeyRing.length === data.n;
2015-02-16 14:54:38 -08:00
2015-02-16 16:10:14 -08:00
if (!pkrComplete) {
return self._tryToComplete(data, cb);
2015-02-16 15:23:25 -08:00
}
2015-02-12 19:00:54 -08:00
}
2015-02-16 15:23:25 -08:00
return cb(null, data);
});
2015-02-12 13:54:17 -08:00
};
2015-02-15 06:12:04 -08:00
2015-02-15 08:03:48 -08:00
API.prototype._doRequest = function(method, url, args, data, cb) {
2015-02-15 06:12:04 -08:00
var reqSignature = _signRequest(method, url, args, data.signingPrivKey);
2015-02-13 20:28:43 -08:00
var absUrl = _getUrl(url);
2015-02-15 08:14:36 -08:00
var args = {
2015-02-13 20:28:43 -08:00
headers: {
'x-identity': data.copayerId,
'x-signature': reqSignature,
},
2015-02-15 06:12:04 -08:00
method: method,
2015-02-13 20:28:43 -08:00
url: absUrl,
body: args,
json: true,
2015-02-15 08:14:36 -08:00
};
log.verbose('Request Args', util.inspect(args));
2015-02-16 10:02:20 -08:00
this.request(args, function(err, res, body) {
2015-02-15 08:14:36 -08:00
log.verbose('Response:', err, body);
2015-02-13 20:28:43 -08:00
if (err) return cb(err);
if (res.statusCode != 200) {
_parseError(body);
return cb('Request error');
}
2015-02-16 10:02:20 -08:00
2015-02-13 20:28:43 -08:00
return cb(null, body);
});
};
2015-02-15 06:33:04 -08:00
API.prototype._doPostRequest = function(url, args, data, cb) {
2015-02-13 20:28:43 -08:00
return this._doRequest('post', url, args, data, cb);
};
2015-02-15 06:33:04 -08:00
API.prototype._doGetRequest = function(url, data, cb) {
2015-02-13 20:28:43 -08:00
return this._doRequest('get', url, {}, data, cb);
};
2015-02-12 11:42:32 -08:00
2015-02-15 06:33:04 -08:00
API.prototype.createWallet = function(walletName, copayerName, m, n, network, cb) {
2015-02-12 19:00:54 -08:00
var self = this;
2015-02-13 13:24:35 -08:00
network = network || 'livenet';
2015-02-13 17:59:05 -08:00
if (!_.contains(['testnet', 'livenet'], network))
return cb('Invalid network');
2015-02-12 19:00:54 -08:00
2015-02-16 15:23:25 -08:00
this.storage.load(function(err, data) {
if (data)
return cb('Storage already contains a wallet');
console.log('[API.js.132]'); //TODO
// Generate wallet key pair to verify copayers
var privKey = new Bitcore.PrivateKey(null, network);
var pubKey = privKey.toPublicKey();
data = {
m: m,
n: n,
walletPrivKey: privKey.toWIF(),
network: network,
};
var args = {
name: walletName,
m: m,
n: n,
pubKey: pubKey.toString(),
network: network,
};
var url = '/v1/wallets/';
self._doPostRequest(url, args, data, function(err, body) {
if (err) return cb(err);
2015-02-15 06:33:04 -08:00
2015-02-16 15:23:25 -08:00
var walletId = body.walletId;
var secret = walletId + ':' + privKey.toString() + ':' + (network == 'testnet' ? 'T' : 'L');
var ret;
2015-02-15 13:26:05 -08:00
2015-02-16 15:23:25 -08:00
if (n > 1)
ret = data.secret = secret;
2015-02-12 13:54:17 -08:00
2015-02-16 15:23:25 -08:00
self.storage.save(data, function(err) {
if (err) return cb(err);
self._joinWallet(data, secret, copayerName, function(err) {
return cb(err, ret);
});
2015-02-12 11:42:32 -08:00
2015-02-16 15:23:25 -08:00
});
2015-02-12 11:42:32 -08:00
});
});
};
2015-02-15 06:33:04 -08:00
API.prototype._joinWallet = function(data, secret, copayerName, cb) {
2015-02-12 19:00:54 -08:00
var self = this;
2015-02-13 13:24:35 -08:00
data = data || {};
2015-02-12 13:54:17 -08:00
2015-02-12 19:23:59 -08:00
var secretSplit = secret.split(':');
2015-02-12 11:42:32 -08:00
var walletId = secretSplit[0];
2015-02-13 13:24:35 -08:00
2015-02-13 11:26:33 -08:00
var walletPrivKey = Bitcore.PrivateKey.fromString(secretSplit[1]);
2015-02-13 13:24:35 -08:00
var network = secretSplit[2] == 'T' ? 'testnet' : 'livenet';
2015-02-13 13:53:49 -08:00
data.xPrivKey = _createXPrivKey(network);
2015-02-12 11:42:32 -08:00
2015-02-12 19:00:54 -08:00
var xPubKey = new Bitcore.HDPublicKey(data.xPrivKey);
2015-02-13 11:26:33 -08:00
var xPubKeySignature = SignUtils.sign(xPubKey.toString(), walletPrivKey);
2015-02-12 19:23:59 -08:00
var signingPrivKey = (new Bitcore.HDPrivateKey(data.xPrivKey)).derive('m/1/0').privateKey;
2015-02-12 11:42:32 -08:00
var args = {
walletId: walletId,
name: copayerName,
2015-02-12 19:00:54 -08:00
xPubKey: xPubKey.toString(),
2015-02-12 11:42:32 -08:00
xPubKeySignature: xPubKeySignature,
};
2015-02-13 20:28:43 -08:00
var url = '/v1/wallets/' + walletId + '/copayers';
2015-02-12 11:42:32 -08:00
2015-02-13 20:28:43 -08:00
this._doPostRequest(url, args, data, function(err, body) {
2015-02-12 19:00:54 -08:00
var wallet = body.wallet;
data.copayerId = body.copayerId;
2015-02-16 14:54:38 -08:00
data.walletPrivKey = walletPrivKey.toWIF();
2015-02-12 19:00:54 -08:00
data.signingPrivKey = signingPrivKey.toString();
data.m = wallet.m;
data.n = wallet.n;
data.publicKeyRing = wallet.publicKeyRing;
2015-02-16 14:54:38 -08:00
data.network = wallet.network,
2015-02-16 15:23:25 -08:00
self.storage.save(data, cb);
2015-02-12 19:00:54 -08:00
});
};
2015-02-12 13:54:17 -08:00
2015-02-15 06:33:04 -08:00
API.prototype.joinWallet = function(secret, copayerName, cb) {
2015-02-12 19:00:54 -08:00
var self = this;
2015-02-12 13:54:17 -08:00
2015-02-16 15:23:25 -08:00
this.storage.load(function(err, data) {
if (data)
return cb('Storage already contains a wallet');
2015-02-12 13:54:17 -08:00
2015-02-16 15:23:25 -08:00
self._joinWallet(data, secret, copayerName, cb);
});
2015-02-12 11:42:32 -08:00
};
2015-02-15 06:33:04 -08:00
API.prototype.getStatus = function(cb) {
2015-02-12 19:00:54 -08:00
var self = this;
2015-02-16 16:10:14 -08:00
this._loadAndCheck(function(err, data) {
2015-02-12 11:42:32 -08:00
if (err) return cb(err);
2015-02-16 15:23:25 -08:00
var url = '/v1/wallets/';
self._doGetRequest(url, data, function(err, body) {
2015-02-16 16:10:14 -08:00
return cb(err, body);
2015-02-16 15:23:25 -08:00
});
2015-02-12 11:42:32 -08:00
});
};
2015-02-13 07:45:05 -08:00
/**
* send
*
* @param inArgs
* @param inArgs.toAddress
* @param inArgs.amount
* @param inArgs.message
*/
2015-02-15 06:33:04 -08:00
API.prototype.sendTxProposal = function(inArgs, cb) {
2015-02-13 06:38:25 -08:00
var self = this;
2015-02-16 16:10:14 -08:00
this._loadAndCheck(
function(err, data) {
if (err) return cb(err);
2015-02-13 07:55:07 -08:00
2015-02-16 16:10:14 -08:00
var args = _createProposalOpts(inArgs, data.signingPrivKey);
2015-02-13 07:55:07 -08:00
2015-02-16 16:10:14 -08:00
var url = '/v1/txproposals/';
self._doPostRequest(url, args, data, cb);
});
2015-02-13 07:55:07 -08:00
};
2015-02-15 06:33:04 -08:00
API.prototype.createAddress = function(cb) {
2015-02-13 07:55:07 -08:00
var self = this;
2015-02-16 16:10:14 -08:00
this._loadAndCheck(
function(err, data) {
2015-02-16 15:23:25 -08:00
if (err) return cb(err);
2015-02-16 16:10:14 -08:00
var url = '/v1/addresses/';
self._doPostRequest(url, {}, data, function(err, address) {
if (err) return cb(err);
if (!Verifier.checkAddress(data, address)) {
return cb(new ServerCompromisedError('Server sent fake address'));
}
return cb(null, address);
});
2015-02-16 15:23:25 -08:00
});
2015-02-12 11:42:32 -08:00
};
2015-02-15 06:33:04 -08:00
API.prototype.history = function(limit, cb) {
2015-02-12 11:42:32 -08:00
};
2015-02-15 06:33:04 -08:00
API.prototype.getBalance = function(cb) {
2015-02-13 08:35:20 -08:00
var self = this;
2015-02-16 16:10:14 -08:00
this._loadAndCheck(function(err, data) {
2015-02-16 15:23:25 -08:00
if (err) return cb(err);
var url = '/v1/balance/';
self._doGetRequest(url, data, cb);
});
2015-02-13 08:35:20 -08:00
};
2015-02-15 06:33:04 -08:00
API.prototype.getTxProposals = function(opts, cb) {
2015-02-13 08:35:20 -08:00
var self = this;
2015-02-16 16:10:14 -08:00
this._loadAndCheck(
function(err, data) {
if (err) return cb(err);
var url = '/v1/txproposals/';
self._doGetRequest(url, data, cb);
});
2015-02-13 08:35:20 -08:00
};
2015-02-15 06:33:04 -08:00
API.prototype.signTxProposal = function(txp, cb) {
2015-02-13 12:02:56 -08:00
var self = this;
2015-02-16 16:10:14 -08:00
this._loadAndCheck(
function(err, data) {
if (err) return cb(err);
2015-02-13 12:02:56 -08:00
2015-02-13 13:53:49 -08:00
2015-02-16 16:10:14 -08:00
//Derive proper key to sign, for each input
var privs = [],
derived = {};
2015-02-13 12:02:56 -08:00
2015-02-16 16:10:14 -08:00
var network = new Bitcore.Address(txp.toAddress).network.name;
var xpriv = new Bitcore.HDPrivateKey(data.xPrivKey, network);
2015-02-13 12:02:56 -08:00
2015-02-16 16:10:14 -08:00
_.each(txp.inputs, function(i) {
if (!derived[i.path]) {
derived[i.path] = xpriv.derive(i.path).privateKey;
}
privs.push(derived[i.path]);
});
2015-02-13 12:02:56 -08:00
2015-02-16 16:10:14 -08:00
var t = new Bitcore.Transaction();
_.each(txp.inputs, function(i) {
t.from(i, i.publicKeys, txp.requiredSignatures);
});
2015-02-13 12:02:56 -08:00
2015-02-16 16:10:14 -08:00
t.to(txp.toAddress, txp.amount)
.change(txp.changeAddress)
.sign(privs);
2015-02-13 13:24:35 -08:00
2015-02-16 16:10:14 -08:00
var signatures = [];
_.each(privs, function(p) {
var s = t.getSignatures(p)[0].signature.toDER().toString('hex');
signatures.push(s);
});
2015-02-13 13:53:49 -08:00
2015-02-16 16:10:14 -08:00
var url = '/v1/txproposals/' + txp.id + '/signatures/';
var args = {
signatures: signatures
};
2015-02-16 15:23:25 -08:00
2015-02-16 16:10:14 -08:00
self._doPostRequest(url, args, data, cb);
});
2015-02-13 12:02:56 -08:00
};
2015-02-15 06:33:04 -08:00
API.prototype.rejectTxProposal = function(txp, reason, cb) {
2015-02-13 17:51:40 -08:00
var self = this;
2015-02-16 16:10:14 -08:00
this._loadAndCheck(
function(err, data) {
if (err) return cb(err);
2015-02-16 15:23:25 -08:00
2015-02-16 16:10:14 -08:00
var url = '/v1/txproposals/' + txp.id + '/rejections/';
var args = {
reason: reason || '',
};
self._doPostRequest(url, args, data, cb);
});
2015-02-13 17:51:40 -08:00
};
2015-02-13 08:35:20 -08:00
2015-02-15 13:52:48 -08:00
API.prototype.broadcastTxProposal = function(txp, cb) {
var self = this;
2015-02-16 16:10:14 -08:00
this._loadAndCheck(
function(err, data) {
if (err) return cb(err);
2015-02-16 15:23:25 -08:00
2015-02-16 16:10:14 -08:00
var url = '/v1/txproposals/' + txp.id + '/broadcast/';
self._doPostRequest(url, {}, data, cb);
});
2015-02-15 13:52:48 -08:00
};
2015-02-15 08:03:48 -08:00
API.prototype.removeTxProposal = function(txp, cb) {
2015-02-14 07:54:00 -08:00
var self = this;
2015-02-16 16:10:14 -08:00
this._loadAndCheck(
function(err, data) {
if (err) return cb(err);
var url = '/v1/txproposals/' + txp.id;
self._doRequest('delete', url, {}, data, cb);
});
2015-02-14 07:54:00 -08:00
};
2015-02-15 06:33:04 -08:00
module.exports = API;