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

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

Loading…
Cancel
Save