bitcore-wallet-service/lib/clilib.js

221 lines
4.9 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-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)) {
body = JSON.parse(body);
}
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) {
var message = url + (args ? '|' + JSON.stringify(args) : '');
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) {
this.filename = opts.filename;
2015-02-12 11:42:32 -08:00
};
2015-02-12 19:00:54 -08:00
CliLib.prototype._save = function(data) {
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);
}
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('Only one wallet is supported in this version');
2015-02-12 13:54:17 -08:00
data = {
xPrivKey: _createXPrivKey(),
m: m,
};
2015-02-12 11:42:32 -08:00
var privKey = new Bitcore.PrivateKey();
var pubKey = privKey.toPublicKey();
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 11:50:10 -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;
var secret = walletId + '|' + privKey.toString();
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 11:42:32 -08:00
var secretSplit = secret.split('|');
var walletId = secretSplit[0];
var privKey = Bitcore.PrivateKey.fromString(secretSplit[1]);
var pubKey = privKey.toPublicKey();
2015-02-12 19:00:54 -08:00
var xPubKey = new Bitcore.HDPublicKey(data.xPrivKey);
var xPubKeySignature = SignUtils.sign(xPubKey.toString(), privKey);
var signingPrivKey = xPubKey.derive('m/1/0');
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 11:50:10 -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;
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('Only one wallet is supported in this version');
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:00:54 -08:00
var url = 'v1/wallets/';
var signature = _signRequest(url, null, 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');
}
2015-02-12 11:42:32 -08:00
2015-02-12 13:54:17 -08:00
return cb(null, body);
2015-02-12 11:42:32 -08:00
});
};
2015-02-12 19:00:54 -08:00
CliLib.prototype.send = function(addressTo, amount, message, cb) {
2015-02-12 11:42:32 -08:00
};
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-12 19:00:54 -08:00
CliLib.prototype.address = function(cb) {
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-12 19:00:54 -08:00
module.exports = CliLib;