Browse Source

Merge pull request #12 from matiu/feat/ref-request

refactor request
activeAddress
Ivan Socolsky 10 years ago
parent
commit
043ba5d601
  1. 217
      lib/clientlib.js

217
lib/clientlib.js

@ -84,6 +84,39 @@ ClientLib.prototype._loadAndCheck = function() {
return data; return data;
}; };
ClientLib.prototype._doRequest = function(type, url, args, data, cb) {
var reqSignature = _signRequest(url, args, data.signingPrivKey);
var absUrl = _getUrl(url);
request({
headers: {
'x-identity': data.copayerId,
'x-signature': reqSignature,
},
method: type,
url: absUrl,
body: args,
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);
});
};
ClientLib.prototype._doPostRequest = function(url, args, data, cb) {
return this._doRequest('post', url, args, data, cb);
};
ClientLib.prototype._doGetRequest = function(url, data, cb) {
return this._doRequest('get', url, {}, data, cb);
};
ClientLib.prototype.createWallet = function(walletName, copayerName, m, n, network, cb) { ClientLib.prototype.createWallet = function(walletName, copayerName, m, n, network, cb) {
var self = this; var self = this;
network = network || 'livenet'; network = network || 'livenet';
@ -110,19 +143,9 @@ ClientLib.prototype.createWallet = function(walletName, copayerName, m, n, netwo
pubKey: pubKey.toString(), pubKey: pubKey.toString(),
network: network, network: network,
}; };
var url = '/v1/wallets/';
request({ this._doPostRequest(url, args, data, function(err, body) {
method: 'post',
url: _getUrl('/v1/wallets/'),
body: args,
json: true,
}, function(err, res, body) {
if (err) return cb(err);
if (res.statusCode != 200) {
_parseError(body);
return cb('Request error');
}
var walletId = body.walletId; var walletId = body.walletId;
var secret = walletId + ':' + privKey.toString() + ':' + (network == 'testnet' ? 'T' : 'L'); var secret = walletId + ':' + privKey.toString() + ':' + (network == 'testnet' ? 'T' : 'L');
data.secret = secret; data.secret = secret;
@ -157,19 +180,9 @@ ClientLib.prototype._joinWallet = function(data, secret, copayerName, cb) {
xPubKey: xPubKey.toString(), xPubKey: xPubKey.toString(),
xPubKeySignature: xPubKeySignature, xPubKeySignature: xPubKeySignature,
}; };
var url = '/v1/wallets/' + walletId + '/copayers';
request({ this._doPostRequest(url, args, data, function(err, body) {
method: 'post',
url: _getUrl('/v1/wallets/' + walletId + '/copayers'),
body: args,
json: true,
}, function(err, res, body) {
if (err) return cb(err);
if (res.statusCode != 200) {
_parseError(body);
return cb('Request error');
}
var wallet = body.wallet; var wallet = body.wallet;
data.copayerId = body.copayerId; data.copayerId = body.copayerId;
data.walletPrivKey = walletPrivKey; data.walletPrivKey = walletPrivKey;
@ -198,26 +211,10 @@ ClientLib.prototype.status = function(cb) {
var data = this._loadAndCheck(); var data = this._loadAndCheck();
var url = '/v1/wallets/'; var url = '/v1/wallets/';
var signature = _signRequest(url, {}, data.signingPrivKey); this._doGetRequest(url, data, function(err, body) {
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 (err) return cb(err);
if (res.statusCode != 200) {
_parseError(body);
return cb('Request error');
}
var wallet = body;
// TODO
//console.log('[clilib.js.214:wallet:]',wallet); //TODO
var wallet = body;
if (wallet.n > 0 && wallet.status === 'complete' && !data.verified) { if (wallet.n > 0 && wallet.status === 'complete' && !data.verified) {
var pubKey = Bitcore.PrivateKey.fromString(data.walletPrivKey).toPublicKey().toString(); var pubKey = Bitcore.PrivateKey.fromString(data.walletPrivKey).toPublicKey().toString();
var fake = []; var fake = [];
@ -259,25 +256,7 @@ ClientLib.prototype.send = function(inArgs, cb) {
var args = _createProposalOpts(inArgs, data.signingPrivKey); var args = _createProposalOpts(inArgs, data.signingPrivKey);
var url = '/v1/txproposals/'; var url = '/v1/txproposals/';
var signature = _signRequest(url, args, data.signingPrivKey); this._doPostRequest(url, args, data, cb);
request({
headers: {
'x-identity': data.copayerId,
'x-signature': signature,
},
method: 'post',
url: _getUrl(url),
body: args,
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);
});
}; };
// Get addresses // Get addresses
@ -287,24 +266,7 @@ ClientLib.prototype.addresses = function(cb) {
var data = this._loadAndCheck(); var data = this._loadAndCheck();
var url = '/v1/addresses/'; var url = '/v1/addresses/';
var signature = _signRequest(url, {}, data.signingPrivKey); this._doGetRequest(url, data, cb);
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);
});
}; };
@ -316,24 +278,7 @@ ClientLib.prototype.address = function(cb) {
var data = this._loadAndCheck(); var data = this._loadAndCheck();
var url = '/v1/addresses/'; var url = '/v1/addresses/';
var signature = _signRequest(url, {}, data.signingPrivKey); this._doPostRequest(url, {}, data, cb);
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);
});
}; };
ClientLib.prototype.history = function(limit, cb) { ClientLib.prototype.history = function(limit, cb) {
@ -346,24 +291,7 @@ ClientLib.prototype.balance = function(cb) {
var data = this._loadAndCheck(); var data = this._loadAndCheck();
var url = '/v1/balance/'; var url = '/v1/balance/';
var signature = _signRequest(url, {}, data.signingPrivKey); this._doGetRequest(url, data, cb);
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);
});
}; };
@ -373,24 +301,7 @@ ClientLib.prototype.txProposals = function(opts, cb) {
var data = this._loadAndCheck(); var data = this._loadAndCheck();
var url = '/v1/txproposals/'; var url = '/v1/txproposals/';
var signature = _signRequest(url, {}, data.signingPrivKey); this._doGetRequest(url, data, cb);
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);
});
}; };
ClientLib.prototype.sign = function(txp, cb) { ClientLib.prototype.sign = function(txp, cb) {
@ -431,26 +342,8 @@ ClientLib.prototype.sign = function(txp, cb) {
var args = { var args = {
signatures: signatures signatures: signatures
}; };
var reqSignature = _signRequest(url, args, data.signingPrivKey);
console.log('[clientlib.js.441:reqSignature:]', url, args, reqSignature); //TODO
request({ this._doPostRequest(url, args, data, cb);
headers: {
'x-identity': data.copayerId,
'x-signature': reqSignature,
},
method: 'post',
url: _getUrl(url),
body: args,
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);
});
}; };
ClientLib.prototype.reject = function(txp, reason, cb) { ClientLib.prototype.reject = function(txp, reason, cb) {
@ -461,25 +354,7 @@ ClientLib.prototype.reject = function(txp, reason, cb) {
var args = { var args = {
reason: reason || '', reason: reason || '',
}; };
var reqSignature = _signRequest(url, args, data.signingPrivKey); this._doPostRequest(url, args, data, cb);
request({
headers: {
'x-identity': data.copayerId,
'x-signature': reqSignature,
},
method: 'post',
url: _getUrl(url),
body: args,
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);
});
}; };
module.exports = ClientLib; module.exports = ClientLib;

Loading…
Cancel
Save