bitcore-wallet-service/lib/clilib.js

408 lines
9.3 KiB
JavaScript
Raw Normal View History

2015-02-12 11:42:32 -08:00
'use strict';
var _ = require('lodash');
var async = require('async');
var log = require('npmlog');
var request = require('request')
log.debug = log.verbose;
log.level = 'debug';
var fs = require('fs')
var Bitcore = require('bitcore')
2015-02-12 11:50:10 -08:00
var SignUtils = require('./signutils');
2015-02-12 11:42:32 -08:00
var BASE_URL = 'http://localhost:3001/copay/api/';
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);
};
function _signRequest(url, args, privKey) {
2015-02-12 19:50:57 -08:00
var message = 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-12 19:00:54 -08:00
function _createXPrivKey() {
return new Bitcore.HDPrivateKey().toString();
};
2015-02-12 11:42:32 -08:00
2015-02-12 19:00:54 -08:00
function CliLib(opts) {
2015-02-12 18:57:16 -08:00
if (!opts.filename) {
throw new Error('Please set the config filename');
}
2015-02-12 19:00:54 -08:00
this.filename = opts.filename;
2015-02-12 11:42:32 -08:00
};
2015-02-12 18:57:16 -08:00
2015-02-12 19:50:57 -08:00
CliLib.prototype._save = function(data) {
2015-02-12 19:00:54 -08:00
fs.writeFileSync(this.filename, JSON.stringify(data));
2015-02-12 11:42:32 -08:00
};
2015-02-12 19:00:54 -08:00
CliLib.prototype._load = function() {
2015-02-12 11:42:32 -08:00
try {
2015-02-12 19:00:54 -08:00
return JSON.parse(fs.readFileSync(this.filename));
2015-02-12 11:42:32 -08:00
} catch (ex) {}
};
2015-02-12 19:00:54 -08:00
CliLib.prototype._loadAndCheck = function() {
var data = this._load();
if (!data) {
log.error('Wallet file not found.');
process.exit(1);
}
2015-02-13 08:35:20 -08:00
if (data.verified == 'corrupt') {
log.error('The wallet is tagged as corrupt. Some of the copayers cannot be verified to have known the wallet secret.');
process.exit(1);
}
2015-02-12 19:00:54 -08:00
if (data.n > 1) {
var pkrComplete = data.publicKeyRing && data.m && data.publicKeyRing.length === data.n;
if (!pkrComplete) {
log.warn('The file ' + this.filename + ' is incomplete. It will allow you to operate with the wallet but it should not be trusted as a backup. Please wait for all copayers to join the wallet and run the tool with -export flag.')
}
}
return data;
2015-02-12 13:54:17 -08:00
};
2015-02-12 11:42:32 -08:00
2015-02-12 19:00:54 -08:00
CliLib.prototype.createWallet = function(walletName, copayerName, m, n, network, cb) {
var self = this;
var data = this._load();
if (data) return cb('File ' + this.filename + ' already contains a wallet');
2015-02-12 13:54:17 -08:00
2015-02-13 05:58:49 -08:00
// Generate wallet key pair to verify copayers
var privKey = new Bitcore.PrivateKey();
var pubKey = privKey.toPublicKey();
2015-02-12 13:54:17 -08:00
data = {
xPrivKey: _createXPrivKey(),
m: m,
2015-02-13 05:58:49 -08:00
n: n,
walletPrivKey: privKey.toString(),
2015-02-12 13:54:17 -08:00
};
2015-02-12 11:42:32 -08:00
var args = {
name: walletName,
m: m,
n: n,
pubKey: pubKey.toString(),
2015-02-12 19:00:54 -08:00
network: network || 'livenet',
2015-02-12 11:42:32 -08:00
};
request({
method: 'post',
2015-02-12 19:50:57 -08:00
url: _getUrl('/v1/wallets/'),
2015-02-12 11:42:32 -08:00
body: args,
json: true,
}, function(err, res, body) {
if (err) return cb(err);
2015-02-12 19:00:54 -08:00
if (res.statusCode != 200) {
_parseError(body);
return cb('Request error');
}
2015-02-12 11:42:32 -08:00
2015-02-12 19:00:54 -08:00
var walletId = body.walletId;
2015-02-12 19:23:59 -08:00
var secret = walletId + ':' + privKey.toString();
2015-02-12 19:00:54 -08:00
data.secret = secret;
2015-02-12 13:54:17 -08:00
2015-02-12 19:00:54 -08:00
self._save(data);
self._joinWallet(data, secret, copayerName, function(err) {
2015-02-12 11:42:32 -08:00
if (err) return cb(err);
2015-02-12 13:54:17 -08:00
return cb(null, data.secret);
2015-02-12 11:42:32 -08:00
});
});
};
2015-02-12 19:00:54 -08:00
CliLib.prototype._joinWallet = function(data, secret, copayerName, cb) {
var self = this;
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 11:26:33 -08:00
var walletPrivKey = Bitcore.PrivateKey.fromString(secretSplit[1]);
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,
};
request({
method: 'post',
2015-02-12 19:50:57 -08:00
url: _getUrl('/v1/wallets/' + walletId + '/copayers'),
2015-02-12 11:42:32 -08:00
body: args,
json: true,
}, function(err, res, body) {
if (err) return cb(err);
2015-02-12 19:00:54 -08:00
if (res.statusCode != 200) {
_parseError(body);
return cb('Request error');
}
var wallet = body.wallet;
data.copayerId = body.copayerId;
2015-02-13 11:26:33 -08:00
data.walletPrivKey = walletPrivKey;
2015-02-12 19:00:54 -08:00
data.signingPrivKey = signingPrivKey.toString();
data.m = wallet.m;
data.n = wallet.n;
data.publicKeyRing = wallet.publicKeyRing;
self._save(data);
return cb();
});
};
2015-02-12 13:54:17 -08:00
2015-02-12 19:00:54 -08:00
CliLib.prototype.joinWallet = function(secret, copayerName, cb) {
var self = this;
2015-02-12 13:54:17 -08:00
2015-02-12 19:00:54 -08:00
var data = this._load();
if (data) return cb('File ' + this.filename + ' already contains a wallet');
2015-02-12 13:54:17 -08:00
2015-02-12 19:00:54 -08:00
data = {
xPrivKey: _createXPrivKey(),
};
self._joinWallet(data, secret, copayerName, cb);
2015-02-12 11:42:32 -08:00
};
2015-02-12 19:00:54 -08:00
CliLib.prototype.status = function(cb) {
var self = this;
var data = this._loadAndCheck();
2015-02-12 13:54:17 -08:00
2015-02-12 19:50:57 -08:00
var url = '/v1/wallets/';
var signature = _signRequest(url, {}, data.signingPrivKey);
2015-02-12 13:54:17 -08:00
2015-02-12 11:42:32 -08:00
request({
2015-02-12 13:54:17 -08:00
headers: {
'x-identity': data.copayerId,
'x-signature': signature,
},
2015-02-12 11:42:32 -08:00
method: 'get',
2015-02-12 13:54:17 -08:00
url: _getUrl(url),
2015-02-12 19:00:54 -08:00
json: true,
2015-02-12 11:42:32 -08:00
}, function(err, res, body) {
if (err) return cb(err);
2015-02-12 19:00:54 -08:00
if (res.statusCode != 200) {
_parseError(body);
return cb('Request error');
}
var wallet = body;
2015-02-13 08:35:20 -08:00
// TODO
//console.log('[clilib.js.214:wallet:]',wallet); //TODO
2015-02-13 05:58:49 -08:00
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) {
2015-02-13 08:35:20 -08:00
console.log('[clilib.js.224]', copayer.xPubKey, copayer.xPubKeySignature, pubKey); //TODO
2015-02-13 05:58:49 -08:00
if (!SignUtils.verify(copayer.xPubKey, copayer.xPubKeySignature, pubKey)) {
2015-02-13 08:35:20 -08:00
console.log('[clilib.js.227] FAKE'); //TODO
fake.push(copayer);
}
});
if (fake.length > 0) {
log.error('Some copayers in the wallet could not be verified to have known the wallet secret');
data.verified = 'corrupt';
} else {
data.verified = 'ok';
}
self._save(data);
}
2015-02-12 11:42:32 -08:00
return cb(null, wallet);
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
*/
CliLib.prototype.send = function(inArgs, cb) {
2015-02-13 06:38:25 -08:00
var self = this;
var data = this._loadAndCheck();
2015-02-13 07:45:05 -08:00
var args = _createProposalOpts(inArgs, data.signingPrivKey);
2015-02-13 06:38:25 -08:00
2015-02-13 07:45:05 -08:00
var url = '/v1/txproposals/';
var signature = _signRequest(url, args, data.signingPrivKey);
2015-02-13 06:38:25 -08:00
request({
headers: {
'x-identity': data.copayerId,
'x-signature': signature,
},
2015-02-13 07:45:05 -08:00
method: 'post',
2015-02-13 06:38:25 -08:00
url: _getUrl(url),
2015-02-13 07:45:05 -08:00
body: args,
2015-02-13 06:38:25 -08:00
json: true,
}, function(err, res, body) {
if (err) return cb(err);
if (res.statusCode != 200) {
_parseError(body);
return cb('Request error');
}
2015-02-13 07:45:05 -08:00
return cb(null, body);
2015-02-13 06:38:25 -08:00
});
2015-02-12 11:42:32 -08:00
};
2015-02-13 08:35:20 -08:00
// TODO check change address
2015-02-12 19:00:54 -08:00
CliLib.prototype.sign = function(proposalId, cb) {
2015-02-12 11:42:32 -08:00
};
2015-02-12 19:00:54 -08:00
CliLib.prototype.reject = function(proposalId, cb) {
2015-02-12 11:42:32 -08:00
};
2015-02-13 07:55:07 -08:00
// Get addresses
CliLib.prototype.addresses = function(cb) {
var self = this;
var data = this._loadAndCheck();
var url = '/v1/addresses/';
var signature = _signRequest(url, {}, data.signingPrivKey);
request({
headers: {
'x-identity': data.copayerId,
'x-signature': signature,
},
method: 'get',
url: _getUrl(url),
json: true,
}, function(err, res, body) {
if (err) return cb(err);
if (res.statusCode != 200) {
_parseError(body);
return cb('Request error');
}
return cb(null, body);
});
};
// Creates a new address
// TODO: verify derivation!!
2015-02-12 19:00:54 -08:00
CliLib.prototype.address = function(cb) {
2015-02-13 07:55:07 -08:00
var self = this;
var data = this._loadAndCheck();
var url = '/v1/addresses/';
var signature = _signRequest(url, {}, data.signingPrivKey);
2015-02-12 11:42:32 -08:00
2015-02-13 07:55:07 -08:00
request({
headers: {
'x-identity': data.copayerId,
'x-signature': signature,
},
method: 'post',
url: _getUrl(url),
json: true,
}, function(err, res, body) {
if (err) return cb(err);
if (res.statusCode != 200) {
_parseError(body);
return cb('Request error');
}
return cb(null, body);
});
2015-02-12 11:42:32 -08:00
};
2015-02-12 19:00:54 -08:00
CliLib.prototype.history = function(limit, cb) {
2015-02-12 11:42:32 -08:00
};
2015-02-13 08:35:20 -08:00
CliLib.prototype.balance = function(cb) {
var self = this;
var data = this._loadAndCheck();
var url = '/v1/balance/';
var signature = _signRequest(url, {}, data.signingPrivKey);
request({
headers: {
'x-identity': data.copayerId,
'x-signature': signature,
},
method: 'get',
url: _getUrl(url),
json: true,
}, function(err, res, body) {
if (err) return cb(err);
if (res.statusCode != 200) {
_parseError(body);
return cb('Request error');
}
return cb(null, body);
});
};
CliLib.prototype.txProposals = function(cb) {
var self = this;
var data = this._loadAndCheck();
var url = '/v1/txproposals/';
var signature = _signRequest(url, {}, data.signingPrivKey);
request({
headers: {
'x-identity': data.copayerId,
'x-signature': signature,
},
method: 'get',
url: _getUrl(url),
json: true,
}, function(err, res, body) {
if (err) return cb(err);
if (res.statusCode != 200) {
_parseError(body);
return cb('Request error');
}
return cb(null, body);
});
};
2015-02-12 19:00:54 -08:00
module.exports = CliLib;