mtx: pass keyring into all signing methods.

This commit is contained in:
Christopher Jeffrey 2016-11-02 21:20:19 -07:00
parent 2bd3ac4618
commit f1b266e53b
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 17 additions and 19 deletions

View File

@ -23,7 +23,6 @@ var Output = require('./output');
var Coin = require('./coin'); var Coin = require('./coin');
var KeyRing = require('./keyring'); var KeyRing = require('./keyring');
var Address = require('./address'); var Address = require('./address');
var ec = require('../crypto/ec');
var workers = require('../workers/workers'); var workers = require('../workers/workers');
/** /**
@ -396,27 +395,28 @@ MTX.prototype.scriptVector = function scriptVector(prev, vector, ring) {
* Sign a transaction input on the worker pool * Sign a transaction input on the worker pool
* (if workers are enabled). * (if workers are enabled).
* @param {Number} index * @param {Number} index
* @param {Buffer} key * @param {KeyRing} ring
* @param {SighashType?} type * @param {SighashType?} type
* @returns {Promise} * @returns {Promise}
*/ */
MTX.prototype.signInputAsync = function signInputAsync(index, key, type) { MTX.prototype.signInputAsync = function signInputAsync(index, ring, type) {
return workers.pool.signInput(this, index, key, type); return workers.pool.signInput(this, index, ring, type);
}; };
/** /**
* Sign an input. * Sign an input.
* @param {Number} index - Index of input being signed. * @param {Number} index - Index of input being signed.
* @param {Buffer} key - Private key. * @param {KeyRing} ring - Private key.
* @param {SighashType} type * @param {SighashType} type
* @returns {Boolean} Whether the input was able to be signed. * @returns {Boolean} Whether the input was able to be signed.
*/ */
MTX.prototype.signInput = function signInput(index, key, type) { MTX.prototype.signInput = function signInput(index, ring, type) {
var input = this.inputs[index]; var input = this.inputs[index];
var version = 0; var version = 0;
var redeem = false; var redeem = false;
var key = ring.privateKey;
var prev, vector, sig, result; var prev, vector, sig, result;
assert(input, 'Input does not exist.'); assert(input, 'Input does not exist.');
@ -462,13 +462,13 @@ MTX.prototype.signInput = function signInput(index, key, type) {
if (redeem) { if (redeem) {
redeem = vector.pop(); redeem = vector.pop();
result = this.signVector(prev, vector, sig, key); result = this.signVector(prev, vector, sig, ring);
vector.push(redeem); vector.push(redeem);
vector.compile(); vector.compile();
return result; return result;
} }
return this.signVector(prev, vector, sig, key); return this.signVector(prev, vector, sig, ring);
}; };
/** /**
@ -477,12 +477,12 @@ MTX.prototype.signInput = function signInput(index, key, type) {
* @param {Script} prev * @param {Script} prev
* @param {Witness|Script} vector * @param {Witness|Script} vector
* @param {Buffer} sig * @param {Buffer} sig
* @param {Buffer} key * @param {KeyRing} ring
* @return {Boolean} * @return {Boolean}
*/ */
MTX.prototype.signVector = function signVector(prev, vector, sig, key) { MTX.prototype.signVector = function signVector(prev, vector, sig, ring) {
var pub = ec.publicKeyCreate(key, true); var pub = ring.publicKey;
var i, m, n, keys, keyIndex, total; var i, m, n, keys, keyIndex, total;
// P2PK // P2PK
@ -870,7 +870,7 @@ MTX.prototype.template = function template(ring) {
MTX.prototype.sign = function sign(ring, type) { MTX.prototype.sign = function sign(ring, type) {
var total = 0; var total = 0;
var i, key; var i;
if (Array.isArray(ring)) { if (Array.isArray(ring)) {
for (i = 0; i < ring.length; i++) for (i = 0; i < ring.length; i++)
@ -878,9 +878,7 @@ MTX.prototype.sign = function sign(ring, type) {
return total; return total;
} }
key = ring.privateKey; assert(ring.privateKey, 'No private key available.');
assert(key, 'No private key available.');
for (i = 0; i < this.inputs.length; i++) { for (i = 0; i < this.inputs.length; i++) {
if (!ring.ownInput(this, i)) if (!ring.ownInput(this, i))
@ -891,7 +889,7 @@ MTX.prototype.sign = function sign(ring, type) {
continue; continue;
// Sign input // Sign input
if (!this.signInput(i, key, type)) if (!this.signInput(i, ring, type))
continue; continue;
total++; total++;

View File

@ -76,12 +76,12 @@ jobs.sign = function sign(tx, ring, type) {
* @see MTX#signInput * @see MTX#signInput
* @param {MTX} tx * @param {MTX} tx
* @param {Number} index * @param {Number} index
* @param {Buffer} key * @param {KeyRing} ring
* @param {SighashType} type * @param {SighashType} type
*/ */
jobs.signInput = function signInput(tx, index, key, type) { jobs.signInput = function signInput(tx, index, ring, type) {
var result = tx.signInput(tx, index, key, type); var result = tx.signInput(tx, index, ring, type);
var input = tx.inputs[index]; var input = tx.inputs[index];
if (!result) if (!result)