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')
|
|
|
|
var commander = require('commander')
|
|
|
|
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
|
|
|
var cli = {};
|
|
|
|
|
|
|
|
|
|
|
|
function _getUrl(path) {
|
2015-02-12 11:42:32 -08:00
|
|
|
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) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-02-12 11:50:10 -08:00
|
|
|
clilib.createWallet = function(walletName, copayerName, m, n, cb) {
|
2015-02-12 11:42:32 -08:00
|
|
|
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',
|
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);
|
|
|
|
var walletId = body;
|
|
|
|
var secret = walletId + '|' + privKey.toString();
|
|
|
|
|
|
|
|
joinWallet(secret, copayerName, function(err) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
|
|
save(data);
|
|
|
|
return cb(null, secret);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-02-12 11:50:10 -08:00
|
|
|
clilib.joinWallet = function(secret, copayerName, cb) {
|
2015-02-12 11:42:32 -08:00
|
|
|
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',
|
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);
|
|
|
|
|
|
|
|
var copayerId = body;
|
|
|
|
data.copayerId = copayerId;
|
|
|
|
save(data);
|
|
|
|
return status(cb);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-02-12 11:50:10 -08:00
|
|
|
clilib.status = function(cb) {
|
2015-02-12 11:42:32 -08:00
|
|
|
request({
|
|
|
|
method: 'get',
|
2015-02-12 11:50:10 -08:00
|
|
|
url: _getUrl('v1/dump/'),
|
2015-02-12 11:42:32 -08:00
|
|
|
}, function(err, res, body) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
|
|
console.log(body);
|
|
|
|
return cb();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-02-12 11:50:10 -08:00
|
|
|
clilib.send = function(addressTo, amount, message, cb) {
|
2015-02-12 11:42:32 -08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-02-12 11:50:10 -08:00
|
|
|
clilib.sign = function(proposalId, cb) {
|
2015-02-12 11:42:32 -08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-02-12 11:50:10 -08:00
|
|
|
clilib.reject = function(proposalId, cb) {
|
2015-02-12 11:42:32 -08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-02-12 11:50:10 -08:00
|
|
|
clilib.address = function(cb) {
|
2015-02-12 11:42:32 -08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-02-12 11:50:10 -08:00
|
|
|
clilib.history = function(limit, cb) {
|
2015-02-12 11:42:32 -08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-02-12 11:50:10 -08:00
|
|
|
module.exports = clilib;
|