completes pkr
This commit is contained in:
parent
2121565070
commit
d56dcd41de
|
@ -62,7 +62,43 @@ function API(opts) {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
API.prototype._loadAndCheck = function(opts, cb) {
|
API.prototype._tryToComplete = function(data, cb) {
|
||||||
|
var self = this;
|
||||||
|
var isCorrupted;
|
||||||
|
|
||||||
|
var url = '/v1/wallets/';
|
||||||
|
self._doGetRequest(url, data, function(err, wallet) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
if (wallet.n > 0 && wallet.status === 'complete' && !data.verified) {
|
||||||
|
var pubKey = Bitcore.PrivateKey.fromString(data.walletPrivKey).toPublicKey().toString();
|
||||||
|
var fake = [];
|
||||||
|
_.each(wallet.copayers, function(copayer) {
|
||||||
|
if (!SignUtils.verify(copayer.xPubKey, copayer.xPubKeySignature, pubKey)) {
|
||||||
|
fake.push(copayer);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (fake.length > 0) {
|
||||||
|
isCorrupted = true;
|
||||||
|
data.verified = 'corrupt';
|
||||||
|
} else {
|
||||||
|
data.verified = 'ok';
|
||||||
|
}
|
||||||
|
self.storage.save(data, function(err) {
|
||||||
|
if (isCorrupted) {
|
||||||
|
return cb('Some copayers in the wallet could not be verified to have known the wallet secret');
|
||||||
|
}
|
||||||
|
return cb(err, data);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return cb(null, data);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
API.prototype._loadAndCheck = function(cb) {
|
||||||
|
var self = this;
|
||||||
this.storage.load(function(err, data) {
|
this.storage.load(function(err, data) {
|
||||||
if (err || !data) {
|
if (err || !data) {
|
||||||
return cb(err || 'Wallet file not found.');
|
return cb(err || 'Wallet file not found.');
|
||||||
|
@ -74,8 +110,8 @@ API.prototype._loadAndCheck = function(opts, cb) {
|
||||||
if (data.n > 1) {
|
if (data.n > 1) {
|
||||||
var pkrComplete = data.publicKeyRing && data.m && data.publicKeyRing.length === data.n;
|
var pkrComplete = data.publicKeyRing && data.m && data.publicKeyRing.length === data.n;
|
||||||
|
|
||||||
if (opts.requireCompletePKR && !pkrComplete) {
|
if (!pkrComplete) {
|
||||||
return cb('Wallet Incomplete, cannot derive address');
|
return self._tryToComplete(data, cb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cb(null, data);
|
return cb(null, data);
|
||||||
|
@ -222,39 +258,12 @@ API.prototype.joinWallet = function(secret, copayerName, cb) {
|
||||||
API.prototype.getStatus = function(cb) {
|
API.prototype.getStatus = function(cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this._loadAndCheck({}, function(err, data) {
|
this._loadAndCheck(function(err, data) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
|
||||||
var url = '/v1/wallets/';
|
var url = '/v1/wallets/';
|
||||||
self._doGetRequest(url, data, function(err, body) {
|
self._doGetRequest(url, data, function(err, body) {
|
||||||
if (err) return cb(err);
|
return cb(err, body);
|
||||||
|
|
||||||
var wallet = body;
|
|
||||||
if (wallet.n > 0 && wallet.status === 'complete' && !data.verified) {
|
|
||||||
var pubKey = Bitcore.PrivateKey.fromString(data.walletPrivKey).toPublicKey().toString();
|
|
||||||
var fake = [];
|
|
||||||
_.each(wallet.copayers, function(copayer) {
|
|
||||||
|
|
||||||
|
|
||||||
console.log('[clilib.js.224]', copayer.xPubKey, copayer.xPubKeySignature, pubKey); //TODO
|
|
||||||
if (!SignUtils.verify(copayer.xPubKey, copayer.xPubKeySignature, pubKey)) {
|
|
||||||
|
|
||||||
console.log('[clilib.js.227] FAKE'); //TODO
|
|
||||||
fake.push(copayer);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (fake.length > 0) {
|
|
||||||
log.error('Some copayers in the wallet could not be verified to have known the wallet secret');
|
|
||||||
data.verified = 'corrupt';
|
|
||||||
} else {
|
|
||||||
data.verified = 'ok';
|
|
||||||
}
|
|
||||||
self.storage.save(data, function(err) {
|
|
||||||
return cb(err, wallet);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return cb(null, wallet);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -270,36 +279,34 @@ API.prototype.getStatus = function(cb) {
|
||||||
API.prototype.sendTxProposal = function(inArgs, cb) {
|
API.prototype.sendTxProposal = function(inArgs, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this._loadAndCheck({
|
this._loadAndCheck(
|
||||||
requireCompletePKR: true
|
function(err, data) {
|
||||||
}, function(err, data) {
|
if (err) return cb(err);
|
||||||
if (err) return cb(err);
|
|
||||||
|
|
||||||
var args = _createProposalOpts(inArgs, data.signingPrivKey);
|
var args = _createProposalOpts(inArgs, data.signingPrivKey);
|
||||||
|
|
||||||
var url = '/v1/txproposals/';
|
var url = '/v1/txproposals/';
|
||||||
self._doPostRequest(url, args, data, cb);
|
self._doPostRequest(url, args, data, cb);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
API.prototype.createAddress = function(cb) {
|
API.prototype.createAddress = function(cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this._loadAndCheck({
|
this._loadAndCheck(
|
||||||
requireCompletePKR: true
|
function(err, data) {
|
||||||
}, function(err, data) {
|
|
||||||
if (err) return cb(err);
|
|
||||||
|
|
||||||
var url = '/v1/addresses/';
|
|
||||||
self._doPostRequest(url, {}, data, function(err, address) {
|
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
if (!Verifier.checkAddress(data, address)) {
|
|
||||||
return cb(new ServerCompromisedError('Server sent fake address'));
|
|
||||||
}
|
|
||||||
|
|
||||||
return cb(null, address);
|
var url = '/v1/addresses/';
|
||||||
|
self._doPostRequest(url, {}, data, function(err, address) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
if (!Verifier.checkAddress(data, address)) {
|
||||||
|
return cb(new ServerCompromisedError('Server sent fake address'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return cb(null, address);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
API.prototype.history = function(limit, cb) {
|
API.prototype.history = function(limit, cb) {
|
||||||
|
@ -309,7 +316,7 @@ API.prototype.history = function(limit, cb) {
|
||||||
API.prototype.getBalance = function(cb) {
|
API.prototype.getBalance = function(cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this._loadAndCheck({}, function(err, data) {
|
this._loadAndCheck(function(err, data) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
var url = '/v1/balance/';
|
var url = '/v1/balance/';
|
||||||
self._doGetRequest(url, data, cb);
|
self._doGetRequest(url, data, cb);
|
||||||
|
@ -320,102 +327,97 @@ API.prototype.getBalance = function(cb) {
|
||||||
API.prototype.getTxProposals = function(opts, cb) {
|
API.prototype.getTxProposals = function(opts, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this._loadAndCheck({
|
this._loadAndCheck(
|
||||||
requireCompletePKR: true
|
function(err, data) {
|
||||||
}, function(err, data) {
|
if (err) return cb(err);
|
||||||
if (err) return cb(err);
|
var url = '/v1/txproposals/';
|
||||||
var url = '/v1/txproposals/';
|
self._doGetRequest(url, data, cb);
|
||||||
self._doGetRequest(url, data, cb);
|
});
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
API.prototype.signTxProposal = function(txp, cb) {
|
API.prototype.signTxProposal = function(txp, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this._loadAndCheck({
|
this._loadAndCheck(
|
||||||
requireCompletePKR: true
|
function(err, data) {
|
||||||
}, function(err, data) {
|
if (err) return cb(err);
|
||||||
if (err) return cb(err);
|
|
||||||
|
|
||||||
|
|
||||||
//Derive proper key to sign, for each input
|
//Derive proper key to sign, for each input
|
||||||
var privs = [],
|
var privs = [],
|
||||||
derived = {};
|
derived = {};
|
||||||
|
|
||||||
var network = new Bitcore.Address(txp.toAddress).network.name;
|
var network = new Bitcore.Address(txp.toAddress).network.name;
|
||||||
var xpriv = new Bitcore.HDPrivateKey(data.xPrivKey, network);
|
var xpriv = new Bitcore.HDPrivateKey(data.xPrivKey, network);
|
||||||
|
|
||||||
_.each(txp.inputs, function(i) {
|
_.each(txp.inputs, function(i) {
|
||||||
if (!derived[i.path]) {
|
if (!derived[i.path]) {
|
||||||
derived[i.path] = xpriv.derive(i.path).privateKey;
|
derived[i.path] = xpriv.derive(i.path).privateKey;
|
||||||
}
|
}
|
||||||
privs.push(derived[i.path]);
|
privs.push(derived[i.path]);
|
||||||
|
});
|
||||||
|
|
||||||
|
var t = new Bitcore.Transaction();
|
||||||
|
_.each(txp.inputs, function(i) {
|
||||||
|
t.from(i, i.publicKeys, txp.requiredSignatures);
|
||||||
|
});
|
||||||
|
|
||||||
|
t.to(txp.toAddress, txp.amount)
|
||||||
|
.change(txp.changeAddress)
|
||||||
|
.sign(privs);
|
||||||
|
|
||||||
|
var signatures = [];
|
||||||
|
_.each(privs, function(p) {
|
||||||
|
var s = t.getSignatures(p)[0].signature.toDER().toString('hex');
|
||||||
|
signatures.push(s);
|
||||||
|
});
|
||||||
|
|
||||||
|
var url = '/v1/txproposals/' + txp.id + '/signatures/';
|
||||||
|
var args = {
|
||||||
|
signatures: signatures
|
||||||
|
};
|
||||||
|
|
||||||
|
self._doPostRequest(url, args, data, cb);
|
||||||
});
|
});
|
||||||
|
|
||||||
var t = new Bitcore.Transaction();
|
|
||||||
_.each(txp.inputs, function(i) {
|
|
||||||
t.from(i, i.publicKeys, txp.requiredSignatures);
|
|
||||||
});
|
|
||||||
|
|
||||||
t.to(txp.toAddress, txp.amount)
|
|
||||||
.change(txp.changeAddress)
|
|
||||||
.sign(privs);
|
|
||||||
|
|
||||||
var signatures = [];
|
|
||||||
_.each(privs, function(p) {
|
|
||||||
var s = t.getSignatures(p)[0].signature.toDER().toString('hex');
|
|
||||||
signatures.push(s);
|
|
||||||
});
|
|
||||||
|
|
||||||
var url = '/v1/txproposals/' + txp.id + '/signatures/';
|
|
||||||
var args = {
|
|
||||||
signatures: signatures
|
|
||||||
};
|
|
||||||
|
|
||||||
self._doPostRequest(url, args, data, cb);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
API.prototype.rejectTxProposal = function(txp, reason, cb) {
|
API.prototype.rejectTxProposal = function(txp, reason, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this._loadAndCheck({
|
this._loadAndCheck(
|
||||||
requireCompletePKR: true
|
function(err, data) {
|
||||||
}, function(err, data) {
|
if (err) return cb(err);
|
||||||
if (err) return cb(err);
|
|
||||||
|
|
||||||
var url = '/v1/txproposals/' + txp.id + '/rejections/';
|
var url = '/v1/txproposals/' + txp.id + '/rejections/';
|
||||||
var args = {
|
var args = {
|
||||||
reason: reason || '',
|
reason: reason || '',
|
||||||
};
|
};
|
||||||
self._doPostRequest(url, args, data, cb);
|
self._doPostRequest(url, args, data, cb);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
API.prototype.broadcastTxProposal = function(txp, cb) {
|
API.prototype.broadcastTxProposal = function(txp, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this._loadAndCheck({
|
this._loadAndCheck(
|
||||||
requireCompletePKR: true
|
function(err, data) {
|
||||||
}, function(err, data) {
|
if (err) return cb(err);
|
||||||
if (err) return cb(err);
|
|
||||||
|
|
||||||
var url = '/v1/txproposals/' + txp.id + '/broadcast/';
|
var url = '/v1/txproposals/' + txp.id + '/broadcast/';
|
||||||
self._doPostRequest(url, {}, data, cb);
|
self._doPostRequest(url, {}, data, cb);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
API.prototype.removeTxProposal = function(txp, cb) {
|
API.prototype.removeTxProposal = function(txp, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
this._loadAndCheck({
|
this._loadAndCheck(
|
||||||
requireCompletePKR: true
|
function(err, data) {
|
||||||
}, function(err, data) {
|
if (err) return cb(err);
|
||||||
if (err) return cb(err);
|
var url = '/v1/txproposals/' + txp.id;
|
||||||
var url = '/v1/txproposals/' + txp.id;
|
self._doRequest('delete', url, {}, data, cb);
|
||||||
self._doRequest('delete', url, {}, data, cb);
|
});
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = API;
|
module.exports = API;
|
||||||
|
|
Loading…
Reference in New Issue