You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.5 KiB

10 years ago
var $ = require('preconditions').singleton();
var _ = require('lodash');
var log = require('npmlog');
10 years ago
var Bitcore = require('bitcore');
10 years ago
var BitcoinUtils = require('../bitcoinutils')
var SignUtils = require('../signutils');
/*
* Checks data given by the server
*/
10 years ago
function Verifier(opts) {};
Verifier.checkAddress = function(data, address) {
var local = BitcoinUtils.deriveAddress(data.publicKeyRing, address.path, data.m, data.network);
return (local.address == address.address && JSON.stringify(local.publicKeys) == JSON.stringify(address.publicKeys));
10 years ago
};
//
Verifier.checkCopayers = function(copayers, walletPrivKey, myXPrivKey, n) {
var walletPubKey = Bitcore.PrivateKey.fromString(walletPrivKey).toPublicKey().toString();
if (copayers.length != n) {
log.error('Missing public keys in server response');
return false;
}
// Repeated xpub kes?
var uniq = [];
var error;
_.each(copayers, function(copayer) {
if (uniq[copayers.xPubKey]++) {
log.error('Repeated public keys in server response');
error = true;
}
// Not signed pub keys
if (!SignUtils.verify(copayer.xPubKey, copayer.xPubKeySignature, walletPubKey)) {
log.error('Invalid signatures in server response');
error = true;
}
});
if (error)
return false;
var myXPubKey = new Bitcore.HDPublicKey(myXPrivKey).toString();
if (!_.contains(_.pluck(copayers, 'xPubKey'), myXPubKey)) {
log.error('Server response does not contains our public keys')
return false;
}
return true;
};
10 years ago
module.exports = Verifier;