|
|
@ -4,7 +4,6 @@ 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') |
|
|
@ -14,7 +13,7 @@ var SignUtils = require('./signutils'); |
|
|
|
|
|
|
|
var BASE_URL = 'http://localhost:3001/copay/api/'; |
|
|
|
|
|
|
|
var cli = {}; |
|
|
|
var clilib = {}; |
|
|
|
|
|
|
|
|
|
|
|
function _getUrl(path) { |
|
|
@ -22,29 +21,33 @@ function _getUrl(path) { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
function signRequest(url, args) { |
|
|
|
function _signRequest(url, args) { |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
function save(data) { |
|
|
|
fs.writeFileSync('./.bit', JSON.stringify(data)); |
|
|
|
function _save(data) { |
|
|
|
fs.writeFileSync('.bit', JSON.stringify(data)); |
|
|
|
}; |
|
|
|
|
|
|
|
function load() { |
|
|
|
function _load() { |
|
|
|
try { |
|
|
|
return JSON.parse(fs.readFileSync('./.bit')); |
|
|
|
return JSON.parse(fs.readFileSync('.bit')); |
|
|
|
} catch (ex) {} |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
function _createXPrivKey() { |
|
|
|
return new Bitcore.HDPrivateKey().toString(); |
|
|
|
}; |
|
|
|
|
|
|
|
clilib.createWallet = function(walletName, copayerName, m, n, cb) { |
|
|
|
var data = load(); |
|
|
|
if (!data) { |
|
|
|
data = {}; |
|
|
|
data.xPrivKey = new Bitcore.HDPrivateKey().toString(); |
|
|
|
data.m = m; |
|
|
|
} |
|
|
|
var data = _load(); |
|
|
|
if (data) return cb('Only one wallet can exist'); |
|
|
|
|
|
|
|
data = { |
|
|
|
xPrivKey: _createXPrivKey(), |
|
|
|
m: m, |
|
|
|
}; |
|
|
|
|
|
|
|
var privKey = new Bitcore.PrivateKey(); |
|
|
|
var pubKey = privKey.toPublicKey(); |
|
|
|
|
|
|
@ -63,23 +66,27 @@ clilib.createWallet = function(walletName, copayerName, m, n, cb) { |
|
|
|
}, function(err, res, body) { |
|
|
|
if (err) return cb(err); |
|
|
|
var walletId = body; |
|
|
|
var secret = walletId + '|' + privKey.toString(); |
|
|
|
data.secret = walletId + '|' + privKey.toString(); |
|
|
|
|
|
|
|
_save(data); |
|
|
|
|
|
|
|
joinWallet(secret, copayerName, function(err) { |
|
|
|
clilib.joinWallet(data.secret, copayerName, function(err) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
save(data); |
|
|
|
return cb(null, secret); |
|
|
|
return cb(null, data.secret); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
clilib.joinWallet = function(secret, copayerName, cb) { |
|
|
|
var data = load(); |
|
|
|
var data = _load(); |
|
|
|
if (data && data.copayerId) return cb('Only one wallet can exist'); |
|
|
|
if (!data) { |
|
|
|
data = {}; |
|
|
|
data.xPrivKey = new Bitcore.HDPrivateKey().toString(); |
|
|
|
data = { |
|
|
|
xPrivKey: _createXPrivKey(), |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
var secretSplit = secret.split('|'); |
|
|
|
var walletId = secretSplit[0]; |
|
|
|
var privKey = Bitcore.PrivateKey.fromString(secretSplit[1]); |
|
|
@ -102,23 +109,35 @@ clilib.joinWallet = function(secret, copayerName, cb) { |
|
|
|
json: true, |
|
|
|
}, function(err, res, body) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var copayerId = body; |
|
|
|
data.copayerId = copayerId; |
|
|
|
save(data); |
|
|
|
return status(cb); |
|
|
|
|
|
|
|
_save(data); |
|
|
|
|
|
|
|
// TODO: call status to retrieve wallet.m
|
|
|
|
|
|
|
|
return clilib.status(cb); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
clilib.status = function(cb) { |
|
|
|
var data = _load(); |
|
|
|
if (!data || !data.copayerId) return cb('Not a part of an active wallet'); |
|
|
|
|
|
|
|
var url = 'v1/dump/'; |
|
|
|
var signature = _signRequest(url); |
|
|
|
|
|
|
|
request({ |
|
|
|
headers: { |
|
|
|
'x-identity': data.copayerId, |
|
|
|
'x-signature': signature, |
|
|
|
}, |
|
|
|
method: 'get', |
|
|
|
url: _getUrl('v1/dump/'), |
|
|
|
url: _getUrl(url), |
|
|
|
}, function(err, res, body) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
console.log(body); |
|
|
|
return cb(); |
|
|
|
return cb(null, body); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|