Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var pkr = _.map(w.publicKeyRing.copayersExtPubKeys, function(xPubStr) {
var isMe = xPubStr === credentials.xPubKey;
var requestDerivation;
if (isMe) {
var path = Constants.PATHS.REQUEST_KEY;
requestDerivation = (new Bitcore.HDPrivateKey(credentials.xPrivKey))
.derive(path).hdPublicKey;
} else {
// this
var path = Constants.PATHS.REQUEST_KEY_AUTH;
requestDerivation = (new Bitcore.HDPublicKey(xPubStr)).derive(path);
}
// Grab Copayer Name
var hd = new Bitcore.HDPublicKey(xPubStr).derive('m/2147483646/0/0');
var pubKey = hd.publicKey.toString('hex');
var copayerName = w.publicKeyRing.nicknameFor[pubKey];
if (isMe) {
credentials.copayerName = copayerName;
}
return {
API.signTxp = function(txp, derivedXPrivKey) {
//Derive proper key to sign, for each input
var privs = [];
var derived = {};
var xpriv = new Bitcore.HDPrivateKey(derivedXPrivKey);
_.each(txp.inputs, function(i) {
$.checkState(i.path, "Input derivation path not available (signing transaction)")
if (!derived[i.path]) {
derived[i.path] = xpriv.deriveChild(i.path).privateKey;
privs.push(derived[i.path]);
}
});
var t = Utils.buildTx(txp);
var signatures = _.map(privs, function(priv, i) {
return t.getSignatures(priv);
});
signatures = _.map(_.sortBy(_.flatten(signatures), 'inputIndex'), function(s) {
derivePrivateKey(network, xPriv, addressIndex, isChange) {
const xpriv = new BitcoreLib.HDPrivateKey(xPriv, network);
const changeNum = isChange ? 1 : 0;
const path = `m/${changeNum}/${addressIndex}`;
const derivedPrivKey = xpriv.derive(path);
const privKey = derivedPrivKey.privateKey.toString('hex');
const pubKeyObj = derivedPrivKey.publicKey;
const pubKey = pubKeyObj.toString('hex');
const pubKeyBuffer = pubKeyObj.toBuffer();
const address = this.addressFromPublicKeyBuffer(pubKeyBuffer);
return { address, privKey, pubKey };
}
}
API.prototype.addAccess = function(opts, cb) {
$.checkState(this.credentials && this.credentials.canSign());
opts = opts || {};
var reqPrivKey = new Bitcore.PrivateKey(opts.generateNewKey ? null : this.credentials.requestPrivKey);
var requestPubKey = reqPrivKey.toPublicKey().toString();
var xPriv = new Bitcore.HDPrivateKey(this.credentials.xPrivKey)
.deriveChild(this.credentials.getBaseAddressDerivationPath());
var sig = Utils.signRequestPubKey(requestPubKey, xPriv);
var copayerId = this.credentials.copayerId;
var encCopayerName = opts.name ? Utils.encryptMessage(opts.name, this.credentials.sharedEncryptingKey) : null;
var opts = {
copayerId: copayerId,
requestPubKey: requestPubKey,
signature: sig,
name: encCopayerName,
restrictions: opts.restrictions,
};
this._doPutRequest('/v1/copayers/' + copayerId + '/', opts, function(err, res) {
if (err) return cb(err);
Credentials.create = function(coin, network) {
_checkCoin(coin);
_checkNetwork(coin, network);
var x = new Credentials();
x.coin = coin;
x.network = network;
x.xPrivKey = (new Bitcore.HDPrivateKey(network)).toString();
x.compliantDerivation = true;
x._expand();
return x;
};
API.signTxp = function(txp, derivedXPrivKey) {
//Derive proper key to sign, for each input
var privs = [];
var derived = {};
var xpriv = new Bitcore.HDPrivateKey(derivedXPrivKey);
_.each(txp.inputs, function(i) {
$.checkState(i.path, "Input derivation path not available (signing transaction)")
if (!derived[i.path]) {
derived[i.path] = xpriv.deriveChild(i.path).privateKey;
privs.push(derived[i.path]);
}
});
var t = Utils.buildTx(txp);
var signatures = _.map(privs, function(priv, i) {
return t.getSignatures(priv);
});
signatures = _.map(_.sortBy(_.flatten(signatures), 'inputIndex'), function(s) {
function testLiveKeys() {
var words;
try {
words = c.getMnemonic();
} catch (ex) {}
var xpriv;
if (words && (!c.mnemonicHasPassphrase || opts.passphrase)) {
var m = new Mnemonic(words);
xpriv = m.toHDPrivateKey(opts.passphrase, c.network);
}
if (!xpriv) {
xpriv = new Bitcore.HDPrivateKey(c.xPrivKey);
}
xpriv = xpriv.deriveChild(c.getBaseAddressDerivationPath());
var xpub = new Bitcore.HDPublicKey(c.xPubKey);
return testMessageSigning(xpriv, xpub);
};
function deriveOne(xpriv, path, compliant) {
var hdPrivateKey = bitcore.HDPrivateKey(xpriv);
var xPrivKey = compliant ? hdPrivateKey.deriveChild(path) : hdPrivateKey.deriveNonCompliantChild(path);
return xPrivKey;
};
derivePrivateKey(network, xPriv, addressIndex, isChange) {
const xpriv = new BitcoreLib.HDPrivateKey(xPriv, network);
const changeNum = isChange ? 1 : 0;
const path = `m/${changeNum}/${addressIndex}`;
const derivedPrivKey = xpriv.derive(path);
const privKey = derivedPrivKey.privateKey.toString('hex');
const pubKeyObj = derivedPrivKey.publicKey;
const pubKey = pubKeyObj.toString('hex');
const pubKeyBuffer = pubKeyObj.toBuffer();
const address = this.addressFromPublicKeyBuffer(pubKeyBuffer);
return { address, privKey, pubKey };
}
}
constructor(privateKeychainString) {
if (privateKeychainString) {
this.hdKeychain = new HDPrivateKey(privateKeychainString)
} else {
this.hdKeychain = new HDPrivateKey()
}
}