script: less static methods.

This commit is contained in:
Christopher Jeffrey 2017-06-13 18:45:42 -07:00
parent b722e5f6e9
commit 8593bd9410
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 70 additions and 68 deletions

View File

@ -782,34 +782,6 @@ MTX.prototype.signVector = function signVector(prev, vector, sig, ring) {
return false;
};
/**
* Create a signature suitable for inserting into scriptSigs/witnesses.
* @param {Number} index - Index of input being signed.
* @param {Script} prev - Previous output script or redeem script
* (in the case of witnesspubkeyhash, this should be the generated
* p2pkh script).
* @param {Amount} value - Previous output value.
* @param {SighashType} type
* @param {Number} version - Sighash version (0=legacy, 1=segwit).
* @returns {Buffer} Signature in DER format.
*/
MTX.prototype.signature = function signature(index, prev, value, key, type, version) {
var hash;
if (type == null)
type = Script.hashType.ALL;
if (typeof type === 'string') {
type = Script.hashType[type.toUpperCase()];
assert(type != null, 'Unknown sighash type.');
}
hash = this.signatureHash(index, prev, value, type, version);
return Script.sign(hash, key, type);
};
/**
* Test whether the transaction is fully-signed.
* @returns {Boolean}

View File

@ -12,6 +12,7 @@ var util = require('../utils/util');
var encoding = require('../utils/encoding');
var co = require('../utils/co');
var crypto = require('../crypto/crypto');
var ec = require('../crypto/ec');
var Amount = require('../btc/amount');
var Network = require('../protocol/network');
var Script = require('../script/script');
@ -431,11 +432,6 @@ TX.prototype.hasWitness = function hasWitness() {
*/
TX.prototype.signatureHash = function signatureHash(index, prev, value, type, version) {
if (typeof type === 'string') {
type = Script.hashType[type.toUpperCase()];
assert(type != null, 'Unknown sighash type.');
}
assert(index >= 0 && index < this.inputs.length);
assert(prev instanceof Script);
assert(typeof value === 'number');
@ -725,6 +721,61 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, value, type
return crypto.hash256(bw.render());
};
/**
* Verify signature.
* @param {Number} index
* @param {Script} prev
* @param {Amount} value
* @param {Buffer} sig
* @param {Buffer} key
* @param {Number} version
* @returns {Boolean}
*/
TX.prototype.checksig = function checksig(index, prev, value, sig, key, version) {
var type, hash;
if (type == null)
type = Script.hashType.ALL;
if (sig.length === 0)
return false;
type = sig[sig.length - 1];
hash = this.signatureHash(index, prev, value, type, version);
return ec.verify(hash, sig.slice(0, -1), key);
};
/**
* Create a signature suitable for inserting into scriptSigs/witnesses.
* @param {Number} index - Index of input being signed.
* @param {Script} prev - Previous output script or redeem script
* (in the case of witnesspubkeyhash, this should be the generated
* p2pkh script).
* @param {Amount} value - Previous output value.
* @param {SighashType} type
* @param {Number} version - Sighash version (0=legacy, 1=segwit).
* @returns {Buffer} Signature in DER format.
*/
TX.prototype.signature = function signature(index, prev, value, key, type, version) {
var hash, sig, bw;
if (type == null)
type = Script.hashType.ALL;
hash = this.signatureHash(index, prev, value, type, version);
sig = ec.sign(hash, key);
bw = new StaticWriter(sig.length + 1);
bw.writeBytes(sig);
bw.writeU8(type);
return bw.render();
};
/**
* Verify all transaction inputs.
* @param {CoinView} view

View File

@ -19,7 +19,6 @@ var StaticWriter = require('../utils/staticwriter');
var Program = require('./program');
var Opcode = require('./opcode');
var Stack = require('./stack');
var sigcache = require('./sigcache');
var common = require('./common');
var encoding = require('../utils/encoding');
var ec = require('../crypto/ec');
@ -1117,7 +1116,7 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
if (sig.length > 0) {
type = sig[sig.length - 1];
hash = tx.signatureHash(index, subscript, value, type, version);
res = Script.checksig(hash, sig, key);
res = checksig(hash, sig, key);
}
if (!res && (flags & Script.flags.VERIFY_NULLFAIL)) {
@ -1197,7 +1196,7 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
type = sig[sig.length - 1];
hash = tx.signatureHash(index, subscript, value, type, version);
if (Script.checksig(hash, sig, key)) {
if (checksig(hash, sig, key)) {
isig++;
m--;
}
@ -3105,38 +3104,6 @@ Script.verifyMast = function verifyMast(program, stack, output, flags, tx, i, va
return true;
};
/**
* Verify a signature, taking into account sighash type.
* @param {Buffer} msg - Signature hash.
* @param {Buffer} sig
* @param {Buffer} key
* @returns {Boolean}
*/
Script.checksig = function checksig(msg, sig, key) {
return sigcache.verify(msg, sig.slice(0, -1), key);
};
/**
* Sign a message, appending the sighash type.
* @param {Buffer} msg - Signature hash.
* @param {Buffer} key - Public key.
* @param {Number} type - Sighash type.
* @returns {Buffer} signature
*/
Script.sign = function sign(msg, key, type) {
var sig = ec.sign(msg, key);
var bw = new StaticWriter(sig.length + 1);
// Add the sighash type as a single byte
// to the signature.
bw.writeBytes(sig);
bw.writeU8(type);
return bw.render();
};
/**
* Inject properties from buffer reader.
* @private
@ -3278,6 +3245,18 @@ function validateSignature(sig, flags) {
return true;
}
/**
* Verify a signature, taking into account sighash type.
* @param {Buffer} msg - Signature hash.
* @param {Buffer} sig
* @param {Buffer} key
* @returns {Boolean}
*/
function checksig(msg, sig, key) {
return ec.verify(msg, sig.slice(0, -1), key);
}
/*
* Expose
*/