validate xpubkeys

This commit is contained in:
Matias Alejo Garcia 2018-01-17 20:45:47 -03:00
parent 031e47ad5e
commit 23b183714a
No known key found for this signature in database
GPG Key ID: 02470DB551277AB3
2 changed files with 47 additions and 2 deletions

View File

@ -821,11 +821,15 @@ WalletService.prototype.joinWallet = function(opts, cb) {
if (!Utils.checkValueInCollection(opts.coin, Constants.COINS))
return cb(new ClientError('Invalid coin'));
var xPubKey;
try {
Bitcore.HDPublicKey(opts.xPubKey);
xPubKey = Bitcore.HDPublicKey(opts.xPubKey);
} catch (ex) {
return cb(new ClientError('Invalid extended public key'));
}
if (_.isUndefined(xPubKey.network)) {
return cb(new ClientError('Invalid extended public key'));
}
opts.supportBIP44AndP2PKH = _.isBoolean(opts.supportBIP44AndP2PKH) ? opts.supportBIP44AndP2PKH : true;
@ -835,10 +839,15 @@ WalletService.prototype.joinWallet = function(opts, cb) {
if (err) return cb(err);
if (!wallet) return cb(Errors.WALLET_NOT_FOUND);
if (opts.coin != wallet.coin) {
return cb(new ClientError('The wallet you are trying to join was created for a different coin'));
}
if (wallet.network != xPubKey.network.name) {
return cb(new ClientError('The wallet you are trying to join was created for a different network'));
}
if (opts.supportBIP44AndP2PKH) {
// New client trying to join legacy wallet
if (wallet.derivationStrategy == Constants.DERIVATION_STRATEGIES.BIP45) {
@ -991,7 +1000,15 @@ WalletService.prototype.createAddress = function(opts, cb) {
opts = opts || {};
function createNewAddress(wallet, cb) {
var address = wallet.createAddress(false);
var address;
try{
address = wallet.createAddress(false);
} catch(e){
log.warn("Error creating address for " + self.walletId, e);
return cb("Bad xPub");
};
self.storage.storeAddressAndWallet(wallet, address, function(err) {
if (err) return cb(err);

View File

@ -596,6 +596,34 @@ describe('Wallet service', function() {
});
});
it('should fail join existing wallet with bad xpub', function(done) {
var copayerOpts = helpers.getSignedCopayerOpts({
walletId: walletId,
name: 'me',
xPubKey: 'Ttub4pHUfyVU2mpjaM6YDGDJXWP6j5SL5AJzbViBuTaJEsybcrWZZoGkW7RSUSH9VRQKJtjqY2LfC2bF3FM4UqC1Ba9EP5M64SdTsv9575VAUwh',
requestPubKey: TestData.copayers[0].pubKey_1H_0,
customData: 'dummy custom data',
});
server.joinWallet(copayerOpts, function(err, result) {
err.message.should.match(/Invalid extended public key/);
done();
});
});
it('should fail join existing wallet with wrong network xpub', function(done) {
var copayerOpts = helpers.getSignedCopayerOpts({
walletId: walletId,
name: 'me',
xPubKey: 'tpubD6NzVbkrYhZ4Wbwwqah5kj1RGPK9BYeGbowB1jegxMoAkKbNhYUAcRTZ5fyxDcpjNXxziiy2ZkUQ3kR1ycPNycTD7Q2Dr6UfLcNTYHrzS3U',
requestPubKey: TestData.copayers[0].pubKey_1H_0,
customData: 'dummy custom data',
});
server.joinWallet(copayerOpts, function(err, result) {
err.message.should.match(/different network/);
done();
});
});
it('should fail to join with no name', function(done) {
var copayerOpts = helpers.getSignedCopayerOpts({
walletId: walletId,