Browse Source

command line client

activeAddress
Ivan Socolsky 10 years ago
parent
commit
050e5701ec
  1. 4
      .gitignore
  2. 2
      app.js
  3. 153
      cli.js
  4. 15
      lib/server.js

4
.gitignore

@ -29,4 +29,6 @@ node_modules
*.swp
out/
db/
db/
.bit

2
app.js

@ -94,7 +94,7 @@ router.post('/v1/wallets/', function(req, res) {
});
});
router.post('/v1/wallets/join/', function(req, res) {
router.post('/v1/wallets/:id/copayers/', function(req, res) {
req.body.walletId = req.params['id'];
var server = CopayServer.getInstance();
server.joinWallet(req.body, function(err) {

153
cli.js

@ -0,0 +1,153 @@
'use strict';
var _ = require('lodash');
var async = require('async');
var log = require('npmlog');
var request = require('request')
var commander = require('commander')
log.debug = log.verbose;
log.level = 'debug';
var fs = require('fs')
var Bitcore = require('bitcore')
var SignUtils = require('./lib/signutils');
var BASE_URL = 'http://localhost:3001/copay/api/';
function getUrl(path) {
return BASE_URL + path;
};
function signRequest(url, args) {
};
function save(data) {
fs.writeFileSync('./.bit', JSON.stringify(data));
};
function load() {
try {
return JSON.parse(fs.readFileSync('./.bit'));
} catch (ex) {}
};
function createWallet(walletName, copayerName, m, n, cb) {
var data = load();
if (!data) {
data = {};
data.xPrivKey = new Bitcore.HDPrivateKey().toString();
data.m = m;
}
var privKey = new Bitcore.PrivateKey();
var pubKey = privKey.toPublicKey();
var args = {
name: walletName,
m: m,
n: n,
pubKey: pubKey.toString(),
};
request({
method: 'post',
url: getUrl('v1/wallets'),
body: args,
json: true,
}, function(err, res, body) {
if (err) return cb(err);
var walletId = body;
var secret = walletId + '|' + privKey.toString();
joinWallet(secret, copayerName, function(err) {
if (err) return cb(err);
save(data);
return cb(null, secret);
});
});
};
function joinWallet(secret, copayerName, cb) {
var data = load();
if (!data) {
data = {};
data.xPrivKey = new Bitcore.HDPrivateKey().toString();
}
var secretSplit = secret.split('|');
var walletId = secretSplit[0];
var privKey = Bitcore.PrivateKey.fromString(secretSplit[1]);
var pubKey = privKey.toPublicKey();
var xPubKey = new Bitcore.HDPublicKey(data.xPrivKey).toString();
var xPubKeySignature = SignUtils.sign(xPubKey, privKey);
var args = {
walletId: walletId,
name: copayerName,
xPubKey: xPubKey,
xPubKeySignature: xPubKeySignature,
};
request({
method: 'post',
url: getUrl('v1/wallets/' + walletId + '/copayers'),
body: args,
json: true,
}, function(err, res, body) {
if (err) return cb(err);
var copayerId = body;
data.copayerId = copayerId;
save(data);
return status(cb);
});
};
function status(cb) {
request({
method: 'get',
url: getUrl('v1/dump/'),
}, function(err, res, body) {
if (err) return cb(err);
console.log(body);
return cb();
});
};
function send(addressTo, amount, message, cb) {
};
function sign(proposalId, cb) {
};
function reject(proposalId, cb) {
};
function address(cb) {
};
function history(limit, cb) {
};
// createWallet('test wallet', 'test copayer', 2, 2, function(err, secret) {
// if (err) process.exit(err);
// var data = load();
// console.log('ESTE ES EL SECRET', secret);
// console.log('ESTE ES EL STORAGE', data);
// });
// joinWallet('1b44f598-ced5-4fe1-bac3-d9f4563c3011|9b483158c271036035ea639a57761247902222784ef03142b552800e35082929', 'otro copayer', function(err) {
// if (err) process.exit(err);
// });

15
lib/server.js

@ -66,7 +66,7 @@ CopayServer.getInstance = function() {
*/
CopayServer.getInstanceWithAuth = function(opts, cb) {
if (!Utils.checkRequired(opts, ['copayerId', 'message', 'signature']))
if (!Utils.checkRequired(opts, ['copayerId', 'message', 'signature']))
return cb(new ClientError('Required argument missing'));
var server = new CopayServer();
@ -124,6 +124,7 @@ CopayServer.prototype.createWallet = function(opts, cb) {
});
self.storage.storeWallet(wallet, function(err) {
log.debug('Wallet created', wallet.id);
return cb(err, wallet.id);
});
};
@ -164,6 +165,8 @@ CopayServer.prototype._verifySignature = function(text, signature, pubKey) {
CopayServer.prototype._notify = function(type, data) {
var self = this;
log.debug('Notification', type, data);
var walletId = self.walletId || data.walletId;
$.checkState(walletId);
@ -191,7 +194,7 @@ CopayServer.prototype.joinWallet = function(opts, cb) {
if (!Utils.checkRequired(opts, ['walletId', 'name', 'xPubKey', 'xPubKeySignature']))
return cb(new ClientError('Required argument missing'));
if (_.isEmpty(opts.name))
if (_.isEmpty(opts.name))
return cb(new ClientError('Invalid copayer name'));
Utils.runLocked(opts.walletId, cb, function(cb) {
@ -206,7 +209,7 @@ CopayServer.prototype.joinWallet = function(opts, cb) {
if (_.find(wallet.copayers, {
xPubKey: opts.xPubKey
})) return cb(new ClientError('CINWALLET', 'Copayer already in wallet'));
if (wallet.copayers.length == wallet.n)
if (wallet.copayers.length == wallet.n)
return cb(new ClientError('WFULL', 'Wallet full'));
var copayer = new Copayer({
@ -239,7 +242,7 @@ CopayServer.prototype.createAddress = function(opts, cb) {
Utils.runLocked(self.walletId, cb, function(cb) {
self.getWallet({}, function(err, wallet) {
if (err) return cb(err);
if (!wallet.isComplete())
if (!wallet.isComplete())
return cb(new ClientError('Wallet is not complete'));
var address = wallet.createAddress(false);
@ -733,14 +736,14 @@ CopayServer.prototype.getTxs = function(opts, cb) {
/**
* Retrieves notifications in the range (maxTs-minTs).
* Retrieves notifications in the range (maxTs-minTs).
* Times are in UNIX EPOCH. Order is assured even for events with the same time
*
* @param {Object} opts.minTs (defaults to 0)
* @param {Object} opts.maxTs (defaults to now)
* @param {Object} opts.limit
* @param {Object} opts.reverse (default false)
* @returns {Notification[]} Notifications
* @returns {Notification[]} Notifications
*/
CopayServer.prototype.getNotifications = function(opts, cb) {
var self = this;

Loading…
Cancel
Save