refactor ledger signature, restrict ledger to m-n, n>1

This commit is contained in:
Matias Alejo Garcia 2015-09-14 09:46:45 -03:00
commit 7d4fee4aef
7 changed files with 61 additions and 105 deletions

View file

@ -68,37 +68,6 @@ angular.module('copayApp.services')
});
};
root._signP2PKH = function(txp, account, callback) {
root.callbacks["sign_p2sh"] = callback;
var redeemScripts = [];
var paths = [];
var tx = bwcService.getUtils().buildTx(txp);
for (var i = 0; i < tx.inputs.length; i++) {
redeemScripts.push(new ByteString(tx.inputs[i].redeemScript.toBuffer().toString('hex'), GP.HEX).toString());
paths.push(root._getPath(account) + txp.inputs[i].path.substring(1));
}
var splitTransaction = root._splitTransaction(new ByteString(tx.toString(), GP.HEX));
var inputs = [];
for (var i = 0; i < splitTransaction.inputs.length; i++) {
var input = splitTransaction.inputs[i];
inputs.push([
root._reverseBytestring(input.prevout.bytes(0, 32)).toString(),
root._reverseBytestring(input.prevout.bytes(32)).toString()
]);
}
$log.debug('Ledger signing paths:', paths);
root._messageAfterSession({
command: "sign_p2sh",
inputs: inputs,
scripts: redeemScripts,
outputs_number: splitTransaction.outputs.length,
outputs_script: splitTransaction.outputScript.toString(),
paths: paths
});
};
root._signP2SH = function(txp, account, callback) {
root.callbacks["sign_p2sh"] = callback;
var redeemScripts = [];
@ -129,9 +98,10 @@ angular.module('copayApp.services')
};
root.signTx = function(txp, account, callback) {
console.log('[ledger.js.72:txp:]', txp, account); //TODO
if (txp.addressType == 'P2PKH') {
root._signP2PKH(txp, account, callback);
var msg = 'P2PKH wallets are not supported with ledger';
$log.error(msg);
return callback(msg);
} else {
root._signP2SH(txp, account, callback);
}

View file

@ -195,7 +195,7 @@ angular.module('copayApp.services')
try {
walletClient.seedFromExtendedPublicKey(opts.extendedPublicKey, opts.externalSource, opts.entropySource);
} catch (ex) {
$log.warn(ex);
$log.warn("Creating wallet from Extended Public Key Arg:", ex, opts);
return cb(gettext('Could not create using the specified extended public key'));
}
} else {
@ -483,7 +483,7 @@ angular.module('copayApp.services')
$log.debug('Encrypting private key for', fc.credentials.walletName);
fc.setPrivateKeyEncryption(password);
fc.lock();
root.lockFC();
root.updateCredentialsFC(function() {
$log.debug('Wallet encrypted');
return cb();
@ -562,7 +562,39 @@ angular.module('copayApp.services')
return lodash.sortBy(ret, 'name');
};
root._signWithLedger = function(txp,cb) {
var fc = root.focusedClient;
$log.info('Requesting Ledger Chrome app to sign the transaction');
ledger.signTx(txp, 0, function(result) {
if (!result.success)
return cb(result);
txp.signatures = [];
for (var i=0; i<result.signatures.length; i++) {
txp.signatures.push(result.signatures[i].substring(0, result.signatures[i].length - 2));
return fc.signTxProposal(txp, cb);
}
});
};
root.signTxProposal = function(txp, cb) {
var fc = root.focusedClient;
if (fc.isPrivKeyExternal()) {
if (fc.getPrivKeyExternalSourceName() != 'ledger') {
var msg = 'Unsupported External Key:' + fc.getPrivKeyExternalSourceName();
$log.error(msg);
return cb(msg);
}
return root._signWithLedger(txp,cb);
} else {
return fc.signTxProposal(txp, function(err, signedTxp) {
root.lockFC();
return cb(err, signedTxp);
});
}
};
return root;
});