This commit is contained in:
Matias Alejo Garcia 2015-02-13 18:24:35 -03:00
parent 802746889c
commit edce55b6cc
4 changed files with 43 additions and 25 deletions

View File

@ -29,10 +29,7 @@ cli.txProposals({}, function(err, x) {
console.log('* Raw Server Response:\n', x); //TODO
var txps = _.filter(x, function(x) {
var sid = common.shortID(x.id);
return _.endsWith(sid, txpid);
return _.endsWith(common.shortID(x.id), txpid);
});
if (!txps.length)
@ -44,6 +41,5 @@ cli.txProposals({}, function(err, x) {
}).join(' '));;
var txp = txps[0];
cli.sign(txp);
});

View File

@ -43,8 +43,8 @@ function _signRequest(url, args, privKey) {
return SignUtils.sign(message, privKey);
};
function _createXPrivKey() {
return new Bitcore.HDPrivateKey().toString();
function _createXPrivKey(network) {
return new Bitcore.HDPrivateKey(network).toString();
};
function ClientLib(opts) {
@ -86,16 +86,16 @@ ClientLib.prototype._loadAndCheck = function() {
ClientLib.prototype.createWallet = function(walletName, copayerName, m, n, network, cb) {
var self = this;
network = network || 'livenet';
var data = this._load();
if (data) return cb('File ' + this.filename + ' already contains a wallet');
// Generate wallet key pair to verify copayers
var privKey = new Bitcore.PrivateKey();
var privKey = new Bitcore.PrivateKey(null, network);
var pubKey = privKey.toPublicKey();
data = {
xPrivKey: _createXPrivKey(),
m: m,
n: n,
walletPrivKey: privKey.toString(),
@ -106,7 +106,7 @@ ClientLib.prototype.createWallet = function(walletName, copayerName, m, n, netwo
m: m,
n: n,
pubKey: pubKey.toString(),
network: network || 'livenet',
network: network,
};
request({
@ -122,11 +122,10 @@ ClientLib.prototype.createWallet = function(walletName, copayerName, m, n, netwo
}
var walletId = body.walletId;
var secret = walletId + ':' + privKey.toString();
var secret = walletId + ':' + privKey.toString() + ':' + (network ? 'T' : 'L');
data.secret = secret;
self._save(data);
self._joinWallet(data, secret, copayerName, function(err) {
if (err) return cb(err);
@ -135,18 +134,21 @@ ClientLib.prototype.createWallet = function(walletName, copayerName, m, n, netwo
});
};
ClientLib.prototype._joinWallet = function(data, secret, copayerName, cb) {
ClientLib.prototype._joinWallet = function(data, secret, copayerName, cb) {
var self = this;
data = data || {};
var secretSplit = secret.split(':');
var walletId = secretSplit[0];
var walletPrivKey = Bitcore.PrivateKey.fromString(secretSplit[1]);
var network = secretSplit[2] == 'T' ? 'testnet' : 'livenet';
data.xPrivKey = _createXPrivKey(network);
var xPubKey = new Bitcore.HDPublicKey(data.xPrivKey);
var xPubKeySignature = SignUtils.sign(xPubKey.toString(), walletPrivKey);
var signingPrivKey = (new Bitcore.HDPrivateKey(data.xPrivKey)).derive('m/1/0').privateKey;
var args = {
walletId: walletId,
name: copayerName,
@ -185,10 +187,6 @@ ClientLib.prototype.joinWallet = function(secret, copayerName, cb) {
var data = this._load();
if (data) return cb('File ' + this.filename + ' already contains a wallet');
data = {
xPrivKey: _createXPrivKey(),
};
self._joinWallet(data, secret, copayerName, cb);
};
@ -410,7 +408,9 @@ ClientLib.prototype.sign = function(txp, cb) {
//Derive proper key to sign, for each input
var privs = [],
derived = {};
var xpriv = new Bitcore.HDPrivateKey(data.xprivKey);
var network = new Bitcore.Address(txp.toAddress).network.name;
var xpriv = new Bitcore.HDPrivateKey(data.xPrivKey, network);
_.each(txp.inputs, function(i) {
if (!derived[i.path]) {
@ -419,8 +419,6 @@ ClientLib.prototype.sign = function(txp, cb) {
privs.push(derived[i.path]);
});
console.log('[clientlib.js.419:privs:]',privs); //TODO
var t = new Bitcore.Transaction();
_.each(txp.inputs, function(i) {
t.from(i, i.publicKeys, txp.requiredSignatures);
@ -435,8 +433,28 @@ console.log('[clientlib.js.419:privs:]',privs); //TODO
var s = t.getSignatures(p)[0].signature.toDER().toString('hex');
signatures.push(s);
});
console.log('[clientlib.js.436:signatures:]',signatures); //TODO
//
var url = '/v1//';
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);
});
return signatures;
};

View File

@ -341,8 +341,11 @@ CopayServer.prototype._getUtxos = function(cb) {
var networkName = Bitcore.Address(addressStrs[0]).toObject().network;
var bc = self._getBlockExplorer('insight', networkName);
bc.getUnspentUtxos(addressStrs, function(err, utxos) {
bc.getUnspentUtxos(addressStrs, function(err, inutxos) {
if (err) return cb(err);
var utxos = _.map(inutxos, function(i) {
return i.toObject();
});
self.getPendingTxs({}, function(err, txps) {
if (err) return cb(err);
@ -371,6 +374,7 @@ CopayServer.prototype._getUtxos = function(cb) {
utxo.path = addressToPath[utxo.address].path;
utxo.publicKeys = addressToPath[utxo.address].publicKeys;
});
console.log('[server.js.375:utxos:]', utxos); //TODO
return cb(null, utxos);
});

View File

@ -18,7 +18,7 @@
},
"dependencies": {
"async": "^0.9.0",
"bitcore": "0.10.0",
"bitcore": "^0.10.3",
"bitcore-explorers": "^0.9.1",
"body-parser": "^1.11.0",
"commander": "^2.6.0",